@@ -129,6 +129,24 @@ static GOptionEntry entries[] = {
129
129
{NULL }
130
130
};
131
131
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
+
132
150
static gint
133
151
compare_child_info (gconstpointer p1 , gconstpointer p2 )
134
152
{
@@ -141,27 +159,20 @@ compare_child_info(gconstpointer p1, gconstpointer p2)
141
159
static const char *
142
160
get_child_status (struct child_info_s * ci , struct keyword_set_s * kw )
143
161
{
144
-
145
-
146
- if (ci -> broken ) {
162
+ if (ci -> broken )
147
163
return kw -> broken ;
148
- }
149
- if (!ci -> enabled ) {
164
+ if (!ci -> enabled )
150
165
return kw -> disabled ;
151
- }
152
- if (ci -> pid <= 0 ) {
166
+ if (ci -> pid <= 0 )
153
167
return kw -> down ;
154
- }
155
-
156
168
return kw -> up ;
157
169
}
158
170
159
171
static size_t
160
172
get_longest_group (GList * all_jobs )
161
173
{
162
174
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 ) {
165
176
struct child_info_s * ci = l -> data ;
166
177
size_t len = strlen (ci -> group );
167
178
if (len > maxlen )
@@ -567,7 +578,7 @@ command_kill(int argc, char **args)
567
578
{
568
579
struct dump_as_is_arg_s dump_args = {};
569
580
570
- int rc = send_commandv (dump_as , & dump_args , "stop" , argc , args );
581
+ int rc = send_commandv (dump_as , & dump_args , "stop" , argc , args );
571
582
return !rc
572
583
|| dump_args .count_errors != 0
573
584
|| dump_args .count_success == 0 ;
@@ -658,80 +669,53 @@ struct command_s {
658
669
static void
659
670
usage (void )
660
671
{
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 );
677
673
g_option_context_add_main_entries (context , entries , NULL );
678
674
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 );
681
678
}
682
679
683
- static int
684
- main_options (int argc , char * * args )
680
+ static gboolean
681
+ main_options (int * argc , char * * * args )
685
682
{
686
- GError * error = NULL ;
687
- GOptionContext * context ;
688
-
689
683
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
- }
695
684
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 );
697
690
}
698
691
699
692
700
693
int
701
694
main (int argc , char * * args )
702
695
{
703
- struct command_s * cmd ;
704
- int opt_index = 1 ;
705
-
706
- if (argc == 1 )
707
- usage ();
708
-
709
696
close (0 );
710
697
711
- argc = main_options (argc , args );
698
+ if (!main_options (& argc , & args )) {
699
+ usage ();
700
+ return 1 ;
701
+ }
712
702
713
703
if (flag_version ) {
714
704
fprintf (stdout , "gridinit_cmd version: %s\n" , API_VERSION );
715
- close (1 );
716
- close (2 );
717
705
return 0 ;
718
706
}
707
+
708
+ int opt_index = 1 ;
719
709
if (!args [opt_index ]) {
720
710
usage ();
721
- close (1 );
722
- close (2 );
723
- return 1 ;
711
+ return 2 ;
724
712
}
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 ));
732
717
}
718
+
733
719
usage ();
734
- close (1 );
735
- close (2 );
736
720
return 1 ;
737
721
}
0 commit comments