xref: /illumos-gate/usr/src/cmd/svc/startd/file.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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  * file.c - file dependency vertex code
31  *
32  *   In principle, file dependencies should be retested on mount/unmount
33  *   events, and dependency error flow used to determine whether a lost file
34  *   affects the dependent service.  If mount/unmount events are not available,
35  *   the kstat facility (which registers or deregisters a statistic at
36  *   mount/umount) could be used as an indirect filesystem event detector.
37  *
38  *   In practice, file dependencies are checked only for existence at start
39  *   time.
40  */
41 
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <strings.h>
48 
49 #include <startd.h>
50 
51 int
52 file_ready(graph_vertex_t *v)
53 {
54 	char *fn;
55 	struct stat sbuf;
56 	int r;
57 	char *file_fmri = v->gv_name;
58 
59 	/*
60 	 * Advance through file: FMRI until we have an absolute file path.
61 	 */
62 	if (strncmp(file_fmri, "file:///", sizeof ("file:///") - 1) == 0) {
63 		fn = file_fmri + sizeof ("file://") - 1;
64 	} else if (strncmp(file_fmri, "file://localhost/",
65 		sizeof ("file://localhost/") - 1) == 0) {
66 		fn = file_fmri + sizeof ("file://localhost") - 1;
67 	} else if (strncmp(file_fmri, "file://", sizeof ("file://") - 1)
68 	    == 0) {
69 		fn = file_fmri + sizeof ("file://") - 1;
70 
71 		/*
72 		 * Again, search for the next '/'.
73 		 */
74 		if ((fn = strchr(fn, '/')) == NULL)
75 			return (0);
76 	}
77 
78 	/*
79 	 * If stat(2) succeeds for that path, then the dependency is satisfied.
80 	 */
81 	do {
82 		r = stat(fn, &sbuf);
83 	} while (r == -1 && errno == EINTR);
84 
85 	return (r == -1 ? 0 : 1);
86 }
87