systemctl.c 180 KB
Newer Older
1
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

/***
  This file is part of systemd.

  Copyright 2010 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

22
#include <sys/reboot.h>
23 24 25 26 27 28 29 30
#include <stdio.h>
#include <getopt.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
31
#include <fcntl.h>
32
#include <sys/socket.h>
33
#include <sys/stat.h>
34
#include <stddef.h>
35
#include <sys/prctl.h>
36 37 38 39 40 41 42

#include <dbus/dbus.h>

#include "log.h"
#include "util.h"
#include "macro.h"
#include "set.h"
43
#include "utmp-wtmp.h"
44
#include "special.h"
45
#include "initreq.h"
46
#include "strv.h"
47
#include "dbus-common.h"
48
#include "cgroup-show.h"
49
#include "cgroup-util.h"
50
#include "list.h"
51 52 53
#include "path-lookup.h"
#include "conf-parser.h"
#include "sd-daemon.h"
54
#include "shutdownd.h"
55
#include "exit-status.h"
56
#include "bus-errors.h"
57
#include "build.h"
58
#include "unit-name.h"
59 60

static const char *arg_type = NULL;
61
static char **arg_property = NULL;
62
static bool arg_all = false;
63
static bool arg_fail = false;
64
static bool arg_user = false;
65
static bool arg_global = false;
66
static bool arg_immediate = false;
67
static bool arg_no_block = false;
68 69
static bool arg_no_wtmp = false;
static bool arg_no_sync = false;
70
static bool arg_no_wall = false;
71
static bool arg_no_reload = false;
72
static bool arg_dry = false;
73
static bool arg_quiet = false;
74 75 76
static bool arg_full = false;
static bool arg_force = false;
static bool arg_defaults = false;
77
static bool arg_ask_password = false;
78
static char **arg_wall = NULL;
79 80 81
static const char *arg_kill_who = NULL;
static const char *arg_kill_mode = NULL;
static int arg_signal = SIGTERM;
82
static usec_t arg_when = 0;
83
static enum action {
84 85 86 87 88
        ACTION_INVALID,
        ACTION_SYSTEMCTL,
        ACTION_HALT,
        ACTION_POWEROFF,
        ACTION_REBOOT,
89 90
        ACTION_KEXEC,
        ACTION_EXIT,
91 92 93 94 95
        ACTION_RUNLEVEL2,
        ACTION_RUNLEVEL3,
        ACTION_RUNLEVEL4,
        ACTION_RUNLEVEL5,
        ACTION_RESCUE,
96 97
        ACTION_EMERGENCY,
        ACTION_DEFAULT,
98 99 100
        ACTION_RELOAD,
        ACTION_REEXEC,
        ACTION_RUNLEVEL,
101
        ACTION_CANCEL_SHUTDOWN,
102 103
        _ACTION_MAX
} arg_action = ACTION_SYSTEMCTL;
104 105 106 107 108
static enum dot {
        DOT_ALL,
        DOT_ORDER,
        DOT_REQUIRE
} arg_dot = DOT_ALL;
109

110 111
static bool private_bus = false;

112 113
static int daemon_reload(DBusConnection *bus, char **args, unsigned n);

114
static bool on_tty(void) {
115 116 117 118 119
        static int t = -1;

        if (_unlikely_(t < 0))
                t = isatty(STDOUT_FILENO) > 0;

120 121 122
        return t;
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static void spawn_ask_password_agent(void) {
        pid_t parent, child;

        /* We check STDIN here, not STDOUT, since this is about input,
         * not output */
        if (!isatty(STDIN_FILENO))
                return;

        if (!arg_ask_password)
                return;

        parent = getpid();

        /* Spawns a temporary TTY agent, making sure it goes away when
         * we go away */

        if ((child = fork()) < 0)
                return;

        if (child == 0) {
                /* In the child */

                const char * const args[] = {
                        SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
                        "--watch",
                        NULL
                };

                if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
                        _exit(EXIT_FAILURE);

                /* Check whether our parent died before we were able
                 * to set the death signal */
                if (getppid() != parent)
                        _exit(EXIT_SUCCESS);

                execv(args[0], (char **) args);
                _exit(EXIT_FAILURE);
        }
}

164 165 166
static const char *ansi_highlight(bool b) {

        if (!on_tty())
167 168 169 170 171
                return "";

        return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
}

172 173 174 175 176 177 178 179
static const char *ansi_highlight_green(bool b) {

        if (!on_tty())
                return "";

        return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
}

180
static bool error_is_no_service(const DBusError *error) {
181 182
        assert(error);

183 184 185 186 187 188 189 190 191 192 193
        if (!dbus_error_is_set(error))
                return false;

        if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
                return true;

        if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
                return true;

        return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
}
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
        assert(error);

        if (!dbus_error_is_set(error))
                return r;

        if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
            dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
            dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
            dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
                return EXIT_NOPERMISSION;

        if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
                return EXIT_NOTINSTALLED;

        if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
            dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
                return EXIT_NOTIMPLEMENTED;

        if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
                return EXIT_NOTCONFIGURED;

        if (r != 0)
                return r;

        return EXIT_FAILURE;
}

