PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/systemd/src/update-utmp.c

https://review.tizen.org/git/
C | 420 lines | 297 code | 87 blank | 36 comment | 58 complexity | b4de78a036f500c924b63a84e5dcfbf1 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-3.0, GPL-2.0, MPL-2.0, JSON, WTFPL, CC-BY-SA-4.0, CC-BY-3.0, BSD-3-Clause, LGPL-2.0, MPL-2.0-no-copyleft-exception, AGPL-1.0, 0BSD, Zlib, Unlicense, BSD-2-Clause, Apache-2.0, LGPL-3.0, ISC, MIT, CC-BY-SA-3.0, CC0-1.0, LGPL-2.1
  1. /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
  2. /***
  3. This file is part of systemd.
  4. Copyright 2010 Lennart Poettering
  5. systemd is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. systemd is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with systemd; If not, see <http://www.gnu.org/licenses/>.
  15. ***/
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <dbus/dbus.h>
  22. #ifdef HAVE_AUDIT
  23. #include <libaudit.h>
  24. #endif
  25. #include "log.h"
  26. #include "macro.h"
  27. #include "util.h"
  28. #include "special.h"
  29. #include "utmp-wtmp.h"
  30. #include "dbus-common.h"
  31. typedef struct Context {
  32. DBusConnection *bus;
  33. #ifdef HAVE_AUDIT
  34. int audit_fd;
  35. #endif
  36. } Context;
  37. static usec_t get_startup_time(Context *c) {
  38. const char
  39. *interface = "org.freedesktop.systemd1.Manager",
  40. *property = "StartupTimestamp";
  41. DBusError error;
  42. usec_t t = 0;
  43. DBusMessage *m = NULL, *reply = NULL;
  44. DBusMessageIter iter, sub;
  45. dbus_error_init(&error);
  46. assert(c);
  47. if (!(m = dbus_message_new_method_call(
  48. "org.freedesktop.systemd1",
  49. "/org/freedesktop/systemd1",
  50. "org.freedesktop.DBus.Properties",
  51. "Get"))) {
  52. log_error("Could not allocate message.");
  53. goto finish;
  54. }
  55. if (!dbus_message_append_args(m,
  56. DBUS_TYPE_STRING, &interface,
  57. DBUS_TYPE_STRING, &property,
  58. DBUS_TYPE_INVALID)) {
  59. log_error("Could not append arguments to message.");
  60. goto finish;
  61. }
  62. if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
  63. log_error("Failed to send command: %s", bus_error_message(&error));
  64. goto finish;
  65. }
  66. if (!dbus_message_iter_init(reply, &iter) ||
  67. dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
  68. log_error("Failed to parse reply.");
  69. goto finish;
  70. }
  71. dbus_message_iter_recurse(&iter, &sub);
  72. if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT64) {
  73. log_error("Failed to parse reply.");
  74. goto finish;
  75. }
  76. dbus_message_iter_get_basic(&sub, &t);
  77. finish:
  78. if (m)
  79. dbus_message_unref(m);
  80. if (reply)
  81. dbus_message_unref(reply);
  82. dbus_error_free(&error);
  83. return t;
  84. }
  85. static int get_current_runlevel(Context *c) {
  86. static const struct {
  87. const int runlevel;
  88. const char *special;
  89. } table[] = {
  90. /* The first target of this list that is active or has
  91. * a job scheduled wins. We prefer runlevels 5 and 3
  92. * here over the others, since these are the main
  93. * runlevels used on Fedora. It might make sense to
  94. * change the order on some distributions. */
  95. { '5', SPECIAL_RUNLEVEL5_TARGET },
  96. { '3', SPECIAL_RUNLEVEL3_TARGET },
  97. { '4', SPECIAL_RUNLEVEL4_TARGET },
  98. { '2', SPECIAL_RUNLEVEL2_TARGET },
  99. { 'S', SPECIAL_RESCUE_TARGET },
  100. };
  101. const char
  102. *interface = "org.freedesktop.systemd1.Unit",
  103. *property = "ActiveState";
  104. DBusMessage *m = NULL, *reply = NULL;
  105. int r = 0;
  106. unsigned i;
  107. DBusError error;
  108. assert(c);
  109. dbus_error_init(&error);
  110. for (i = 0; i < ELEMENTSOF(table); i++) {
  111. const char *path = NULL, *state;
  112. DBusMessageIter iter, sub;
  113. if (!(m = dbus_message_new_method_call(
  114. "org.freedesktop.systemd1",
  115. "/org/freedesktop/systemd1",
  116. "org.freedesktop.systemd1.Manager",
  117. "GetUnit"))) {
  118. log_error("Could not allocate message.");
  119. r = -ENOMEM;
  120. goto finish;
  121. }
  122. if (!dbus_message_append_args(m,
  123. DBUS_TYPE_STRING, &table[i].special,
  124. DBUS_TYPE_INVALID)) {
  125. log_error("Could not append arguments to message.");
  126. r = -ENOMEM;
  127. goto finish;
  128. }
  129. if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
  130. dbus_error_free(&error);
  131. continue;
  132. }
  133. if (!dbus_message_get_args(reply, &error,
  134. DBUS_TYPE_OBJECT_PATH, &path,
  135. DBUS_TYPE_INVALID)) {
  136. log_error("Failed to parse reply: %s", bus_error_message(&error));
  137. r = -EIO;
  138. goto finish;
  139. }
  140. dbus_message_unref(m);
  141. if (!(m = dbus_message_new_method_call(
  142. "org.freedesktop.systemd1",
  143. path,
  144. "org.freedesktop.DBus.Properties",
  145. "Get"))) {
  146. log_error("Could not allocate message.");
  147. r = -ENOMEM;
  148. goto finish;
  149. }
  150. if (!dbus_message_append_args(m,
  151. DBUS_TYPE_STRING, &interface,
  152. DBUS_TYPE_STRING, &property,
  153. DBUS_TYPE_INVALID)) {
  154. log_error("Could not append arguments to message.");
  155. r = -ENOMEM;
  156. goto finish;
  157. }
  158. dbus_message_unref(reply);
  159. if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
  160. log_error("Failed to send command: %s", bus_error_message(&error));
  161. r = -EIO;
  162. goto finish;
  163. }
  164. if (!dbus_message_iter_init(reply, &iter) ||
  165. dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
  166. log_error("Failed to parse reply.");
  167. r = -EIO;
  168. goto finish;
  169. }
  170. dbus_message_iter_recurse(&iter, &sub);
  171. if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
  172. log_error("Failed to parse reply.");
  173. r = -EIO;
  174. goto finish;
  175. }
  176. dbus_message_iter_get_basic(&sub, &state);
  177. if (streq(state, "active") || streq(state, "reloading"))
  178. r = table[i].runlevel;
  179. dbus_message_unref(m);
  180. dbus_message_unref(reply);
  181. m = reply = NULL;
  182. if (r)
  183. break;
  184. }
  185. finish:
  186. if (m)
  187. dbus_message_unref(m);
  188. if (reply)
  189. dbus_message_unref(reply);
  190. dbus_error_free(&error);
  191. return r;
  192. }
  193. static int on_reboot(Context *c) {
  194. int r = 0, q;
  195. usec_t t;
  196. assert(c);
  197. /* We finished start-up, so let's write the utmp
  198. * record and send the audit msg */
  199. #ifdef HAVE_AUDIT
  200. if (c->audit_fd >= 0)
  201. if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "init", NULL, NULL, NULL, 1) < 0) {
  202. log_error("Failed to send audit message: %m");
  203. r = -errno;
  204. }
  205. #endif
  206. /* If this call fails it will return 0, which
  207. * utmp_put_reboot() will then fix to the current time */
  208. t = get_startup_time(c);
  209. if ((q = utmp_put_reboot(t)) < 0) {
  210. log_error("Failed to write utmp record: %s", strerror(-q));
  211. r = q;
  212. }
  213. return r;
  214. }
  215. static int on_shutdown(Context *c) {
  216. int r = 0, q;
  217. assert(c);
  218. /* We started shut-down, so let's write the utmp
  219. * record and send the audit msg */
  220. #ifdef HAVE_AUDIT
  221. if (c->audit_fd >= 0)
  222. if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "init", NULL, NULL, NULL, 1) < 0) {
  223. log_error("Failed to send audit message: %m");
  224. r = -errno;
  225. }
  226. #endif
  227. if ((q = utmp_put_shutdown(0)) < 0) {
  228. log_error("Failed to write utmp record: %s", strerror(-q));
  229. r = q;
  230. }
  231. return r;
  232. }
  233. static int on_runlevel(Context *c) {
  234. int r = 0, q, previous, runlevel;
  235. assert(c);
  236. /* We finished changing runlevel, so let's write the
  237. * utmp record and send the audit msg */
  238. /* First, get last runlevel */
  239. if ((q = utmp_get_runlevel(&previous, NULL)) < 0) {
  240. if (q != -ESRCH && q != -ENOENT) {
  241. log_error("Failed to get current runlevel: %s", strerror(-q));
  242. return q;
  243. }
  244. /* Hmm, we didn't find any runlevel, that means we
  245. * have been rebooted */
  246. r = on_reboot(c);
  247. previous = 0;
  248. }
  249. /* Secondly, get new runlevel */
  250. if ((runlevel = get_current_runlevel(c)) < 0)
  251. return runlevel;
  252. if (previous == runlevel)
  253. return 0;
  254. #ifdef HAVE_AUDIT
  255. if (c->audit_fd >= 0) {
  256. char *s = NULL;
  257. if (asprintf(&s, "old-level=%c new-level=%c",
  258. previous > 0 ? previous : 'N',
  259. runlevel > 0 ? runlevel : 'N') < 0)
  260. return -ENOMEM;
  261. if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0) {
  262. log_error("Failed to send audit message: %m");
  263. r = -errno;
  264. }
  265. free(s);
  266. }
  267. #endif
  268. if ((q = utmp_put_runlevel(0, runlevel, previous)) < 0) {
  269. log_error("Failed to write utmp record: %s", strerror(-q));
  270. r = q;
  271. }
  272. return r;
  273. }
  274. int main(int argc, char *argv[]) {
  275. int r;
  276. DBusError error;
  277. Context c;
  278. dbus_error_init(&error);
  279. zero(c);
  280. #ifdef HAVE_AUDIT
  281. c.audit_fd = -1;
  282. #endif
  283. if (getppid() != 1) {
  284. log_error("This program should be invoked by init only.");
  285. return EXIT_FAILURE;
  286. }
  287. if (argc != 2) {
  288. log_error("This program requires one argument.");
  289. return EXIT_FAILURE;
  290. }
  291. log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
  292. log_parse_environment();
  293. log_open();
  294. umask(0022);
  295. #ifdef HAVE_AUDIT
  296. if ((c.audit_fd = audit_open()) < 0)
  297. log_error("Failed to connect to audit log: %m");
  298. #endif
  299. if (bus_connect(DBUS_BUS_SYSTEM, &c.bus, NULL, &error) < 0) {
  300. log_error("Failed to get D-Bus connection: %s", bus_error_message(&error));
  301. r = -EIO;
  302. goto finish;
  303. }
  304. log_debug("systemd-update-utmp running as pid %lu", (unsigned long) getpid());
  305. if (streq(argv[1], "reboot"))
  306. r = on_reboot(&c);
  307. else if (streq(argv[1], "shutdown"))
  308. r = on_shutdown(&c);
  309. else if (streq(argv[1], "runlevel"))
  310. r = on_runlevel(&c);
  311. else {
  312. log_error("Unknown command %s", argv[1]);
  313. r = -EINVAL;
  314. }
  315. log_debug("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid());
  316. finish:
  317. #ifdef HAVE_AUDIT
  318. if (c.audit_fd >= 0)
  319. audit_close(c.audit_fd);
  320. #endif
  321. if (c.bus) {
  322. dbus_connection_flush(c.bus);
  323. dbus_connection_close(c.bus);
  324. dbus_connection_unref(c.bus);
  325. }
  326. dbus_error_free(&error);
  327. dbus_shutdown();
  328. return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
  329. }