1 /* mknod.c - make block or character special file 2 * 3 * Copyright 2012 Elie De Brauwer <eliedebrauwer (at) gmail.com> 4 * 5 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mknod.html 6 7 USE_MKNOD(NEWTOY(mknod, "<2>4m(mode):"USE_MKNOD_Z("Z:"), TOYFLAG_BIN|TOYFLAG_UMASK)) 8 9 config MKNOD 10 bool "mknod" 11 default y 12 help 13 usage: mknod [-m MODE] NAME TYPE [MAJOR MINOR] 14 15 Create a special file NAME with a given type. TYPE is b for block device, 16 c or u for character device, p for named pipe (which ignores MAJOR/MINOR). 17 18 -m Mode (file permissions) of new device, in octal or u+x format 19 20 config MKNOD_Z 21 bool 22 default y 23 depends on MKNOD && !TOYBOX_LSM_NONE 24 help 25 usage: mknod [-Z CONTEXT] ... 26 27 -Z Set security context to created file 28 */ 29 30 #define FOR_mknod 31 #include "toys.h" 32 33 GLOBALS( 34 char *arg_context; 35 char *m; 36 ) 37 38 void mknod_main(void) 39 { 40 mode_t modes[] = {S_IFIFO, S_IFCHR, S_IFCHR, S_IFBLK}; 41 int major=0, minor=0, type; 42 int mode = TT.m ? string_to_mode(TT.m, 0777) : 0660; 43 44 type = stridx("pcub", *toys.optargs[1]); 45 if (type == -1) perror_exit("bad type '%c'", *toys.optargs[1]); 46 if (type) { 47 if (toys.optc != 4) perror_exit("need major/minor"); 48 49 major = atoi(toys.optargs[2]); 50 minor = atoi(toys.optargs[3]); 51 } 52 53 if (toys.optflags & FLAG_Z) 54 if (-1 == lsm_set_create(TT.arg_context)) 55 perror_exit("-Z '%s' failed", TT.arg_context); 56 if (mknod(*toys.optargs, mode|modes[type], dev_makedev(major, minor))) 57 perror_exit_raw(*toys.optargs); 58 } 59