223 224
static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {

225 226 227
        assert(iter);
        assert(data);

228 229 230 231 232 233 234 235 236 237 238
        if (dbus_message_iter_get_arg_type(iter) != type)
                return -EIO;

        dbus_message_iter_get_basic(iter, data);

        if (!dbus_message_iter_next(iter) != !next)
                return -EIO;

        return 0;
}

239
static void warn_wall(enum action action) {
240
        static const char *table[_ACTION_MAX] = {
241 242 243
                [ACTION_HALT]      = "The system is going down for system halt NOW!",
                [ACTION_REBOOT]    = "The system is going down for reboot NOW!",
                [ACTION_POWEROFF]  = "The system is going down for power-off NOW!",
244
                [ACTION_KEXEC]     = "The system is going down for kexec reboot NOW!",
245 246
                [ACTION_RESCUE]    = "The system is going down to rescue mode NOW!",
                [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
247 248
        };

249 250 251
        if (arg_no_wall)
                return;

252 253 254 255 256 257 258 259 260
        if (arg_wall) {
                char *p;

                if (!(p = strv_join(arg_wall, " "))) {
                        log_error("Failed to join strings.");
                        return;
                }

                if (*p) {
261
                        utmp_wall(p, NULL);
262 263 264 265 266 267 268
                        free(p);
                        return;
                }

                free(p);
        }

269
        if (!table[action])
270 271
                return;

272
        utmp_wall(table[action], NULL);
273 274
}

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
struct unit_info {
        const char *id;
        const char *description;
        const char *load_state;
        const char *active_state;
        const char *sub_state;
        const char *following;
        const char *unit_path;
        uint32_t job_id;
        const char *job_type;
        const char *job_path;
};

static int compare_unit_info(const void *a, const void *b) {
        const char *d1, *d2;
        const struct unit_info *u = a, *v = b;

        d1 = strrchr(u->id, '.');
        d2 = strrchr(v->id, '.');

        if (d1 && d2) {
                int r;

298
                if ((r = strcasecmp(d1, d2)) != 0)
299 300 301
                        return r;
        }

302
        return strcasecmp(u->id, v->id);
303 304
}

305 306
static bool output_show_job(const struct unit_info *u) {
        const char *dot;
307

308 309 310 311 312
        return (!arg_type || ((dot = strrchr(u->id, '.')) &&
                              streq(dot+1, arg_type))) &&
                (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
}

313
static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
314
        unsigned active_len, sub_len, job_len, n_shown = 0;
315
        const struct unit_info *u;
316

317 318 319 320 321 322 323 324 325 326 327 328
        active_len = sizeof("ACTIVE")-1;
        sub_len = sizeof("SUB")-1;
        job_len = sizeof("JOB")-1;

        for (u = unit_infos; u < unit_infos + c; u++) {
                if (!output_show_job(u))
                        continue;

                active_len = MAX(active_len, strlen(u->active_state));
                sub_len = MAX(sub_len, strlen(u->sub_state));
                if (u->job_id != 0)
                        job_len = MAX(job_len, strlen(u->job_type));
329 330
        }

331
        if (on_tty()) {
332 333
                printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
                       active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
334
                if (columns() >= 80+12 || arg_full)
335
                        printf(" %s\n", "DESCRIPTION");
336
                else
337
                        printf("\n");
338 339
        }

340 341 342 343 344 345 346 347 348
        for (u = unit_infos; u < unit_infos + c; u++) {
                char *e;
                int a = 0, b = 0;
                const char *on_loaded, *off_loaded;
                const char *on_active, *off_active;

                if (!output_show_job(u))
                        continue;

349 350
                n_shown++;

351 352
                if (!streq(u->load_state, "loaded") &&
                    !streq(u->load_state, "banned")) {
353 354 355 356 357 358 359 360 361 362
                        on_loaded = ansi_highlight(true);
                        off_loaded = ansi_highlight(false);
                } else
                        on_loaded = off_loaded = "";

                if (streq(u->active_state, "failed")) {
                        on_active = ansi_highlight(true);
                        off_active = ansi_highlight(false);
                } else
                        on_active = off_active = "";
363

364
                e = arg_full ? NULL : ellipsize(u->id, 25, 33);
365

366 367 368 369 370 371
                printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
                       e ? e : u->id,
                       on_loaded, u->load_state, off_loaded,
                       on_active, active_len, u->active_state,
                       sub_len, u->sub_state, off_active,
                       &a);
372

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
                free(e);

                a -= strlen(on_loaded) + strlen(off_loaded);
                a -= strlen(on_active) + strlen(off_active);

                if (u->job_id != 0)
                        printf(" %-*s", job_len, u->job_type);
                else
                        b = 1 + job_len;

                if (a + b + 1 < columns()) {
                        if (u->job_id == 0)
                                printf(" %-*s", job_len, "");

                        if (arg_full)
                                printf(" %s", u->description);
                        else
                                printf(" %.*s", columns() - a - b - 1, u->description);
391
                }
392 393

                fputs("\n", stdout);
394 395
        }

396
        if (on_tty()) {
397 398 399 400 401 402
                printf("\nLOAD   = Reflects whether the unit definition was properly loaded.\n"
                       "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
                       "SUB    = The low-level unit activation state, values depend on unit type.\n"
                       "JOB    = Pending job for the unit.\n");

                if (arg_all)
403
                        printf("\n%u units listed.\n", n_shown);
404
                else
405
                        printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
406 407 408
        }
}

