xref: /titanic_51/usr/src/cmd/svc/startd/misc.c (revision dcda19f50b2b80bfc622fff718ac04fb0e1cb670)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * misc.c - miscellaneous and utility functions
31  */
32 
33 #include <sys/stat.h>
34 #include <sys/statvfs.h>
35 #include <sys/types.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <libscf_priv.h>
40 #include <libuutil.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <strings.h>
45 #include <syslog.h>
46 #include <unistd.h>
47 
48 #include "startd.h"
49 
50 void
51 startd_close(int fd)
52 {
53 	if (close(fd) == 0)
54 		return;
55 
56 	log_error(LOG_WARNING, "close(%d) failed: %s\n", fd, strerror(errno));
57 	abort();
58 }
59 
60 void
61 startd_fclose(FILE *fp)
62 {
63 	if (fclose(fp) == 0)
64 		return;
65 
66 	log_error(LOG_WARNING, "fclose() failed\n");
67 	abort();
68 }
69 
70 /*
71  * Canonify fmri.  On success, sets *retp to a string which should be freed
72  * with startd_free( , max_scf_fmri_size) and returns 0.  On failure returns
73  * EINVAL.
74  *
75  * If 'isinstance' is non-zero, then return EINVAL if the FMRI specificies
76  * anything other than an instance.
77  */
78 int
79 fmri_canonify(const char *fmri, char **retp, boolean_t isinstance)
80 {
81 	char *cf;
82 
83 	cf = startd_alloc(max_scf_fmri_size);
84 
85 	if (isinstance) {
86 		const char *instance, *pg;
87 
88 		/*
89 		 * Verify that this fmri specifies an instance, using
90 		 * scf_parse_svc_fmri().
91 		 */
92 		if (strlcpy(cf, fmri, max_scf_fmri_size) >= max_scf_fmri_size ||
93 		    scf_parse_svc_fmri(cf, NULL, NULL, &instance, &pg,
94 		    NULL) != 0) {
95 			startd_free(cf, max_scf_fmri_size);
96 			return (EINVAL);
97 		}
98 
99 		if (instance == NULL || pg != NULL) {
100 			startd_free(cf, max_scf_fmri_size);
101 			return (EINVAL);
102 		}
103 	}
104 
105 	if (scf_canonify_fmri(fmri, cf, max_scf_fmri_size) < 0) {
106 		startd_free(cf, max_scf_fmri_size);
107 		return (EINVAL);
108 	}
109 
110 	*retp = cf;
111 	return (0);
112 }
113 
114 /*
115  * int fs_is_read_only(char *, ulong_t *)
116  *   Returns 1 if the given path is that of a filesystem with the ST_RDONLY flag
117  *   set.  0 if ST_RDONLY is unset.  -1 if the statvfs(2) call failed.  If the
118  *   second parameter is non-NULL, the fsid for the requested filesystem is
119  *   written to the given address on success.
120  */
121 int
122 fs_is_read_only(char *path, ulong_t *fsidp)
123 {
124 	int err;
125 	struct statvfs sfb;
126 
127 	do {
128 		err = statvfs(path, &sfb);
129 	} while (err == -1 && errno == EINTR);
130 
131 	if (err)
132 		return (-1);
133 
134 	if (fsidp != NULL)
135 		*fsidp = sfb.f_fsid;
136 
137 	if (sfb.f_flag & ST_RDONLY)
138 		return (1);
139 
140 	return (0);
141 }
142 
143 /*
144  * int fs_remount(char *)
145  *   Attempt to remount the given filesystem read-write, so that we can unlock
146  *   the repository (or handle other similar failures).
147  *
148  *   Returns 0 on success, -1 on failure.
149  */
150 int
151 fs_remount(char *path)
152 {
153 	if (fork_mount(path, "remount,rw"))
154 		return (-1);
155 
156 	return (0);
157 }
158 
159 /*
160  * void xstr_sanitize(char *s)
161  *   In-place transform any non-alphanumeric characters (or '_') to '_'
162  *   characters.
163  */
164 void
165 xstr_sanitize(char *s)
166 {
167 	for (; *s != '\0'; s++)
168 		if (!isalnum(*s) && *s != '_')
169 			*s = '_';
170 }
171