summaryrefslogtreecommitdiff
path: root/src/machine/machined-dbus.c
blob: 5a016e76bc1c3518ffc18f955a8dd74ec85ad104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
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
601
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
650
651
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
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
910
911
912
913
914
915
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2011 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 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
  Lesser General Public License for more details.

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

#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>

#include <systemd/sd-id128.h>
#include <systemd/sd-messages.h>

#include "machined.h"
#include "dbus-common.h"
#include "strv.h"
#include "mkdir.h"
#include "path-util.h"
#include "special.h"
#include "sleep-config.h"
#include "fileio-label.h"
#include "label.h"
#include "utf8.h"
#include "unit-name.h"
#include "bus-errors.h"
#include "virt.h"

#define BUS_MANAGER_INTERFACE                                           \
        " <interface name=\"org.freedesktop.machine1.Manager\">\n"      \
        "  <method name=\"GetMachine\">\n"                              \
        "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
        "   <arg name=\"machine\" type=\"o\" direction=\"out\"/>\n"     \
        "  </method>\n"                                                 \
        "  <method name=\"GetMachineByPID\">\n"                         \
        "   <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n"          \
        "   <arg name=\"machine\" type=\"o\" direction=\"out\"/>\n"     \
        "  </method>\n"                                                 \
        "  <method name=\"ListMachines\">\n"                            \
        "   <arg name=\"machines\" type=\"a(ssso)\" direction=\"out\"/>\n" \
        "  </method>\n"                                                 \
        "  <method name=\"CreateMachine\">\n"                           \
        "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
        "   <arg name=\"id\" type=\"ay\" direction=\"in\"/>\n"          \
        "   <arg name=\"service\" type=\"s\" direction=\"in\"/>\n"      \
        "   <arg name=\"class\" type=\"s\" direction=\"in\"/>\n"        \
        "   <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n"       \
        "   <arg name=\"root_directory\" type=\"s\" direction=\"in\"/>\n" \
        "   <arg name=\"scope_properties\" type=\"a(sv)\" direction=\"in\"/>\n" \
        "   <arg name=\"path\" type=\"o\" direction=\"out\"/>\n"        \
        "  </method>\n"                                                 \
        "  <method name=\"KillMachine\">\n"                             \
        "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
        "   <arg name=\"who\" type=\"s\" direction=\"in\"/>\n"          \
        "   <arg name=\"signal\" type=\"s\" direction=\"in\"/>\n"       \
        "  </method>\n"                                                 \
        "  <method name=\"TerminateMachine\">\n"                        \
        "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
        "  </method>\n"                                                 \
        "  <signal name=\"MachineNew\">\n"                              \
        "   <arg name=\"machine\" type=\"s\"/>\n"                       \
        "   <arg name=\"path\" type=\"o\"/>\n"                          \
        "  </signal>\n"                                                 \
        "  <signal name=\"MachineRemoved\">\n"                          \
        "   <arg name=\"machine\" type=\"s\"/>\n"                       \
        "   <arg name=\"path\" type=\"o\"/>\n"                          \
        "  </signal>\n"                                                 \
        " </interface>\n"

#define INTROSPECTION_BEGIN                                             \
        DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
        "<node>\n"                                                      \
        BUS_MANAGER_INTERFACE                                           \
        BUS_PROPERTIES_INTERFACE                                        \
        BUS_PEER_INTERFACE                                              \
        BUS_INTROSPECTABLE_INTERFACE

#define INTROSPECTION_END                                               \
        "</node>\n"

#define INTERFACES_LIST                              \
        BUS_GENERIC_INTERFACES_LIST                  \
        "org.freedesktop.machine1.Manager\0"

static bool valid_machine_name(const char *p) {
        size_t l;

        if (!filename_is_safe(p))
                return false;

        if (!ascii_is_valid(p))
                return false;

        l = strlen(p);

        if (l < 1 || l> 64)
                return false;

        return true;
}

