blob: d6cd65ce5d37327f41a15deceaedf9769a2eb374 [file] [log] [blame]
/* mknod.c - make block or character special file
* Copyright 2012 Elie De Brauwer <>
config MKNOD
bool "mknod"
default y
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
default y
depends on MKNOD && !TOYBOX_LSM_NONE
usage: mknod [-Z CONTEXT] ...
-Z Set security context to created file
#define FOR_mknod
#include "toys.h"
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 (toys.optflags & FLAG_Z)
if (-1 == lsm_set_create(TT.arg_context))
error_exit("bad -Z '%s'", TT.arg_context);
if (mknod(*toys.optargs, mode|modes[type], makedev(major, minor)))
perror_exit("%s", *toys.optargs);