409 410 411 412 413
static int list_units(DBusConnection *bus, char **args, unsigned n) {
        DBusMessage *m = NULL, *reply = NULL;
        DBusError error;
        int r;
        DBusMessageIter iter, sub, sub2;
414
        unsigned c = 0, n_units = 0;
415
        struct unit_info *unit_infos = NULL;
416 417 418

        dbus_error_init(&error);

419 420
        assert(bus);

421 422 423 424 425 426 427 428 429 430
        if (!(m = dbus_message_new_method_call(
                              "org.freedesktop.systemd1",
                              "/org/freedesktop/systemd1",
                              "org.freedesktop.systemd1.Manager",
                              "ListUnits"))) {
                log_error("Could not allocate message.");
                return -ENOMEM;
        }

        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
431
                log_error("Failed to issue method call: %s", bus_error_message(&error));
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
                r = -EIO;
                goto finish;
        }

        if (!dbus_message_iter_init(reply, &iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
            dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
                log_error("Failed to parse reply.");
                r = -EIO;
                goto finish;
        }

        dbus_message_iter_recurse(&iter, &sub);

        while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
447
                struct unit_info *u;
448 449 450 451 452 453 454

                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
                if (c >= n_units) {
                        struct unit_info *w;

                        n_units = MAX(2*c, 16);
                        w = realloc(unit_infos, sizeof(struct unit_info) * n_units);

                        if (!w) {
                                log_error("Failed to allocate unit array.");
                                r = -ENOMEM;
                                goto finish;
                        }

                        unit_infos = w;
                }

                u = unit_infos+c;

472 473
                dbus_message_iter_recurse(&sub, &sub2);

474 475 476 477 478 479 480 481 482 483
                if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
484 485 486 487 488
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

489 490 491 492 493
                dbus_message_iter_next(&sub);
                c++;
        }

        qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
494
        output_units_list(unit_infos, c);
495 496 497 498 499 500 501 502 503 504

        r = 0;

finish:
        if (m)
                dbus_message_unref(m);

        if (reply)
                dbus_message_unref(reply);

505 506
        free(unit_infos);

507 508 509 510 511 512 513 514 515 516 517 518 519
        dbus_error_free(&error);

        return r;
}

static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
        static const char * const colors[] = {
                "Requires",              "[color=\"black\"]",
                "RequiresOverridable",   "[color=\"black\"]",
                "Requisite",             "[color=\"darkblue\"]",
                "RequisiteOverridable",  "[color=\"darkblue\"]",
                "Wants",                 "[color=\"darkgrey\"]",
                "Conflicts",             "[color=\"red\"]",
520
                "ConflictedBy",          "[color=\"red\"]",
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
                "After",                 "[color=\"green\"]"
        };

        const char *c = NULL;
        unsigned i;

        assert(name);
        assert(prop);
        assert(iter);

        for (i = 0; i < ELEMENTSOF(colors); i += 2)
                if (streq(colors[i], prop)) {
                        c = colors[i+1];
                        break;
                }

        if (!c)
                return 0;

        if (arg_dot != DOT_ALL)
                if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
                        return 0;

        switch (dbus_message_iter_get_arg_type(iter)) {

        case DBUS_TYPE_ARRAY:

                if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
                        DBusMessageIter sub;

                        dbus_message_iter_recurse(iter, &sub);

                        while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
                                const char *s;

                                assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
                                dbus_message_iter_get_basic(&sub, &s);
                                printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);

                                dbus_message_iter_next(&sub);
                        }

                        return 0;
                }
        }

        return 0;
}

