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)
110 chown(path, uid, gid);
111 // printf("Directory: %s %s UID: %ld GID %ld MODE: %ld\n", path, name, uid, gid, mode);
114 static void add_new_device(char *name, char *path, unsigned long uid,
115 unsigned long gid, unsigned long mode, dev_t rdev)
119 time_t timestamp = time(NULL);
121 memset(&sb, 0, sizeof(struct stat));
122 status = lstat(path, &sb);
125 /* It is ok for some types of files to not exit on disk (such as
126 * device nodes), but if they _do_ exist the specified mode had
127 * better match the actual file or strange things will happen.... */
128 if ((mode & S_IFMT) != (sb.st_mode & S_IFMT))
129 error_msg_and_die("%s: file type does not match specified type!", path);
130 timestamp = sb.st_mtime;
133 mknod(name, mode, rdev);
134 chown(path, uid, gid);
135 // printf("Device: %s %s UID: %ld GID: %ld MODE: %ld MAJOR: %d MINOR: %d\n",
136 // path, name, uid, gid, mode, (short)(rdev >> 8), (short)(rdev & 0xff));
139 static void add_new_file(char *name, char *path, unsigned long uid,
140 unsigned long gid, unsigned long mode)
142 int fd = open(path,O_CREAT | O_WRONLY, mode);
144 error_msg_and_die("%s: file can not be created!", path);
149 chown(path, uid, gid);
150 // printf("File: %s %s UID: %ld GID: %ld MODE: %ld\n",
151 // path, name, gid, uid, mode);
155 static void add_new_fifo(char *name, char *path, unsigned long uid,
156 unsigned long gid, unsigned long mode)
158 if (mknod(path, mode, 0))
159 error_msg_and_die("%s: file can not be created with mknod!", path);
160 chown(path, uid, gid);
161 // printf("File: %s %s UID: %ld GID: %ld MODE: %ld\n",
162 // path, name, gid, uid, mode);
166 /* device table entries take the form of:
167 <path> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>
168 /dev/mem c 640 0 0 1 1 0 0 -
173 c Character special device file
174 b Block special device file
177 I don't bother with symlinks (permissions are irrelevant), hard
178 links (special cases of regular files), or sockets (why bother).
180 Regular files must exist in the target root directory. If a char,
181 block, fifo, or directory does not exist, it will be created.
183 static int interpret_table_entry(char *line)
186 char path[4096], type;
187 unsigned long mode = 0755, uid = 0, gid = 0, major = 0, minor = 0;
188 unsigned long start = 0, increment = 1, count = 0;
190 if (0 > sscanf(line, "%40s %c %lo %lu %lu %lu %lu %lu %lu %lu", path,
191 &type, &mode, &uid, &gid, &major, &minor, &start,
197 if (!strcmp(path, "/")) {
198 error_msg_and_die("Device table entries require absolute paths");
200 name = xstrdup(path + 1);
201 sprintf(path, "%s/%s\0", rootdir, name);
206 add_new_directory(name, path, uid, gid, mode);
210 add_new_file(name, path, uid, gid, mode);
214 add_new_fifo(name, path, uid, gid, mode);
218 mode |= (type == 'c') ? S_IFCHR : S_IFBLK;
224 for (i = start; i < count; i++) {
225 sprintf(buf, "%s%d", name, i);
226 /* FIXME: MKDEV uses illicit insider knowledge of kernel
227 * major/minor representation... */
228 rdev = MKDEV(major, minor + (i * increment - start));
229 add_new_device(buf, path, uid, gid, mode, rdev);
232 /* FIXME: MKDEV uses illicit insider knowledge of kernel
233 * major/minor representation... */
234 dev_t rdev = MKDEV(major, minor);
236 add_new_device(name, path, uid, gid, mode, rdev);
240 error_msg_and_die("Unsupported file type");
242 if (name) free(name);
247 static void parse_device_table(FILE * file)
253 /* Looks ok so far. The general plan now is to read in one
254 * line at a time, check for leading comment delimiters ('#'),
255 * then try and parse the line as a device table. If we fail
256 * to parse things, try and help the poor fool to fix their
257 * device table with a useful error msg... */
259 if((line = (char *)malloc(length)) == NULL) {
264 while ((len = getline(&line, &length, file)) != -1) {
265 /* First trim off any whitespace */
267 /* trim trailing whitespace */
268 while (len > 0 && isspace(line[len - 1]))
271 /* trim leading whitespace */
272 memmove(line, &line[strspn(line, " \n\r\t\v")], len + 1);
274 /* If this is NOT a comment line, try to interpret it */
275 if (*line != '#') interpret_table_entry(line);
277 if (line) free(line);
282 static int go(char *dname, FILE * devtable)
286 if (lstat(dname, &sb)) {
287 perror_msg_and_die("%s", dname);
290 perror_msg_and_die("%s", dname);
293 parse_device_table(devtable);
299 static struct option long_options[] = {
300 {"root", 1, NULL, 'r'},
301 {"help", 0, NULL, 'h'},
302 {"squash", 0, NULL, 'q'},
303 {"version", 0, NULL, 'v'},
304 {"devtable", 1, NULL, 'D'},
308 static char *helptext =
309 "Usage: makedevs [OPTIONS]\n"
310 "Build entries based upon device_table.txt\n\n"
312 " -r, -d, --root=DIR Build filesystem from directory DIR (default: cwd)\n"
313 " -D, --devtable=FILE Use the named FILE as a device table file\n"
314 " -q, --squash Squash permissions and owners making all files be owned by root\n"
315 " -h, --help Display this help text\n"
316 " -v, --version Display version information\n\n";
319 static char *revtext = "$Revision: 0.1 $";
321 int main(int argc, char **argv)
326 FILE *devtable = NULL;
330 while ((opt = getopt_long(argc, argv, "D:d:r:qhv",
331 long_options, &c)) >= 0) {
334 devtable = xfopen(optarg, "r");
335 if (fstat(fileno(devtable), &statbuf) < 0)
336 perror_msg_and_die(optarg);
337 if (statbuf.st_size < 10)
338 error_msg_and_die("%s: not a proper device table file", optarg);
341 fprintf(stderr, helptext);
344 case 'd': /* for compatibility with mkfs.jffs, genext2fs, etc... */
345 if (rootdir != default_rootdir) {
346 error_msg_and_die("root directory specified more than once");
348 rootdir = xstrdup(optarg);
352 fprintf(stderr, "makedevs revision %.*s\n",
353 (int) strlen(revtext) - 13, revtext + 11);
358 go(rootdir, devtable);