launch_roster: The beginnings of a launch_daemon control tool.

This commit is contained in:
Axel Dörfler
2015-11-08 00:09:26 +01:00
parent b0ecbc13f0
commit 7406866340
3 changed files with 184 additions and 1 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ SYSTEM_BIN = [ FFilterByBuildFeatures
hd hey hd hey
ideinfo@ide idestatus@ide ifconfig iroster isvolume ideinfo@ide idestatus@ide ifconfig iroster isvolume
kernel_debugger keymap keystore kernel_debugger keymap keystore
linkcatkeys listarea listattr listimage listdev launch_roster linkcatkeys listarea listattr listimage listdev
listport listres listsem listusb locale logger login lsindex listport listres listsem listusb locale logger login lsindex
makebootable message mimeset mkfs mkindex makebootable message mimeset mkfs mkindex
modifiers mount mountvolume modifiers mount mountvolume
+1
View File
@@ -89,6 +89,7 @@ StdBinCommands
draggers.cpp draggers.cpp
ffm.cpp ffm.cpp
iroster.cpp iroster.cpp
launch_roster.cpp
listattr.cpp listattr.cpp
listfont.cpp listfont.cpp
listres.cpp listres.cpp
+182
View File
@@ -0,0 +1,182 @@
/*
* Copyright 2015, Axel Dörfler, [email protected].
* Distributed under the terms of the MIT License.
*/
/*! The launch_daemon's companion command line tool. */
#include <LaunchRoster.h>
#include <StringList.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
static struct option const kLongOptions[] = {
{"verbose", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{NULL}
};
extern const char *__progname;
static const char *kProgramName = __progname;
static void
list_jobs(bool verbose)
{
BLaunchRoster roster;
BStringList jobs;
status_t status = roster.GetJobs(NULL, jobs);
if (status != B_OK) {
fprintf(stderr, "%s: Could not get job listing: %s\n", kProgramName,
strerror(status));
exit(EXIT_FAILURE);
}
for (int32 i = 0; i < jobs.CountStrings(); i++)
puts(jobs.StringAt(i).String());
}
static void
list_targets(bool verbose)
{
BLaunchRoster roster;
BStringList targets;
status_t status = roster.GetTargets(targets);
if (status != B_OK) {
fprintf(stderr, "%s: Could not get target listing: %s\n", kProgramName,
strerror(status));
exit(EXIT_FAILURE);
}
for (int32 i = 0; i < targets.CountStrings(); i++)
puts(targets.StringAt(i).String());
}
static void
get_info(const char* name)
{
BLaunchRoster roster;
BMessage info;
status_t targetStatus = roster.GetTargetInfo(name, info);
if (targetStatus == B_OK) {
printf("Target: %s\n", name);
info.PrintToStream();
}
info.MakeEmpty();
status_t jobStatus = roster.GetJobInfo(name, info);
if (jobStatus == B_OK) {
printf("Job: %s\n", name);
info.PrintToStream();
}
if (jobStatus != B_OK && targetStatus != B_OK) {
fprintf(stderr, "%s: Could not get target or job info for \"%s\": "
"%s\n", kProgramName, name, strerror(jobStatus));
exit(EXIT_FAILURE);
}
}
static void
start_job(const char* name)
{
BLaunchRoster roster;
status_t status = roster.Start(name);
if (status != B_OK) {
fprintf(stderr, "%s: Starting job \"%s\" failed: %s\n", kProgramName,
name, strerror(status));
exit(EXIT_FAILURE);
}
}
static void
stop_job(const char* name)
{
BLaunchRoster roster;
status_t status = roster.Stop(name);
if (status != B_OK) {
fprintf(stderr, "%s: Stopping job \"%s\" failed: %s\n", kProgramName,
name, strerror(status));
exit(EXIT_FAILURE);
}
}
static void
usage(int status)
{
fprintf(stderr, "Usage: %s <command>\n"
"Where <command> is one of:\n"
" list - Lists all jobs (the default command)\n"
" list-targets - Lists all targets\n"
"The following <command>s have a <name> argument:\n"
" start - Starts a job\n"
" stop - Stops a running job\n"
" info - Shows info for a job/target\n",
kProgramName);
exit(status);
}
int
main(int argc, char** argv)
{
const char* command = "list";
bool verbose = false;
int c;
while ((c = getopt_long(argc, argv, "hv", kLongOptions, NULL)) != -1) {
switch (c) {
case 0:
break;
case 'h':
usage(0);
break;
case 'v':
verbose = true;
break;
default:
usage(1);
break;
}
}
if (argc - optind >= 1)
command = argv[optind];
if (strcmp(command, "list") == 0) {
list_jobs(verbose);
} else if (strcmp(command, "list-targets") == 0) {
list_targets(verbose);
} else {
// All commands that need a name following
if (argc == optind + 1)
usage(1);
const char* name = argv[argc - 1];
if (strcmp(command, "info") == 0) {
get_info(name);
} else if (strcmp(command, "start") == 0) {
start_job(name);
} else if (strcmp(command, "stop") == 0) {
stop_job(name);
} else {
fprintf(stderr, "%s: Unknown command \"%s\".\n", kProgramName,
command);
}
}
return 0;
}