mirror of
https://github.com/systemd/systemd.git
synced 2024-11-06 16:59:03 +03:00
udevd: define main through macro
This removes the call to log_close(), and refactors how fork() is done. Now the parent also goes through normal cleanup. This isn't necessary to use the macro, but it feels cleaner this way.
This commit is contained in:
parent
138715dc12
commit
0c5a109a25
@ -46,6 +46,7 @@
|
|||||||
#include "io-util.h"
|
#include "io-util.h"
|
||||||
#include "libudev-device-internal.h"
|
#include "libudev-device-internal.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
#include "main-func.h"
|
||||||
#include "mkdir.h"
|
#include "mkdir.h"
|
||||||
#include "netlink-util.h"
|
#include "netlink-util.h"
|
||||||
#include "parse-util.h"
|
#include "parse-util.h"
|
||||||
@ -1715,7 +1716,7 @@ static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cg
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int run(int fd_ctrl, int fd_uevent, const char *cgroup) {
|
static int main_loop(int fd_ctrl, int fd_uevent, const char *cgroup) {
|
||||||
_cleanup_(manager_freep) Manager *manager = NULL;
|
_cleanup_(manager_freep) Manager *manager = NULL;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
@ -1750,7 +1751,7 @@ exit:
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
static int run(int argc, char *argv[]) {
|
||||||
_cleanup_free_ char *cgroup = NULL;
|
_cleanup_free_ char *cgroup = NULL;
|
||||||
int fd_ctrl = -1, fd_uevent = -1;
|
int fd_ctrl = -1, fd_uevent = -1;
|
||||||
int r;
|
int r;
|
||||||
@ -1762,7 +1763,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
r = parse_argv(argc, argv);
|
r = parse_argv(argc, argv);
|
||||||
if (r <= 0)
|
if (r <= 0)
|
||||||
goto exit;
|
return r;
|
||||||
|
|
||||||
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
|
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
@ -1777,7 +1778,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
r = must_be_root();
|
r = must_be_root();
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto exit;
|
return r;
|
||||||
|
|
||||||
if (arg_children_max == 0) {
|
if (arg_children_max == 0) {
|
||||||
cpu_set_t cpu_set;
|
cpu_set_t cpu_set;
|
||||||
@ -1796,24 +1797,18 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
/* set umask before creating any file/directory */
|
/* set umask before creating any file/directory */
|
||||||
r = chdir("/");
|
r = chdir("/");
|
||||||
if (r < 0) {
|
if (r < 0)
|
||||||
r = log_error_errno(errno, "Failed to change dir to '/': %m");
|
return log_error_errno(errno, "Failed to change dir to '/': %m");
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
umask(022);
|
umask(022);
|
||||||
|
|
||||||
r = mac_selinux_init();
|
r = mac_selinux_init();
|
||||||
if (r < 0) {
|
if (r < 0)
|
||||||
log_error_errno(r, "could not initialize labelling: %m");
|
return log_error_errno(r, "Could not initialize labelling: %m");
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = mkdir_errno_wrapper("/run/udev", 0755);
|
r = mkdir_errno_wrapper("/run/udev", 0755);
|
||||||
if (r < 0 && r != -EEXIST) {
|
if (r < 0 && r != -EEXIST)
|
||||||
log_error_errno(r, "Failed to create /run/udev: %m");
|
return log_error_errno(r, "Failed to create /run/udev: %m");
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
dev_setup(NULL, UID_INVALID, GID_INVALID);
|
dev_setup(NULL, UID_INVALID, GID_INVALID);
|
||||||
|
|
||||||
@ -1831,10 +1826,8 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r = listen_fds(&fd_ctrl, &fd_uevent);
|
r = listen_fds(&fd_ctrl, &fd_uevent);
|
||||||
if (r < 0) {
|
if (r < 0)
|
||||||
r = log_error_errno(r, "Failed to listen on fds: %m");
|
return log_error_errno(r, "Failed to listen on fds: %m");
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (arg_daemonize) {
|
if (arg_daemonize) {
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
@ -1849,18 +1842,13 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
switch (pid) {
|
if (pid < 0)
|
||||||
case 0:
|
return log_error_errno(errno, "Failed to fork daemon: %m");
|
||||||
break;
|
if (pid > 0)
|
||||||
case -1:
|
/* parent */
|
||||||
r = log_error_errno(errno, "Failed to fork daemon: %m");
|
return 0;
|
||||||
goto exit;
|
|
||||||
default:
|
|
||||||
mac_selinux_finish();
|
|
||||||
log_close();
|
|
||||||
_exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* child */
|
||||||
setsid();
|
setsid();
|
||||||
|
|
||||||
r = set_oom_score_adjust(-1000);
|
r = set_oom_score_adjust(-1000);
|
||||||
@ -1868,10 +1856,7 @@ int main(int argc, char *argv[]) {
|
|||||||
log_debug_errno(r, "Failed to adjust OOM score, ignoring: %m");
|
log_debug_errno(r, "Failed to adjust OOM score, ignoring: %m");
|
||||||
}
|
}
|
||||||
|
|
||||||
r = run(fd_ctrl, fd_uevent, cgroup);
|
return main_loop(fd_ctrl, fd_uevent, cgroup);
|
||||||
|
|
||||||
exit:
|
|
||||||
mac_selinux_finish();
|
|
||||||
log_close();
|
|
||||||
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DEFINE_MAIN_FUNCTION(run);
|
||||||
|
Loading…
Reference in New Issue
Block a user