static int bus_manager_create_machine(Manager *manager, DBusMessage *message) {

        const char *name, *service, *class, *root_directory;
        _cleanup_free_ char *p = NULL;
        DBusMessageIter iter, sub;
        MachineClass c;
        uint32_t leader;
        sd_id128_t id;
        Machine *m;
        int n, r;
        void *v;

        assert(manager);
        assert(message);

        if (!dbus_message_iter_init(message, &iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
                return -EINVAL;

        dbus_message_iter_get_basic(&iter, &name);

        if (!valid_machine_name(name) ||
            !dbus_message_iter_next(&iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
            dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE)
                return -EINVAL;

        dbus_message_iter_recurse(&iter, &sub);
        dbus_message_iter_get_fixed_array(&sub, &v, &n);

        if (n == 0)
                id = SD_ID128_NULL;
        else if (n == 16)
                memcpy(&id, v, n);
        else
                return -EINVAL;

        if (!dbus_message_iter_next(&iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
                return -EINVAL;

        dbus_message_iter_get_basic(&iter, &service);

        if (!dbus_message_iter_next(&iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
                return -EINVAL;

        dbus_message_iter_get_basic(&iter, &class);

        if (isempty(class))
                c = _MACHINE_CLASS_INVALID;
        else {
                c = machine_class_from_string(class);
                if (c < 0)
                        return -EINVAL;
        }

        if (!dbus_message_iter_next(&iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
                return -EINVAL;

        dbus_message_iter_get_basic(&iter, &leader);
        if (!dbus_message_iter_next(&iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
                return -EINVAL;

        dbus_message_iter_get_basic(&iter, &root_directory);

        if (!(isempty(root_directory) || path_is_absolute(root_directory)))
                return -EINVAL;

        if (!dbus_message_iter_next(&iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
            dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT)
                return -EINVAL;

        dbus_message_iter_recurse(&iter, &sub);

        if (hashmap_get(manager->machines, name))
                return -EEXIST;

        if (leader <= 0) {
                leader = bus_get_unix_process_id(manager->bus, dbus_message_get_sender(message), NULL);
                if (leader == 0)
                        return -EINVAL;
        }

        r = manager_add_machine(manager, name, &m);
        if (r < 0)
                goto fail;

        m->leader = leader;
        m->class = c;
        m->id = id;

        if (!isempty(service)) {
                m->service = strdup(service);
                if (!m->service) {
                        r = -ENOMEM;
                        goto fail;
                }
        }

        if (!isempty(root_directory)) {
                m->root_directory = strdup(root_directory);
                if (!m->root_directory) {
                        r = -ENOMEM;
                        goto fail;
                }
        }

        r = machine_start(m, &sub);
        if (r < 0)
                goto fail;

        m->create_message = dbus_message_ref(message);

        return 0;

fail:
        if (m)
                machine_add_to_gc_queue(m);

        return r;
}

static DBusHandlerResult manager_message_handler(
                DBusConnection *connection,
                DBusMessage *message,
                void *userdata) {

        Manager *m = userdata;

        DBusError error;
        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
        int r;

        assert(connection);
        assert(message);
        assert(m);

        dbus_error_init(&error);

        if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "GetMachine")) {
                Machine *machine;
                const char *name;
                char *p;
                bool b;

                if (!dbus_message_get_args(
                                    message,
                                    &error,
                                    DBUS_TYPE_STRING, &name,
                                    DBUS_TYPE_INVALID))
                        return bus_send_error_reply(connection, message, &error, -EINVAL);

                machine = hashmap_get(m->machines, name);
                if (!machine)
                        return bus_send_error_reply(connection, message, &error, -ENOENT);

                reply = dbus_message_new_method_return(message);
                if (!reply)
                        goto oom;

                p = machine_bus_path(machine);
                if (!p)
                        goto oom;

                b = dbus_message_append_args(
                                reply,
                                DBUS_TYPE_OBJECT_PATH, &p,
                                DBUS_TYPE_INVALID);
                free(p);

                if (!b)
                        goto oom;

        } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "GetMachineByPID")) {
                uint32_t pid;
                char *p;
                Machine *machine;
                bool b;

                if (!dbus_message_get_args(
                                    message,
                                    &error,
                                    DBUS_TYPE_UINT32, &pid,
                                    DBUS_TYPE_INVALID))
                        return bus_send_error_reply(connection, message, &error, -EINVAL);

                r = manager_get_machine_by_pid(m, pid, &machine);
                if (r <= 0)
                        return bus_send_error_reply(connection, message, NULL, r < 0 ? r : -ENOENT);

                reply = dbus_message_new_method_return(message);
                if (!reply)
                        goto oom;

                p = machine_bus_path(machine);
                if (!p)
                        goto oom;

                b = dbus_message_append_args(
                                reply,
                                DBUS_TYPE_OBJECT_PATH, &p,
                                DBUS_TYPE_INVALID);
                free(p);

                if (!b)
                        goto oom;

        } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "ListMachines")) {
                Machine *machine;
                Iterator i;
                DBusMessageIter iter, sub;

                reply = dbus_message_new_method_return(message);
                if (!reply)
                        goto oom;

                dbus_message_iter_init_append(reply, &iter);

                if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssso)", &sub))
                        goto oom;

                HASHMAP_FOREACH(machine, m->machines, i) {
                        _cleanup_free_ char *p = NULL;
                        DBusMessageIter sub2;
                        const char *class;

                        if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
                                goto oom;

                        p = machine_bus_path(machine);
                        if (!p)
                                goto oom;

                        class = strempty(machine_class_to_string(machine->class));

                        if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &machine->name) ||
                            !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &class) ||
                            !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &machine->service) ||
                            !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
                                free(p);
                                goto oom;
                        }

                        if (!dbus_message_iter_close_container(&sub, &sub2))
                                goto oom;
                }

                if (!dbus_message_iter_close_container(&iter, &sub))
                        goto oom;

        } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "CreateMachine")) {

                r = bus_manager_create_machine(m, message);
                if (r < 0)
                        return bus_send_error_reply(connection, message, NULL, r);

        } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "KillMachine")) {
                const char *swho;
                int32_t signo;
                KillWho who;
                const char *name;
                Machine *machine;

                if (!dbus_message_get_args(
                                    message,
                                    &error,
                                    DBUS_TYPE_STRING, &name,
                                    DBUS_TYPE_STRING, &swho,
                                    DBUS_TYPE_INT32, &signo,
                                    DBUS_TYPE_INVALID))
                        return bus_send_error_reply(connection, message, &error, -EINVAL);

                if (isempty(swho))
                        who = KILL_ALL;
                else {
                        who = kill_who_from_string(swho);
                        if (who < 0)
                                return bus_send_error_reply(connection, message, &error, -EINVAL);
                }

                if (signo <= 0 || signo >= _NSIG)
                        return bus_send_error_reply(connection, message, &error, -EINVAL);

                machine = hashmap_get(m->machines, name);
                if (!machine)
                        return bus_send_error_reply(connection, message, &error, -ENOENT);

                r = machine_kill(machine, who, signo);
                if (r < 0)
                        return bus_send_error_reply(connection, message, NULL, r);

                reply = dbus_message_new_method_return(message);
                if (!reply)
                        goto oom;

        } else if (dbus_message_is_method_call(message, "org.freedesktop.machine1.Manager", "TerminateMachine")) {
                const char *name;
                Machine *machine;

                if (!dbus_message_get_args(
                                    message,
                                    &error,
                                    DBUS_TYPE_STRING, &name,
                                    DBUS_TYPE_INVALID))
                        return bus_send_error_reply(connection, message, &error, -EINVAL);

                machine = hashmap_get(m->machines, name);
                if (!machine)
                        return bus_send_error_reply(connection, message, &error, -ENOENT);

                r = machine_stop(machine);
                if (r < 0)
                        return bus_send_error_reply(connection, message, NULL, r);

                reply = dbus_message_new_method_return(message);
                if (!reply)
                        goto oom;

        } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
                char *introspection = NULL;
                FILE *f;
                Iterator i;
                Machine *machine;
                size_t size;
                char *p;

                reply = dbus_message_new_method_return(message);
                if (!reply)
                        goto oom;

                /* We roll our own introspection code here, instead of
                 * relying on bus_default_message_handler() because we
                 * need to generate our introspection string
                 * dynamically. */

                f = open_memstream(&introspection, &size);
                if (!f)
                        goto oom;

                fputs(INTROSPECTION_BEGIN, f);

                HASHMAP_FOREACH(machine, m->machines, i) {
                        p = bus_path_escape(machine->name);

                        if (p) {
                                fprintf(f, "<node name=\"machine/%s\"/>", p);
                                free(p);
                        }
                }

                fputs(INTROSPECTION_END, f);

                if (ferror(f)) {
                        fclose(f);
                        free(introspection);
                        goto oom;
                }

                fclose(f);

                if (!introspection)
                        goto oom;

                if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
                        free(introspection);
                        goto oom;
                }

                free(introspection);
        } else
                return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, NULL);

        if (reply) {
                if (!bus_maybe_send_reply(connection, message, reply))
                        goto oom;
        }

        return DBUS_HANDLER_RESULT_HANDLED;