static int dot_one(DBusConnection *bus, const char *name, const char *path) {
        DBusMessage *m = NULL, *reply = NULL;
        const char *interface = "org.freedesktop.systemd1.Unit";
        int r;
        DBusError error;
        DBusMessageIter iter, sub, sub2, sub3;

        assert(bus);
        assert(path);

        dbus_error_init(&error);

        if (!(m = dbus_message_new_method_call(
                              "org.freedesktop.systemd1",
                              path,
                              "org.freedesktop.DBus.Properties",
                              "GetAll"))) {
                log_error("Could not allocate message.");
                r = -ENOMEM;
                goto finish;
        }

        if (!dbus_message_append_args(m,
                                      DBUS_TYPE_STRING, &interface,
                                      DBUS_TYPE_INVALID)) {
                log_error("Could not append arguments to message.");
                r = -ENOMEM;
                goto finish;
        }

        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
601
                log_error("Failed to issue method call: %s", bus_error_message(&error));
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
                r = -EIO;
                goto finish;
        }

        if (!dbus_message_iter_init(reply, &iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
            dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY)  {
                log_error("Failed to parse reply.");
                r = -EIO;
                goto finish;
        }

        dbus_message_iter_recurse(&iter, &sub);

        while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
                const char *prop;

                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

                dbus_message_iter_recurse(&sub, &sub2);

                if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

                if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT)  {
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

                dbus_message_iter_recurse(&sub2, &sub3);

                if (dot_one_property(name, prop, &sub3)) {
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

                dbus_message_iter_next(&sub);
        }

650 651
        r = 0;

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
finish:
        if (m)
                dbus_message_unref(m);

        if (reply)
                dbus_message_unref(reply);

        dbus_error_free(&error);

        return r;
}

static int dot(DBusConnection *bus, char **args, unsigned n) {
        DBusMessage *m = NULL, *reply = NULL;
        DBusError error;
        int r;
        DBusMessageIter iter, sub, sub2;

        dbus_error_init(&error);

        assert(bus);

        if (!(m = dbus_message_new_method_call(
                              "org.freedesktop.systemd1",
                              "/org/freedesktop/systemd1",
                              "org.freedesktop.systemd1.Manager",
                              "ListUnits"))) {
                log_error("Could not allocate message.");
                return -ENOMEM;
        }

        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
684
                log_error("Failed to issue method call: %s", bus_error_message(&error));
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
                r = -EIO;
                goto finish;
        }

        if (!dbus_message_iter_init(reply, &iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
            dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
                log_error("Failed to parse reply.");
                r = -EIO;
                goto finish;
        }

        printf("digraph systemd {\n");

        dbus_message_iter_recurse(&iter, &sub);
        while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
701
                const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
702 703 704 705 706 707 708 709 710 711 712 713 714 715

                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

                dbus_message_iter_recurse(&sub, &sub2);

                if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
716
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

                if ((r = dot_one(bus, id, unit_path)) < 0)
                        goto finish;

                /* printf("\t\"%s\";\n", id); */
                dbus_message_iter_next(&sub);
        }

        printf("}\n");

        log_info("   Color legend: black     = Requires\n"
                 "                 dark blue = Requisite\n"
                 "                 dark grey = Wants\n"
                 "                 red       = Conflicts\n"
                 "                 green     = After\n");

        if (isatty(fileno(stdout)))
                log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
                           "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

        r = 0;

finish:
        if (m)
                dbus_message_unref(m);

        if (reply)
                dbus_message_unref(reply);

        dbus_error_free(&error);

        return r;
}

static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
        DBusMessage *m = NULL, *reply = NULL;
        DBusError error;
        int r;
        DBusMessageIter iter, sub, sub2;
        unsigned k = 0;

        dbus_error_init(&error);

765 766
        assert(bus);

767 768 769 770 771 772 773 774 775 776
        if (!(m = dbus_message_new_method_call(
                              "org.freedesktop.systemd1",
                              "/org/freedesktop/systemd1",
                              "org.freedesktop.systemd1.Manager",
                              "ListJobs"))) {
                log_error("Could not allocate message.");
                return -ENOMEM;
        }

        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
777
                log_error("Failed to issue method call: %s", bus_error_message(&error));
778 779 780 781 782 783 784 785 786 787 788 789 790 791
                r = -EIO;
                goto finish;
        }

        if (!dbus_message_iter_init(reply, &iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
            dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)  {
                log_error("Failed to parse reply.");
                r = -EIO;
                goto finish;
        }

        dbus_message_iter_recurse(&iter, &sub);

792 793
        if (isatty(STDOUT_FILENO))
                printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
794 795 796 797

        while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
                const char *name, *type, *state, *job_path, *unit_path;
                uint32_t id;
798
                char *e;
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818

                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

                dbus_message_iter_recurse(&sub, &sub2);

                if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
                    bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

819 820
                e = arg_full ? NULL : ellipsize(name, 25, 33);
                printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
821 822
                free(e);

823 824 825 826 827
                k++;

                dbus_message_iter_next(&sub);
        }

828 829 830
        if (isatty(STDOUT_FILENO))
                printf("\n%u jobs listed.\n", k);

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
        r = 0;

finish:
        if (m)
                dbus_message_unref(m);

        if (reply)
                dbus_message_unref(reply);

        dbus_error_free(&error);

        return r;
}

