xref: /openbsd-src/usr.sbin/amd/amd/info_union.c (revision 9cd318725bb62a0b091c851d76d4b72897a64f57)
1 /*
2  * Copyright (c) 1990 Jan-Simon Pendry
3  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry at Imperial College, London.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)info_union.c	8.1 (Berkeley) 6/6/93
35  *	$Id: info_union.c,v 1.9 2014/10/26 03:03:34 guenther Exp $
36  */
37 
38 /*
39  * Get info from the system namespace
40  *
41  * NOTE: Cannot handle reads back through the automounter.
42  * THIS WILL CAUSE A DEADLOCK!
43  */
44 
45 #include "am.h"
46 
47 
48 #include <dirent.h>
49 #define	DIRENT struct dirent
50 
51 #define	UNION_PREFIX	"union:"
52 #define	UNION_PREFLEN	6
53 
54 /*
55  * No way to probe - check the map name begins with "union:"
56  */
57 int
union_init(char * map,time_t * tp)58 union_init(char *map, time_t *tp)
59 {
60 	*tp = 0;
61 	return strncmp(map, UNION_PREFIX, UNION_PREFLEN) == 0 ? 0 : ENOENT;
62 }
63 
64 int
union_search(mnt_map * m,char * map,char * key,char ** pval,time_t * tp)65 union_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
66 {
67 	char *mapd = strdup(map + UNION_PREFLEN);
68 	char **v = strsplit(mapd, ':', '\"');
69 	char **p;
70 
71 	for (p = v; p[1]; p++)
72 		;
73 	*pval = xmalloc(strlen(*p) + 5);
74 	snprintf(*pval, strlen(*p) + 5, "fs:=%s", *p);
75 	free(mapd);
76 	free(v);
77 	return 0;
78 }
79 
80 int
union_reload(mnt_map * m,char * map,void (* fn)(mnt_map *,char *,char *))81 union_reload(mnt_map *m, char *map, void (*fn)(mnt_map *, char *, char *))
82 {
83 	char *mapd = strdup(map + UNION_PREFLEN);
84 	char **v = strsplit(mapd, ':', '\"');
85 	char **dir;
86 
87 	/*
88 	 * Add fake /defaults entry
89 	 */
90 	(*fn)(m, strdup("/defaults"),
91 	    strdup("type:=link;opts:=nounmount;sublink:=${key}"));
92 
93 	for (dir = v; *dir; dir++) {
94 		DIR *dirp = opendir(*dir);
95 		DIRENT *dp;
96 		int dlen;
97 
98 		if (!dirp) {
99 			plog(XLOG_USER, "Cannot read directory %s: %m", *dir);
100 			continue;
101 		}
102 		dlen = strlen(*dir);
103 #ifdef DEBUG
104 		dlog("Reading directory %s...", *dir);
105 #endif
106 		while ((dp = readdir(dirp))) {
107 			char *val;
108 
109 			if (dp->d_name[0] == '.' &&
110 			    (dp->d_name[1] == '\0' ||
111 			    (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
112 				continue;
113 
114 #ifdef DEBUG
115 			dlog("... gives %s", dp->d_name);
116 #endif
117 			val = xmalloc(dlen + 5);
118 			snprintf(val, dlen + 5, "fs:=%s", *dir);
119 			(*fn)(m, strdup(dp->d_name), val);
120 		}
121 		closedir(dirp);
122 	}
123 
124 	/*
125 	 * Add wildcard entry
126 	 */
127 	{
128 		char *val = xmalloc(strlen(dir[-1]) + 5);
129 
130 		snprintf(val, strlen(dir[-1]) + 5, "fs:=%s", dir[-1]);
131 		(*fn)(m, strdup("*"), val);
132 	}
133 	free(mapd);
134 	free(v);
135 	return 0;
136 }
137 
138