Skip to content

Commit

Permalink
psh/sysexec: Add option to deamonize a new process
Browse files Browse the repository at this point in the history
DONE: RTOS-776
  • Loading branch information
agkaminski committed Feb 13, 2024
1 parent 992f5fc commit cdd944d
Showing 1 changed file with 66 additions and 10 deletions.
76 changes: 66 additions & 10 deletions psh/sysexec/sysexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include <unistd.h>
#include <termios.h>
#include <getopt.h>
#include <stdbool.h>
#include <paths.h>
#include <fcntl.h>

#include <sys/threads.h>
#include <sys/types.h>
Expand All @@ -34,7 +37,12 @@ static void psh_sysexecinfo(void)

static void psh_sysexecUsage(void)
{
fputs("Usage: sysexec [-m datamap] [-M codemap] progname [args]...\n", stderr);
fputs("Usage: sysexec [OPTIONS] progname [args]...\n"
"Options:\n"
"\t-m datamap select data memory map\n"
"\t-M codemap select code memory map\n"
"\t-d daemonize\n"
"\t-s do not close stdin on daemonization\n", stderr);

Check failure on line 45 in psh/sysexec/sysexec.c

View workflow job for this annotation

GitHub Actions / clang-format

[clang-format] psh/sysexec/sysexec.c#L41-L45

suggested fix
Raw output
psh/sysexec/sysexec.c:41:-		"Options:\n"
psh/sysexec/sysexec.c:42:-		"\t-m datamap   select data memory map\n"
psh/sysexec/sysexec.c:43:-		"\t-M codemap   select code memory map\n"
psh/sysexec/sysexec.c:44:-		"\t-d           daemonize\n"
psh/sysexec/sysexec.c:45:-		"\t-s           do not close stdin on daemonization\n", stderr);
psh/sysexec/sysexec.c:41:+		  "Options:\n"
psh/sysexec/sysexec.c:42:+		  "\t-m datamap   select data memory map\n"
psh/sysexec/sysexec.c:43:+		  "\t-M codemap   select code memory map\n"
psh/sysexec/sysexec.c:44:+		  "\t-d           daemonize\n"
psh/sysexec/sysexec.c:45:+		  "\t-s           do not close stdin on daemonization\n",
psh/sysexec/sysexec.c:46:+		stderr);
}


Expand Down Expand Up @@ -112,13 +120,14 @@ static int psh_sysexec_checkcommand(int argc, const char **argv)

static int psh_sysexec(int argc, char **argv)
{
pid_t pid, err;
int opt, status = 0;
const char *progName;
const char *codeMapName = NULL;
const char *dataMapName = NULL;
bool background = false;
bool keepStdin = false;

while ((opt = getopt(argc, argv, "hm:M:")) != -1) {
while ((opt = getopt(argc, argv, "hm:M:ds")) != -1) {
switch (opt) {
case 'm':
dataMapName = optarg;
Expand All @@ -130,34 +139,77 @@ static int psh_sysexec(int argc, char **argv)

case 'h':
psh_sysexecUsage();
return EOK;
return EXIT_SUCCESS;

case 'd':
background = true;
break;

case 's':
keepStdin = true;
break;

default:
psh_sysexecUsage();
return -EINVAL;
return EXIT_FAILURE;
}
}

if (!background && keepStdin) {
fprintf(stderr, "psh: -s option can be only used with -d option\n");
psh_sysexecUsage();
return EXIT_FAILURE;
}

progName = argv[optind];
if (progName == NULL) {
fprintf(stderr, "psh: missing program name for sysexec\n");
psh_sysexecUsage();
return -EINVAL;
return EXIT_FAILURE;
}

if (psh_sysexec_checkcommand(argc, (const char **)argv) != 1) {
fprintf(stderr, "Unknown command!\n");
return -EINVAL;
return EXIT_FAILURE;
}

pid_t pid;
if (background) {
pid = vfork();
}
else {
pid = spawnSyspage(codeMapName, dataMapName, progName, &argv[optind]);
}

pid = spawnSyspage(codeMapName, dataMapName, progName, &argv[optind]);
if (pid > 0) {
/* Forked path */
if (pid == 0) {
/* Create a new SID for the child process */
pid_t sid = setsid();
if (sid < 0) {
fprintf(stderr, "psh: setsid failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

if (!keepStdin) {
/* Do not allow daemon to interfere with shell's stdin */
close(STDIN_FILENO);
}

pid = spawnSyspage(codeMapName, dataMapName, progName, &argv[optind]);
if (pid > 0) {
exit(EXIT_SUCCESS);
}
}
else if (pid > 0) {
pid_t err;
do {
err = waitpid(pid, &status, 0);
} while (err < 0 && errno == EINTR);
/* Take back terminal control */
tcsetpgrp(STDIN_FILENO, getpgrp());
return err >= 0 ? WEXITSTATUS(status) : errno;
return err >= 0 ? WEXITSTATUS(status) : EXIT_FAILURE;
}
else {
}

switch (pid) {
Expand Down Expand Up @@ -192,6 +244,10 @@ static int psh_sysexec(int argc, char **argv)
fprintf(stderr, "psh: sysexec failed with code %d\n", pid);
}

if (background) {
exit(EXIT_FAILURE);
}

return pid < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

Expand Down

0 comments on commit cdd944d

Please sign in to comment.