#include <sys/wait.h>
#include <sys/types.h>
#include <sys/unistd.h>
+#include <sys/param.h>
#include <time.h>
static pid_t child = -1;
return WOApplicationMain(appName, argc, argv);
}
+ /* ensure we can actually do execve() */
+ if (argv[0][0] != '/') {
+ /*
+ Note: we cannot use getcwd to make the path absolute, the
+ binary could have been found anywhere in $PATH
+ */
+ fprintf(stderr,
+ "program called with a non-absolute path, can't use execve(): "
+ "'%s'\n"
+ " (start using `which %s`)\n",
+ argv[0], argv[0]);
+ exit(123); // TODO: better error code
+ }
+
/* watch dog */
{
int failCount = 0;
Set the path (0) argument and set -WOUseWatchDog to NO so this
process won't execute a parent
*/
- childArgv = malloc(argc + 3);
- childArgv[0] = malloc(strlen(argv[0]) + 1);
- strcpy(childArgv[0], argv[0]);
- childArgv[1] = (char *)malloc(strlen("-WOUseWatchDog") + 1);
- strcpy(childArgv[1], "-WOUseWatchDog");
- childArgv[2] = (char *)malloc(strlen("NO") + 1);
- strcpy(childArgv[2], "NO");
-
+ childArgv = calloc(argc + 5, sizeof(char *));
+ childArgv[0] = strdup(argv[0]);
+ childArgv[1] = strdup("-WOUseWatchDog");
+ childArgv[2] = strdup("NO");
+
/*
Copy any remaining arguments skipping a -WOUseWatchDog if it exists
*/
}
}
- childArgv[childIndex] = (char *)malloc(strlen(argv[index]) + 1);
- strcpy(childArgv[childIndex], argv[index]);
+ childArgv[childIndex] = strdup(argv[index]);
childIndex++;
}
/* Mark the end of the array of arguments */
childArgv[childIndex] = NULL;
-
+
/* transform the child into a new process */
- execve(argv[0], childArgv, environ);
-
- /* shouldn't even get here ! */
- fprintf(stderr,
- "exeve failed in child %i failed: %s\n",
- getpid(), strerror(errno));
+ {
+ const unsigned char *p;
+
+ p = argv[0];
+ if (p[0] != '/') {
+ /*
+ Note: we cannot use getcwd to make the path absolute, the
+ binary could have been found anywhere in $PATH
+ */
+ fprintf(stderr,
+ "program called with a non-absolute path: '%s'\n", p);
+ exit(123); // TODO: better error code
+ }
+
+ execve(p, childArgv, environ);
+
+ /* shouldn't even get here ! */
+ fprintf(stderr,
+ "execve of '%s' failed in child %i failed: %s\n",
+ p, getpid(), strerror(errno));
+ }
abort();
}
else {