-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdinitctl.cc
More file actions
2656 lines (2371 loc) · 93.2 KB
/
dinitctl.cc
File metadata and controls
2656 lines (2371 loc) · 93.2 KB
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
999
1000
#include <cstdio>
#include <cstddef>
#include <cstring>
#include <optional>
#include <string>
#include <iostream>
#include <fstream>
#include <system_error>
#include <memory>
#include <algorithm>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <signal.h>
#include <pwd.h>
#include "control-cmds.h"
#include "control-datatypes.h"
#include "cpbuffer.h"
#include "dinit-client.h"
#include "dinit-util.h"
#include "dinit-iostream.h"
#include "file-input-stack.h"
#include "load-service.h"
#include "options-processing.h"
#include "service-constants.h"
#include "mconfig.h"
// dinitctl: utility to control the Dinit daemon, including starting and stopping of services.
#define DINIT_CHECK_APPNAME "dinit-check"
// This utility communicates with the dinit daemon via a unix stream socket (as specified in
// SYSCONTROLSOCKET, or $HOME/.dinitctl).
// common communication datatypes
using namespace dinit_cptypes;
// minimum and maximum protocol verions we can speak
static constexpr uint16_t min_cp_version = 1;
static constexpr uint16_t max_cp_version = 5;
enum class ctl_cmd;
static int issue_load_service(int socknum, const char *service_name, bool find_only = false);
static int check_load_reply(int socknum, cpbuffer_t &, handle_t *handle_p, service_state_t *state_p,
bool write_error=true);
static int start_stop_service(int socknum, cpbuffer_t &, const char *service_name, ctl_cmd command,
bool do_pin, bool do_force, bool wait_for_service, bool ignore_unstarted, bool verbose);
static int unpin_service(int socknum, cpbuffer_t &, const char *service_name, bool verbose);
static int unload_service(int socknum, cpbuffer_t &, const char *service_name, bool verbose);
static int reload_service(int socknum, cpbuffer_t &, const char *service_name, bool verbose);
static int list_services(int socknum, cpbuffer_t &, uint16_t proto_version);
static int service_status(int socknum, cpbuffer_t &rbuffer, const char *service_name,
ctl_cmd command, uint16_t proto_version, bool verbose);
static int shutdown_dinit(int soclknum, cpbuffer_t &, bool verbose);
static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add, const char *service_from,
const char *service_to, dependency_type dep_type, bool verbose);
static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, service_dir_opt &service_dir_opts,
const char *from, const char *to, bool enable, const char *environment_file, bool verbose,
uint16_t proto_version);
static int do_setenv(int socknum, cpbuffer_t &rbuffer, std::vector<const char *> &env_names, bool unset);
static int trigger_service(int socknum, cpbuffer_t &rbuffer, const char *service_name, bool trigger_value);
static int cat_service_log(int socknum, cpbuffer_t &rbuffer, const char *service_name, bool do_clear);
static int signal_send(int socknum, cpbuffer_t &rbuffer, const char *service_name, sig_num_t sig_num);
static int signal_list();
static std::string get_service_description_dir(int socknum, cpbuffer_t &rbuffer, handle_t service_handle);
enum class ctl_cmd {
NONE,
START_SERVICE,
WAKE_SERVICE,
STOP_SERVICE,
RESTART_SERVICE,
RELEASE_SERVICE,
UNPIN_SERVICE,
UNLOAD_SERVICE,
RELOAD_SERVICE,
LIST_SERVICES,
SERVICE_STATUS,
SHUTDOWN,
ADD_DEPENDENCY,
RM_DEPENDENCY,
ENABLE_SERVICE,
DISABLE_SERVICE,
SETENV,
UNSETENV,
SET_TRIGGER,
UNSET_TRIGGER,
CAT_LOG,
SIG_SEND,
SIG_LIST,
IS_STARTED,
IS_FAILED,
};
// Entry point.
int dinitctl_main(int argc, char **argv)
{
using std::cout;
using std::cerr;
// general options
bool cmdline_error = false;
bool show_help = argc < 2; // show help if no arguments
std::string control_socket_str;
const char *control_socket_path = nullptr;
bool verbose = true;
bool user_dinit = (getuid() != 0); // communicate with user daemon
service_dir_opt service_dir_opts;
bool offline = false;
const char *env_file = nullptr;
// general command options
ctl_cmd command = ctl_cmd::NONE;
std::vector<const char *> cmd_args;
// specific command options
const char *service_name = nullptr;
const char *to_service_name = nullptr;
dependency_type dep_type = dependency_type::AFTER; // avoid maybe-uninitialised warning
bool dep_type_set = false;
bool catlog_clear = false;
bool wait_for_service = true;
bool do_pin = false;
bool do_force = false;
bool ignore_unstarted = false;
bool use_passed_cfd = false;
bool show_siglist = false;
std::string sigstr;
sig_num_t sig_num = -1;
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
if (strcmp(argv[i], "--help") == 0) {
show_help = true;
break;
}
else if (strcmp(argv[i], "--version") == 0) {
cout << "Dinit version " << DINIT_VERSION << ".\n";
return 0;
}
else if (strcmp(argv[i], "--no-wait") == 0) {
wait_for_service = false;
}
else if (strcmp(argv[i], "--ignore-unstarted") == 0) {
ignore_unstarted = true;
}
else if (strcmp(argv[i], "--quiet") == 0) {
verbose = false;
}
else if (strcmp(argv[i], "--system") == 0 || strcmp(argv[i], "-s") == 0) {
user_dinit = false;
}
else if (strcmp(argv[i], "--user") == 0 || strcmp(argv[i], "-u") == 0) {
user_dinit = true;
}
else if (strcmp(argv[i], "--pin") == 0) {
do_pin = true;
}
else if (strcmp(argv[i], "--socket-path") == 0 || strcmp(argv[i], "-p") == 0) {
++i;
if (i == argc || argv[i][0] == '\0') {
cerr << "dinitctl: --socket-path/-p should be followed by socket path\n";
return 1;
}
control_socket_str = argv[i];
}
else if (strcmp(argv[i], "--use-passed-cfd") == 0) {
use_passed_cfd = true;
}
else if (strcmp(argv[i], "--from") == 0) {
if (command == ctl_cmd::ENABLE_SERVICE || command == ctl_cmd::DISABLE_SERVICE) {
++i;
if (i == argc || argv[i][0] == '\0') {
cerr << "dinitctl: --from should be followed by a service name\n";
return 1;
}
service_name = argv[i];
}
else {
cmdline_error = true;
break;
}
}
else if (strcmp(argv[i], "--force") == 0 || strcmp(argv[i], "-f") == 0) {
if (command == ctl_cmd::STOP_SERVICE || command == ctl_cmd::RESTART_SERVICE) {
do_force = true;
}
else {
cmdline_error = true;
break;
}
}
else if (strcmp(argv[i], "--clear") == 0) {
if (command == ctl_cmd::CAT_LOG) {
catlog_clear = true;
}
else {
cmdline_error = true;
break;
}
}
else if (strcmp(argv[i], "--list") == 0 || strcmp(argv[i], "-l") == 0) {
if (command == ctl_cmd::SIG_SEND) {
show_siglist = true;
}
else {
cmdline_error = true;
break;
}
}
else if (strcmp(argv[i], "--services-dir") == 0 || strcmp(argv[i], "-d") == 0) {
if (++i < argc && argv[i][0] != '\0') {
service_dir_opts.set_specified_service_dir(argv[i]);
}
else {
cerr << "dinitctl: '--services-dir' (-d) requires an argument\n";
return 1;
}
}
else if (strcmp(argv[i], "--offline") == 0 || strcmp(argv[i], "-o") == 0) {
offline = true;
}
else {
cerr << "dinitctl: unrecognized/invalid option: " << argv[i] << " (use --help for help)\n";
return 1;
}
}
else if (command == ctl_cmd::NONE) {
if (strcmp(argv[i], "start") == 0) {
command = ctl_cmd::START_SERVICE;
}
else if (strcmp(argv[i], "wake") == 0) {
command = ctl_cmd::WAKE_SERVICE;
}
else if (strcmp(argv[i], "stop") == 0) {
command = ctl_cmd::STOP_SERVICE;
}
else if (strcmp(argv[i], "restart") == 0) {
command = ctl_cmd::RESTART_SERVICE;
}
else if (strcmp(argv[i], "release") == 0) {
command = ctl_cmd::RELEASE_SERVICE;
}
else if (strcmp(argv[i], "unpin") == 0) {
command = ctl_cmd::UNPIN_SERVICE;
}
else if (strcmp(argv[i], "unload") == 0) {
command = ctl_cmd::UNLOAD_SERVICE;
}
else if (strcmp(argv[i], "reload") == 0) {
command = ctl_cmd::RELOAD_SERVICE;
}
else if (strcmp(argv[i], "list") == 0) {
command = ctl_cmd::LIST_SERVICES;
}
else if (strcmp(argv[i], "status") == 0) {
command = ctl_cmd::SERVICE_STATUS;
}
else if (strcmp(argv[i], "is-started") == 0) {
command = ctl_cmd::IS_STARTED;
}
else if (strcmp(argv[i], "is-failed") == 0) {
command = ctl_cmd::IS_FAILED;
}
else if (strcmp(argv[i], "shutdown") == 0) {
command = ctl_cmd::SHUTDOWN;
}
else if (strcmp(argv[i], "add-dep") == 0) {
command = ctl_cmd::ADD_DEPENDENCY;
}
else if (strcmp(argv[i], "rm-dep") == 0) {
command = ctl_cmd::RM_DEPENDENCY;
}
else if (strcmp(argv[i], "enable") == 0) {
command = ctl_cmd::ENABLE_SERVICE;
}
else if (strcmp(argv[i], "disable") == 0) {
command = ctl_cmd::DISABLE_SERVICE;
}
else if (strcmp(argv[i], "setenv") == 0) {
command = ctl_cmd::SETENV;
}
else if (strcmp(argv[i], "unsetenv") == 0) {
command = ctl_cmd::UNSETENV;
}
else if (strcmp(argv[i], "trigger") == 0) {
command = ctl_cmd::SET_TRIGGER;
}
else if (strcmp(argv[i], "untrigger") == 0) {
command = ctl_cmd::UNSET_TRIGGER;
}
else if (strcmp(argv[i], "catlog") == 0) {
command = ctl_cmd::CAT_LOG;
}
else if (strcmp(argv[i], "signal") == 0) {
command = ctl_cmd::SIG_SEND;
}
else {
cerr << "dinitctl: unrecognized command: " << argv[i] << " (use --help for help)\n";
return 1;
}
}
else {
// service name / other non-option
if (argv[i][0] == '\0') {
cerr << "dinitctl: Invalid empty argument\n";
return 1;
}
if (command == ctl_cmd::ADD_DEPENDENCY || command == ctl_cmd::RM_DEPENDENCY) {
if (! dep_type_set) {
if (strcmp(argv[i], "need") == 0 || strcmp(argv[i], "regular") == 0) {
dep_type = dependency_type::REGULAR;
}
else if (strcmp(argv[i], "milestone") == 0) {
dep_type = dependency_type::MILESTONE;
}
else if (strcmp(argv[i], "waits-for") == 0) {
dep_type = dependency_type::WAITS_FOR;
}
else {
cmdline_error = true;
break;
}
dep_type_set = true;
}
else if (service_name == nullptr) {
service_name = argv[i];
}
else if (to_service_name == nullptr) {
to_service_name = argv[i];
}
else {
cmdline_error = true;
break;
}
}
else if (command == ctl_cmd::ENABLE_SERVICE || command == ctl_cmd::DISABLE_SERVICE) {
if (to_service_name != nullptr) {
cmdline_error = true;
break;
}
to_service_name = argv[i];
}
else if (command == ctl_cmd::SIG_SEND) {
if (!show_siglist) {
if (sigstr.empty()) {
sigstr = argv[i];
}
else if (service_name == nullptr) {
service_name = argv[i];
}
else {
cmdline_error = true;
}
}
else {
cmdline_error = true;
}
}
else {
cmd_args.push_back(argv[i]);
}
}
}
// Additional argument checks/processing for various commands:
if (command == ctl_cmd::NONE && !show_help) {
cmdline_error = true;
}
else if (command == ctl_cmd::ENABLE_SERVICE || command == ctl_cmd::DISABLE_SERVICE) {
cmdline_error |= (to_service_name == nullptr);
}
else if (command == ctl_cmd::SETENV || command == ctl_cmd::UNSETENV) {
// Handle (UN)SETENV specially, since it needs arguments but they are not service names
if (cmd_args.empty()) {
cmdline_error = true;
}
}
else if (command == ctl_cmd::SIG_SEND) {
if (show_siglist) {
if (sigstr.empty()) {
command = ctl_cmd::SIG_LIST;
}
else {
cmdline_error = true;
}
}
else {
if (sigstr.empty()) {
cerr << "dinitctl: signal number/name must be specified\n";
return 1;
}
if (service_name == nullptr) {
cerr << "dinitctl: service name must be specified\n";
return 1;
}
sig_num = dinit_load::signal_name_to_number(sigstr);
if (sig_num <= 0) {
// (0 actually means "none"/"NONE", but we'll just fall through and error out when
// it fails to parse as an integer)
try {
size_t pos;
sig_num = std::stoi(sigstr, &pos);
if (sigstr.size() != pos) {
throw std::invalid_argument("");
}
}
catch (std::exception &) { // invalid_argument, out_of_range
cerr << "dinitctl: '" << sigstr
<< "' is not a valid signal name/number" << std::endl;
return 1;
}
}
}
}
else {
bool no_service_cmd = (command == ctl_cmd::LIST_SERVICES
|| command == ctl_cmd::SHUTDOWN
|| command == ctl_cmd::SIG_LIST);
if (no_service_cmd) {
if (!cmd_args.empty()) {
cmdline_error = true;
}
}
else {
if (command == ctl_cmd::ADD_DEPENDENCY || command == ctl_cmd::RM_DEPENDENCY) {
if (! dep_type_set || service_name == nullptr || to_service_name == nullptr) {
cmdline_error = true;
}
}
else if (cmd_args.empty()) {
cmdline_error = true;
}
else {
// No command can currently accept more than one service argument:
if (cmd_args.size() > 1) {
cmdline_error = true;
}
service_name = cmd_args.front();
}
}
}
if (show_help) {
cout << "dinitctl: control Dinit services\n"
"\n"
"Usage:\n"
" dinitctl [options] status <service-name>\n"
" dinitctl [options] is-started <service-name>\n"
" dinitctl [options] is-failed <service-name>\n"
" dinitctl [options] start [options] <service-name>\n"
" dinitctl [options] stop [options] <service-name>\n"
" dinitctl [options] restart [options] <service-name>\n"
" dinitctl [options] wake [options] <service-name>\n"
" dinitctl [options] release [options] <service-name>\n"
" dinitctl [options] unpin <service-name>\n"
" dinitctl [options] unload <service-name>\n"
" dinitctl [options] reload <service-name>\n"
" dinitctl [options] list\n"
" dinitctl [options] shutdown\n"
" dinitctl [options] add-dep <type> <from-service> <to-service>\n"
" dinitctl [options] rm-dep <type> <from-service> <to-service>\n"
" dinitctl [options] enable [--from <from-service>] <to-service>\n"
" dinitctl [options] disable [--from <from-service>] <to-service>\n"
" dinitctl [options] trigger <service-name>\n"
" dinitctl [options] untrigger <service-name>\n"
" dinitctl [options] setenv [name[=value] ...]\n"
" dinitctl [options] unsetenv [name ...]\n"
" dinitctl [options] catlog <service-name>\n"
" dinitctl [options] signal <signal> <service-name>\n"
"\n"
"Note: An activated service continues running when its dependents stop.\n"
"\n"
"General options:\n"
" --help : show this help\n"
" -s, --system : control system daemon (default if run as root)\n"
" -u, --user : control user daemon\n"
" --quiet : suppress output (except errors)\n"
" --socket-path <path>, -p <path>\n"
" : specify socket for communication with daemon\n"
" --use-passed-cfd : use the socket file descriptor identified by the DINIT_CS_FD\n"
" environment variable to communicate with the dinit daemon\n"
" -o, --offline : do not contact running dinit daemon\n"
" -d, --services-dir <dir>\n"
" : specify directory for service definitions (offline mode)\n"
"\n"
"Command options:\n"
" --no-wait : don't wait for service startup/shutdown to complete\n"
" --pin : pin the service in the requested state\n"
" --force : force stop even if dependents will be affected\n"
" -l, --list : (signal) list supported signals\n";
return 0;
}
if (cmdline_error) {
cerr << "dinitctl: Invalid command line.\n"
"Try 'dinitctl --help' for more information.\n";
return 1;
}
// SIG_LIST doesn't need a control socket connection so handle it specially.
if (command == ctl_cmd::SIG_LIST) {
return signal_list();
}
cpbuffer_t rbuffer;
if (offline) {
if (command != ctl_cmd::ENABLE_SERVICE && command != ctl_cmd::DISABLE_SERVICE) {
cerr << "dinitctl: offline mode (--offline/-o) not supported for this command\n";
return 1;
}
service_dir_opts.build_paths(!user_dinit);
if (env_file == nullptr && !user_dinit) {
env_file = "/etc/dinit/environment";
}
if (command == ctl_cmd::ENABLE_SERVICE || command == ctl_cmd::DISABLE_SERVICE) {
return enable_disable_service(-1, rbuffer, service_dir_opts, service_name, to_service_name,
command == ctl_cmd::ENABLE_SERVICE, env_file, verbose, 0);
}
}
// Begin the real work: connect to dinit
signal(SIGPIPE, SIG_IGN);
int socknum = -1;
if (use_passed_cfd) {
socknum = get_passed_cfd();
if (socknum == -1) {
use_passed_cfd = false;
}
}
bool user_specified_cs_path = false;
if (!use_passed_cfd) {
// Locate control socket
if (!control_socket_str.empty()) {
control_socket_path = control_socket_str.c_str();
user_specified_cs_path = true;
}
else {
control_socket_path = get_default_socket_path(control_socket_str, user_dinit);
if (control_socket_path == nullptr) {
cerr << "dinitctl: cannot determine control socket directory (set XDG_RUNTIME_DIR or HOME, check /etc/passwd file, or "
"specify socket path via -p)\n";
return 1;
}
}
}
try {
if (!use_passed_cfd) {
socknum = connect_to_daemon(control_socket_path);
}
// Start by querying protocol version:
uint16_t daemon_protocol_ver = check_protocol_version(min_cp_version, max_cp_version, rbuffer, socknum);
if (command == ctl_cmd::UNPIN_SERVICE) {
return unpin_service(socknum, rbuffer, service_name, verbose);
}
else if (command == ctl_cmd::UNLOAD_SERVICE) {
return unload_service(socknum, rbuffer, service_name, verbose);
}
else if (command == ctl_cmd::RELOAD_SERVICE) {
return reload_service(socknum, rbuffer, service_name, verbose);
}
else if (command == ctl_cmd::LIST_SERVICES) {
return list_services(socknum, rbuffer, daemon_protocol_ver);
}
else if (command == ctl_cmd::SERVICE_STATUS || command == ctl_cmd::IS_STARTED
|| command == ctl_cmd::IS_FAILED) {
return service_status(socknum, rbuffer, service_name, command, daemon_protocol_ver, verbose);
}
else if (command == ctl_cmd::SHUTDOWN) {
return shutdown_dinit(socknum, rbuffer, verbose);
}
else if (command == ctl_cmd::ADD_DEPENDENCY || command == ctl_cmd::RM_DEPENDENCY) {
return add_remove_dependency(socknum, rbuffer, command == ctl_cmd::ADD_DEPENDENCY,
service_name, to_service_name, dep_type, verbose);
}
else if (command == ctl_cmd::ENABLE_SERVICE || command == ctl_cmd::DISABLE_SERVICE) {
// If only one service specified, assume that we enable for 'boot' service:
if (daemon_protocol_ver < 3) {
// We need QUERYSERVICEDSCDIR
throw cp_old_server_exception();
}
return enable_disable_service(socknum, rbuffer, service_dir_opts, service_name, to_service_name,
command == ctl_cmd::ENABLE_SERVICE, env_file, verbose, daemon_protocol_ver);
}
else if (command == ctl_cmd::SETENV || command == ctl_cmd::UNSETENV) {
return do_setenv(socknum, rbuffer, cmd_args, command == ctl_cmd::UNSETENV);
}
else if (command == ctl_cmd::SET_TRIGGER || command == ctl_cmd::UNSET_TRIGGER) {
if (daemon_protocol_ver < 2) {
throw cp_old_server_exception();
}
return trigger_service(socknum, rbuffer, service_name, (command == ctl_cmd::SET_TRIGGER));
}
else if (command == ctl_cmd::CAT_LOG) {
if (daemon_protocol_ver < 2) {
throw cp_old_server_exception();
}
return cat_service_log(socknum, rbuffer, service_name, catlog_clear);
}
else if (command == ctl_cmd::SIG_SEND) {
if (daemon_protocol_ver < 2) {
throw cp_old_server_exception();
}
return signal_send(socknum, rbuffer, service_name, sig_num);
}
else {
return start_stop_service(socknum, rbuffer, service_name, command, do_pin, do_force,
wait_for_service, ignore_unstarted, verbose);
}
}
catch (cp_old_client_exception &e) {
std::cerr << "dinitctl: too old (daemon reports newer protocol version)" << std::endl;
}
catch (cp_old_server_exception &e) {
std::cerr << "dinitctl: daemon too old or protocol error" << std::endl;
}
catch (cp_read_exception &e) {
cerr << "dinitctl: control socket read failure or protocol error\n";
}
catch (cp_write_exception &e) {
cerr << "dinitctl: control socket write error: " << std::strerror(e.errcode) << "\n";
}
catch (dinit_protocol_error &e) {
cerr << "dinitctl: protocol error\n";
}
catch (control_sock_conn_err &ce) {
cerr << "dinitctl: " << ce.get_action() << ": " << ce.get_arg() << ": " << strerror(ce.get_err()) << "\n";
if (user_dinit && ce.get_err() == ENOENT && !user_specified_cs_path) {
// It is common enough that users don't realise they need to have a user instance
// running in order to control it, so elaborate a little:
cerr << "dinitctl: perhaps no user instance is running?\n";
}
}
catch (general_error &ge) {
std::cerr << "dinitctl";
if (ge.get_action() != nullptr) {
std::cerr << ": " << ge.get_action();
std::string &arg = ge.get_arg();
if (!arg.empty()) {
std::cerr << " " << arg;
}
}
if (ge.get_err() != 0) {
std::cerr << ": " << strerror(ge.get_err());
}
std::cerr << '\n';
}
return 1;
}
int main(int argc, char **argv)
{
try {
return dinitctl_main(argc, argv);
}
catch (std::bad_alloc &e) {
std::cerr << "dinitctl: out of memory\n";
}
return 1;
}
static const char *describe_state(bool stopped)
{
return stopped ? "stopped" : "started";
}
static const char *describe_verb(bool stop)
{
return stop ? "stop" : "start";
}
// Load a service: issue load command, wait for reply. Return true on success, display error message
// and return false on failure.
// socknum - the socket fd to communicate via
// rbuffer - the buffer for communication
// name - the name of the service to load
// handle - where to store the handle of the loaded service
// state - where to store the state of the loaded service (may be null).
// write_error - whether to write an error message if the service can't be loaded
static bool load_service(int socknum, cpbuffer_t &rbuffer, const char *name, handle_t *handle,
service_state_t *state, bool write_error=true)
{
// Load 'to' service:
if (issue_load_service(socknum, name)) {
return false;
}
wait_for_reply(rbuffer, socknum);
if (check_load_reply(socknum, rbuffer, handle, state, write_error) != 0) {
return false;
}
return true;
}
// Get the service name for a given handle, by querying the daemon.
static std::string get_service_name(int socknum, cpbuffer_t &rbuffer, handle_t handle)
{
auto m = membuf()
.append((char) cp_cmd::QUERYSERVICENAME)
.append((char) 0)
.append(handle);
write_all_x(socknum, m);
wait_for_reply(rbuffer, socknum);
if (rbuffer[0] != (char)cp_rply::SERVICENAME) {
throw cp_read_exception{0};
}
// 1 byte reserved
// uint16_t size
fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint16_t));
uint16_t namesize;
rbuffer.extract(&namesize, 2, sizeof(uint16_t));
rbuffer.consume(2 + sizeof(uint16_t));
std::string name;
do {
if (rbuffer.get_length() == 0) {
fill_some(rbuffer, socknum);
}
size_t to_extract = std::min(size_t(rbuffer.get_length()), namesize - name.length());
size_t contiguous_len = rbuffer.get_contiguous_length(rbuffer.get_ptr(0));
if (contiguous_len <= to_extract) {
name.append(rbuffer.get_ptr(0), contiguous_len);
rbuffer.consume(contiguous_len);
name.append(rbuffer.get_ptr(0), to_extract - contiguous_len);
rbuffer.consume(to_extract - contiguous_len);
}
else {
name.append(rbuffer.get_ptr(0), to_extract);
rbuffer.consume(to_extract);
break;
}
} while (name.length() < namesize);
return name;
}
static void print_termination_details(int exit_status)
{
using std::cout;
if (WIFSIGNALED(exit_status)) {
cout << "signalled - signal ";
cout << WTERMSIG(exit_status);
}
else if (WIFEXITED(exit_status)) {
cout << "exited - status ";
cout << WEXITSTATUS(exit_status);
}
else {
cout << "unknown reason";
}
}
static void print_termination_details(int exit_si_code, int exit_si_status)
{
using std::cout;
if (exit_si_code == CLD_KILLED) {
cout << "signalled - signal ";
cout << exit_si_status;
}
else if (exit_si_code == CLD_EXITED) {
cout << "exited - status ";
cout << exit_si_status;
}
else {
cout << "unknown reason";
}
}
// Print reason for start failure.
static void print_failure_details(stopped_reason_t stop_reason, uint16_t launch_stage,
int exit_status, int exit_si_code, int exit_si_status)
{
using std::cout;
switch (stop_reason) {
case stopped_reason_t::DEPFAILED:
cout << "Reason: a dependency of the service failed to start. "
"Check dinit log.\n";
break;
case stopped_reason_t::TIMEDOUT:
cout << "Reason: start timed out.\n";
break;
case stopped_reason_t::EXECFAILED:
cout << "Reason: execution of service process failed:\n";
cout << " Stage: " << exec_stage_descriptions[launch_stage] << "\n";
cout << " Error: " << strerror(exit_status) << "\n";
break;
case stopped_reason_t::FAILED:
cout << "Reason: service process terminated before ready: ";
if (exit_si_code != 0 || exit_si_status != 0) {
print_termination_details(exit_si_code, exit_si_status);
}
else {
print_termination_details(exit_status);
}
cout << "\n";
break;
default:
cout << "Reason unknown/unrecognised. Check dinit log.\n";
}
}
// Process a SERVICEEVENT[5] packet if it is related to the specified service handle, and
// optionally report the service status to the user (verbose == true). The caller must ensure that
// a complete packet of type SERVICEEVENT[5] is present in the buffer before calling. The size of
// the packet should be provided as pktlen.
//
// Returns 0 if the service started (do_stop == false) or stopped (do_stop == true), 1 if
// start/stop was cancelled or failed, -1 when the service event is not related to given service
// handle or does not correspond to a start (or stop) or failure.
static int process_service_event(cpbuffer_t &rbuffer, unsigned pktlen, handle_t handle,
const std::string &service_name, bool do_stop, bool verbose)
{
using std::cout;
// earlier versions do not include status info, the size in that case is
// base_pkt_size:
constexpr unsigned base_pkt_size = 2 + sizeof(handle_t) + 1;
if (pktlen < base_pkt_size) {
throw dinit_protocol_error();
}
// version 5 packets include extended staus info:
if (rbuffer[0] == (char)cp_info::SERVICEEVENT5 && pktlen < (base_pkt_size + STATUS_BUFFER5_SIZE)) {
throw dinit_protocol_error();
}
service_event_t completion_event;
service_event_t cancelled_event;
if (do_stop) {
completion_event = service_event_t::STOPPED;
cancelled_event = service_event_t::STOPCANCELLED;
}
else {
completion_event = service_event_t::STARTED;
cancelled_event = service_event_t::STARTCANCELLED;
}
handle_t ev_handle;
rbuffer.extract((char *)&ev_handle, 2, sizeof(ev_handle));
service_event_t event = static_cast<service_event_t>(rbuffer[2 + sizeof(ev_handle)]);
if (ev_handle == handle) {
if (event == completion_event) {
if (verbose) {
cout << "Service '" << service_name << "' " << describe_state(do_stop)
<< ".\n";
}
rbuffer.consume(pktlen);
return 0;
}
else if (event == cancelled_event) {
if (verbose) {
cout << "Service '" << service_name << "' " << describe_verb(do_stop)
<< " cancelled.\n";
}
rbuffer.consume(pktlen);
return 1;
}
else if (!do_stop && event == service_event_t::FAILEDSTART) {
if (verbose) {
cout << "Service '" << service_name << "' failed to start.\n";
if (pktlen >= base_pkt_size + STATUS_BUFFER_SIZE) {
uint16_t launch_stage;
rbuffer.extract((char *)&launch_stage, base_pkt_size + 4,
sizeof(uint16_t));
stopped_reason_t stop_reason = static_cast<stopped_reason_t>(rbuffer[base_pkt_size + 3]);
int exit_status;
int exit_si_code = 0;
int exit_si_status = 0;
rbuffer.extract((char *)&exit_status, base_pkt_size + 6, sizeof(exit_status));
if (rbuffer[0] == (char)cp_info::SERVICEEVENT5) {
exit_si_code = exit_status;
rbuffer.extract((char *)&exit_si_status,
base_pkt_size + 6 + sizeof(exit_si_code), sizeof(exit_si_status));
}
print_failure_details(stop_reason, launch_stage, exit_status, exit_si_code, exit_si_status);
}
}
rbuffer.consume(pktlen);
return 1;
}
}
rbuffer.consume(pktlen);
return -1;
}
// Wait for a service to reached stopped (do_stop == true) or started (do_stop == false) state.
// Returns 0 if the service started/stopped, 1 if start/stop was cancelled or failed.
static int wait_service_state(int socknum, cpbuffer_t &rbuffer, handle_t handle,
const std::string &service_name, bool do_stop, bool verbose)
{
// Wait until service started:
int r = rbuffer.fill_to(socknum, 2);
while (r > 0) {
if (rbuffer[0] >= 100) {
unsigned pktlen = (unsigned char) rbuffer[1];
fill_buffer_to(rbuffer, socknum, pktlen);
if (value((cp_info)rbuffer[0]).is_in(cp_info::SERVICEEVENT, cp_info::SERVICEEVENT5)) {
int ret = process_service_event(rbuffer, pktlen, handle, service_name, do_stop, verbose);
if (ret >= 0) {
return ret;
}
}
else {
rbuffer.consume(pktlen);
}
r = rbuffer.fill_to(socknum, 2);
}
else {
// Not an information packet?
throw dinit_protocol_error();
}
}
if (r == -1) {
perror("dinitctl: read");
}
else {
throw dinit_protocol_error();
}
return 1;
}
// Start/stop/restart a service
static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *service_name,
ctl_cmd command, bool do_pin, bool do_force, bool wait_for_service, bool ignore_unstarted,
bool verbose)
{
using std::cout;
using std::cerr;
bool do_stop = (command == ctl_cmd::STOP_SERVICE || command == ctl_cmd::RELEASE_SERVICE);
service_state_t state;
handle_t handle;
if (command != ctl_cmd::RESTART_SERVICE && command != ctl_cmd::STOP_SERVICE
&& command != ctl_cmd::RELEASE_SERVICE) {
ignore_unstarted = false;
}
if (!load_service(socknum, rbuffer, service_name, &handle, &state, !ignore_unstarted)) {
return ignore_unstarted ? 0 : 1;
}
service_state_t wanted_state = do_stop ? service_state_t::STOPPED : service_state_t::STARTED;
cp_cmd pcommand;
switch (command) {
case ctl_cmd::STOP_SERVICE:
case ctl_cmd::RESTART_SERVICE: // stop, and then start
pcommand = cp_cmd::STOPSERVICE;
break;
case ctl_cmd::RELEASE_SERVICE:
pcommand = cp_cmd::RELEASESERVICE;
break;
case ctl_cmd::START_SERVICE:
pcommand = cp_cmd::STARTSERVICE;
break;
case ctl_cmd::WAKE_SERVICE:
pcommand = cp_cmd::WAKESERVICE;
break;
default:
// can't get here (hopefully)
pcommand = cp_cmd::STOPSERVICE;
}
observed_states_t seen_states;