mirror of
https://github.com/troglobit/finit.git
synced 2026-10-14 10:19:36 +00:00
Relocate functions helpers.c -> util.c
The file helpers.c should strictly only be used for misc. helper functions to the main Finit daemon. The functions moved in this commit are generic enough to be used by any deamon or programs, and for that we have util.c Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
4 files changed
+155
-154
No files matched your search
@@ -26,8 +26,6 @@
|
||||
|
||||
#include <ctype.h> /* isblank() */
|
||||
#include <errno.h>
|
||||
#include <grp.h>
|
||||
#include <pwd.h>
|
||||
#include <limits.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/in.h>
|
||||
@@ -372,98 +370,6 @@ int print_result(int fail)
|
||||
return fail;
|
||||
}
|
||||
|
||||
int getuser(char *username, char **home)
|
||||
{
|
||||
#ifdef ENABLE_STATIC
|
||||
if (home)
|
||||
*home = "/"; /* XXX: Fixme */
|
||||
return fgetint("/etc/passwd", "x:\n", username);
|
||||
#else
|
||||
struct passwd *usr;
|
||||
|
||||
if (!username || (usr = getpwnam(username)) == NULL)
|
||||
return -1;
|
||||
|
||||
if (home)
|
||||
*home = usr->pw_dir;
|
||||
return usr->pw_uid;
|
||||
#endif
|
||||
}
|
||||
|
||||
int getgroup(char *group)
|
||||
{
|
||||
#ifdef ENABLE_STATIC
|
||||
return fgetint("/etc/group", "x:\n", group);
|
||||
#else
|
||||
struct group *grp;
|
||||
|
||||
if ((grp = getgrnam(group)) == NULL)
|
||||
return -1;
|
||||
|
||||
return grp->gr_gid;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* get current user */
|
||||
int getcuser(char *buf, size_t len)
|
||||
{
|
||||
char *ptr = "root";
|
||||
|
||||
#ifndef ENABLE_STATIC
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
|
||||
if (pw)
|
||||
ptr = pw->pw_name;
|
||||
#endif
|
||||
if (strlcpy(buf, ptr, len) > len)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* get current group */
|
||||
int getcgroup(char *buf, size_t len)
|
||||
{
|
||||
char *ptr = "root";
|
||||
|
||||
#ifndef ENABLE_STATIC
|
||||
struct group *gr = getgrgid(getgid());
|
||||
|
||||
if (gr)
|
||||
ptr = gr->gr_name;
|
||||
#endif
|
||||
if (strlcpy(buf, ptr, len) > len)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mksubsys(const char *dir, mode_t mode, char *user, char *group)
|
||||
{
|
||||
mode_t omask;
|
||||
int uid, gid;
|
||||
int rc = 0;
|
||||
|
||||
omask = umask(0);
|
||||
|
||||
uid = getuser(user, NULL);
|
||||
if (uid >= 0) {
|
||||
gid = getgroup(group);
|
||||
if (gid < 0)
|
||||
gid = 0;
|
||||
|
||||
rc = makedir(dir, mode);
|
||||
if (rc && errno == EEXIST)
|
||||
rc = chmod(dir, mode);
|
||||
if (chown(dir, uid, gid))
|
||||
err(1, "Failed chown(%s, %d, %d)", dir, uid, gid);
|
||||
}
|
||||
|
||||
umask(omask);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void set_hostname(char **hostname)
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
#define FINIT_HELPERS_H_
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
@@ -71,14 +70,6 @@ void print (int action, const char *fmt, ...) __attribute__ ((forma
|
||||
void print_desc (char *action, char *desc);
|
||||
int print_result (int fail);
|
||||
|
||||
int getuser (char *username, char **home);
|
||||
int getgroup (char *group);
|
||||
|
||||
int getcuser (char *buf, size_t len);
|
||||
int getcgroup (char *buf, size_t len);
|
||||
|
||||
int mksubsys (const char *dir, mode_t mode, char *user, char *group);
|
||||
|
||||
void set_hostname (char **hostname);
|
||||
void networking (int updown);
|
||||
int in_container (void);
|
||||
@@ -92,57 +83,6 @@ pid_t run_sh (char *tty, int noclear, int nowait, struct rlimit rlimi
|
||||
pid_t run_bg (char *cmd, char *args[]);
|
||||
int run_parts (char *dir, char *cmd, const char *env[], int progress, int sysv);
|
||||
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int ln(const char *target, const char *linkpath)
|
||||
{
|
||||
if (!target || !linkpath) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (symlink(target, linkpath)) {
|
||||
if (errno != EEXIST)
|
||||
warn("Failed creating %s -> %s symlink", target, linkpath);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int dprint(int fd, const char *s, size_t len)
|
||||
{
|
||||
size_t loop = 3;
|
||||
|
||||
+94
@@ -25,6 +25,8 @@
|
||||
|
||||
#include <ctype.h> /* isprint() */
|
||||
#include <errno.h>
|
||||
#include <grp.h>
|
||||
#include <pwd.h>
|
||||
#ifdef HAVE_MNTENT_H
|
||||
#include <mntent.h>
|
||||
#endif
|
||||
@@ -178,6 +180,98 @@ char *str(char *fmt, ...)
|
||||
return buf;
|
||||
}
|
||||
|
||||
int getuser(char *username, char **home)
|
||||
{
|
||||
#ifdef ENABLE_STATIC
|
||||
if (home)
|
||||
*home = "/"; /* XXX: Fixme */
|
||||
return fgetint("/etc/passwd", "x:\n", username);
|
||||
#else
|
||||
struct passwd *usr;
|
||||
|
||||
if (!username || (usr = getpwnam(username)) == NULL)
|
||||
return -1;
|
||||
|
||||
if (home)
|
||||
*home = usr->pw_dir;
|
||||
return usr->pw_uid;
|
||||
#endif
|
||||
}
|
||||
|
||||
int getgroup(char *group)
|
||||
{
|
||||
#ifdef ENABLE_STATIC
|
||||
return fgetint("/etc/group", "x:\n", group);
|
||||
#else
|
||||
struct group *grp;
|
||||
|
||||
if ((grp = getgrnam(group)) == NULL)
|
||||
return -1;
|
||||
|
||||
return grp->gr_gid;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* get current user */
|
||||
int getcuser(char *buf, size_t len)
|
||||
{
|
||||
char *ptr = "root";
|
||||
|
||||
#ifndef ENABLE_STATIC
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
|
||||
if (pw)
|
||||
ptr = pw->pw_name;
|
||||
#endif
|
||||
if (strlcpy(buf, ptr, len) > len)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* get current group */
|
||||
int getcgroup(char *buf, size_t len)
|
||||
{
|
||||
char *ptr = "root";
|
||||
|
||||
#ifndef ENABLE_STATIC
|
||||
struct group *gr = getgrgid(getgid());
|
||||
|
||||
if (gr)
|
||||
ptr = gr->gr_name;
|
||||
#endif
|
||||
if (strlcpy(buf, ptr, len) > len)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mksubsys(const char *dir, mode_t mode, char *user, char *group)
|
||||
{
|
||||
mode_t omask;
|
||||
int uid, gid;
|
||||
int rc = 0;
|
||||
|
||||
omask = umask(0);
|
||||
|
||||
uid = getuser(user, NULL);
|
||||
if (uid >= 0) {
|
||||
gid = getgroup(group);
|
||||
if (gid < 0)
|
||||
gid = 0;
|
||||
|
||||
rc = makedir(dir, mode);
|
||||
if (rc && errno == EEXIST)
|
||||
rc = chmod(dir, mode);
|
||||
if (chown(dir, uid, gid))
|
||||
err(1, "Failed chown(%s, %d, %d)", dir, uid, gid);
|
||||
}
|
||||
|
||||
umask(omask);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int fnread(char *buf, size_t len, char *fmt, ...)
|
||||
{
|
||||
char path[256];
|
||||
|
||||
+61
@@ -55,11 +55,21 @@ extern char *prognm;
|
||||
# include <lite/conio.h>
|
||||
# include <lite/lite.h>
|
||||
#endif
|
||||
#include "log.h"
|
||||
|
||||
char *progname (char *arg0);
|
||||
const char *basenm (const char *path);
|
||||
|
||||
char *str (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
|
||||
|
||||
int getuser (char *username, char **home);
|
||||
int getgroup (char *group);
|
||||
|
||||
int getcuser (char *buf, size_t len);
|
||||
int getcgroup (char *buf, size_t len);
|
||||
|
||||
int mksubsys (const char *dir, mode_t mode, char *user, char *group);
|
||||
|
||||
int fnread (char *buf, size_t len, char *fmt, ...) __attribute__ ((format (printf, 3, 4)));
|
||||
int fnwrite (char *value, char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
|
||||
int fngetint (char *path, int *val);
|
||||
@@ -94,6 +104,57 @@ int ttcooked (void);
|
||||
#define ttcooked() 0
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int ln(const char *target, const char *linkpath)
|
||||
{
|
||||
if (!target || !linkpath) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (symlink(target, linkpath)) {
|
||||
if (errno != EEXIST)
|
||||
warn("Failed creating %s -> %s symlink", target, linkpath);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline char *strterm(char *str, size_t len)
|
||||
{
|
||||
str[len - 1] = 0;
|
||||
|
||||
Reference in new issue
Block a user