Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
juur committed Oct 31, 2023
1 parent 3e7ce65 commit 89be3a6
Showing 1 changed file with 66 additions and 64 deletions.
130 changes: 66 additions & 64 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
#include <stdbool.h>
#include <limits.h>

#ifdef __linux__
# include <sys/sysmacros.h>
#else
# error "No way to define makedev()"
#endif

#include "config.h"
#include "util.h"

Expand Down Expand Up @@ -237,6 +243,19 @@ static int validate_type(const char *raw, char *type)
return ret;
}

__attribute__((nonnull))
static dev_t vet_dev(const char *t)
{
unsigned int major, minor;

if (sscanf(t, "%u:%u", &major, &minor) != 2) {
warnx("vet_dev: invalid format: <%s>", t);
return -1;
}

return makedev(major, minor);
}

/*
* If omitted or - use 0 unless z/Z then leave UID alone
*/
Expand Down Expand Up @@ -1264,45 +1283,6 @@ static int execute_action(
}
break;

/* p - Create a pipe (FIFO) if it does not exist
* p+ - Remove and create a pipe (FIFO)
*
* Argument: ignored
*/
case CREATE_PIPE:
if (do_clean && age) {
/* TODO */
}

if (do_create) {
if ((fd = open(path, O_RDONLY)) == -1 && errno != ENOENT) {
/* OK */
break;
} else if (fd != -1
&& (mod & MOD_PLUS)
&& unlink_wrapper(path, false)) {
warn("CREATE_PIPE: unlink_wrapper: <%s>", path);
break;
}

if (fd != -1)
close(fd);

if (mkfifo(path, mode)) {
warn("CREATE_PIPE: mkfifo: <%s>", path);
break;
}

if ((uid != (uid_t)-1 || gid != (gid_t)-1)
&& lchown(path, uid, gid)) {
warn("CREATE_PIPE: chown: <%s>", path);
}

if (debug)
printf("DEBUG: DONE: create_pipe <%s>\n", path);
}
break;

/* L - Create a symlink if it does not exist
* L+ - Unlink and then create
*
Expand Down Expand Up @@ -1360,12 +1340,18 @@ static int execute_action(
}
break;

/* c - Create a character file if it does not exist
/* c - Create a character file if it does not exist
* c+ - Remove and create a character file
* b - Create a block device node if it does not exist
* b+ - Remove and create
* p - Create a pipe (FIFO) if it does not exist
* p+ - Remove and create a pipe (FIFO)
*
* Argument: ignored
*/
case CREATE_CHAR:
case CREATE_BLK:
case CREATE_PIPE:
if (do_clean && age) {
/* TODO */
}
Expand All @@ -1376,48 +1362,55 @@ static int execute_action(

if (ret == -1 && errno != ENOENT) {
/* failed to stat with unknown error */
warn("CREATE_CHAR: lstat");
warn("CREATE_CHAR/BLK/PIPE: lstat");
break;
} else if (ret == -1) {
/* NOENT: OK */
} else if (ret != -1 && !(mod & MOD_PLUS)) {
/* file exists, but not c+ */
if (debug)
printf("DEBUG: SKIP: create_char %s\n", path);
printf("DEBUG: SKIP: create_char/blk %s\n", path);
break;
} else if (ret != -1 && (mod & MOD_PLUS) && unlink_wrapper(path, false)) {
warn("CREATE_CHAR: unlink_wrapper(%s)", path);
warn("CREATE_CHAR/BLK/PIPE: unlink_wrapper(%s)", path);
goto fail;
}

if (mknod(path, (defmode ? def_file_mode : mode)|S_IFCHR, dev)) {
warn("mknod(%s)", path);
goto fail;
switch (act)
{
case CREATE_CHAR: mode = S_IFCHR; break;
case CREATE_BLK: mode = S_IFBLK; break;
case CREATE_PIPE: mode = 0; break;
}

mode |= (defmode ? def_file_mode : mode);

switch (act)
{
case CREATE_PIPE:
if (mkfifo(path, mode)) {
warn("CREATE_PIPE: mkfifo: <%s>", path);
goto fail;
}
break;

case CREATE_CHAR:
case CREATE_BLK:
if (mknod(path, mode, dev)) {
warn("CREATE_CHAR/BLK: mknod(%s)", path);
goto fail;
}
break;
}

if (chown(path, uid, gid))
if (lchown(path, uid, gid))
warn("chown(%s)", path);

if (debug)
printf("DEBUG: create_char %s\n", path);
printf("DEBUG: create_char/blk %s\n", path);
}
break;

/* b - Create a block device node if it does not exist
* b+ - Remove and create
*
* Argument: ignored
*/
case CREATE_BLK:
if (do_clean && age) {
/* TODO */
}

if (do_create) {
/* TODO */
dest = pathcat(opt_root, arg);
}
break;
default:
break;
}
Expand Down Expand Up @@ -1534,7 +1527,16 @@ static void process_line(const char *line)
goto cleanup;

/* TODO process ARG_NODE */
dev = 0;
if (cfg_elem->arg_type == ARG_NODE) {
if (arg == NULL) {
warn("process_line: missing argument for device node");
goto cleanup;
}

if ((dev = vet_dev(arg)) == (dev_t)-1)
goto cleanup;
} else
dev = -1;

if ((cfg_elem->options & CFG_GLOB)) {
if ((ret = glob_file(path, &globs, &nglobs, &fileglob))) {
Expand Down

0 comments on commit 89be3a6

Please sign in to comment.