oom:
        dbus_error_free(&error);

        return DBUS_HANDLER_RESULT_NEED_MEMORY;
}

const DBusObjectPathVTable bus_manager_vtable = {
        .message_function = manager_message_handler
};

DBusHandlerResult bus_message_filter(
                DBusConnection *connection,
                DBusMessage *message,
                void *userdata) {

        Manager *m = userdata;
        DBusError error;

        assert(m);
        assert(connection);
        assert(message);

        dbus_error_init(&error);

        log_debug("Got message: %s %s %s", strna(dbus_message_get_sender(message)), strna(dbus_message_get_interface(message)), strna(dbus_message_get_member(message)));

        if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
                const char *path, *result, *unit;
                Machine *mm;
                uint32_t id;

                if (!dbus_message_get_args(message, &error,
                                           DBUS_TYPE_UINT32, &id,
                                           DBUS_TYPE_OBJECT_PATH, &path,
                                           DBUS_TYPE_STRING, &unit,
                                           DBUS_TYPE_STRING, &result,
                                           DBUS_TYPE_INVALID)) {
                        log_error("Failed to parse JobRemoved message: %s", bus_error_message(&error));
                        goto finish;
                }

                mm = hashmap_get(m->machine_units, unit);
                if (mm) {
                        if (streq_ptr(path, mm->scope_job)) {
                                free(mm->scope_job);
                                mm->scope_job = NULL;

                                if (mm->started) {
                                        if (streq(result, "done"))
                                                machine_send_create_reply(mm, NULL);
                                        else {
                                                dbus_set_error(&error, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
                                                machine_send_create_reply(mm, &error);
                                        }
                                } else
                                        machine_save(mm);
                        }

                        machine_add_to_gc_queue(mm);
                }

        } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {

                _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
                _cleanup_free_ char *unit = NULL;
                const char *path;

                path = dbus_message_get_path(message);
                if (!path)
                        goto finish;

                unit_name_from_dbus_path(path, &unit);
                if (unit) {
                        Machine *mm;

                        mm = hashmap_get(m->machine_units, unit);
                        if (mm)
                                machine_add_to_gc_queue(mm);
                }

        } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
                const char *path, *unit;
                Machine *mm;

                if (!dbus_message_get_args(message, &error,
                                           DBUS_TYPE_STRING, &unit,
                                           DBUS_TYPE_OBJECT_PATH, &path,
                                           DBUS_TYPE_INVALID)) {
                        log_error("Failed to parse UnitRemoved message: %s", bus_error_message(&error));
                        goto finish;
                }

                mm = hashmap_get(m->machine_units, unit);
                if (mm)
                        machine_add_to_gc_queue(mm);

        } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "Reloading")) {
                dbus_bool_t b;

                if (!dbus_message_get_args(message, &error,
                                           DBUS_TYPE_BOOLEAN, &b,
                                           DBUS_TYPE_INVALID)) {
                        log_error("Failed to parse Reloading message: %s", bus_error_message(&error));
                        goto finish;
                }

                /* systemd finished reloading, let's recheck all our machines */
                if (!b) {
                        Machine *mm;
                        Iterator i;

                        log_debug("System manager has been reloaded, rechecking machines...");

                        HASHMAP_FOREACH(mm, m->machines, i)
                                machine_add_to_gc_queue(mm);
                }
        }

