Browse Source

Don't use /bin/sh -c for ACTIONs

Serj Kalichev 10 years ago
parent
commit
bae5fe10e5
1 changed files with 31 additions and 47 deletions
  1. 31 47
      clish/callback_script.c

+ 31 - 47
clish/callback_script.c

@@ -30,7 +30,6 @@ int clish_script_callback(clish_context_t *context,
 	const char *fifo_name;
 	FILE *rpipe, *wpipe;
 	char *command = NULL;
-	bool_t is_sh = BOOL_FALSE;
 
 	/* Signal vars */
 	struct sigaction sig_old_int;
@@ -48,52 +47,43 @@ int clish_script_callback(clish_context_t *context,
 	if (!shebang)
 		shebang = clish_shell__get_default_shebang(this);
 	assert(shebang);
-	if (0 == lub_string_nocasecmp(shebang, "/bin/sh"))
-		is_sh = BOOL_TRUE;
 
 #ifdef DEBUG
 	fprintf(stderr, "SHEBANG: #!%s\n", shebang);
 	fprintf(stderr, "SCRIPT: %s\n", script);
 #endif /* DEBUG */
 
-	/* If /bin/sh we don't need FIFO */
-	if (!is_sh) {
-		/* Get FIFO */
-		fifo_name = clish_shell__get_fifo(this);
-		if (!fifo_name) {
-			fprintf(stderr, "System error. Can't create temporary FIFO.\n"
-				"The ACTION will be not executed.\n");
-			return BOOL_FALSE;
-		}
+	/* Get FIFO */
+	fifo_name = clish_shell__get_fifo(this);
+	if (!fifo_name) {
+		fprintf(stderr, "System error. Can't create temporary FIFO.\n"
+			"The ACTION will be not executed.\n");
+		return BOOL_FALSE;
+	}
 
-		/* Create process to write to FIFO */
-		cpid = fork();
-		if (cpid == -1) {
-			fprintf(stderr, "System error. Can't fork the write process.\n"
-				"The ACTION will be not executed.\n");
-			return BOOL_FALSE;
-		}
+	/* Create process to write to FIFO */
+	cpid = fork();
+	if (cpid == -1) {
+		fprintf(stderr, "System error. Can't fork the write process.\n"
+			"The ACTION will be not executed.\n");
+		return BOOL_FALSE;
+	}
 
-		/* Child: write to FIFO */
-		if (cpid == 0) {
-			wpipe = fopen(fifo_name, "w");
-			if (!wpipe)
-				_exit(-1);
-			fwrite(script, strlen(script) + 1, 1, wpipe);
-			fclose(wpipe);
-			_exit(0);
-		}
+	/* Child: write to FIFO */
+	if (cpid == 0) {
+		wpipe = fopen(fifo_name, "w");
+		if (!wpipe)
+			_exit(-1);
+		fwrite(script, strlen(script) + 1, 1, wpipe);
+		fclose(wpipe);
+		_exit(0);
 	}
 
 	/* Parent */
 	/* Prepare command */
-	if (!is_sh) {
-		lub_string_cat(&command, shebang);
-		lub_string_cat(&command, " ");
-		lub_string_cat(&command, fifo_name);
-	} else {
-		lub_string_cat(&command, script);
-	}
+	lub_string_cat(&command, shebang);
+	lub_string_cat(&command, " ");
+	lub_string_cat(&command, fifo_name);
 
 	/* If the stdout of script is needed */
 	if (out) {
@@ -113,10 +103,8 @@ int clish_script_callback(clish_context_t *context,
 			fprintf(stderr, "System error. Can't fork the script.\n"
 				"The ACTION will be not executed.\n");
 			lub_string_free(command);
-			if (!is_sh) {
-				kill(cpid, SIGTERM);
-				waitpid(cpid, NULL, 0);
-			}
+			kill(cpid, SIGTERM);
+			waitpid(cpid, NULL, 0);
 
 			/* Restore SIGINT and SIGQUIT */
 			sigaction(SIGINT, &sig_old_int, NULL);
@@ -130,10 +118,8 @@ int clish_script_callback(clish_context_t *context,
 		*out = konf_buf__dup_line(buf);
 		konf_buf_delete(buf);
 		/* Wait for the writing process */
-		if (!is_sh) {
-			kill(cpid, SIGTERM);
-			waitpid(cpid, NULL, 0);
-		}
+		kill(cpid, SIGTERM);
+		waitpid(cpid, NULL, 0);
 		/* Wait for script */
 		res = pclose(rpipe);
 
@@ -143,10 +129,8 @@ int clish_script_callback(clish_context_t *context,
 	} else {
 		res = system(command);
 		/* Wait for the writing process */
-		if (!is_sh) {
-			kill(cpid, SIGTERM);
-			waitpid(cpid, NULL, 0);
-		}
+		kill(cpid, SIGTERM);
+		waitpid(cpid, NULL, 0);
 	}
 	lub_string_free(command);