mknod.c revision 7c8a2f4adf8502ab2ca921944427e35cde069b6c
1/* mknod.c - make block or character special file
2 *
3 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
4 *
5 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mknod.html
6
7USE_MKNOD(NEWTOY(mknod, "<2>4m(mode):"USE_MKNOD_Z("Z:"), TOYFLAG_BIN|TOYFLAG_UMASK))
8
9config 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
20config 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
33GLOBALS(
34  char *arg_context;
35  char *m;
36)
37
38void 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 (mknod(toys.optargs[0], mode | modes[type], makedev(major, minor))) {
54    perror_exit("mknod %s failed", toys.optargs[0]);
55  }
56  else if (CFG_MKNOD_Z && (toys.optflags & FLAG_Z)) {
57    if (lsm_set_context(toys.optargs[0], TT.arg_context) < 0) {
58      unlink(toys.optargs[0]);
59      error_msg("'%s': bad -Z '%s'", toys.optargs[0], TT.arg_context);
60    }
61  }
62}
63