finish:
        dbus_error_free(&error);

        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static int copy_many_fields(DBusMessageIter *dest, DBusMessageIter *src);

static int copy_one_field(DBusMessageIter *dest, DBusMessageIter *src) {
        int type, r;

        type = dbus_message_iter_get_arg_type(src);

        switch (type) {

        case DBUS_TYPE_STRUCT: {
                DBusMessageIter dest_sub, src_sub;

                dbus_message_iter_recurse(src, &src_sub);

                if (!dbus_message_iter_open_container(dest, DBUS_TYPE_STRUCT, NULL, &dest_sub))
                        return log_oom();

                r = copy_many_fields(&dest_sub, &src_sub);
                if (r < 0)
                        return r;

                if (!dbus_message_iter_close_container(dest, &dest_sub))
                        return log_oom();

                return 0;
        }

        case DBUS_TYPE_ARRAY: {
                DBusMessageIter dest_sub, src_sub;

                dbus_message_iter_recurse(src, &src_sub);

                if (!dbus_message_iter_open_container(dest, DBUS_TYPE_ARRAY, dbus_message_iter_get_signature(&src_sub), &dest_sub))
                        return log_oom();

                r = copy_many_fields(&dest_sub, &src_sub);
                if (r < 0)
                        return r;

                if (!dbus_message_iter_close_container(dest, &dest_sub))
                        return log_oom();

                return 0;
        }

        case DBUS_TYPE_VARIANT: {
                DBusMessageIter dest_sub, src_sub;

                dbus_message_iter_recurse(src, &src_sub);

                if (!dbus_message_iter_open_container(dest, DBUS_TYPE_VARIANT, dbus_message_iter_get_signature(&src_sub), &dest_sub))
                        return log_oom();

                r = copy_one_field(&dest_sub, &src_sub);
                if (r < 0)
                        return r;

                if (!dbus_message_iter_close_container(dest, &dest_sub))
                        return log_oom();

                return 0;
        }

        case DBUS_TYPE_STRING:
        case DBUS_TYPE_OBJECT_PATH:
        case DBUS_TYPE_BYTE:
        case DBUS_TYPE_BOOLEAN:
        case DBUS_TYPE_UINT16:
        case DBUS_TYPE_INT16:
        case DBUS_TYPE_UINT32:
        case DBUS_TYPE_INT32:
        case DBUS_TYPE_UINT64:
        case DBUS_TYPE_INT64:
        case DBUS_TYPE_DOUBLE:
        case DBUS_TYPE_SIGNATURE: {
                const void *p;

                dbus_message_iter_get_basic(src, &p);
                dbus_message_iter_append_basic(dest, type, &p);
                return 0;
        }

        default:
                return -EINVAL;
        }
}