static int load_unit(DBusConnection *bus, char **args, unsigned n) {
        DBusMessage *m = NULL, *reply = NULL;
        DBusError error;
        int r;
        unsigned i;

        dbus_error_init(&error);

853 854 855
        assert(bus);
        assert(args);

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
        for (i = 1; i < n; i++) {

                if (!(m = dbus_message_new_method_call(
                                      "org.freedesktop.systemd1",
                                      "/org/freedesktop/systemd1",
                                      "org.freedesktop.systemd1.Manager",
                                      "LoadUnit"))) {
                        log_error("Could not allocate message.");
                        r = -ENOMEM;
                        goto finish;
                }

                if (!dbus_message_append_args(m,
                                              DBUS_TYPE_STRING, &args[i],
                                              DBUS_TYPE_INVALID)) {
                        log_error("Could not append arguments to message.");
                        r = -ENOMEM;
                        goto finish;
                }

                if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
877
                        log_error("Failed to issue method call: %s", bus_error_message(&error));
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
                        r = -EIO;
                        goto finish;
                }

                dbus_message_unref(m);
                dbus_message_unref(reply);

                m = reply = NULL;
        }

        r = 0;

finish:
        if (m)
                dbus_message_unref(m);

        if (reply)
                dbus_message_unref(reply);

        dbus_error_free(&error);

        return r;
}

static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
        DBusMessage *m = NULL, *reply = NULL;
        DBusError error;
        int r;
        unsigned i;

        dbus_error_init(&error);

910 911 912
        assert(bus);
        assert(args);

913 914 915
        if (n <= 1)
                return daemon_reload(bus, args, n);

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
        for (i = 1; i < n; i++) {
                unsigned id;
                const char *path;

                if (!(m = dbus_message_new_method_call(
                                      "org.freedesktop.systemd1",
                                      "/org/freedesktop/systemd1",
                                      "org.freedesktop.systemd1.Manager",
                                      "GetJob"))) {
                        log_error("Could not allocate message.");
                        r = -ENOMEM;
                        goto finish;
                }

                if ((r = safe_atou(args[i], &id)) < 0) {
                        log_error("Failed to parse job id: %s", strerror(-r));
                        goto finish;
                }

                assert_cc(sizeof(uint32_t) == sizeof(id));
                if (!dbus_message_append_args(m,
                                              DBUS_TYPE_UINT32, &id,
                                              DBUS_TYPE_INVALID)) {
                        log_error("Could not append arguments to message.");
                        r = -ENOMEM;
                        goto finish;
                }

                if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
945
                        log_error("Failed to issue method call: %s", bus_error_message(&error));
946 947 948 949 950 951 952
                        r = -EIO;
                        goto finish;
                }

                if (!dbus_message_get_args(reply, &error,
                                           DBUS_TYPE_OBJECT_PATH, &path,
                                           DBUS_TYPE_INVALID)) {
953
                        log_error("Failed to parse reply: %s", bus_error_message(&error));
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
                        r = -EIO;
                        goto finish;
                }

                dbus_message_unref(m);
                if (!(m = dbus_message_new_method_call(
                                      "org.freedesktop.systemd1",
                                      path,
                                      "org.freedesktop.systemd1.Job",
                                      "Cancel"))) {
                        log_error("Could not allocate message.");
                        r = -ENOMEM;
                        goto finish;
                }

                dbus_message_unref(reply);
                if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
971
                        log_error("Failed to issue method call: %s", bus_error_message(&error));
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
                        r = -EIO;
                        goto finish;
                }

                dbus_message_unref(m);
                dbus_message_unref(reply);
                m = reply = NULL;
        }

        r = 0;

finish:
        if (m)
                dbus_message_unref(m);

        if (reply)
                dbus_message_unref(reply);

        dbus_error_free(&error);

        return r;
}

995
static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
996
        DBusMessage *m = NULL, *reply = NULL;
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
        dbus_bool_t b = FALSE;
        DBusMessageIter iter, sub;
        const char
                *interface = "org.freedesktop.systemd1.Unit",
                *property = "NeedDaemonReload",
                *path;

        /* We ignore all errors here, since this is used to show a warning only */

        if (!(m = dbus_message_new_method_call(
                              "org.freedesktop.systemd1",
                              "/org/freedesktop/systemd1",
                              "org.freedesktop.systemd1.Manager",
                              "GetUnit")))
                goto finish;

        if (!dbus_message_append_args(m,
                                      DBUS_TYPE_STRING, &unit,
                                      DBUS_TYPE_INVALID))
                goto finish;

        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
                goto finish;

        if (!dbus_message_get_args(reply, NULL,
                                   DBUS_TYPE_OBJECT_PATH, &path,
                                   DBUS_TYPE_INVALID))
                goto finish;

        dbus_message_unref(m);
        if (!(m = dbus_message_new_method_call(
                              "org.freedesktop.systemd1",
                              path,
                              "org.freedesktop.DBus.Properties",
                              "Get")))
                goto finish;

        if (!dbus_message_append_args(m,
                                      DBUS_TYPE_STRING, &interface,
                                      DBUS_TYPE_STRING, &property,
                                      DBUS_TYPE_INVALID)) {
                goto finish;
        }

        dbus_message_unref(reply);
        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
                goto finish;

        if (!dbus_message_iter_init(reply, &iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
                goto finish;

        dbus_message_iter_recurse(&iter, &sub);

        if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
                goto finish;

        dbus_message_iter_get_basic(&sub, &b);

finish:
        if (m)
                dbus_message_unref(m);

        if (reply)
                dbus_message_unref(reply);

        return b;
}

1066 1067 1068 1069 1070
typedef struct WaitData {
        Set *set;
        bool failed;
} WaitData;

1071 1072
static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
        DBusError error;
1073
        WaitData *d = data;
1074 1075 1076

        assert(connection);
        assert(message);
1077
        assert(d);
1078 1079 1080

        dbus_error_init(&error);

1081 1082 1083 1084
        log_debug("Got D-Bus request: %s.%s() on %s",
                  dbus_message_get_interface(message),
                  dbus_message_get_member(message),
                  dbus_message_get_path(message));
1085 1086 1087 1088 1089 1090 1091 1092

        if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
                log_error("Warning! D-Bus connection terminated.");
                dbus_connection_close(connection);

        } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
                uint32_t id;
                const char *path;
