From 74068663408da669701309728e1ab8ce6b0aead2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sun, 8 Nov 2015 00:06:15 +0100 Subject: [PATCH] launch_roster: The beginnings of a launch_daemon control tool. --- build/jam/images/definitions/minimum | 2 +- src/bin/Jamfile | 1 + src/bin/launch_roster.cpp | 182 +++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 src/bin/launch_roster.cpp diff --git a/build/jam/images/definitions/minimum b/build/jam/images/definitions/minimum index 6321369a2e..eba4c4f797 100644 --- a/build/jam/images/definitions/minimum +++ b/build/jam/images/definitions/minimum @@ -13,7 +13,7 @@ SYSTEM_BIN = [ FFilterByBuildFeatures hd hey ideinfo@ide idestatus@ide ifconfig iroster isvolume kernel_debugger keymap keystore - linkcatkeys listarea listattr listimage listdev + launch_roster linkcatkeys listarea listattr listimage listdev listport listres listsem listusb locale logger login lsindex makebootable message mimeset mkfs mkindex modifiers mount mountvolume diff --git a/src/bin/Jamfile b/src/bin/Jamfile index f0fa7edf65..6aae7b68d0 100644 --- a/src/bin/Jamfile +++ b/src/bin/Jamfile @@ -89,6 +89,7 @@ StdBinCommands draggers.cpp ffm.cpp iroster.cpp + launch_roster.cpp listattr.cpp listfont.cpp listres.cpp diff --git a/src/bin/launch_roster.cpp b/src/bin/launch_roster.cpp new file mode 100644 index 0000000000..f66a6f1e6b --- /dev/null +++ b/src/bin/launch_roster.cpp @@ -0,0 +1,182 @@ +/* + * Copyright 2015, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ + + +/*! The launch_daemon's companion command line tool. */ + + +#include +#include + +#include +#include +#include +#include +#include + + +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 \n" + "Where is one of:\n" + " list - Lists all jobs (the default command)\n" + " list-targets - Lists all targets\n" + "The following s have a 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; +}