diff options
author | lns <matzeton@googlemail.com> | 2018-04-29 18:48:51 +0200 |
---|---|---|
committer | lns <matzeton@googlemail.com> | 2018-04-29 18:48:51 +0200 |
commit | 8002952a4e49ed0a38779727783e1eb273e536ff (patch) | |
tree | 48d49601253aac2c6e07715765fe6edd85f27ee0 /src/utils.c | |
parent | a4fc4120d474ee277e3f24c8c7ea5929b489d477 (diff) |
POTD sekeleton #38.
Signed-off-by: lns <matzeton@googlemail.com>
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c index b4820d0..aa62beb 100644 --- a/src/utils.c +++ b/src/utils.c @@ -8,10 +8,13 @@ #include <pwd.h> #include <grp.h> #include <sys/types.h> +#undef makedev +#include <sys/sysmacros.h> #include <sys/stat.h> #include <sys/wait.h> #include <sys/prctl.h> #include <sys/mount.h> +#include <linux/limits.h> #include <assert.h> #include "utils.h" @@ -305,7 +308,42 @@ int mount_pts(const char *mount_path) return 0; } +int create_device_file_checked(const char *mount_path, const char *device_file, + mode_t mode, int add_mode, dev_t dev) +{ + int s; + mode_t defmode = S_IRUSR|S_IWUSR| + S_IRGRP|S_IWGRP| + S_IROTH; + size_t plen = strnlen(mount_path, PATH_MAX); + size_t dlen = strnlen(device_file, PATH_MAX); + struct stat devbuf = {0}; + char devpath[plen+dlen+1]; + + snprintf(devpath, plen+dlen+1, "%s/%s", mount_path, device_file); + s = stat(devpath, &devbuf); + + if (s && errno != EEXIST) { + return 1; + } else if (s && errno == EEXIST) { + } + + D2("Create device file: %s", devpath); + if (!add_mode) + defmode = 0; + s = mknod(devpath, defmode|mode, dev); + if (s) { + E2("Device file creation '%s' failed", devpath); + E_STRERR("Device creation"); + return 1; + } + + return 0; +} + int create_device_files(const char *mount_path) { + create_device_file_checked(mount_path, "ptmx", 0, 1, makedev(5,2)); + return 0; } |