1093
                dbus_bool_t success = true;
1094 1095 1096 1097

                if (!dbus_message_get_args(message, &error,
                                           DBUS_TYPE_UINT32, &id,
                                           DBUS_TYPE_OBJECT_PATH, &path,
1098
                                           DBUS_TYPE_BOOLEAN, &success,
1099
                                           DBUS_TYPE_INVALID))
1100
                        log_error("Failed to parse message: %s", bus_error_message(&error));
1101 1102 1103
                else {
                        char *p;

1104
                        if ((p = set_remove(d->set, (char*) path)))
1105
                                free(p);
1106 1107 1108

                        if (!success)
                                d->failed = true;
1109 1110 1111 1112 1113 1114 1115
                }
        }

        dbus_error_free(&error);
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

1116
static int enable_wait_for_jobs(DBusConnection *bus) {
1117 1118 1119 1120
        DBusError error;

        assert(bus);

1121 1122 1123
        if (private_bus)
                return 0;

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
        dbus_error_init(&error);
        dbus_bus_add_match(bus,
                           "type='signal',"
                           "sender='org.freedesktop.systemd1',"
                           "interface='org.freedesktop.systemd1.Manager',"
                           "member='JobRemoved',"
                           "path='/org/freedesktop/systemd1'",
                           &error);

        if (dbus_error_is_set(&error)) {
1134
                log_error("Failed to add match: %s", bus_error_message(&error));
1135 1136
                dbus_error_free(&error);
                return -EIO;
1137 1138
        }

1139
        /* This is slightly dirty, since we don't undo the match registrations. */
1140
        return 0;
1141 1142
}

1143 1144
static int wait_for_jobs(DBusConnection *bus, Set *s) {
        int r;
1145
        WaitData d;
1146 1147 1148 1149

        assert(bus);
        assert(s);

1150 1151 1152 1153 1154
        zero(d);
        d.set = s;
        d.failed = false;

        if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1155 1156 1157 1158 1159 1160 1161 1162 1163
                log_error("Failed to add filter.");
                r = -ENOMEM;
                goto finish;
        }

        while (!set_isempty(s) &&
               dbus_connection_read_write_dispatch(bus, -1))
                ;

1164
        if (!arg_quiet && d.failed)
1165
                log_error("Job failed. See system logs and 'systemctl status' for details.");
1166 1167

        r = d.failed ? -EIO : 0;
1168 1169 1170 1171 1172 1173 1174

finish:
        /* This is slightly dirty, since we don't undo the filter registration. */

        return r;
}

1175 1176 1177 1178 1179
static int start_unit_one(
                DBusConnection *bus,
                const char *method,
                const char *name,
                const char *mode,
1180
                DBusError *error,
1181
                Set *s) {
1182 1183

        DBusMessage *m = NULL, *reply = NULL;
1184
        const char *path;
1185 1186
        int r;

1187 1188 1189 1190
        assert(bus);
        assert(method);
        assert(name);
        assert(mode);
1191
        assert(error);
1192
        assert(arg_no_block || s);
1193 1194 1195 1196 1197

        if (!(m = dbus_message_new_method_call(
                              "org.freedesktop.systemd1",
                              "/org/freedesktop/systemd1",
                              "org.freedesktop.systemd1.Manager",
1198
                              method))) {
1199 1200 1201 1202 1203 1204
                log_error("Could not allocate message.");
                r = -ENOMEM;
                goto finish;
        }

        if (!dbus_message_append_args(m,
1205
                                      DBUS_TYPE_STRING, &name,
1206 1207 1208 1209 1210 1211 1212
                                      DBUS_TYPE_STRING, &mode,
                                      DBUS_TYPE_INVALID)) {
                log_error("Could not append arguments to message.");
                r = -ENOMEM;
                goto finish;
        }

1213
        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1214

1215
                if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1216 1217
                        /* There's always a fallback possible for
                         * legacy actions. */
1218
                        r = -EADDRNOTAVAIL;
1219 1220 1221
                        goto finish;
                }

