mirror of
https://github.com/troglobit/finit.git
synced 2026-10-31 13:00:32 +00:00
helpers.c:run() - Fix resource and memory leaks.
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
22ed4e7f73
commit
2fdd64a2d8
1 file changed
+5
-6
@@ -404,7 +404,6 @@ int complete(char *cmd, int pid)
|
|||||||
int run(char *cmd)
|
int run(char *cmd)
|
||||||
{
|
{
|
||||||
int status, result, i = 0;
|
int status, result, i = 0;
|
||||||
FILE *fp;
|
|
||||||
char *args[NUM_ARGS + 1], *arg, *backup;
|
char *args[NUM_ARGS + 1], *arg, *backup;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
@@ -444,17 +443,18 @@ int run(char *cmd)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fp = fopen("/dev/null", "w");
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (0 == pid) {
|
if (0 == pid) {
|
||||||
int i;
|
int i;
|
||||||
|
FILE *fp;
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
|
|
||||||
/* Reset signal handlers that were set by the parent process */
|
/* Reset signal handlers that were set by the parent process */
|
||||||
for (i = 1; i < NSIG; i++)
|
for (i = 1; i < NSIG; i++)
|
||||||
DFLSIG(sa, i, 0);
|
DFLSIG(sa, i, 0);
|
||||||
|
|
||||||
/* Redirect stdio if the caller requested so. */
|
/* Always redirect stdio for run() */
|
||||||
|
fp = fopen("/dev/null", "w");
|
||||||
if (fp) {
|
if (fp) {
|
||||||
int fd = fileno(fp);
|
int fd = fileno(fp);
|
||||||
|
|
||||||
@@ -467,14 +467,14 @@ int run(char *cmd)
|
|||||||
_exit(1); /* Only if execv() fails. */
|
_exit(1); /* Only if execv() fails. */
|
||||||
} else if (-1 == pid) {
|
} else if (-1 == pid) {
|
||||||
_pe("%s", args[0]);
|
_pe("%s", args[0]);
|
||||||
|
free(backup);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
status = complete(args[0], pid);
|
status = complete(args[0], pid);
|
||||||
if (-1 == status) {
|
if (-1 == status) {
|
||||||
if (fp) fclose(fp);
|
|
||||||
free(backup);
|
free(backup);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,7 +489,6 @@ int run(char *cmd)
|
|||||||
* change their return code accordingly. --Jocke */
|
* change their return code accordingly. --Jocke */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fp) fclose(fp);
|
|
||||||
free(backup);
|
free(backup);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in new issue
Block a user