static int copy_many_fields(DBusMessageIter *dest, DBusMessageIter *src) {
        int r;

        assert(dest);
        assert(src);

        while (dbus_message_iter_get_arg_type(src) != DBUS_TYPE_INVALID) {

                r = copy_one_field(dest, src);
                if (r < 0)
                        return r;

                dbus_message_iter_next(src);
        }

        return 0;
}

int manager_start_scope(
                Manager *manager,
                const char *scope,
                pid_t pid,
                const char *slice,
                const char *description,
                DBusMessageIter *more_properties,
                DBusError *error,
                char **job) {

        _cleanup_dbus_message_unref_ DBusMessage *m = NULL, *reply = NULL;
        DBusMessageIter iter, sub, sub2, sub3, sub4;
        const char *timeout_stop_property = "TimeoutStopUSec";
        const char *pids_property = "PIDs";
        uint64_t timeout = 500 * USEC_PER_MSEC;
        const char *fail = "fail";
        uint32_t u;
        int r;

        assert(manager);
        assert(scope);
        assert(pid > 1);

        if (!slice)
                slice = "";

        m = dbus_message_new_method_call(
                        "org.freedesktop.systemd1",
                        "/org/freedesktop/systemd1",
                        "org.freedesktop.systemd1.Manager",
                        "StartTransientUnit");
        if (!m)
                return log_oom();

        dbus_message_iter_init_append(m, &iter);

        if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &scope) ||
            !dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &fail) ||
            !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sv)", &sub))
                return log_oom();

        if (!isempty(slice)) {
                const char *slice_property = "Slice";

                if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
                    !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &slice_property) ||
                    !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, "s", &sub3) ||
                    !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_STRING, &slice) ||
                    !dbus_message_iter_close_container(&sub2, &sub3) ||
                    !dbus_message_iter_close_container(&sub, &sub2))
                        return log_oom();
        }

        if (!isempty(description)) {
                const char *description_property = "Description";

                if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
                    !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description_property) ||
                    !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, "s", &sub3) ||
                    !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_STRING, &description) ||
                    !dbus_message_iter_close_container(&sub2, &sub3) ||
                    !dbus_message_iter_close_container(&sub, &sub2))
                        return log_oom();
        }

        /* cgroup empty notification is not available in containers
         * currently. To make this less problematic, let's shorten the
         * stop timeout for sessions, so that we don't wait
         * forever. */

        if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
            !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &timeout_stop_property) ||
            !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, "t", &sub3) ||
            !dbus_message_iter_append_basic(&sub3, DBUS_TYPE_UINT64, &timeout) ||
            !dbus_message_iter_close_container(&sub2, &sub3) ||
            !dbus_message_iter_close_container(&sub, &sub2))
                return log_oom();

        u = pid;
        if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
            !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &pids_property) ||
            !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, "au", &sub3) ||
            !dbus_message_iter_open_container(&sub3, DBUS_TYPE_ARRAY, "u", &sub4) ||
            !dbus_message_iter_append_basic(&sub4, DBUS_TYPE_UINT32, &u) ||
            !dbus_message_iter_close_container(&sub3, &sub4) ||
            !dbus_message_iter_close_container(&sub2, &sub3) ||
            !dbus_message_iter_close_container(&sub, &sub2))
                return log_oom();

        if (more_properties) {
                r = copy_many_fields(&sub, more_properties);
                if (r < 0)
                        return r;
        }

        if (!dbus_message_iter_close_container(&iter, &sub))
                return log_oom();

        reply = dbus_connection_send_with_reply_and_block(manager->bus, m, -1, error);
        if (!reply)
                return -EIO;

        if (job) {
                const char *j;
                char *copy;

                if (!dbus_message_get_args(reply, error, DBUS_TYPE_OBJECT_PATH, &j, DBUS_TYPE_INVALID))
                        return -EIO;

                copy = strdup(j);
                if (!copy)
                        return -ENOMEM;

                *job = copy;
        }

        return 0;
}