1222
                log_error("Failed to issue method call: %s", bus_error_message(error));
1223 1224 1225 1226
                r = -EIO;
                goto finish;
        }

1227
        if (!dbus_message_get_args(reply, error,
1228 1229
                                   DBUS_TYPE_OBJECT_PATH, &path,
                                   DBUS_TYPE_INVALID)) {
1230
                log_error("Failed to parse reply: %s", bus_error_message(error));
1231 1232 1233 1234
                r = -EIO;
                goto finish;
        }

1235
        if (need_daemon_reload(bus, name))
1236
                log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1237
                            arg_user ? "--user" : "--system");
1238

1239
        if (!arg_no_block) {
1240
                char *p;
1241 1242 1243 1244 1245 1246 1247 1248

                if (!(p = strdup(path))) {
                        log_error("Failed to duplicate path.");
                        r = -ENOMEM;
                        goto finish;
                }

                if ((r = set_put(s, p)) < 0) {
1249
                        free(p);
1250 1251 1252
                        log_error("Failed to add path to set.");
                        goto finish;
                }
1253
        }
1254

1255
        r = 0;
1256

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
finish:
        if (m)
                dbus_message_unref(m);

        if (reply)
                dbus_message_unref(reply);

        return r;
}

1267 1268 1269 1270 1271 1272 1273
static enum action verb_to_action(const char *verb) {
        if (streq(verb, "halt"))
                return ACTION_HALT;
        else if (streq(verb, "poweroff"))
                return ACTION_POWEROFF;
        else if (streq(verb, "reboot"))
                return ACTION_REBOOT;
1274 1275
        else if (streq(verb, "kexec"))
                return ACTION_KEXEC;
1276 1277 1278 1279 1280 1281
        else if (streq(verb, "rescue"))
                return ACTION_RESCUE;
        else if (streq(verb, "emergency"))
                return ACTION_EMERGENCY;
        else if (streq(verb, "default"))
                return ACTION_DEFAULT;
1282 1283
        else if (streq(verb, "exit"))
                return ACTION_EXIT;
1284 1285 1286 1287
        else
                return ACTION_INVALID;
}

