[Initng-svn] r4363 - initng/trunk/plugins/bash_parser

svn at initng.thinktux.net svn at initng.thinktux.net
Mon Jun 5 12:12:42 CEST 2006


Author: jimmy
Date: Mon Jun  5 12:12:40 2006
New Revision: 4363

Modified:
   initng/trunk/plugins/bash_parser/runiscript.c

Log:
Big cleanup of runiscript.c


Modified: initng/trunk/plugins/bash_parser/runiscript.c
==============================================================================
--- initng/trunk/plugins/bash_parser/runiscript.c	(original)
+++ initng/trunk/plugins/bash_parser/runiscript.c	Mon Jun  5 12:12:40 2006
@@ -17,111 +17,126 @@
  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include <sys/types.h>
+#include <sys/stat.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <stdio.h>
 
+
+#define DEFAULT_PATH "/etc/init/"
+
 extern char **environ;
 
-int main(int argc, char *argv[])
-{
-	char path[1025];
-	char *servname;
-	char *new_argv[24];
+/* These commands will be forwarded to /sbin/ngc if issued */
+const char *ngc_args[] = { "start", "stop", "restart", "zap", "status", NULL };
 
-#ifdef DEBUG_EXTRA
-	{										/* for debug */
+/* this lists the commands the service can be executed with directly */
+static void print_usage(void)
+{
 		int i;
+		printf("Usage: /etc/init/service");
+		for(i=0; ngc_args[i]; i++)
+			printf(" <%s>", ngc_args[i]);
+		printf("\n");
+}
 
-		printf("%i args: \n", argc);
-		for (i = 0; argv[i]; i++)
-			printf("argv[%i]: %s\n", i, argv[i]);
-	}
-#endif
+/* here is main */
+int main(int argc, char *argv[])
+{
+	char path[1025];  /* the argv[0] is not always the full path, so we make a full path and put in here */
+	char script[1024]; /* plenty of space for the bash script header we crate to execute. */
+	char *new_argv[24]; /* used for execve, 24 arguments is really enoght */
+	char *servname; /* local storage of the service name, cut from / pointing in path abow */
+	struct stat st; /* file stat storage, used to check that files exist */
 
 	/* check to no of arguments */
 	if (argc != 3)
 	{
-		printf("Usage script <start> <stop> <setup>\n");
+		print_usage();
 		exit(1);
 	}
 
-	/* replace startin '.' with full path */
+	/* replace starting '.' with full path to local cwd */
 	if (argv[1][0] == '.')
 	{
 		if (!getcwd(path, 1024))
 		{
-			printf("error exeuting /lib/ibin/runiscript.sh\n");
-			exit(3);
+			printf("Cud not get path to pwd.\n");
+			print_usage();
+			exit(1);
 		}
 		strncat(path, &argv[1][1], 1024 - strlen(path));
 	}
-	else
+	/* replace starting '~' with path to HOME */
+	else if (argv[1][0] == '~')
+	{
+		strncpy(path, getenv("HOME"), 1024);
+		strncpy(path, &argv[1][1], 1024 - strlen(path));
+	}
+	/* if it is a full path, this is really good */
+	else if (argv[1][0] == '/')
 	{
 		strncpy(path, argv[1], 1024);
 	}
+	/* else, guess the full path */
+	else
+	{
+		strcpy(path, DEFAULT_PATH);
+		strncat(path, argv[1], 1024 - strlen(path));
+	}
 
-	/* check that full path is provided */
-	if (path[0] != '/')
+	/* check that path is correct */
+	if(stat(path, &st)!=0 || !S_ISREG(st.st_mode))
 	{
-		printf("Please, always call script with full path.\n");
+		printf("Full path not provided, Guessed path to \"%s\" but no file existed in that place.\n", path);
+		print_usage();
 		exit(2);
 	}
+	
 
 	/* cut service name from the last '/' found in service path */
 	servname = strrchr(path, '/') + 1;
-	/*printf("servname: %s\n", servname); */
-
-	/* check if these are direct commands, then use ngc */
-	if (strcmp(argv[2], "start") == 0)
+	
+	/* check if command shud forward to a ngc command */
 	{
-		new_argv[0] = strdup("/sbin/ngc");
-		new_argv[1] = strdup("--start");
-		new_argv[2] = strdup(servname);
-		new_argv[3] = NULL;
-		execve(new_argv[0], new_argv, environ);
-		exit(30);
-	}
-	if (strcmp(argv[2], "stop") == 0)
-	{
-		new_argv[0] = strdup("/sbin/ngc");
-		new_argv[1] = strdup("--stop");
-		new_argv[2] = strdup(servname);
-		new_argv[3] = NULL;
-		execve(new_argv[0], new_argv, environ);
-		exit(31);
-	}
-	if (strcmp(argv[2], "zap") == 0)
-	{
-		new_argv[0] = strdup("/sbin/ngc");
-		new_argv[1] = strdup("--zap");
-		new_argv[2] = strdup(servname);
-		new_argv[3] = NULL;
-		execve(new_argv[0], new_argv, environ);
-		exit(32);
-	}
-	if (strcmp(argv[2], "status") == 0)
-	{
-		new_argv[0] = strdup("/sbin/ngc");
-		new_argv[1] = strdup("--status");
-		new_argv[2] = strdup(servname);
-		new_argv[3] = NULL;
-		execve(new_argv[0], new_argv, environ);
-		exit(33);
+		int i;
+		for(i=0; ngc_args[i]; i++)
+		{
+
+			/* check if these are direct commands, then use ngc */
+			if (strcmp(argv[2], ngc_args[i]) == 0)
+			{
+				/* set up an arg like "/sbin/ngc --start service" */
+				new_argv[0] = strdup("/sbin/ngc");
+				/* put new_argv = "--start" */
+				new_argv[1] = calloc(strlen(ngc_args[i] + 4), sizeof(char));
+				new_argv[1][0]='-';
+				new_argv[1][1]='-';
+				strcat(new_argv[1], ngc_args[i]);
+				/* put service name */
+				new_argv[2] = strdup(servname);
+				new_argv[3] = NULL;
+				
+				/* execute this call */
+				execve(new_argv[0], new_argv, environ);
+				printf("/sbin/ngc is missing or invalid.\n");
+				exit(30);
+			}
+		}
 	}
 	/* end check */
 
 	/* check if command is valid */
 	if (strncmp(argv[2], "internal_", 9) != 0)
 	{
-		printf("Bad command\n");
+		printf("Bad command.\n");
+		print_usage();
 		exit(3);
 	}
 
 	/* set up the bash script to run */
-	char script[1024];			/* plenty of space */
-
 	strcpy(script, "#/bin/bash\n");
 	strcat(script, &argv[2][9]);
 	strcat(script, "() {\necho \"ERROR: ");
@@ -135,9 +150,6 @@
 	strcat(script, &argv[2][9]);
 	strcat(script, "\nexit $?\n");
 
-
-	/* printf("strlen script: %i : \n\"%s\"\n", strlen(script), script); */
-
 	/* set up new argv */
 	new_argv[0] = strdup("/bin/bash");
 	new_argv[1] = strdup("-c");
@@ -150,8 +162,6 @@
 	setenv("COMMAND", strdup(argv[2]), 1);
 	setenv("THE_RIGHT_WAY", "TRUE", 1);
 
-
-	/*printf("\nexecuting...\n\n"); */
 	/* now call the bash script */
 	execve(new_argv[0], new_argv, environ);
 


More information about the Initng-svn mailing list