Debian Codegraph @

src/udev/udevd.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright © 2004 Chris Friesen <[email protected]>
4 * Copyright © 2009 Canonical Ltd.
5 * Copyright © 2009 Scott James Remnant <[email protected]>
6 */
7
8 #include <sys/stat.h>
9 #include <unistd.h>
10
11 #include "acl-util.h"
12 #include "blkid-util.h"
13 #include "errno-util.h"
14 #include "fd-util.h"
15 #include "label-util.h"
16 #include "libmount-util.h"
17 #include "log.h"
18 #include "module-util.h"
19 #include "process-util.h"
20 #include "rlimit-util.h"
21 #include "terminal-util.h"
22 #include "tpm2-util.h"
23 #include "udev-config.h"
24 #include "udev-manager.h"
25 #include "udevd.h"
26 #include "version.h"
27
28 int run_udevd(int argc, char *argv[]) {
29 _cleanup_(manager_freep) Manager *manager = NULL;
30 int r;
31
32 log_setup();
33
34 manager = manager_new();
35 if (!manager)
36 return log_oom();
37
38 r = manager_load(manager, argc, argv);
39 if (r <= 0)
40 return r;
41
42 r = must_be_root();
43 if (r < 0)
44 return r;
45
46 /* set umask before creating any file/directory */
47 umask(022);
48
49 r = mac_init();
50 if (r < 0)
51 return r;
52
53 /* Make sure we can have plenty fds (for example for pidfds) */
54 (void) rlimit_nofile_bump(-1);
55
56 r = RET_NERRNO(mkdir("/run/udev", 0755));
57 if (r < 0 && r != -EEXIST)
58 return log_error_errno(r, "Failed to create /run/udev: %m");
59
60 /* Load some shared libraries before we fork any workers */
61 (void) DLOPEN_LIBACL(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
62 (void) DLOPEN_LIBBLKID(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
63 (void) DLOPEN_LIBKMOD(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
64 (void) DLOPEN_LIBMOUNT(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
65 (void) DLOPEN_TPM2(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
66
67 if (arg_daemonize) {
68 pid_t pid;
69
70 log_info("Starting systemd-udevd version " GIT_VERSION);
71
72 /* connect /dev/null to stdin, stdout, stderr */
73 if (log_get_max_level() < LOG_DEBUG) {
74 r = make_null_stdio();
75 if (r < 0)
76 log_warning_errno(r, "Failed to redirect standard streams to /dev/null: %m");
77 }
78
79 pid = fork();
80 if (pid < 0)
81 return log_error_errno(errno, "Failed to fork daemon: %m");
82 if (pid > 0)
83 /* parent */
84 return 0;
85
86 /* child */
87 terminal_detach_session();
88 }
89
90 return manager_main(manager);
91 }