1288 1289 1290
static int start_unit(DBusConnection *bus, char **args, unsigned n) {

        static const char * const table[_ACTION_MAX] = {
1291 1292 1293
                [ACTION_HALT] = SPECIAL_HALT_TARGET,
                [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
                [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1294
                [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1295 1296 1297 1298 1299
                [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
                [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
                [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
                [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
                [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1300
                [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1301 1302
                [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
                [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1303 1304
        };

1305
        int r, ret = 0;
1306
        unsigned i;
1307
        const char *method, *mode, *one_name;
1308
        Set *s = NULL;
1309 1310 1311
        DBusError error;

        dbus_error_init(&error);
1312

1313 1314
        assert(bus);

1315 1316
        spawn_ask_password_agent();

1317 1318
        if (arg_action == ACTION_SYSTEMCTL) {
                method =
1319 1320 1321
                        streq(args[0], "stop")                  ? "StopUnit" :
                        streq(args[0], "reload")                ? "ReloadUnit" :
                        streq(args[0], "restart")               ? "RestartUnit" :
1322 1323
                        streq(args[0], "try-restart")           ||
                        streq(args[0], "condrestart")           ? "TryRestartUnit" :
1324
                        streq(args[0], "reload-or-restart")     ? "ReloadOrRestartUnit" :
1325
                        streq(args[0], "reload-or-try-restart") ||
1326
                        streq(args[0], "force-reload")          ? "ReloadOrTryRestartUnit" :
1327
                                                                  "StartUnit";
1328 1329

                mode =
1330 1331 1332
                        (streq(args[0], "isolate") ||
                         streq(args[0], "rescue")  ||
                         streq(args[0], "emergency")) ? "isolate" :
1333 1334
                                             arg_fail ? "fail" :
                                                        "replace";
1335

1336
                one_name = table[verb_to_action(args[0])];
1337 1338 1339 1340 1341 1342

        } else {
                assert(arg_action < ELEMENTSOF(table));
                assert(table[arg_action]);

                method = "StartUnit";
1343 1344

                mode = (arg_action == ACTION_EMERGENCY ||
1345 1346 1347 1348 1349
                        arg_action == ACTION_RESCUE ||
                        arg_action == ACTION_RUNLEVEL2 ||
                        arg_action == ACTION_RUNLEVEL3 ||
                        arg_action == ACTION_RUNLEVEL4 ||
                        arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1350 1351 1352 1353

                one_name = table[arg_action];
        }

1354
        if (!arg_no_block) {
1355 1356
                if ((ret = enable_wait_for_jobs(bus)) < 0) {
                        log_error("Could not watch jobs: %s", strerror(-ret));
1357 1358 1359 1360 1361
                        goto finish;
                }

                if (!(s = set_new(string_hash_func, string_compare_func))) {
                        log_error("Failed to allocate set.");
1362
                        ret = -ENOMEM;
1363 1364
                        goto finish;
                }
1365 1366
        }

1367
        if (one_name) {
1368
                if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1369 1370
                        goto finish;
        } else {
1371
                for (i = 1; i < n; i++)
1372
                        if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) {
1373
                                ret = translate_bus_error_to_exit_status(r, &error);
1374 1375
                                dbus_error_free(&error);
                        }
1376 1377
        }

1378
        if (!arg_no_block)
1379 1380
                if ((r = wait_for_jobs(bus, s)) < 0) {
                        ret = r;
1381
                        goto finish;
1382
                }
1383

1384 1385 1386 1387
finish:
        if (s)
                set_free_free(s);

1388 1389 1390
        dbus_error_free(&error);

        return ret;
1391 1392
}

1393
static int start_special(DBusConnection *bus, char **args, unsigned n) {
1394 1395
        int r;

1396 1397 1398
        assert(bus);
        assert(args);

1399 1400 1401 1402 1403 1404 1405 1406
        if (arg_force &&
            (streq(args[0], "halt") ||
             streq(args[0], "poweroff") ||
             streq(args[0], "reboot") ||
             streq(args[0], "kexec") ||
             streq(args[0], "exit")))
                return daemon_reload(bus, args, n);

1407 1408 1409 1410
        r = start_unit(bus, args, n);

        if (r >= 0)
                warn_wall(verb_to_action(args[0]));
1411

1412
        return r;
1413 1414
}

1415 1416 1417 1418 1419
static int check_unit(DBusConnection *bus, char **args, unsigned n) {
        DBusMessage *m = NULL, *reply = NULL;
        const char
                *interface = "org.freedesktop.systemd1.Unit",
                *property = "ActiveState";
1420
        int r = 3; /* According to LSB: "program is not running" */
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
        DBusError error;
        unsigned i;

        assert(bus);
        assert(args);

        dbus_error_init(&error);

        for (i = 1; i < n; i++) {
                const char *path = NULL;
                const char *state;
                DBusMessageIter iter, sub;

                if (!(m = dbus_message_new_method_call(
                                      "org.freedesktop.systemd1",
                                      "/org/freedesktop/systemd1",
                                      "org.freedesktop.systemd1.Manager",
                                      "GetUnit"))) {
                        log_error("Could not allocate message.");
                        r = -ENOMEM;
                        goto finish;
                }

                if (!dbus_message_append_args(m,
                                              DBUS_TYPE_STRING, &args[i],
                                              DBUS_TYPE_INVALID)) {
                        log_error("Could not append arguments to message.");
                        r = -ENOMEM;
                        goto finish;
                }

                if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {

                        /* Hmm, cannot figure out anything about this unit... */
                        if (!arg_quiet)
                                puts("unknown");

1458
                        dbus_error_free(&error);
1459
                        dbus_message_unref(m);
1460 1461 1462 1463 1464 1465
                        continue;
                }

                if (!dbus_message_get_args(reply, &error,
                                           DBUS_TYPE_OBJECT_PATH, &path,
                                           DBUS_TYPE_INVALID)) {
1466
                        log_error("Failed to parse reply: %s", bus_error_message(&error));
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
                        r = -EIO;
                        goto finish;
                }

                dbus_message_unref(m);
                if (!(m = dbus_message_new_method_call(
                                      "org.freedesktop.systemd1",
                                      path,
                                      "org.freedesktop.DBus.Properties",
                                      "Get"))) {
                        log_error("Could not allocate message.");
                        r = -ENOMEM;
                        goto finish;
                }

                if (!dbus_message_append_args(m,
                                              DBUS_TYPE_STRING, &interface,
                                              DBUS_TYPE_STRING, &property,
                                              DBUS_TYPE_INVALID)) {
                        log_error("Could not append arguments to message.");
                        r = -ENOMEM;
                        goto finish;
                }

                dbus_message_unref(reply);
                if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1493
                        log_error("Failed to issue method call: %s", bus_error_message(&error));
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
                        r = -EIO;
                        goto finish;
                }

                if (!dbus_message_iter_init(reply, &iter) ||
                    dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

                dbus_message_iter_recurse(&iter, &sub);

                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
                        log_error("Failed to parse reply.");
                        r = -EIO;
                        goto finish;
                }

                dbus_message_iter_get_basic(&sub, &state);

                if (!arg_quiet)
                        puts(state);

1518
                if (streq(state, "active") || streq(state, "reloading"))
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
                        r = 0;

                dbus_message_unref(m);
                dbus_message_unref(reply);
                m = reply = NULL;
        }

finish:
        if (m)
                dbus_message_unref(m);

        if (reply)
                dbus_message_unref(reply);

        dbus_error_free(&error);

        return r;
1536 1537
}

1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589