From 8002952a4e49ed0a38779727783e1eb273e536ff Mon Sep 17 00:00:00 2001 From: lns Date: Sun, 29 Apr 2018 18:48:51 +0200 Subject: POTD sekeleton #38. Signed-off-by: lns --- src/utils.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/utils.c') 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 #include #include +#undef makedev +#include #include #include #include #include +#include #include #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; } -- cgit v1.2.3