Refactor, change API for create()

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2023-02-28 00:48:25 +01:00
1 parent a90cfd4a75
commit bd4efb59ca
2 files changed
+34 -17

No files matched your search

+30 -3
View File
@@ -26,9 +26,11 @@
#define FINIT_HELPERS_H_
#include <ctype.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ttydefaults.h> /* Not included by default in musl libc */
#include <termios.h>
@@ -88,10 +90,35 @@ pid_t run_getty (char *tty, char *cmd, char *args[], int noclear, int no
pid_t run_sh (char *tty, int noclear, int nowait, struct rlimit rlimit[]);
int run_parts (char *dir, char *cmd, const char *env[], int progress);
static inline int create(char *path, mode_t mode, uid_t uid, gid_t gid)
/*
* Defaults to user "root" and group "wheel" (root) if:
* 1) the user is missing
* 2) the group or the file /etc/group is missing
*/
static inline int create(char *path, mode_t mode, char *user, char *group)
{
if (touch(path) || chmod(path, mode) || chown(path, uid, gid)) {
warnx("Failed creating %s properly.", path);
int fd, uid, gid;
if (!path) {
errno = EINVAL;
return -1;
}
uid = getuser(user, NULL);
if (uid < 0)
uid = 0;
gid = getgroup(group);
if (gid < 0)
gid = 0;
fd = creat(path, mode);
if (fd == -1) {
warn("Failed creating %s properly", path);
return -1;
}
close(fd);
if (chown(path, uid, gid)) {
warn("Failed chowning %s properly", path);
return -1;
}