53
57
#include "com.ubuntu.Upstart.Job.h"
54
58
#include "com.ubuntu.Upstart.Instance.h"
56
#include "../init/events.h"
60
#include "init/events.h"
57
62
#include "initctl.h"
60
65
/* Prototypes for local functions */
61
66
NihDBusProxy *upstart_open (const void *parent)
62
__attribute__ ((warn_unused_result, malloc));
67
__attribute__ ((warn_unused_result));
63
68
char * job_status (const void *parent,
64
69
NihDBusProxy *job_class, NihDBusProxy *job)
65
__attribute__ ((warn_unused_result, malloc));
70
__attribute__ ((warn_unused_result));
66
71
char * job_usage (const void *parent,
67
72
NihDBusProxy *job_class)
68
__attribute__ ((warn_unused_result, malloc));
73
__attribute__ ((warn_unused_result));
70
75
/* Prototypes for static functions */
71
76
static void start_reply_handler (char **job_path, NihDBusMessage *message,
1378
* @command: NihCommand invoked,
1379
* @args: command-line arguments.
1381
* This function is called for the "get-env" command.
1383
* Returns: command exit status.
1386
get_env_action (NihCommand *command, char * const *args)
1388
nih_local NihDBusProxy *upstart = NULL;
1389
nih_local char *envvar = NULL;
1390
nih_local char **job_details = NULL;
1394
nih_assert (command != NULL);
1395
nih_assert (args != NULL);
1400
fprintf (stderr, _("%s: missing variable name\n"), program_name);
1401
nih_main_suggest_help ();
1405
job_details = get_job_details ();
1409
upstart = upstart_open (NULL);
1413
if (upstart_get_env_sync (NULL, upstart, job_details, name, &envvar) < 0)
1416
nih_message ("%s", envvar);
1421
err = nih_error_get ();
1422
nih_error ("%s", err->message);
1430
* @command: NihCommand invoked,
1431
* @args: command-line arguments.
1433
* This function is called for the "set-env" command.
1435
* Returns: command exit status.
1438
set_env_action (NihCommand *command, char * const *args)
1440
nih_local NihDBusProxy *upstart = NULL;
1443
nih_local char **job_details = NULL;
1446
nih_assert (command != NULL);
1447
nih_assert (args != NULL);
1450
fprintf (stderr, _("%s: missing variable value\n"), program_name);
1451
nih_main_suggest_help ();
1455
job_details = get_job_details ();
1461
upstart = upstart_open (NULL);
1465
ret = upstart_set_env_sync (NULL, upstart, job_details,
1466
envvar, ! retain_var);
1473
err = nih_error_get ();
1474
nih_error ("%s", err->message);
1483
* @command: NihCommand invoked,
1484
* @args: command-line arguments.
1486
* This function is called for the "unset-env" command.
1488
* Returns: command exit status.
1491
unset_env_action (NihCommand *command, char * const *args)
1493
nih_local NihDBusProxy *upstart = NULL;
1496
nih_local char **job_details = NULL;
1498
nih_assert (command != NULL);
1499
nih_assert (args != NULL);
1504
fprintf (stderr, _("%s: missing variable name\n"), program_name);
1505
nih_main_suggest_help ();
1509
job_details = get_job_details ();
1513
upstart = upstart_open (NULL);
1517
if (upstart_unset_env_sync (NULL, upstart, job_details, name) < 0)
1522
err = nih_error_get ();
1523
nih_error ("%s", err->message);
1531
* @command: NihCommand invoked,
1532
* @args: command-line arguments.
1534
* This function is called for the "reset-env" command.
1536
* Returns: command exit status.
1539
reset_env_action (NihCommand *command, char * const *args)
1541
nih_local NihDBusProxy *upstart = NULL;
1543
nih_local char **job_details = NULL;
1545
nih_assert (command != NULL);
1546
nih_assert (args != NULL);
1548
job_details = get_job_details ();
1552
upstart = upstart_open (NULL);
1556
if (upstart_reset_env_sync (NULL, upstart, job_details) < 0)
1561
err = nih_error_get ();
1562
nih_error ("%s", err->message);
1569
* list_env_qsort_compar:
1571
* @a: first string to compare,
1572
* @b: second string to compare.
1574
* qsort() function to sort environment variables for list_env_action().
1577
list_env_qsort_compar (const void *a, const void *b)
1579
return strcoll (*(char * const *)a, *(char * const *)b);
1584
* @command: NihCommand invoked,
1585
* @args: command-line arguments.
1587
* This function is called for the "list-env" command.
1589
* Output is in lexicographically sorted order.
1591
* Returns: command exit status.
1594
list_env_action (NihCommand *command, char * const *args)
1596
nih_local NihDBusProxy *upstart = NULL;
1597
nih_local char **env = NULL;
1601
nih_local char **job_details = NULL;
1603
nih_assert (command != NULL);
1604
nih_assert (args != NULL);
1606
job_details = get_job_details ();
1610
upstart = upstart_open (NULL);
1614
if (upstart_list_env_sync (NULL, upstart, job_details, &env) < 0)
1617
/* Determine array size */
1618
for (len = 0; env[len]; len++)
1621
qsort (env, len, sizeof (env[0]), list_env_qsort_compar);
1623
for (e = env; e && *e; e++)
1624
nih_message ("%s", *e);
1629
err = nih_error_get ();
1630
nih_error ("%s", err->message);
1320
1637
* usage_action:
1321
1638
* @command: NihCommand invoked,
1322
1639
* @args: command-line arguments.
2001
* list_sessions_action:
2002
* @command: NihCommand invoked,
2003
* @args: command-line arguments.
2005
* This function is called for the "list-sessions" command.
2007
* Unlike other commands, this does not attempt to connect to Upstart.
2009
* Returns: command exit status.
2012
list_sessions_action (NihCommand *command, char * const *args)
2014
nih_local const char *session_dir = NULL;
2018
nih_assert (command);
2021
session_dir = get_session_dir ();
2023
if (! session_dir) {
2024
nih_error (_("Unable to query session directory"));
2028
dir = opendir (session_dir);
2032
while ((ent = readdir (dir))) {
2033
nih_local char *contents = NULL;
2035
nih_local char *path = NULL;
2037
nih_local char *name = NULL;
2042
int all_digits = TRUE;
2046
if (! strcmp (file, ".") || ! strcmp (file, ".."))
2049
ext = p = strchr (file, '.');
2055
/* Invalid extension */
2056
if (strcmp (ext, ".session"))
2059
NIH_MUST (nih_strncat (&name, NULL, file, (p - file)));
2061
for (p = name; p && *p; p++) {
2062
if (! isdigit (*p)) {
2072
pid = (pid_t) atol (name);
2074
NIH_MUST (nih_strcat_sprintf (&path, NULL, "%s/%s", session_dir, file));
2076
if (kill (pid, 0)) {
2077
nih_info ("%s: %s", _("Ignoring stale session file"), path);
2081
contents = nih_file_read (NULL, path, &len);
2086
if (contents[len-1] == '\n')
2087
contents[len-1] = '\0';
2089
p = strstr (contents, "UPSTART_SESSION" "=");
2093
session = p + strlen ("UPSTART_SESSION") + 1;
2095
if (! session || ! *session)
2098
nih_message ("%d %s", (int)pid, session);
2106
nih_error ("unable to determine sessions");
1683
2113
start_reply_handler (char ** job_path,
1684
2114
NihDBusMessage *message,
2778
* Determine the job and job instance name that the caller should act
2779
* upon. Used by the job environment commands. The caller can determine
2780
* how to react based on the values in the returned array:
2782
* - If user has requested global operation via the command line, return
2783
* an allocated array with zero elements.
2785
* - If user has specified a job name and possible instance value via the
2786
* command line, return an array containing first the job name, then the
2789
* - If no command-line options have been specified, try to extract the
2790
* job and instance names from the environment variables set within a
2793
* Returns: Newly-allocated array containing job name and job
2794
* instance, or an empty array if job and instance cannot be resolved,
2798
get_job_details (void)
2801
const char *upstart_job = NULL;
2802
const char *upstart_instance = NULL;
2804
details = nih_str_array_new (NULL);
2809
if (apply_globally) {
2810
upstart_job = upstart_instance = NULL;
2811
} else if ((upstart_job = getenv ("UPSTART_JOB")) != NULL) {
2812
upstart_instance = getenv ("UPSTART_INSTANCE");
2814
if (! (upstart_job && upstart_instance)) {
2815
fprintf (stderr, _("%s: missing job name\n"), program_name);
2816
nih_main_suggest_help ();
2820
/* Running outside of job implies global */
2821
apply_globally = TRUE;
2822
upstart_job = upstart_instance = NULL;
2826
NIH_MUST (nih_str_array_add (&details, NULL, NULL, upstart_job));
2827
NIH_MUST (nih_str_array_add (&details, NULL, NULL, upstart_instance));
2987
* Command-line options accepted for the set-env command.
2989
NihOption set_env_options[] = {
2990
{ 'g', "global", N_("apply to global job environment table"),
2991
NULL, NULL, &apply_globally, NULL },
2992
{ 'r', "retain", N_("do not replace the value of the variable if already set"),
2993
NULL, NULL, &retain_var, NULL },
3000
* Command-line options accepted for the get-env command.
3002
NihOption get_env_options[] = {
3003
{ 'g', "global", N_("apply to global job environment table"),
3004
NULL, NULL, &apply_globally, NULL },
3009
* unset_env_options:
3011
* Command-line options accepted for the unset-env command.
3013
NihOption unset_env_options[] = {
3014
{ 'g', "global", N_("apply to global job environment table"),
3015
NULL, NULL, &apply_globally, NULL },
3022
* Command-line options accepted for the list-env command.
3024
NihOption list_env_options[] = {
3025
{ 'g', "global", N_("apply to global job environment table"),
3026
NULL, NULL, &apply_globally, NULL },
3031
* reset_env_options:
3033
* Command-line options accepted for the reset-env command.
3035
NihOption reset_env_options[] = {
3036
{ 'g', "global", N_("apply to global job environment table"),
3037
NULL, NULL, &apply_globally, NULL },
2495
3042
* usage_options:
2497
3044
* Command-line options accepted for the usage command.
2617
3171
{ "check-config", N_("[CONF]"),
2618
3172
N_("Check for unreachable jobs/event conditions."),
2619
3173
N_("List all jobs and events which cannot be satisfied by "
2620
"currently available job configuration files"),
3174
"currently available job configuration files."),
2621
3175
NULL, check_config_options, check_config_action },
3177
{ "get-env", N_("VARIABLE"),
3178
N_("Retrieve value of a job environment variable."),
3179
N_("Display the value of a variable from the job environment table."),
3180
&env_group, get_env_options, get_env_action },
3183
N_("Show all job environment variables."),
3184
N_("Displays sorted list of variables and their values from the job environment table."),
3185
&env_group, list_env_options, list_env_action },
3187
{ "reset-env", N_("VARIABLE"),
3188
N_("Revert all job environment variable changes."),
3189
N_("Discards all changes make to the job environment table, setting it back to its default value."),
3190
&env_group, reset_env_options, reset_env_action },
3192
{ "set-env", N_("VARIABLE[=VALUE]"),
3193
N_("Set a job environment variable."),
3194
N_("Adds or updates a variable in the job environment table."),
3195
&env_group, set_env_options, set_env_action },
3197
{ "unset-env", N_("VARIABLE"),
3198
N_("Remove a job environment variable."),
3199
N_("Discards the specified variable from the job environment table."),
3200
&env_group, unset_env_options, unset_env_action },
2623
3202
{ "usage", N_("JOB"),
2624
3203
N_("Show job usage message if available."),
2625
3204
N_("JOB is the name of the job which usage is to be shown.\n" ),