xref: /illumos-gate/usr/src/cmd/svc/startd/misc.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
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 /*
28  * misc.c - miscellaneous and utility functions
29  */
30 
31 #include <sys/stat.h>
32 #include <sys/statvfs.h>
33 #include <sys/types.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <libscf_priv.h>
38 #include <libuutil.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <strings.h>
43 #include <syslog.h>
44 #include <unistd.h>
45 
46 #include "startd.h"
47 
48 void
49 startd_close(int fd)
50 {
51 	if (close(fd) == 0)
52 		return;
53 
54 	log_error(LOG_WARNING, "close(%d) failed: %s\n", fd, strerror(errno));
55 	abort();
56 }
57 
58 void
59 startd_fclose(FILE *fp)
60 {
61 	if (fclose(fp) == 0)
62 		return;
63 
64 	log_error(LOG_WARNING, "fclose() failed\n");
65 	abort();
66 }
67 
68 /*
69  * Canonify fmri.  On success, sets *retp to a string which should be freed
70  * with startd_free( , max_scf_fmri_size) and returns 0.  On failure returns
71  * EINVAL.
72  *
73  * If 'isinstance' is non-zero, then return EINVAL if the FMRI specificies
74  * anything other than an instance.
75  */
76 int
77 fmri_canonify(const char *fmri, char **retp, boolean_t isinstance)
78 {
79 	char *cf;
80 
81 	cf = startd_alloc(max_scf_fmri_size);
82 
83 	if (isinstance) {
84 		const char *instance, *pg;
85 
86 		/*
87 		 * Verify that this fmri specifies an instance, using
88 		 * scf_parse_svc_fmri().
89 		 */
90 		if (strlcpy(cf, fmri, max_scf_fmri_size) >= max_scf_fmri_size ||
91 		    scf_parse_svc_fmri(cf, NULL, NULL, &instance, &pg,
92 		    NULL) != 0) {
93 			startd_free(cf, max_scf_fmri_size);
94 			return (EINVAL);
95 		}
96 
97 		if (instance == NULL || pg != NULL) {
98 			startd_free(cf, max_scf_fmri_size);
99 			return (EINVAL);
100 		}
101 	}
102 
103 	if (scf_canonify_fmri(fmri, cf, max_scf_fmri_size) < 0) {
104 		startd_free(cf, max_scf_fmri_size);
105 		return (EINVAL);
106 	}
107 
108 	*retp = cf;
109 	return (0);
110 }
111 
112 /*
113  * int fs_is_read_only(char *, ulong_t *)
114  *   Returns 1 if the given path is that of a filesystem with the ST_RDONLY flag
115  *   set.  0 if ST_RDONLY is unset.  -1 if the statvfs(2) call failed.  If the
116  *   second parameter is non-NULL, the fsid for the requested filesystem is
117  *   written to the given address on success.
118  */
119 int
120 fs_is_read_only(char *path, ulong_t *fsidp)
121 {
122 	int err;
123 	struct statvfs sfb;
124 
125 	do {
126 		err = statvfs(path, &sfb);
127 	} while (err == -1 && errno == EINTR);
128 
129 	if (err)
130 		return (-1);
131 
132 	if (fsidp != NULL)
133 		*fsidp = sfb.f_fsid;
134 
135 	if (sfb.f_flag & ST_RDONLY)
136 		return (1);
137 
138 	return (0);
139 }
140 
141 /*
142  * int fs_remount(char *)
143  *   Attempt to remount the given filesystem read-write, so that we can unlock
144  *   the repository (or handle other similar failures).
145  *
146  *   Returns 0 on success, -1 on failure.
147  */
148 int
149 fs_remount(char *path)
150 {
151 	if (fork_mount(path, "remount,rw"))
152 		return (-1);
153 
154 	return (0);
155 }
156 
157 /*
158  * void xstr_sanitize(char *s)
159  *   In-place transform any non-alphanumeric characters (or '_') to '_'
160  *   characters.
161  */
162 void
163 xstr_sanitize(char *s)
164 {
165 	for (; *s != '\0'; s++)
166 		if (!isalnum(*s) && *s != '_')
167 			*s = '_';
168 }
169