dinit.cc 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. #include <iostream>
  2. #include <fstream>
  3. #include <list>
  4. #include <cstring>
  5. #include <csignal>
  6. #include <cstddef>
  7. #include <cstdlib>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <sys/un.h>
  11. #include <sys/socket.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <pwd.h>
  15. #include <termios.h>
  16. #ifdef __linux__
  17. #include <sys/prctl.h>
  18. #include <sys/klog.h>
  19. #include <sys/reboot.h>
  20. #endif
  21. #if defined(__FreeBSD__) || defined(__DragonFly__)
  22. #include <sys/procctl.h>
  23. #endif
  24. #include <dasynq.h>
  25. #include "dinit.h"
  26. #include "service.h"
  27. #include "control.h"
  28. #include "dinit-log.h"
  29. #include "dinit-socket.h"
  30. #include "static-string.h"
  31. #include "dinit-utmp.h"
  32. #include "dinit-env.h"
  33. #include "options-processing.h"
  34. #include "mconfig.h"
  35. /*
  36. * When running as the system init process, Dinit processes the following signals:
  37. *
  38. * SIGTERM - roll back services and then fork/exec /sbin/halt
  39. * SIGINT - roll back services and then fork/exec /sbin/reboot
  40. * SIGQUIT - exec() /sbin/shutdown without rolling back services
  41. *
  42. * It's an open question about whether Dinit should roll back services *before*
  43. * running halt/reboot, since those commands should prompt rollback of services
  44. * anyway. But it seems safe to do so, and it means the user can at least stop
  45. * services even if the halt/reboot commands are unavailable for some reason.
  46. */
  47. using namespace cts;
  48. using eventloop_t = dasynq::event_loop<dasynq::null_mutex>;
  49. eventloop_t event_loop(dasynq::delayed_init {});
  50. static void sigint_reboot_cb(eventloop_t &eloop) noexcept;
  51. static void sigquit_cb(eventloop_t &eloop) noexcept;
  52. static void sigterm_cb(eventloop_t &eloop) noexcept;
  53. static bool open_control_socket(bool report_ro_failure = true) noexcept;
  54. static void close_control_socket() noexcept;
  55. static void control_socket_ready() noexcept;
  56. static void confirm_restart_boot() noexcept;
  57. static void flush_log() noexcept;
  58. static void control_socket_cb(eventloop_t *loop, int fd) noexcept;
  59. #ifdef SUPPORT_CGROUPS
  60. static void find_cgroup_path() noexcept;
  61. #endif
  62. static void printVersion();
  63. // Variables
  64. static dirload_service_set *services;
  65. static bool am_system_mgr = false; // true if we are PID 1
  66. static bool am_system_init = false; // true if we are the system init process
  67. static bool did_log_boot = false;
  68. static bool control_socket_open = false;
  69. bool external_log_open = false;
  70. int active_control_conns = 0;
  71. int socket_ready_fd = -1;
  72. sigset_t orig_signal_mask; // signal mask when started
  73. // Control socket path. We maintain a string (control_socket_str) in case we need
  74. // to allocate storage, but control_socket_path is the authoritative value.
  75. static const char *control_socket_path = SYSCONTROLSOCKET;
  76. static std::string control_socket_str;
  77. static const char *env_file_path = "/etc/dinit/environment";
  78. static const char *log_path = "/dev/log";
  79. static bool log_is_syslog = true; // if false, log is a file
  80. // Set to true (when console_input_watcher is active) if console input becomes available
  81. static bool console_input_ready = false;
  82. #ifdef SUPPORT_CGROUPS
  83. // Path of the root cgroup according to dinit. This will be dinit's own cgroup path.
  84. std::string cgroups_path;
  85. bool have_cgroups_path = false;
  86. #endif
  87. namespace {
  88. // Event-loop handler for a signal, which just delegates to a function (pointer).
  89. class callback_signal_handler : public eventloop_t::signal_watcher_impl<callback_signal_handler>
  90. {
  91. using rearm = dasynq::rearm;
  92. public:
  93. typedef void (*cb_func_t)(eventloop_t &);
  94. private:
  95. cb_func_t cb_func;
  96. public:
  97. callback_signal_handler() : cb_func(nullptr) { }
  98. callback_signal_handler(cb_func_t pcb_func) : cb_func(pcb_func) { }
  99. void set_cb_func(cb_func_t cb_func)
  100. {
  101. this->cb_func = cb_func;
  102. }
  103. rearm received(eventloop_t &eloop, int signo, siginfo_p siginfo)
  104. {
  105. cb_func(eloop);
  106. return rearm::REARM;
  107. }
  108. };
  109. // Event-loop handler for when a connection is made to the control socket.
  110. class control_socket_watcher : public eventloop_t::fd_watcher_impl<control_socket_watcher>
  111. {
  112. using rearm = dasynq::rearm;
  113. public:
  114. rearm fd_event(eventloop_t &loop, int fd, int flags) noexcept
  115. {
  116. control_socket_cb(&loop, fd);
  117. return rearm::REARM;
  118. }
  119. };
  120. // Watch for console input and set a flag when it is available.
  121. class console_input_watcher : public eventloop_t::fd_watcher_impl<console_input_watcher>
  122. {
  123. using rearm = dasynq::rearm;
  124. public:
  125. rearm fd_event(eventloop_t &loop, int fd, int flags) noexcept
  126. {
  127. console_input_ready = true;
  128. return rearm::DISARM;
  129. }
  130. };
  131. // Simple timer used to limit the amount of time waiting for the log flush to complete (at shutdown)
  132. class log_flush_timer_t : public eventloop_t::timer_impl<log_flush_timer_t>
  133. {
  134. using rearm = dasynq::rearm;
  135. bool expired;
  136. public:
  137. rearm timer_expiry(eventloop_t &, int expiry_count)
  138. {
  139. expired = true;
  140. return rearm::DISARM;
  141. }
  142. bool has_expired()
  143. {
  144. return expired;
  145. }
  146. void reset()
  147. {
  148. expired = false;
  149. }
  150. };
  151. control_socket_watcher control_socket_io;
  152. console_input_watcher console_input_io;
  153. log_flush_timer_t log_flush_timer;
  154. // These need to be at namespace scope to prevent causing stack allocations when using them:
  155. constexpr auto shutdown_exec = literal(SBINDIR) + "/" + SHUTDOWN_PREFIX + "shutdown";
  156. constexpr auto error_exec_sd = literal("Error executing ") + shutdown_exec + ": ";
  157. }
  158. // Options handled in dinit_main
  159. struct options {
  160. const char * env_file = nullptr;
  161. bool control_socket_path_set = false;
  162. bool env_file_set = false;
  163. bool log_specified = false;
  164. bool process_sys_args = false;
  165. service_dir_opt service_dir_opts;
  166. // list of services to start
  167. std::list<const char *> services_to_start;
  168. };
  169. // Process a command line argument (and possibly its follow-up value)
  170. // Returns -1 for clean exit required, 0 for success, 1 for error exit required
  171. static int process_commandline_arg(char **argv, int argc, int &i, options &opts)
  172. {
  173. using std::cerr;
  174. using std::cout;
  175. using std::endl;
  176. using std::list;
  177. const char * &env_file = opts.env_file;
  178. bool &control_socket_path_set = opts.control_socket_path_set;
  179. bool &env_file_set = opts.env_file_set;
  180. bool &log_specified = opts.log_specified;
  181. service_dir_opt &service_dir_opts = opts.service_dir_opts;
  182. list<const char *> &services_to_start = opts.services_to_start;
  183. if (argv[i][0] == '-') {
  184. // An option...
  185. if (strcmp(argv[i], "--env-file") == 0 || strcmp(argv[i], "-e") == 0) {
  186. if (++i < argc && argv[i][0] != '\0') {
  187. env_file_set = true;
  188. env_file = argv[i];
  189. }
  190. else {
  191. cerr << "dinit: '--env-file' (-e) requires an argument\n";
  192. return 1;
  193. }
  194. }
  195. else if (strcmp(argv[i], "--services-dir") == 0 || strcmp(argv[i], "-d") == 0) {
  196. if (++i < argc && argv[i][0] != '\0') {
  197. service_dir_opts.set_specified_service_dir(argv[i]);
  198. }
  199. else {
  200. cerr << "dinit: '--services-dir' (-d) requires an argument\n";
  201. return 1;
  202. }
  203. }
  204. else if (strcmp(argv[i], "--system") == 0 || strcmp(argv[i], "-s") == 0) {
  205. am_system_init = true;
  206. }
  207. else if (strcmp(argv[i], "--system-mgr") == 0 || strcmp(argv[i], "-m") == 0) {
  208. am_system_mgr = true;
  209. opts.process_sys_args = false;
  210. }
  211. else if (strcmp(argv[i], "--user") == 0 || strcmp(argv[i], "-u") == 0) {
  212. am_system_init = false;
  213. }
  214. else if (strcmp(argv[i], "--container") == 0 || strcmp(argv[i], "-o") == 0) {
  215. am_system_mgr = false;
  216. opts.process_sys_args = false;
  217. }
  218. else if (strcmp(argv[i], "--socket-path") == 0 || strcmp(argv[i], "-p") == 0) {
  219. if (++i < argc && argv[i][0] != '\0') {
  220. control_socket_path = argv[i];
  221. control_socket_path_set = true;
  222. }
  223. else {
  224. cerr << "dinit: '--socket-path' (-p) requires an argument\n";
  225. return 1;
  226. }
  227. }
  228. else if (strcmp(argv[i], "--ready-fd") == 0 || strcmp(argv[i], "-F") == 0) {
  229. if (++i < argc) {
  230. char *endp = nullptr;
  231. auto fdn = strtoul(argv[i], &endp, 10);
  232. if (endp == argv[i] || *endp) {
  233. cerr << "dinit: '--ready-fd' (-F) requires a numerical argument\n";
  234. return 1;
  235. }
  236. socket_ready_fd = int(fdn);
  237. auto fl = fcntl(socket_ready_fd, F_GETFD);
  238. // We also want to make sure stdin is not allowed
  239. if (socket_ready_fd == 0 || fl < 0) {
  240. cerr << "dinit: '--ready-fd' (-F) requires an open file descriptor\n";
  241. return 1;
  242. }
  243. // Leave standard file descriptors alone, but make sure
  244. // anything else is not leaked to child processes
  245. if (socket_ready_fd > 2) {
  246. fcntl(socket_ready_fd, F_SETFD, FD_CLOEXEC | fl);
  247. }
  248. }
  249. else {
  250. cerr << "dinit: '--ready-fd' (-F) requires an argument\n";
  251. return 1;
  252. }
  253. }
  254. else if (strcmp(argv[i], "--log-file") == 0 || strcmp(argv[i], "-l") == 0) {
  255. if (++i < argc && argv[i][0] != '\0') {
  256. log_path = argv[i];
  257. log_is_syslog = false;
  258. log_specified = true;
  259. }
  260. else {
  261. cerr << "dinit: '--log-file' (-l) requires an argument\n";
  262. return 1;
  263. }
  264. }
  265. else if (strcmp(argv[i], "--quiet") == 0 || strcmp(argv[i], "-q") == 0) {
  266. console_service_status = false;
  267. log_level[DLOG_CONS] = loglevel_t::ZERO;
  268. }
  269. #ifdef SUPPORT_CGROUPS
  270. else if (strcmp(argv[i], "--cgroup-path") == 0 || strcmp(argv[i], "-b") == 0) {
  271. if (++i < argc && argv[i][0] != '\0') {
  272. cgroups_path = argv[i];
  273. have_cgroups_path = true;
  274. }
  275. else {
  276. cerr << "dinit: '--cgroup-path' (-b) requires an argument\n";
  277. return 1;
  278. }
  279. }
  280. #endif
  281. else if (strcmp(argv[i], "--service") == 0 || strcmp(argv[i], "-t") == 0) {
  282. if (++i < argc && argv[i][0] != '\0') {
  283. services_to_start.push_back(argv[i]);
  284. }
  285. else {
  286. cerr << "dinit: '--service' (-t) requires an argument\n";
  287. return 1;
  288. }
  289. }
  290. else if (strcmp(argv[i], "--version") == 0) {
  291. printVersion();
  292. return -1;
  293. }
  294. else if (strcmp(argv[i], "--help") == 0) {
  295. cout << "dinit: init/service manager daemon\n"
  296. " --help display (this) help\n"
  297. " --version display version\n"
  298. " --env-file <file>, -e <file>\n"
  299. " environment variable initialisation file\n"
  300. " --services-dir <dir>, -d <dir>\n"
  301. " set base directory for service description\n"
  302. " files, can be specified multiple times\n"
  303. " --system, -s run as the system service manager\n"
  304. " --system-mgr, -m run as system manager (perform shutdown etc)\n"
  305. " --user, -u run as a user service manager\n"
  306. " --container, -o run in container mode (do not manage system)\n"
  307. " --socket-path <path>, -p <path>\n"
  308. " path to control socket\n"
  309. " --ready-fd <fd>, -F <fd>\n"
  310. " file descriptor to report readiness\n"
  311. #ifdef SUPPORT_CGROUPS
  312. " --cgroup-path <path>, -b <path>\n"
  313. " cgroup base path (for resolving relative paths)\n"
  314. #endif
  315. " --log-file <file>, -l <file> log to the specified file\n"
  316. " --quiet, -q disable output to standard output\n"
  317. " <service-name>, --service <service-name>, -t <service-name>\n"
  318. " start service with name <service-name>\n";
  319. return -1;
  320. }
  321. else {
  322. // unrecognized
  323. if (!opts.process_sys_args) {
  324. cerr << "dinit: unrecognized option: " << argv[i] << endl;
  325. return 1;
  326. }
  327. }
  328. }
  329. else {
  330. if (argv[i][0] == '\0') {
  331. cerr << "dinit: error: empty command-line argument\n";
  332. return 1;
  333. }
  334. #ifdef __linux__
  335. // If we are running as init (PID=1), the Linux kernel gives us all command line arguments it was
  336. // given but didn't recognize, and, uh, *some* that it did recognize, which means we can't assume
  337. // that anything is a service name (for example "nopti" seems to get passed through to init).
  338. // However, we can look for special names that we know aren't kernel parameters, such as "single".
  339. //
  340. // (Note this may have been fixed in recent kernels: see changelog for 5.15.46/5.18.3,
  341. // "x86: Fix return value of __setup handlers")
  342. //
  343. // LILO puts "auto" on the command line for unattended boots, but we don't care about that and want
  344. // it filtered.
  345. //
  346. // We don't expect to see options beginning with '-' appear on the kernel command line either, so we
  347. // can interpret those as dinit arguments. In particular if we see -m or -o, we assume that every
  348. // name we see from then is a service name (i.e. process_sys_args is set false when we seem them,
  349. // see above).
  350. //
  351. // (Note, you can give "--" on the kernel command line to pass every option from that point to init
  352. // directly, but init doesn't see the "--" itself, which makes it less useful, since we still can't
  353. // tell whether a "name" was intended as a kernel parameter or init parameter).
  354. // So, long story short: if we think we're PID 1 and we haven't seen -m or -c options yet, only
  355. // recognise "single" as a service name and ignore everything else.
  356. if (!opts.process_sys_args || strcmp(argv[i], "single") == 0) {
  357. services_to_start.push_back(argv[i]);
  358. }
  359. #else
  360. services_to_start.push_back(argv[i]);
  361. #endif
  362. }
  363. return 0;
  364. }
  365. // Main entry point
  366. int dinit_main(int argc, char **argv)
  367. {
  368. using namespace std;
  369. am_system_mgr = (getpid() == 1);
  370. am_system_init = (getuid() == 0);
  371. struct options opts;
  372. // if we are PID 1 and user id 0, we are *most probably* the system init. (Or on linux at least, we
  373. // could instead be in a container; then we expect -o argument and unset this then).
  374. opts.process_sys_args = am_system_mgr && am_system_init;
  375. const char * &env_file = opts.env_file;
  376. bool &control_socket_path_set = opts.control_socket_path_set;
  377. bool &env_file_set = opts.env_file_set;
  378. bool &log_specified = opts.log_specified;
  379. service_dir_opt &service_dir_opts = opts.service_dir_opts;
  380. list<const char *> &services_to_start = opts.services_to_start;
  381. for (int i = 1; i < argc; i++) {
  382. int p = process_commandline_arg(argv, argc, i, opts);
  383. if (p == -1) {
  384. // clean exit
  385. return 0;
  386. }
  387. if (p == 1) {
  388. // error exit
  389. return 1;
  390. }
  391. }
  392. if (am_system_mgr) {
  393. // setup STDIN, STDOUT, STDERR so that we can use them
  394. int onefd = open("/dev/console", O_RDONLY, 0);
  395. if (onefd != -1) {
  396. dup2(onefd, 0);
  397. }
  398. int twofd = open("/dev/console", O_RDWR, 0);
  399. if (twofd != -1) {
  400. dup2(twofd, 1);
  401. dup2(twofd, 2);
  402. }
  403. if (onefd > 2) close(onefd);
  404. if (twofd > 2) close(twofd);
  405. if (! env_file_set) {
  406. env_file = env_file_path;
  407. }
  408. // we will assume an empty cgroups root path
  409. #if SUPPORT_CGROUPS
  410. have_cgroups_path = true;
  411. #endif
  412. }
  413. /* Set up signal handlers etc */
  414. sigset_t sigwait_set;
  415. if (am_system_mgr) {
  416. // Block all signals in system manager mode - don't want to chance provoking a signal that
  417. // will suspend or terminate the process
  418. sigfillset(&sigwait_set);
  419. }
  420. else {
  421. sigemptyset(&sigwait_set);
  422. sigaddset(&sigwait_set, SIGCHLD);
  423. sigaddset(&sigwait_set, SIGINT);
  424. sigaddset(&sigwait_set, SIGTERM);
  425. }
  426. sigprocmask(SIG_BLOCK, &sigwait_set, &orig_signal_mask);
  427. // Terminal access control signals - we ignore these so that dinit can't be
  428. // suspended if it writes to the terminal after some other process has claimed
  429. // ownership of it.
  430. signal(SIGTSTP, SIG_IGN);
  431. signal(SIGTTIN, SIG_IGN);
  432. signal(SIGTTOU, SIG_IGN);
  433. signal(SIGPIPE, SIG_IGN);
  434. event_loop.init();
  435. if (!am_system_init && !control_socket_path_set) {
  436. const char * rundir = getenv("XDG_RUNTIME_DIR");
  437. const char * sockname = "dinitctl";
  438. if (rundir == nullptr) {
  439. rundir = service_dir_opt::get_user_home();
  440. sockname = ".dinitctl";
  441. }
  442. if (rundir != nullptr) {
  443. control_socket_str = rundir;
  444. control_socket_str.push_back('/');
  445. control_socket_str += sockname;
  446. control_socket_path = control_socket_str.c_str();
  447. }
  448. }
  449. if (services_to_start.empty()) {
  450. services_to_start.push_back("boot");
  451. }
  452. // Set up signal handlers
  453. callback_signal_handler sigterm_watcher {sigterm_cb};
  454. callback_signal_handler sigint_watcher;
  455. callback_signal_handler sigquit_watcher;
  456. if (am_system_mgr) {
  457. sigint_watcher.set_cb_func(sigint_reboot_cb);
  458. sigquit_watcher.set_cb_func(sigquit_cb);
  459. }
  460. else {
  461. sigint_watcher.set_cb_func(sigterm_cb);
  462. }
  463. sigint_watcher.add_watch(event_loop, SIGINT);
  464. sigterm_watcher.add_watch(event_loop, SIGTERM);
  465. if (am_system_mgr) {
  466. // PID 1: we may ask for console input; SIGQUIT exec's shutdown
  467. console_input_io.add_watch(event_loop, STDIN_FILENO, dasynq::IN_EVENTS, false);
  468. sigquit_watcher.add_watch(event_loop, SIGQUIT);
  469. // (If not PID 1, we instead just let SIGQUIT perform the default action.)
  470. }
  471. init_log(log_is_syslog);
  472. log_flush_timer.add_timer(event_loop, dasynq::clock_type::MONOTONIC);
  473. #if SUPPORT_CGROUPS
  474. if (!have_cgroups_path) {
  475. find_cgroup_path();
  476. // We will press on if the cgroup root path could not be identified, since services might
  477. // not require cgroups anyway and/or might only specify absolute cgroups paths.
  478. }
  479. #endif
  480. // Try to open control socket (may fail due to readonly filesystem, we ignore that if we are
  481. // system init)
  482. if (!open_control_socket(!am_system_init)) {
  483. flush_log();
  484. return EXIT_FAILURE;
  485. }
  486. #ifdef __linux__
  487. if (am_system_mgr) {
  488. // Disable non-critical kernel output to console
  489. klogctl(6 /* SYSLOG_ACTION_CONSOLE_OFF */, nullptr, 0);
  490. // Make ctrl+alt+del combination send SIGINT to PID 1 (this process)
  491. reboot(RB_DISABLE_CAD);
  492. }
  493. // Mark ourselves as a subreaper. This means that if a process we start double-forks, the
  494. // orphaned child will re-parent to us rather than to PID 1 (although that could be us too).
  495. prctl(PR_SET_CHILD_SUBREAPER, 1);
  496. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  497. // Documentation (man page) for this kind-of sucks. PROC_REAP_ACQUIRE "acquires the reaper status for
  498. // the current process" but does that mean the first two arguments still need valid values to be
  499. // supplied? We'll play it safe and explicitly target our own process:
  500. procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL);
  501. #endif
  502. service_dir_opts.build_paths(am_system_init);
  503. // Start requested services
  504. services = new dirload_service_set(std::move(service_dir_opts.get_paths()));
  505. setup_log_console_handoff(services);
  506. if (am_system_init) {
  507. log(loglevel_t::NOTICE, false, "Starting system");
  508. }
  509. // If a log file was specified, open it now.
  510. if (log_specified) {
  511. setup_external_log();
  512. if (!am_system_init && !external_log_open) {
  513. flush_log(); // flush console messages
  514. return EXIT_FAILURE;
  515. }
  516. }
  517. if (env_file != nullptr) {
  518. read_env_file(env_file, true, main_env, false, nullptr);
  519. }
  520. for (auto svc : services_to_start) {
  521. try {
  522. services->start_service(svc);
  523. // Note in general if we fail to start a service we don't need any special error handling,
  524. // since we either leave other services running or, if it was the only service, then no
  525. // services will be running and we will process normally (reboot if system process,
  526. // exit if user process).
  527. }
  528. catch (service_not_found &snf) {
  529. log(loglevel_t::ERROR, snf.service_name, ": could not find service description.");
  530. }
  531. catch (service_description_exc &sde) {
  532. log_service_load_failure(sde);
  533. }
  534. catch (service_load_exc &sle) {
  535. log(loglevel_t::ERROR, sle.service_name, ": error loading: ", sle.exc_description);
  536. }
  537. catch (std::bad_alloc &badalloce) {
  538. log(loglevel_t::ERROR, "Out of memory when trying to start service: ", svc, ".");
  539. break;
  540. }
  541. }
  542. // Notify readiness just before the event loop starts (and after services
  543. // are scheduled to start). If the socket is not ready yet (may be in case
  544. // of read-only file system), we will report it when it is.
  545. control_socket_ready();
  546. run_event_loop:
  547. // Process events until all services have terminated.
  548. while (services->count_active_services() != 0) {
  549. event_loop.run();
  550. }
  551. shutdown_type_t shutdown_type = services->get_shutdown_type();
  552. if (shutdown_type == shutdown_type_t::REMAIN) {
  553. goto run_event_loop;
  554. }
  555. if (am_system_mgr) {
  556. log_msg_begin(loglevel_t::NOTICE, "No more active services.");
  557. if (shutdown_type == shutdown_type_t::REBOOT) {
  558. log_msg_end(" Will reboot.");
  559. }
  560. else if (shutdown_type == shutdown_type_t::HALT) {
  561. log_msg_end(" Will halt.");
  562. }
  563. else if (shutdown_type == shutdown_type_t::POWEROFF) {
  564. log_msg_end(" Will power down.");
  565. }
  566. }
  567. flush_log();
  568. close_control_socket();
  569. if (am_system_mgr) {
  570. if (shutdown_type == shutdown_type_t::NONE) {
  571. // Services all stopped but there was no shutdown issued. Inform user, wait for ack, and
  572. // re-start boot sequence.
  573. sync(); // Sync to minimise data loss if user elects to power off / hard reset
  574. confirm_restart_boot();
  575. if (services->count_active_services() != 0) {
  576. // Recovery service started
  577. goto run_event_loop;
  578. }
  579. shutdown_type = services->get_shutdown_type();
  580. if (shutdown_type == shutdown_type_t::NONE) {
  581. try {
  582. services->start_service("boot");
  583. goto run_event_loop; // yes, the "evil" goto
  584. }
  585. catch (...) {
  586. // Couldn't start boot service, let's reboot the system
  587. log(loglevel_t::ERROR, "Could not start 'boot' service. Will attempt reboot.");
  588. shutdown_type = shutdown_type_t::REBOOT;
  589. }
  590. }
  591. }
  592. const char * cmd_arg;
  593. if (shutdown_type == shutdown_type_t::HALT) {
  594. cmd_arg = "-h";
  595. }
  596. else if (shutdown_type == shutdown_type_t::REBOOT) {
  597. cmd_arg = "-r";
  598. }
  599. else {
  600. // power off.
  601. cmd_arg = "-p";
  602. }
  603. // Fork and execute dinit-reboot.
  604. execl(shutdown_exec.c_str(), shutdown_exec.c_str(), "--system", cmd_arg, nullptr);
  605. log(loglevel_t::ERROR, error_exec_sd, strerror(errno));
  606. // PID 1 must not actually exit, although we should never reach this point:
  607. while (true) {
  608. event_loop.run();
  609. }
  610. }
  611. else if (shutdown_type == shutdown_type_t::REBOOT) {
  612. // Non-system-process. If we got SIGINT, let's die due to it:
  613. sigset_t sigwait_set_int;
  614. sigemptyset(&sigwait_set_int);
  615. sigaddset(&sigwait_set_int, SIGINT);
  616. raise(SIGINT);
  617. sigprocmask(SIG_UNBLOCK, &sigwait_set_int, NULL);
  618. }
  619. return EXIT_SUCCESS;
  620. }
  621. // Get user confirmation before proceeding with restarting boot sequence.
  622. // Returns after confirmation, possibly with shutdown type altered.
  623. static void confirm_restart_boot() noexcept
  624. {
  625. // Bypass log; we want to make certain the message is seen:
  626. std::cout << "All services have stopped with no shutdown issued; boot failure?\n";
  627. // Drain input, set non-canonical input mode (receive characters as they are typed)
  628. struct termios term_attr;
  629. if (tcgetattr(STDIN_FILENO, &term_attr) != 0) {
  630. // Not a terminal?
  631. std::cout << "Halting." << std::endl;
  632. services->stop_all_services(shutdown_type_t::HALT);
  633. return;
  634. }
  635. term_attr.c_lflag &= ~ICANON;
  636. tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attr);
  637. // Set non-blocking mode
  638. int origFlags = fcntl(STDIN_FILENO, F_GETFL);
  639. fcntl(STDIN_FILENO, F_SETFL, origFlags | O_NONBLOCK);
  640. do_prompt:
  641. std::cout << "Choose: (r)eboot, r(e)covery, re(s)tart boot sequence, (p)ower off? " << std::flush;
  642. console_input_io.set_enabled(event_loop, true);
  643. do {
  644. event_loop.run();
  645. } while (! console_input_ready && services->get_shutdown_type() == shutdown_type_t::NONE);
  646. console_input_io.set_enabled(event_loop, false);
  647. // We either have input, or shutdown type has been set, or both.
  648. if (console_input_ready) {
  649. console_input_ready = false;
  650. char buf[1];
  651. int r = read(STDIN_FILENO, buf, 1); // read a single character, to make sure we wait for input
  652. if (r == 1) {
  653. std::cout << "\n"; // force new line after input
  654. if (buf[0] == 'r' || buf[0] == 'R') {
  655. services->stop_all_services(shutdown_type_t::REBOOT);
  656. }
  657. else if (buf[0] == 'e' || buf[0] == 'E') {
  658. try {
  659. services->start_service("recovery");
  660. }
  661. catch (...) {
  662. std::cout << "Unable to start recovery service.\n";
  663. goto do_prompt;
  664. }
  665. }
  666. else if (buf[0] == 's' || buf[0] == 'S') {
  667. // nothing - leave no shutdown type
  668. }
  669. else if (buf[0] == 'p' || buf[0] == 'P') {
  670. services->stop_all_services(shutdown_type_t::POWEROFF);
  671. }
  672. else {
  673. goto do_prompt;
  674. }
  675. }
  676. tcflush(STDIN_FILENO, TCIFLUSH); // discard the rest of input
  677. }
  678. term_attr.c_lflag |= ICANON;
  679. tcsetattr(STDIN_FILENO, TCSANOW, &term_attr);
  680. fcntl(STDIN_FILENO, F_SETFL, origFlags);
  681. }
  682. // Callback for control socket
  683. static void control_socket_cb(eventloop_t *loop, int sockfd) noexcept
  684. {
  685. // Considered keeping a limit the number of active connections, however, there doesn't
  686. // seem much to be gained from that. Only root can create connections and not being
  687. // able to establish a control connection is as much a denial-of-service as is not being
  688. // able to start a service due to lack of fd's.
  689. // Accept a connection
  690. int newfd = dinit_accept4(sockfd, nullptr, nullptr, SOCK_NONBLOCK | SOCK_CLOEXEC);
  691. if (newfd != -1) {
  692. try {
  693. new control_conn_t(*loop, services, newfd); // will delete itself when it's finished
  694. }
  695. catch (std::exception &exc) {
  696. log(loglevel_t::ERROR, "Error accepting control connection: ", exc.what());
  697. close(newfd);
  698. }
  699. }
  700. }
  701. static void control_socket_ready() noexcept
  702. {
  703. if (!control_socket_open || socket_ready_fd < 0) {
  704. return;
  705. }
  706. write(socket_ready_fd, control_socket_path, strlen(control_socket_path) + 1);
  707. // Once done with, close it (but leave stdout/stderr alone)
  708. if (socket_ready_fd > 2) {
  709. close(socket_ready_fd);
  710. }
  711. // Ensure that we don't try to issue readiness again:
  712. socket_ready_fd = -1;
  713. }
  714. // Callback when the root filesystem is read/write:
  715. void rootfs_is_rw() noexcept
  716. {
  717. open_control_socket(true);
  718. control_socket_ready();
  719. if (!log_is_syslog && !external_log_open) {
  720. // Try (again) to open log file if we couldn't do so earlier.
  721. setup_external_log();
  722. }
  723. if (!did_log_boot) {
  724. did_log_boot = log_boot();
  725. }
  726. }
  727. // Open/create the control socket, normally /run/dinitctl, used to allow client programs to connect
  728. // and issue service orders and shutdown commands etc. This can safely be called multiple times;
  729. // once the socket has been successfully opened, further calls will check the socket file is still
  730. // present and re-create it if not.
  731. static bool open_control_socket(bool report_ro_failure) noexcept
  732. {
  733. if (control_socket_open) {
  734. struct stat stat_buf;
  735. if (stat(control_socket_path, &stat_buf) != 0 && errno == ENOENT) {
  736. // Looks like our control socket has disappeared from the filesystem. Close our control
  737. // socket and re-create it:
  738. control_socket_io.deregister(event_loop);
  739. close(control_socket_io.get_watched_fd());
  740. control_socket_open = false; // now re-open below
  741. }
  742. }
  743. if (!control_socket_open) {
  744. const char * saddrname = control_socket_path;
  745. size_t saddrname_len = strlen(saddrname);
  746. uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + saddrname_len + 1;
  747. struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
  748. if (name == nullptr) {
  749. log(loglevel_t::ERROR, "Opening control socket: out of memory");
  750. return false;
  751. }
  752. name->sun_family = AF_UNIX;
  753. memcpy(name->sun_path, saddrname, saddrname_len + 1);
  754. int sockfd = dinit_socket(AF_UNIX, SOCK_STREAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
  755. if (sockfd == -1) {
  756. log(loglevel_t::ERROR, "Error creating control socket: ", strerror(errno));
  757. free(name);
  758. return false;
  759. }
  760. // Check if there is already an active control socket (from another instance).
  761. // Unfortunately, there's no way to check atomically if a socket file is stale. Still, we
  762. // will try to check, since the consequences of running a system dinit instance twice are
  763. // potentially severe.
  764. int connr = connect(sockfd, (struct sockaddr *) name, sockaddr_size);
  765. if (connr != -1 || errno == EAGAIN) {
  766. log(loglevel_t::ERROR, "Control socket is already active"
  767. " (another instance already running?)");
  768. close(connr);
  769. close(sockfd);
  770. free(name);
  771. return false;
  772. }
  773. // Unlink any stale control socket file.
  774. //
  775. // In the worst case, this potentially removes a socket which was not active at the time
  776. // we checked (just above) but has since become active; there's just no good API to avoid
  777. // this (we'd have to use a file lock, on yet another file). Since that's unlikely to
  778. // occur in practice, and because a stale socket will prevent communication with dinit (or
  779. // prevent it starting), then we'll take the chance on unlinking here.
  780. unlink(saddrname);
  781. if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
  782. bool have_error = (errno != EROFS || report_ro_failure);
  783. if (have_error) {
  784. log(loglevel_t::ERROR, "Error binding control socket: ", strerror(errno));
  785. }
  786. close(sockfd);
  787. free(name);
  788. return !have_error;
  789. }
  790. free(name);
  791. // No connections can be made until we listen, so it is fine to change the permissions now
  792. // (and anyway there is no way to atomically create the socket and set permissions):
  793. if (chmod(saddrname, S_IRUSR | S_IWUSR) == -1) {
  794. log(loglevel_t::ERROR, "Error setting control socket permissions: ", strerror(errno));
  795. close(sockfd);
  796. return false;
  797. }
  798. if (listen(sockfd, 10) == -1) {
  799. log(loglevel_t::ERROR, "Error listening on control socket: ", strerror(errno));
  800. close(sockfd);
  801. return false;
  802. }
  803. try {
  804. control_socket_io.add_watch(event_loop, sockfd, dasynq::IN_EVENTS);
  805. control_socket_open = true;
  806. }
  807. catch (std::exception &e)
  808. {
  809. log(loglevel_t::ERROR, "Could not setup I/O on control socket: ", e.what());
  810. close(sockfd);
  811. }
  812. }
  813. return control_socket_open;
  814. }
  815. static void close_control_socket() noexcept
  816. {
  817. if (control_socket_open) {
  818. int fd = control_socket_io.get_watched_fd();
  819. control_socket_io.deregister(event_loop);
  820. close(fd);
  821. // Unlink the socket:
  822. unlink(control_socket_path);
  823. control_socket_open = false;
  824. }
  825. }
  826. void setup_external_log() noexcept
  827. {
  828. if (!external_log_open) {
  829. if (log_is_syslog) {
  830. const char * saddrname = log_path;
  831. size_t saddrname_len = strlen(saddrname);
  832. uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + saddrname_len + 1;
  833. struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
  834. if (name == nullptr) {
  835. log(loglevel_t::ERROR, "Connecting to log socket: out of memory");
  836. return;
  837. }
  838. name->sun_family = AF_UNIX;
  839. memcpy(name->sun_path, saddrname, saddrname_len + 1);
  840. int sockfd = dinit_socket(AF_UNIX, SOCK_DGRAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
  841. if (sockfd == -1) {
  842. log(loglevel_t::ERROR, "Error creating log socket: ", strerror(errno));
  843. free(name);
  844. return;
  845. }
  846. if (connect(sockfd, (struct sockaddr *) name, sockaddr_size) == 0 || errno == EINPROGRESS) {
  847. // For EINPROGRESS, connection is still being established; however, we can select on
  848. // the file descriptor so we will be notified when it's ready. In other words we can
  849. // basically use it anyway.
  850. try {
  851. setup_main_log(sockfd);
  852. external_log_open = true;
  853. }
  854. catch (std::exception &e) {
  855. log(loglevel_t::ERROR, "Setting up log failed: ", e.what());
  856. close(sockfd);
  857. }
  858. }
  859. else {
  860. // Note if connect fails, we haven't warned at all, because the syslog server might not
  861. // have started yet.
  862. close(sockfd);
  863. }
  864. free(name);
  865. }
  866. else {
  867. // log to file:
  868. int log_fd = open(log_path, O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK | O_CLOEXEC, 0644);
  869. if (log_fd >= 0) {
  870. try {
  871. setup_main_log(log_fd);
  872. external_log_open = true;
  873. }
  874. catch (std::exception &e) {
  875. log(loglevel_t::ERROR, "Setting up log failed: ", e.what());
  876. close(log_fd);
  877. }
  878. }
  879. else {
  880. // log failure to log? It makes more sense than first appears, because we also log
  881. // to console:
  882. log(loglevel_t::ERROR, "Opening log file failed: ", strerror(errno));
  883. }
  884. }
  885. }
  886. }
  887. static void flush_log() noexcept
  888. {
  889. log_flush_timer.reset();
  890. log_flush_timer.arm_timer_rel(event_loop, timespec{5,0}); // 5 seconds
  891. while (!is_log_flushed() && !log_flush_timer.has_expired()) {
  892. event_loop.run();
  893. }
  894. }
  895. #ifdef SUPPORT_CGROUPS
  896. static void find_cgroup_path() noexcept
  897. {
  898. if (have_cgroups_path) {
  899. return;
  900. }
  901. int pfd = open("/proc/self/cgroup", O_RDONLY);
  902. if (pfd == -1) {
  903. return;
  904. }
  905. try {
  906. size_t cgroup_line_sz = 64;
  907. size_t cur_read = 0;
  908. size_t line_end_pos = (size_t)-1;
  909. size_t colon_count = 0; // how many colons have we seen?
  910. size_t second_colon_pos = 0;
  911. std::vector<char, default_init_allocator<char>> cgroup_line(cgroup_line_sz);
  912. while (true) {
  913. ssize_t r = read(pfd, cgroup_line.data() + cur_read, cgroup_line_sz - cur_read);
  914. if (r == 0) {
  915. if (line_end_pos == (size_t)-1) {
  916. line_end_pos = cur_read + 1;
  917. }
  918. break;
  919. }
  920. if (r == -1) {
  921. close(pfd);
  922. return;
  923. }
  924. size_t rr = (size_t)r;
  925. for (size_t i = 0; i < rr; ++i) {
  926. if (cgroup_line[cur_read + i] == '\n') {
  927. line_end_pos = cur_read + i;
  928. }
  929. else if (line_end_pos != (size_t)-1) {
  930. log(loglevel_t::WARN, "In multiple cgroups, cannot determine cgroup root path");
  931. close(pfd);
  932. return;
  933. }
  934. else if (cgroup_line[cur_read + i] == ':') {
  935. if (++colon_count == 2) {
  936. second_colon_pos = cur_read + i;
  937. }
  938. }
  939. }
  940. cur_read += rr;
  941. if (line_end_pos != (size_t)-1) {
  942. break;
  943. }
  944. if (cur_read == cgroup_line_sz) {
  945. cgroup_line.resize(cgroup_line_sz * 2);
  946. cgroup_line_sz *= 2;
  947. }
  948. };
  949. close(pfd);
  950. pfd = -1;
  951. // Now extract the path
  952. // The group line should look something like:
  953. //
  954. // 0::/some/path
  955. //
  956. // We want "some/path", i.e. we'll skip the leading slash.
  957. if (colon_count < 2 || (line_end_pos - second_colon_pos) == 1
  958. || cgroup_line[second_colon_pos+1] != '/') {
  959. // path is from 2nd colon to end
  960. log(loglevel_t::WARN, "Could not determine cgroup root path");
  961. return;
  962. }
  963. cgroups_path.clear();
  964. size_t first_char_pos = second_colon_pos + 2;
  965. size_t root_path_len = line_end_pos - first_char_pos;
  966. cgroups_path.append(cgroup_line.data() + first_char_pos, root_path_len);
  967. have_cgroups_path = true;
  968. return;
  969. }
  970. catch (std::bad_alloc &b) {
  971. if (pfd != -1) {
  972. close(pfd);
  973. }
  974. log(loglevel_t::WARN, "Out-of-memory reading cgroup root path");
  975. return;
  976. }
  977. }
  978. #endif // SUPPORT_CGROUPS
  979. static void printVersion()
  980. {
  981. std::cout << "Dinit version " << DINIT_VERSION << '.' << std::endl;
  982. const unsigned feature_count = 0
  983. #ifdef SUPPORT_CGROUPS
  984. +1
  985. #endif
  986. #ifdef USE_UTMPX
  987. +1
  988. #endif
  989. #if USE_INITGROUPS
  990. +1
  991. #endif
  992. ;
  993. if (feature_count != 0) {
  994. std::cout << "Supported features:"
  995. #ifdef SUPPORT_CGROUPS
  996. " cgroups"
  997. #endif
  998. #ifdef USE_UTMPX
  999. " utmp"
  1000. #endif
  1001. #if USE_INITGROUPS
  1002. " supplemental-groups"
  1003. #endif
  1004. "\n";
  1005. }
  1006. }
  1007. /* handle SIGINT signal (generated by Linux kernel when ctrl+alt+del pressed) */
  1008. static void sigint_reboot_cb(eventloop_t &eloop) noexcept
  1009. {
  1010. services->stop_all_services(shutdown_type_t::REBOOT);
  1011. }
  1012. /* handle SIGQUIT (if we are system init) */
  1013. static void sigquit_cb(eventloop_t &eloop) noexcept
  1014. {
  1015. // This performs an immediate shutdown, without service rollback.
  1016. close_control_socket();
  1017. execl(shutdown_exec.c_str(), shutdown_exec.c_str(), "--system", (char *) 0);
  1018. log(loglevel_t::ERROR, error_exec_sd, strerror(errno));
  1019. sync(); // since a hard poweroff might be required at this point...
  1020. }
  1021. /* handle SIGTERM/SIGQUIT(non-system-daemon) - stop all services and shut down */
  1022. static void sigterm_cb(eventloop_t &eloop) noexcept
  1023. {
  1024. services->stop_all_services();
  1025. }