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
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]
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
34 #include <sys/statvfs.h>
35 #include <sys/types.h>
39 #include <libscf_priv.h>
56 log_error(LOG_WARNING
, "close(%d) failed: %s\n", fd
, strerror(errno
));
61 startd_fclose(FILE *fp
)
66 log_error(LOG_WARNING
, "fclose() failed\n");
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
75 * If 'isinstance' is non-zero, then return EINVAL if the FMRI specificies
76 * anything other than an instance.
79 fmri_canonify(const char *fmri
, char **retp
, boolean_t isinstance
)
83 cf
= startd_alloc(max_scf_fmri_size
);
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
,
95 startd_free(cf
, max_scf_fmri_size
);
99 if (instance
== NULL
|| pg
!= NULL
) {
100 startd_free(cf
, max_scf_fmri_size
);
105 if (scf_canonify_fmri(fmri
, cf
, max_scf_fmri_size
) < 0) {
106 startd_free(cf
, max_scf_fmri_size
);
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
)
128 err
= statvfs(path
, &sfb
);
129 } while (err
== -1 && errno
== EINTR
);
137 if (sfb
.f_flag
& ST_RDONLY
)
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"))
160 * void xstr_sanitize(char *s)
161 * In-place transform any non-alphanumeric characters (or '_') to '_'
165 xstr_sanitize(char *s
)
167 for (; *s
!= '\0'; s
++)
168 if (!isalnum(*s
) && *s
!= '_')