2 * Copyright 2015-2016, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
7 /*! The launch_daemon's companion command line tool. */
10 #include <LaunchRoster.h>
11 #include <StringList.h>
20 static struct option
const kLongOptions
[] = {
21 {"verbose", no_argument
, 0, 'v'},
22 {"help", no_argument
, 0, 'h'},
26 extern const char *__progname
;
27 static const char *kProgramName
= __progname
;
31 list_jobs(bool verbose
)
35 status_t status
= roster
.GetJobs(NULL
, jobs
);
37 fprintf(stderr
, "%s: Could not get job listing: %s\n", kProgramName
,
42 for (int32 i
= 0; i
< jobs
.CountStrings(); i
++)
43 puts(jobs
.StringAt(i
).String());
48 list_targets(bool verbose
)
52 status_t status
= roster
.GetTargets(targets
);
54 fprintf(stderr
, "%s: Could not get target listing: %s\n", kProgramName
,
59 for (int32 i
= 0; i
< targets
.CountStrings(); i
++)
60 puts(targets
.StringAt(i
).String());
65 get_info(const char* name
)
69 status_t targetStatus
= roster
.GetTargetInfo(name
, info
);
70 if (targetStatus
== B_OK
) {
71 printf("Target: %s\n", name
);
76 status_t jobStatus
= roster
.GetJobInfo(name
, info
);
77 if (jobStatus
== B_OK
) {
78 printf("Job: %s\n", name
);
82 if (jobStatus
!= B_OK
&& targetStatus
!= B_OK
) {
83 fprintf(stderr
, "%s: Could not get target or job info for \"%s\": "
84 "%s\n", kProgramName
, name
, strerror(jobStatus
));
91 start_job(const char* name
)
94 status_t status
= roster
.Start(name
);
95 if (status
== B_NAME_NOT_FOUND
)
96 status
= roster
.Target(name
);
99 fprintf(stderr
, "%s: Starting job \"%s\" failed: %s\n", kProgramName
,
100 name
, strerror(status
));
107 stop_job(const char* name
)
109 BLaunchRoster roster
;
110 status_t status
= roster
.Stop(name
);
111 if (status
== B_NAME_NOT_FOUND
)
112 status
= roster
.StopTarget(name
);
114 if (status
!= B_OK
) {
115 fprintf(stderr
, "%s: Stopping job \"%s\" failed: %s\n", kProgramName
,
116 name
, strerror(status
));
123 restart_job(const char* name
)
131 enable_job(const char* name
, bool enable
)
133 BLaunchRoster roster
;
134 status_t status
= roster
.SetEnabled(name
, enable
);
135 if (status
!= B_OK
) {
136 fprintf(stderr
, "%s: %s job \"%s\" failed: %s\n", kProgramName
,
137 enable
? "Enabling" : "Disabling", name
, strerror(status
));
146 fprintf(stderr
, "Usage: %s <command>\n"
147 "Where <command> is one of:\n"
148 " list - Lists all jobs (the default command)\n"
149 " list-targets - Lists all targets\n"
150 "The following <command>s have a <name> argument:\n"
151 " start - Starts a job/target\n"
152 " stop - Stops a running job/target\n"
153 " restart - Restarts a running job/target\n"
154 " info - Shows info for a job/target\n",
162 main(int argc
, char** argv
)
164 const char* command
= "list";
165 bool verbose
= false;
168 while ((c
= getopt_long(argc
, argv
, "hv", kLongOptions
, NULL
)) != -1) {
184 if (argc
- optind
>= 1)
185 command
= argv
[optind
];
187 if (strcmp(command
, "list") == 0) {
189 } else if (strcmp(command
, "list-targets") == 0) {
190 list_targets(verbose
);
191 } else if (argc
== optind
+ 1) {
192 // For convenience (the "info" command can be omitted)
195 // All commands that need a name following
197 const char* name
= argv
[argc
- 1];
199 if (strcmp(command
, "info") == 0) {
201 } else if (strcmp(command
, "start") == 0) {
203 } else if (strcmp(command
, "stop") == 0) {
205 } else if (strcmp(command
, "restart") == 0) {
207 } else if (strcmp(command
, "enable") == 0) {
208 enable_job(name
, true);
209 } else if (strcmp(command
, "disable") == 0) {
210 enable_job(name
, false);
212 fprintf(stderr
, "%s: Unknown command \"%s\".\n", kProgramName
,