Skip to content

Commit e961102

Browse files
committed
gridinit_cmd: Minor fixes and factorization
1 parent 8c0f6c1 commit e961102

File tree

1 file changed

+46
-62
lines changed

1 file changed

+46
-62
lines changed

main/gridinit_cmd.c

Lines changed: 46 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,24 @@ static GOptionEntry entries[] = {
129129
{NULL}
130130
};
131131

132+
static const gchar options[] = "(status{,2,3}|start|stop|reload|repair) [ID...]";
133+
134+
static const gchar description[] =
135+
"\n COMMANDS:\n"
136+
" status* : Displays the status of the given processes or groups\n"
137+
" start : Starts the given processes or groups, even if broken\n"
138+
" kill : Stops the given processes or groups, they won't be automatically\n"
139+
" restarted even after a configuration reload\n"
140+
" stop : Calls 'kill' until the children exit\n"
141+
" restart : Restarts the given processes or groups\n"
142+
" reload : Reloads the configuration, stopping obsolete processes, starting\n"
143+
" the newly discovered. Broken or stopped processes are not restarted\n"
144+
" repair : Removes the broken flag set on a process. Start must be called to\n"
145+
" restart the process.\n"
146+
"with ID the key of a process, or '@GROUP', with GROUP the name of a process\n"
147+
"group\n";
148+
149+
132150
static gint
133151
compare_child_info(gconstpointer p1, gconstpointer p2)
134152
{
@@ -141,27 +159,20 @@ compare_child_info(gconstpointer p1, gconstpointer p2)
141159
static const char *
142160
get_child_status(struct child_info_s *ci, struct keyword_set_s *kw)
143161
{
144-
145-
146-
if (ci->broken) {
162+
if (ci->broken)
147163
return kw->broken;
148-
}
149-
if (!ci->enabled) {
164+
if (!ci->enabled)
150165
return kw->disabled;
151-
}
152-
if (ci->pid <= 0) {
166+
if (ci->pid <= 0)
153167
return kw->down;
154-
}
155-
156168
return kw->up;
157169
}
158170

159171
static size_t
160172
get_longest_group(GList *all_jobs)
161173
{
162174
size_t maxlen = 5;
163-
GList *l;
164-
for (l=all_jobs; l ;l=l->next) {
175+
for (GList *l=all_jobs; l ;l=l->next) {
165176
struct child_info_s *ci = l->data;
166177
size_t len = strlen(ci->group);
167178
if (len > maxlen)
@@ -567,7 +578,7 @@ command_kill(int argc, char **args)
567578
{
568579
struct dump_as_is_arg_s dump_args = {};
569580

570-
int rc = send_commandv(dump_as, &dump_args, "stop", argc, args);
581+
int rc = send_commandv(dump_as, &dump_args, "stop", argc, args);
571582
return !rc
572583
|| dump_args.count_errors != 0
573584
|| dump_args.count_success == 0;
@@ -658,80 +669,53 @@ struct command_s {
658669
static void
659670
usage(void)
660671
{
661-
GOptionContext *context;
662-
gchar description[] =
663-
"\n COMMANDS:\n"
664-
" status* : Displays the status of the given processes or groups\n"
665-
" start : Starts the given processes or groups, even if broken\n"
666-
" kill : Stops the given processes or groups, they won't be automatically\n"
667-
" restarted even after a configuration reload\n"
668-
" stop : Calls 'kill' until the children exit\n"
669-
" restart : Restarts the given processes or groups\n"
670-
" reload : Reloads the configuration, stopping obsolete processes, starting\n"
671-
" the newly discovered. Broken or stopped processes are not restarted\n"
672-
" repair : Removes the broken flag set on a process. Start must be called to\n"
673-
" restart the process.\n"
674-
"with ID the key of a process, or '@GROUP', with GROUP the name of a process\n"
675-
"group\n";
676-
context = g_option_context_new("(status{,2,3}|start|stop|reload|repair) [ID...]\n");
672+
GOptionContext *context = g_option_context_new(options);
677673
g_option_context_add_main_entries(context, entries, NULL);
678674
g_option_context_set_summary(context, description);
679-
gchar *usage = g_option_context_get_help (context, TRUE, NULL);
680-
g_print("%s", usage);
675+
gchar *str_usage = g_option_context_get_help (context, TRUE, NULL);
676+
g_printerr("%s", str_usage);
677+
g_free(str_usage);
681678
}
682679

683-
static int
684-
main_options(int argc, char **args)
680+
static gboolean
681+
main_options(int *argc, char ***args)
685682
{
686-
GError *error = NULL;
687-
GOptionContext *context;
688-
689683
sock_path = g_strdup(GRIDINIT_SOCK_PATH);
690-
context = g_option_context_new("(status{,2,3}|start|stop|reload|repair) [ID...]\n");
691-
g_option_context_add_main_entries(context, entries, NULL);
692-
if (!g_option_context_parse(context, &argc, &args, &error)) {
693-
g_print("option parsing failed: %s\n", error->message);
694-
}
695684

696-
return argc;
685+
GError *error = NULL;
686+
GOptionContext *context = g_option_context_new(options);
687+
g_option_context_add_main_entries(context, entries, NULL);
688+
g_option_context_set_summary(context, description);
689+
return g_option_context_parse(context, argc, args, &error);
697690
}
698691

699692

700693
int
701694
main(int argc, char ** args)
702695
{
703-
struct command_s *cmd;
704-
int opt_index = 1;
705-
706-
if (argc == 1)
707-
usage();
708-
709696
close(0);
710697

711-
argc = main_options(argc, args);
698+
if (!main_options(&argc, &args)) {
699+
usage();
700+
return 1;
701+
}
712702

713703
if (flag_version) {
714704
fprintf(stdout, "gridinit_cmd version: %s\n", API_VERSION);
715-
close(1);
716-
close(2);
717705
return 0;
718706
}
707+
708+
int opt_index = 1;
719709
if (!args[opt_index]) {
720710
usage();
721-
close(1);
722-
close(2);
723-
return 1;
711+
return 2;
724712
}
725-
for (cmd=COMMANDS; cmd->name ;cmd++) {
726-
if (0 == g_ascii_strcasecmp(cmd->name, args[opt_index])) {
727-
int rc = cmd->action(argc-(opt_index+1), args+(opt_index+1));
728-
close(1);
729-
close(2);
730-
return rc;
731-
}
713+
714+
for (struct command_s *cmd=COMMANDS; cmd->name ;cmd++) {
715+
if (0 == g_ascii_strcasecmp(cmd->name, args[opt_index]))
716+
return cmd->action(argc-(opt_index+1), args+(opt_index+1));
732717
}
718+
733719
usage();
734-
close(1);
735-
close(2);
736720
return 1;
737721
}

0 commit comments

Comments
 (0)