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 *Z, *m; 35 ) 36 37 void mknod_main(void) 38 { 39 mode_t modes[] = {S_IFIFO, S_IFCHR, S_IFCHR, S_IFBLK}; 40 int major=0, minor=0, type; 41 int mode = TT.m ? string_to_mode(TT.m, 0777) : 0660; 42 43 type = stridx("pcub", *toys.optargs[1]); 44 if (type == -1) perror_exit("bad type '%c'", *toys.optargs[1]); 45 if (type) { 46 if (toys.optc != 4) perror_exit("need major/minor"); 47 48 major = atoi(toys.optargs[2]); 49 minor = atoi(toys.optargs[3]); 50 } 51 52 if (toys.optflags & FLAG_Z) 53 if (-1 == lsm_set_create(TT.Z)) 54 perror_exit("-Z '%s' failed", TT.Z); 55 if (mknod(*toys.optargs, mode|modes[type], dev_makedev(major, minor))) 56 perror_exit_raw(*toys.optargs); 57 } 58