int manager_stop_unit(Manager *manager, const char *unit, DBusError *error, char **job) {
        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
        const char *fail = "fail";
        int r;

        assert(manager);
        assert(unit);

        r = bus_method_call_with_reply(
                        manager->bus,
                        "org.freedesktop.systemd1",
                        "/org/freedesktop/systemd1",
                        "org.freedesktop.systemd1.Manager",
                        "StopUnit",
                        &reply,
                        error,
                        DBUS_TYPE_STRING, &unit,
                        DBUS_TYPE_STRING, &fail,
                        DBUS_TYPE_INVALID);
        if (r < 0) {
                if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
                    dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {

                        if (job)
                                *job = NULL;

                        dbus_error_free(error);
                        return 0;
                }

                log_error("Failed to stop unit %s: %s", unit, bus_error(error, r));
                return r;
        }

        if (job) {
                const char *j;
                char *copy;

                if (!dbus_message_get_args(reply, error,
                                           DBUS_TYPE_OBJECT_PATH, &j,
                                           DBUS_TYPE_INVALID)) {
                        log_error("Failed to parse reply.");
                        return -EIO;
                }

                copy = strdup(j);
                if (!copy)
                        return -ENOMEM;

                *job = copy;
        }

        return 1;
}

int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, DBusError *error) {
        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
        const char *w;
        int r;

        assert(manager);
        assert(unit);

        w = who == KILL_LEADER ? "process" : "cgroup";
        assert_cc(sizeof(signo) == sizeof(int32_t));

        r = bus_method_call_with_reply(
                        manager->bus,
                        "org.freedesktop.systemd1",
                        "/org/freedesktop/systemd1",
                        "org.freedesktop.systemd1.Manager",
                        "KillUnit",
                        &reply,
                        error,
                        DBUS_TYPE_STRING, &unit,
                        DBUS_TYPE_STRING, &w,
                        DBUS_TYPE_INT32, &signo,
                        DBUS_TYPE_INVALID);
        if (r < 0) {
                log_error("Failed to stop unit %s: %s", unit, bus_error(error, r));
                return r;
        }

        return 0;
}

int manager_unit_is_active(Manager *manager, const char *unit) {

        const char *interface = "org.freedesktop.systemd1.Unit";
        const char *property = "ActiveState";
        _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
        _cleanup_free_ char *path = NULL;
        DBusMessageIter iter, sub;
        const char *state;
        DBusError error;
        int r;

        assert(manager);
        assert(unit);

        dbus_error_init(&error);

        path = unit_dbus_path_from_name(unit);
        if (!path)
                return -ENOMEM;

        r = bus_method_call_with_reply(
                        manager->bus,
                        "org.freedesktop.systemd1",
                        path,
                        "org.freedesktop.DBus.Properties",
                        "Get",
                        &reply,
                        &error,
                        DBUS_TYPE_STRING, &interface,
                        DBUS_TYPE_STRING, &property,
                        DBUS_TYPE_INVALID);
        if (r < 0) {
                if (dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY) ||
                    dbus_error_has_name(&error, DBUS_ERROR_DISCONNECTED)) {
                        dbus_error_free(&error);
                        return true;
                }

                if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
                    dbus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) {
                        dbus_error_free(&error);
                        return false;
                }

                log_error("Failed to query ActiveState: %s", bus_error(&error, r));
                dbus_error_free(&error);
                return r;
        }

        if (!dbus_message_iter_init(reply, &iter) ||
            dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
                log_error("Failed to parse reply.");
                return -EINVAL;
        }

        dbus_message_iter_recurse(&iter, &sub);
        if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
                log_error("Failed to parse reply.");
                return -EINVAL;
        }

        dbus_message_iter_get_basic(&sub, &state);

        return !streq(state, "inactive") && !streq(state, "failed");
}