8354 sync regcomp(3C) with upstream (fix make catalog)
[unleashed/tickless.git] / usr / src / cmd / svc / startd / misc.c
blob68981951cc21a3e40d9d2941bf64f658c4435fd1
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * misc.c - miscellaneous and utility functions
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>
48 #include "startd.h"
50 void
51 startd_close(int fd)
53 if (close(fd) == 0)
54 return;
56 log_error(LOG_WARNING, "close(%d) failed: %s\n", fd, strerror(errno));
57 abort();
60 void
61 startd_fclose(FILE *fp)
63 if (fclose(fp) == 0)
64 return;
66 log_error(LOG_WARNING, "fclose() failed\n");
67 abort();
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.
75 * If 'isinstance' is non-zero, then return EINVAL if the FMRI specificies
76 * anything other than an instance.
78 int
79 fmri_canonify(const char *fmri, char **retp, boolean_t isinstance)
81 char *cf;
83 cf = startd_alloc(max_scf_fmri_size);
85 if (isinstance) {
86 const char *instance, *pg;
89 * Verify that this fmri specifies an instance, using
90 * scf_parse_svc_fmri().
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);
99 if (instance == NULL || pg != NULL) {
100 startd_free(cf, max_scf_fmri_size);
101 return (EINVAL);
105 if (scf_canonify_fmri(fmri, cf, max_scf_fmri_size) < 0) {
106 startd_free(cf, max_scf_fmri_size);
107 return (EINVAL);
110 *retp = cf;
111 return (0);
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.
122 fs_is_read_only(char *path, ulong_t *fsidp)
124 int err;
125 struct statvfs sfb;
127 do {
128 err = statvfs(path, &sfb);
129 } while (err == -1 && errno == EINTR);
131 if (err)
132 return (-1);
134 if (fsidp != NULL)
135 *fsidp = sfb.f_fsid;
137 if (sfb.f_flag & ST_RDONLY)
138 return (1);
140 return (0);
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).
148 * Returns 0 on success, -1 on failure.
151 fs_remount(char *path)
153 if (fork_mount(path, "remount,rw"))
154 return (-1);
156 return (0);
160 * void xstr_sanitize(char *s)
161 * In-place transform any non-alphanumeric characters (or '_') to '_'
162 * characters.
164 void
165 xstr_sanitize(char *s)
167 for (; *s != '\0'; s++)
168 if (!isalnum(*s) && *s != '_')
169 *s = '_';