-
Notifications
You must be signed in to change notification settings - Fork 0
/
pseudo.c
365 lines (315 loc) · 8.32 KB
/
pseudo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Create a squashfs filesystem. This is a highly compressed read only filesystem.
*
* Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
* Phillip Lougher <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* pseudo.c
*/
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include "pseudo.h"
#ifdef SQUASHFS_TRACE
#define TRACE(s, args...) do { \
printf("mksquashfs: "s, ## args); \
} while(0)
#else
#define TRACE(s, args...)
#endif
#define ERROR(s, args...) do { \
fprintf(stderr, s, ## args); \
} while(0)
#define EXIT_MKSQUASHFS() do { \
exit(1); \
} while(0)
#define BAD_ERROR(s, args...) do {\
fprintf(stderr, "FATAL ERROR:" s, ##args);\
EXIT_MKSQUASHFS();\
} while(0);
#define TRUE 1
#define FALSE 0
static void dump_pseudo(struct pseudo *pseudo, char *string)
{
int i;
char path[1024];
for(i = 0; i < pseudo->names; i++) {
struct pseudo_entry *entry = &pseudo->name[i];
if(string)
strcat(strcat(strcpy(path, string), "/"), entry->name);
else
strcpy(path, entry->name);
if(entry->pseudo == NULL)
ERROR("%s %c %o %d %d %d %d\n", path, entry->dev->type,
entry->dev->mode, entry->dev->uid,
entry->dev->gid, entry->dev->major,
entry->dev->minor);
else
dump_pseudo(entry->pseudo, path);
}
}
static char *get_component(char *target, char *targname)
{
while(*target == '/')
target ++;
while(*target != '/' && *target!= '\0')
*targname ++ = *target ++;
*targname = '\0';
return target;
}
/*
* Add pseudo device target to the set of pseudo devices. Pseudo_dev
* describes the pseudo device attributes.
*/
struct pseudo *add_pseudo(struct pseudo *pseudo, struct pseudo_dev *pseudo_dev,
char *target, char *alltarget)
{
char targname[1024];
int i, error;
target = get_component(target, targname);
if(pseudo == NULL) {
if((pseudo = malloc(sizeof(struct pseudo))) == NULL)
BAD_ERROR("failed to allocate pseudo file\n");
pseudo->names = 0;
pseudo->count = 0;
pseudo->name = NULL;
}
for(i = 0; i < pseudo->names; i++)
if(strcmp(pseudo->name[i].name, targname) == 0)
break;
if(i == pseudo->names) {
/* allocate new name entry */
pseudo->names ++;
pseudo->name = realloc(pseudo->name, (i + 1) *
sizeof(struct pseudo_entry));
if(pseudo->name == NULL)
BAD_ERROR("failed to allocate pseudo file\n");
pseudo->name[i].name = strdup(targname);
if(target[0] == '\0') {
/* at leaf pathname component */
pseudo->name[i].pseudo = NULL;
pseudo->name[i].dev = malloc(sizeof(struct pseudo_dev));
if(pseudo->name[i].dev == NULL)
BAD_ERROR("failed to allocate pseudo file\n");
pseudo->name[i].pathname = strdup(alltarget);
memcpy(pseudo->name[i].dev, pseudo_dev,
sizeof(struct pseudo_dev));
} else {
/* recurse adding child components */
pseudo->name[i].dev = NULL;
pseudo->name[i].pseudo = add_pseudo(NULL, pseudo_dev,
target, alltarget);
}
} else {
/* existing matching entry */
if(pseudo->name[i].pseudo == NULL) {
/* No sub-directory which means this is the leaf
* component of a pre-existing pseudo file.
*/
if(target[0] != '\0') {
/* entry must exist as a 'd' type pseudo file */
if(pseudo->name[i].dev->type == 'd')
/* recurse adding child components */
pseudo->name[i].pseudo =
add_pseudo(NULL, pseudo_dev,
target, alltarget);
else
ERROR("%s already exists as a non "
"directory. Ignoring %s!\n",
targname, alltarget);
} else if(memcmp(pseudo_dev, pseudo->name[i].dev,
sizeof(struct pseudo_dev)) != 0)
ERROR("%s already exists as a different pseudo "
"definition. Ignoring!\n", alltarget);
else ERROR("%s already exists as an identical "
"pseudo definition!\n", alltarget);
} else {
/* sub-directory exists which means this can only be a
* 'd' type pseudo file */
if(target[0] == '\0') {
if(pseudo->name[i].dev == NULL &&
pseudo_dev->type == 'd') {
pseudo->name[i].dev =
malloc(sizeof(struct pseudo_dev));
if(pseudo->name[i].dev == NULL)
BAD_ERROR("failed to allocate "
"pseudo file\n");
pseudo->name[i].pathname =
strdup(alltarget);
memcpy(pseudo->name[i].dev, pseudo_dev,
sizeof(struct pseudo_dev));
} else
ERROR("%s already exists as a "
"directory. Ignoring %s!\n",
targname, alltarget);
} else
/* recurse adding child components */
add_pseudo(pseudo->name[i].pseudo, pseudo_dev,
target, alltarget);
}
}
return pseudo;
}
/*
* Find subdirectory in pseudo directory referenced by pseudo, matching
* filename. If filename doesn't exist or if filename is a leaf file
* return NULL
*/
struct pseudo *pseudo_subdir(char *filename, struct pseudo *pseudo)
{
int i;
if(pseudo == NULL)
return NULL;
for(i = 0; i < pseudo->names; i++)
if(strcmp(filename, pseudo->name[i].name) == 0)
return pseudo->name[i].pseudo;
return NULL;
}
struct pseudo_entry *pseudo_readdir(struct pseudo *pseudo)
{
if(pseudo == NULL)
return NULL;
while(pseudo->count < pseudo->names) {
if(pseudo->name[pseudo->count].dev != NULL)
return &pseudo->name[pseudo->count++];
else
pseudo->count++;
}
return NULL;
}
int read_pseudo_def(struct pseudo **pseudo, char *def)
{
int n;
unsigned int major = 0, minor = 0, mode;
char filename[2048], type, suid[100], sgid[100], *ptr;
long long uid, gid;
struct pseudo_dev dev;
n = sscanf(def, "%s %c %o %s %s %u %u", filename, &type, &mode, suid, sgid,
&major, &minor);
if(n < 5) {
ERROR("Not enough or invalid arguments in pseudo file "
"definition\n");
goto error;
}
switch(type) {
case 'b':
case 'c':
if(n < 7) {
ERROR("Not enough or invalid arguments in pseudo file "
"definition\n");
goto error;
}
if(major > 0xfff) {
ERROR("Major %d out of range\n", major);
goto error;
}
if(minor > 0xfffff) {
ERROR("Minor %d out of range\n", minor);
goto error;
}
/* fall through */
case 'd':
if(mode > 0777) {
ERROR("Mode %o out of range\n", mode);
goto error;
}
uid = strtoll(suid, &ptr, 10);
if(*ptr == '\0') {
if(uid < 0 || uid > ((1LL << 32) - 1)) {
ERROR("Uid %s out of range\n", suid);
goto error;
}
} else {
struct passwd *pwuid = getpwnam(suid);
if(pwuid)
uid = pwuid->pw_uid;
else {
ERROR("Uid %s invalid uid or unknown user\n",
suid);
goto error;
}
}
gid = strtoll(sgid, &ptr, 10);
if(*ptr == '\0') {
if(gid < 0 || gid > ((1LL << 32) - 1)) {
ERROR("Gid %s out of range\n", sgid);
goto error;
}
} else {
struct group *grgid = getgrnam(sgid);
if(grgid)
gid = grgid->gr_gid;
else {
ERROR("Gid %s invalid uid or unknown user\n",
sgid);
goto error;
}
}
break;
default:
ERROR("Unsupported type %c\n", type);
goto error;
}
switch(type) {
case 'b':
mode |= S_IFBLK;
break;
case 'c':
mode |= S_IFCHR;
break;
case 'd':
mode |= S_IFDIR;
break;
}
dev.type = type;
dev.mode = mode;
dev.uid = uid;
dev.gid = gid;
dev.major = major;
dev.minor = minor;
*pseudo = add_pseudo(*pseudo, &dev, filename, filename);
return TRUE;
error:
ERROR("Bad pseudo file definition \"%s\"\n", def);
return FALSE;
}
int read_pseudo_file(struct pseudo **pseudo, char *filename)
{
FILE *fd;
char line[2048];
int res = TRUE;
fd = fopen(filename, "r");
if(fd == NULL) {
ERROR("Could not open pseudo device file \"%s\" because %s\n",
filename, strerror(errno));
return FALSE;
}
while(fscanf(fd, "%2047[^\n]\n", line) > 0) {
res = read_pseudo_def(pseudo, line);
if(res == FALSE)
break;
};
fclose(fd);
return res;
}