[Initng-svn] r2690 - initng/plugins/ngc2
svn at initng.thinktux.net
svn at initng.thinktux.net
Tue Jan 3 15:01:00 CET 2006
Author: jimmy
Date: Tue Jan 3 15:00:59 2006
New Revision: 2690
Modified:
initng/plugins/ngc2/ngc2.c
Log:
Added ngc --quite option.
Modified: initng/plugins/ngc2/ngc2.c
==============================================================================
--- initng/plugins/ngc2/ngc2.c (original)
+++ initng/plugins/ngc2/ngc2.c Tue Jan 3 15:00:59 2006
@@ -54,6 +54,9 @@
int header_printed = FALSE;
int try_count = 0;
+int quite = FALSE;
+
+#define print_out(...); { if(quite==FALSE) printf(__VA_ARGS__ ); }
/* Global comunication socket */
static int sock = -1;
@@ -105,7 +108,7 @@
sock = socket(PF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
{
- printf("Failed to init socket\n");
+ print_out("Failed to init socket\n");
return (FALSE);
}
@@ -121,8 +124,8 @@
if (connect(sock, (struct sockaddr *) &sockname, len) < 0)
{
close(sock);
- printf("Error connecting to initng socket\n");
- printf("This probably means that you're not running initng.\n");
+ print_out("Error connecting to initng socket\n");
+ print_out("This probably means that you're not running initng.\n");
exit(1);
return (FALSE);
}
@@ -167,17 +170,17 @@
header.o[0] = '\0';
- /*printf("Sending: %c, %s, %s\n", c, l ,o); */
+ /*print_out("Sending: %c, %s, %s\n", c, l ,o); */
if ((sock < 0) && (open_socket() == FALSE))
{
- printf("Socket is not open, or failed to open!\n");
+ print_out("Socket is not open, or failed to open!\n");
exit(1);
}
if (write(sock, &header, sizeof(read_header)) <
(signed) sizeof(read_header))
{
- printf("Could not send command!\n");
+ print_out("Could not send command!\n");
exit(1);
}
@@ -187,7 +190,7 @@
/* if we are reloading initng, we wont have an answ from it, so don't fetch any */
if (header.c == 'c')
{
- printf
+ print_out
("Can't give you feedback if reloading of initng succeeds or not.\n");
exit(0);
}
@@ -201,34 +204,34 @@
if (TEMP_FAILURE_RETRY(read(sock, result, sizeof(result_desc))) !=
sizeof(*result))
{
- printf("failed to fetch the result\n");
+ print_out("failed to fetch the result\n");
exit(1);
}
/* check that protocol matches */
if (result->p_ver != PROTOCOL_VERSION)
{
- printf("PROTOCOL_VERSION missmatch :\n");
- printf
+ print_out("PROTOCOL_VERSION missmatch :\n");
+ print_out
(" initng_protocol_version: %i, client_protocol_version: %i !\n",
result->p_ver, PROTOCOL_VERSION);
- printf(" initng_version: %s, ngc version: %s\n", result->version,
+ print_out(" initng_version: %s, ngc version: %s\n", result->version,
VERSION);
exit(1);
}
if (header_printed == FALSE)
{
- printf("Got an response from initng, version: %s\n", result->version);
+ print_out("Got an response from initng, version: %s\n", result->version);
if (strcmp(result->version, VERSION) != 0)
{
- printf("Version of initng and version of ngc differs!\n");
- printf
+ print_out("Version of initng and version of ngc differs!\n");
+ print_out
("this might be a bad thing, that can in worst case cause problems!\n");
}
- /*printf("got an read: command %c, status %i, type %i\n", result->c,
+ /*print_out("got an read: command %c, status %i, type %i\n", result->c,
result->s, result->t); */
header_printed = TRUE;
}
@@ -236,30 +239,30 @@
switch (result->s)
{
case S_FALSE:
- printf("\nThe result of -%c is false = %i!\n\n", result->c,
+ print_out("\nThe result of -%c is false = %i!\n\n", result->c,
result->s);
free(result);
return (NULL);
case S_REQUIRES_OPT:
- printf("\nThe command -%c requires an option.\n\n", result->c);
+ print_out("\nThe command -%c requires an option.\n\n", result->c);
free(result);
return (NULL);
case S_NOT_REQUIRES_OPT:
- printf("\nThe command -%c must have no option.\n\n", result->c);
+ print_out("\nThe command -%c must have no option.\n\n", result->c);
free(result);
return (NULL);
case S_INVALID_TYPE:
- printf
+ print_out
("\nThe data returning of this command ( -%c ) is an unknown type.\n\n",
result->c);
free(result);
return (NULL);
case S_COMMAND_NOT_FOUND:
- printf("\nCommand -%c is not found.\n\n", result->c);
+ print_out("\nCommand -%c is not found.\n\n", result->c);
free(result);
return (NULL);
@@ -279,7 +282,7 @@
TEMP_FAILURE_RETRY(read(sock, &tmp_read, MAX_RESCIVE_SIZE))) > 0)
{
tmp_read[len] = '\0';
- printf("%s", tmp_read);
+ print_out("%s", tmp_read);
}
/* TODO: error handling??? */
@@ -303,20 +306,20 @@
result_desc *result = NULL;
int counter = 0;
- printf("Starting service: \n");
+ print_out("Starting service: \n");
while (counter < 30)
{
if (TEMP_FAILURE_RETRY(read(sock, &row, sizeof(active_row))) !=
sizeof(row))
{
- printf("\tFailed to fetch result\n");
+ print_out("\tFailed to fetch result\n");
close_socket();
return FALSE;
}
close_socket();
- /*printf("row.s = %s, row.n = %s, row.t = %i, row.i = %i\n", row.s, row.n, (int) row.t, row.i); */
+ /*print_out("row.s = %s, row.n = %s, row.t = %i, row.i = %i\n", row.s, row.n, (int) row.t, row.i); */
if (row.i == IS_UP || row.i == IS_WAITING || row.i == IS_FAILED)
break;
@@ -327,13 +330,13 @@
sleep(1);
/* And see if status has changed */
- /*printf("Fetch result on %s\n", row.n); */
+ /*print_out("Fetch result on %s\n", row.n); */
result = send_command('s', NULL, row.n);
/* make sure we got one */
if (!result || result->c != 's')
{
- printf("\tInvalid result!\n");
+ print_out("\tInvalid result!\n");
close_socket();
return (FALSE);
}
@@ -344,22 +347,22 @@
if (row.i == IS_UP || row.i == IS_WAITING)
{
- printf("\tService %s started sucessfully! (%s)\n", row.n, row.s);
+ print_out("\tService %s started sucessfully! (%s)\n", row.n, row.s);
return (TRUE);
}
if (counter == 10)
{
- printf("Timeout on start service, might be started soon anyway.\n");
+ print_out("Timeout on start service, might be started soon anyway.\n");
return (FALSE);
}
if (row.i == IS_FAILED && try_count <= 2)
{
try_count++;
- printf("\n\n");
- printf("Warning, service %s is in status: \"%s\"\n\n", row.n, row.s);
- printf
+ print_out("\n\n");
+ print_out("Warning, service %s is in status: \"%s\"\n\n", row.n, row.s);
+ print_out
("I will now try to reset the service, wait 1 second, and restart %s.\n",
row.n);
@@ -368,14 +371,14 @@
return (FALSE);
sleep(1);
- printf("\n\nTrying to start %s again ...\n\n", row.n);
+ print_out("\n\nTrying to start %s again ...\n\n", row.n);
/* try start again */
if (send_and_handle('u', NULL, row.n))
return (TRUE);
}
- printf("\tFailed to start service, %s (%s)\n", row.n, row.s);
+ print_out("\tFailed to start service, %s (%s)\n", row.n, row.s);
return (FALSE);
}
@@ -387,12 +390,12 @@
int counter = 0;
int once_found = 0;
- printf("Stopping service: \n");
+ print_out("Stopping service: \n");
if (TEMP_FAILURE_RETRY(read(sock, &row, sizeof(active_row))) !=
sizeof(row))
{
- printf("failed to read response\n");
+ print_out("failed to read response\n");
close_socket();
return FALSE;
}
@@ -402,7 +405,7 @@
while (counter < 30)
{
- /*printf("row.s = %s, row.n = %s, row.t = %i, row.i = %i\n", row.s, row.n, (int) row.t, row.i); */
+ /*print_out("row.s = %s, row.n = %s, row.t = %i, row.i = %i\n", row.s, row.n, (int) row.t, row.i); */
if (row.i == IS_DOWN || row.i == IS_FAILED)
break;
@@ -414,14 +417,14 @@
sleep(1);
/* And see if status has changed */
- /*printf("Fetch result on %s\n", row.n); */
+ /*print_out("Fetch result on %s\n", row.n); */
result = send_command('s', NULL, row.n);
/* make sure we got one */
if (!result || result->c != 's')
{
- printf("\tInvalid result!\n");
+ print_out("\tInvalid result!\n");
close_socket();
return (FALSE);
}
@@ -429,7 +432,7 @@
if (TEMP_FAILURE_RETRY(read(sock, &row, sizeof(active_row))) !=
sizeof(row))
{
- printf("failed to read response\n");
+ print_out("failed to read response\n");
close_socket();
return FALSE;
}
@@ -449,13 +452,13 @@
/* not found can be goood */
if (once_found == 1 && strcmp(row.s, "NOT_FOUND") == 0)
{
- printf("\tService stopped sucessfully!\n");
+ print_out("\tService stopped sucessfully!\n");
return (TRUE);
}
if (row.i == IS_FAILED)
{
- printf
+ print_out
("Service %s has status FAILED\n\n Clearing status in 1 second.",
row.n);
sleep(1);
@@ -464,9 +467,9 @@
if (!send_and_handle('z', NULL, row.n))
return (FALSE);
- printf
+ print_out
("Done, WARNING, clearing service meens that its removed from initng,\n");
- printf
+ print_out
("This dont actually meens that the service is really stopped.\n");
return (TRUE);
}
@@ -474,17 +477,17 @@
/* Down is to */
if (row.i == IS_DOWN)
{
- printf("\tService stopped sucessfully!\n");
+ print_out("\tService stopped sucessfully!\n");
return (TRUE);
}
if (counter == 10)
{
- printf("Timeout on stop service, might be stopped soon anyway.\n");
+ print_out("Timeout on stop service, might be stopped soon anyway.\n");
return (FALSE);
}
- printf("\tFailed to stop service, %s (%s)\n", row.n, row.s);
+ print_out("\tFailed to stop service, %s (%s)\n", row.n, row.s);
return (FALSE);
}
@@ -512,7 +515,7 @@
switch (result->t)
{
case DATA_COMMAND:
- printf("Uncatched data command!\n");
+ print_out("Uncatched data command!\n");
bad_syntax();
exit(1);
@@ -521,31 +524,35 @@
int r = command_fetch_int();
if (r == TRUE)
- printf(" command -%c returned SUCCESS! \n", result->c);
+ {
+ print_out(" command -%c returned SUCCESS! \n", result->c);
+ }
else
- printf(" command -%c returned FAILURE! \n", result->c);
+ {
+ print_out(" command -%c returned FAILURE! \n", result->c);
+ }
return (r);
}
case INT_COMMAND:
- printf("INT_COMMAND_RESULT: %i\n", command_fetch_int());
+ print_out("INT_COMMAND_RESULT: %i\n", command_fetch_int());
return (TRUE);
case PRINT_COMMAND:
case STRING_COMMAND:
command_fetch_string();
return (TRUE);
case VOID_COMMAND:
- printf("VOID_COMMAND\n");
+ print_out("VOID_COMMAND\n");
return (TRUE);
case COMMAND_FAIL:
default:
close_socket();
- printf("unkown data!, no haldler!\n");
+ print_out("unkown data!, no haldler!\n");
bad_syntax();
exit(1);
}
- printf("Error, should not got here!\n");
+ print_out("Error, should not got here!\n");
exit(22);
}
@@ -555,12 +562,12 @@
if (!(result = send_command(c, l, o)))
{
- printf("Command -%c --%s, %s - Unable to send command!\n", c, l, o);
+ print_out("Command -%c --%s, %s - Unable to send command!\n", c, l, o);
return (FALSE);
}
if (!handle_result(result))
{
- printf("Command -%c --%s, %s - Bad result!\n", c, l, o);
+ print_out("Command -%c --%s, %s - Bad result!\n", c, l, o);
return (FALSE);
}
@@ -575,26 +582,31 @@
{
int cc = 1;
+ assert(argv[0]);
+
header_printed = FALSE;
Argv = argv[0];
+ if(argv[1] && strcmp(argv[1], "--quite")==0)
+ quite=TRUE;
+
/* print banner */
- printf("Next Generation init Control. version: %s\n", VERSION);
- printf("written by Jimmy Wennlund <jimmy.wennlund at gmail.com>\n\n");
+ print_out("Next Generation init Control. version: %s\n", VERSION);
+ print_out("written by Jimmy Wennlund <jimmy.wennlund at gmail.com>\n\n");
/* check if cmd-line contains "ngdc", if set debug */
if (strstr(argv[0], "ngdc"))
{
- printf("Warning. This is ngdc!\n");
+ print_out("Warning. This is ngdc!\n");
debug = TRUE;
}
if (debug == FALSE && getuid() != 0)
{
- printf("You need root access to communicate with initng.\n");
+ print_out("You need root access to communicate with initng.\n");
exit(2);
}
@@ -620,7 +632,7 @@
bad_syntax();
exit(1);
}
-
+
/* if next option contains data, but not starting with an '-', its considered an option */
if (argv[cc + 1] && argv[cc + 1][0] != '-')
opt = argv[cc + 1];
@@ -629,8 +641,8 @@
/* if it is an --option */
if (argv[cc][1] == '-')
{
- if (!send_and_handle('\0', &argv[cc][2], opt))
- exit(1);
+ if (!send_and_handle('\0', &argv[cc][2], opt))
+ exit(1);
}
else
{
@@ -675,9 +687,9 @@
help_row row;
/* print head */
- printf(" ngc understand this commands:\n");
- printf(" option : description\n");
- printf(" ----------------------------------------------------------\n");
+ print_out(" ngc understand this commands:\n");
+ print_out(" option : description\n");
+ print_out(" ----------------------------------------------------------\n");
while (ngc2_recvall(sock, &row, sizeof(help_row)))
@@ -707,9 +719,13 @@
lname[31] = '\0';
if (row.c != '\0')
- printf(" [-%c] --%s: %s\n", row.c, lname, row.d);
+ {
+ print_out(" [-%c] --%s: %s\n", row.c, lname, row.d);
+ }
else
- printf(" --%s: %s\n", lname, row.d);
+ {
+ print_out(" --%s: %s\n", lname, row.d);
+ }
}
return (TRUE);
}
@@ -723,16 +739,16 @@
gettimeofday(&now, NULL);
/* print head */
- printf
+ print_out
(" hh:mm:ss service : status since\n");
- printf
+ print_out
(" ----------------------------------------------------------------------------------\n");
while (ngc2_recvall(sock, &row, sizeof(active_row)))
{
struct tm *ts = localtime(&row.t.tv_sec);
- printf(" %.2i:%.2i:%.2i %-38s : %-22s %8is.\n", ts->tm_hour,
+ print_out(" %.2i:%.2i:%.2i %-38s : %-22s %8is.\n", ts->tm_hour,
ts->tm_min, ts->tm_sec, row.n, row.s, MS_DIFF(now,
row.t) / 1000);
@@ -748,9 +764,9 @@
char ct[20];
/* print head */
- printf(" %-10s %-24s = %-8s %s\n", "WhereToUSE", "Name", "ValueToUSE",
+ print_out(" %-10s %-24s = %-8s %s\n", "WhereToUSE", "Name", "ValueToUSE",
"Description");
- printf
+ print_out
(" ----------------------------------------------------------------\n");
while (ngc2_recvall(sock, &row, sizeof(option_row)))
@@ -777,7 +793,7 @@
break;
}
- printf(" %-10s %-24s = %-8s %s\n", row.o, row.n, ct, row.d);
+ print_out(" %-10s %-24s = %-8s %s\n", row.o, row.n, ct, row.d);
}
close_socket();
@@ -788,6 +804,6 @@
/* Avoid using goto */
void bad_syntax(void)
{
- printf("Bad syntax!\n\n");
+ print_out("Bad syntax!\n\n");
print_usage();
}
More information about the Initng-svn
mailing list