aboutsummaryrefslogtreecommitdiff
path: root/toys/lsb/mknod.c
blob: 0fec5a259a3dbe0bf7d9e624b213de430560e4ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* mknod.c - make block or character special file
 *
 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
 *
 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mknod.html

USE_MKNOD(NEWTOY(mknod, "<2>4m(mode):"USE_MKNOD_Z("Z:"), TOYFLAG_BIN|TOYFLAG_UMASK))

config MKNOD
  bool "mknod"
  default y
  help
    usage: mknod [-m MODE] NAME TYPE [MAJOR MINOR]

    Create a special file NAME with a given type. TYPE is b for block device,
    c or u for character device, p for named pipe (which ignores MAJOR/MINOR).

    -m	Mode (file permissions) of new device, in octal or u+x format

config MKNOD_Z
  bool
  default y
  depends on MKNOD && !TOYBOX_LSM_NONE
  help
    usage: mknod [-Z CONTEXT] ...

    -Z	Set security context to created file
*/

#define FOR_mknod
#include "toys.h"

GLOBALS(
  char *arg_context;
  char *m;
)

void mknod_main(void)
{
  mode_t modes[] = {S_IFIFO, S_IFCHR, S_IFCHR, S_IFBLK};
  int major=0, minor=0, type;
  int mode = TT.m ? string_to_mode(TT.m, 0777) : 0660;

  type = stridx("pcub", *toys.optargs[1]);
  if (type == -1) perror_exit("bad type '%c'", *toys.optargs[1]);
  if (type) {
    if (toys.optc != 4) perror_exit("need major/minor");

    major = atoi(toys.optargs[2]);
    minor = atoi(toys.optargs[3]);
  }

  if (mknod(toys.optargs[0], mode | modes[type], makedev(major, minor))) {
    perror_exit("mknod %s failed", toys.optargs[0]);
  }
  else if (CFG_MKNOD_Z && (toys.optflags & FLAG_Z)) {
    if (lsm_set_context(toys.optargs[0], TT.arg_context) < 0) {
      unlink(toys.optargs[0]);
      error_msg("'%s': bad -Z '%s'", toys.optargs[0], TT.arg_context);
    }
  }
}