Refactor, change API for create()

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-02-28 00:48:25 +01:00
parent a90cfd4a75
commit bd4efb59ca
2 changed files with 34 additions and 17 deletions
+4 -14
View File
@@ -197,21 +197,11 @@ static void setup(void *arg)
* UTMP actually needs multiple db files
*/
if (has_utmp()) {
int gid;
/*
* If /etc/group or "utmp" group is missing, default to
* "root", or "wheel", group.
*/
dbg("Setting up necessary UTMP files ...");
gid = getgroup("utmp");
if (gid < 0)
gid = 0;
create("/var/run/utmp", 0644, 0, gid); /* Currently logged in */
create("/var/log/wtmp", 0644, 0, gid); /* Login history */
create("/var/log/btmp", 0600, 0, gid); /* Failed logins */
create("/var/log/lastlog", 0644, 0, gid);
create("/var/run/utmp", 0644, "root", "utmp"); /* Currently logged in */
create("/var/log/wtmp", 0644, "root", "utmp"); /* Login history */
create("/var/log/btmp", 0600, "root", "utmp"); /* Failed logins */
create("/var/log/lastlog", 0644, "root", "utmp");
}
/* Set BOOT_TIME UTMP entry */
+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;
}