14 #include <sys/types.h>
18 #define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))
20 /* These are all stolen from busybox's libbb to make
21 * error handling simpler (and since I maintain busybox,
22 * I'm rather partial to these for error handling).
25 static const char *const app_name = "makedevs";
26 static const char *const memory_exhausted = "memory exhausted";
27 static char default_rootdir[]=".";
28 static char *rootdir = default_rootdir;
30 static void verror_msg(const char *s, va_list p)
33 fprintf(stderr, "%s: ", app_name);
34 vfprintf(stderr, s, p);
37 static void error_msg_and_die(const char *s, ...)
48 static void vperror_msg(const char *s, va_list p)
57 fprintf(stderr, "%s%s\n", s, strerror(err));
61 static void perror_msg(const char *s, ...)
71 static void perror_msg_and_die(const char *s, ...)
81 static FILE *xfopen(const char *path, const char *mode)
85 if ((fp = fopen(path, mode)) == NULL)
86 perror_msg_and_die("%s", path);
90 static char *xstrdup(const char *s)
100 error_msg_and_die(memory_exhausted);
106 static void add_new_directory(char *name, char *path,
107 unsigned long uid, unsigned long gid, unsigned long mode)
109 // printf("Directory: %s %s UID: %ld GID %ld MODE: %ld\n", path, name, uid, gid, mode);
112 static void add_new_device(char *name, char *path, unsigned long uid,
113 unsigned long gid, unsigned long mode, dev_t rdev)
117 time_t timestamp = time(NULL);
119 memset(&sb, 0, sizeof(struct stat));
120 status = lstat(path, &sb);
123 /* It is ok for some types of files to not exit on disk (such as
124 * device nodes), but if they _do_ exist the specified mode had
125 * better match the actual file or strange things will happen.... */
126 if ((mode & S_IFMT) != (sb.st_mode & S_IFMT))
127 error_msg_and_die("%s: file type does not match specified type!", path);
128 timestamp = sb.st_mtime;
131 mknod(name, mode, rdev);
132 // printf("Device: %s %s UID: %ld GID: %ld MODE: %ld MAJOR: %d MINOR: %d\n",
133 // path, name, uid, gid, mode, (short)(rdev >> 8), (short)(rdev & 0xff));
136 static void add_new_file(char *name, char *path, unsigned long uid,
137 unsigned long gid, unsigned long mode)
139 // printf("File: %s %s UID: %ld GID: %ld MODE: %ld\n",
140 // path, name, gid, uid, mode);
144 /* device table entries take the form of:
145 <path> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>
146 /dev/mem c 640 0 0 1 1 0 0 -
151 c Character special device file
152 b Block special device file
155 I don't bother with symlinks (permissions are irrelevant), hard
156 links (special cases of regular files), or sockets (why bother).
158 Regular files must exist in the target root directory. If a char,
159 block, fifo, or directory does not exist, it will be created.
161 static int interpret_table_entry(char *line)
164 char path[4096], type;
165 unsigned long mode = 0755, uid = 0, gid = 0, major = 0, minor = 0;
166 unsigned long start = 0, increment = 1, count = 0;
168 if (0 > sscanf(line, "%40s %c %lo %lu %lu %lu %lu %lu %lu %lu", path,
169 &type, &mode, &uid, &gid, &major, &minor, &start,
175 if (!strcmp(path, "/")) {
176 error_msg_and_die("Device table entries require absolute paths");
178 name = xstrdup(path + 1);
179 sprintf(path, "%s/%s\0", rootdir, name);
184 add_new_directory(name, path, uid, gid, mode);
188 add_new_file(name, path, uid, gid, mode);
192 add_new_file(name, path, uid, gid, mode);
196 mode |= (type == 'c') ? S_IFCHR : S_IFBLK;
202 for (i = start; i < count; i++) {
203 sprintf(buf, "%s%d", name, i);
204 /* FIXME: MKDEV uses illicit insider knowledge of kernel
205 * major/minor representation... */
206 rdev = MKDEV(major, minor + (i * increment - start));
207 add_new_device(buf, path, uid, gid, mode, rdev);
210 /* FIXME: MKDEV uses illicit insider knowledge of kernel
211 * major/minor representation... */
212 dev_t rdev = MKDEV(major, minor);
214 add_new_device(name, path, uid, gid, mode, rdev);
218 error_msg_and_die("Unsupported file type");
220 if (name) free(name);
225 static void parse_device_table(FILE * file)
231 /* Looks ok so far. The general plan now is to read in one
232 * line at a time, check for leading comment delimiters ('#'),
233 * then try and parse the line as a device table. If we fail
234 * to parse things, try and help the poor fool to fix their
235 * device table with a useful error msg... */
237 if((line = (char *)malloc(length)) == NULL) {
242 while ((len = getline(&line, &length, file)) != -1) {
243 /* First trim off any whitespace */
245 /* trim trailing whitespace */
246 while (len > 0 && isspace(line[len - 1]))
249 /* trim leading whitespace */
250 memmove(line, &line[strspn(line, " \n\r\t\v")], len + 1);
252 /* If this is NOT a comment line, try to interpret it */
253 if (*line != '#') interpret_table_entry(line);
255 if (line) free(line);
260 static int go(char *dname, FILE * devtable)
264 if (lstat(dname, &sb)) {
265 perror_msg_and_die("%s", dname);
268 perror_msg_and_die("%s", dname);
271 parse_device_table(devtable);
277 static struct option long_options[] = {
278 {"root", 1, NULL, 'r'},
279 {"help", 0, NULL, 'h'},
280 {"squash", 0, NULL, 'q'},
281 {"version", 0, NULL, 'v'},
282 {"devtable", 1, NULL, 'D'},
286 static char *helptext =
287 "Usage: makedevs [OPTIONS]\n"
288 "Build entries based upon device_table.txt\n\n"
290 " -r, -d, --root=DIR Build filesystem from directory DIR (default: cwd)\n"
291 " -D, --devtable=FILE Use the named FILE as a device table file\n"
292 " -q, --squash Squash permissions and owners making all files be owned by root\n"
293 " -h, --help Display this help text\n"
294 " -v, --version Display version information\n\n";
297 static char *revtext = "$Revision: 0.1 $";
299 int main(int argc, char **argv)
304 FILE *devtable = NULL;
308 while ((opt = getopt_long(argc, argv, "D:d:r:qhv",
309 long_options, &c)) >= 0) {
312 devtable = xfopen(optarg, "r");
313 if (fstat(fileno(devtable), &statbuf) < 0)
314 perror_msg_and_die(optarg);
315 if (statbuf.st_size < 10)
316 error_msg_and_die("%s: not a proper device table file", optarg);
319 fprintf(stderr, helptext);
322 case 'd': /* for compatibility with mkfs.jffs, genext2fs, etc... */
323 if (rootdir != default_rootdir) {
324 error_msg_and_die("root directory specified more than once");
326 rootdir = xstrdup(optarg);
330 fprintf(stderr, "makedevs revision %.*s\n",
331 (int) strlen(revtext) - 13, revtext + 11);
336 go(rootdir, devtable);