Clean up code style

This commit is contained in:
madmaurice 2021-01-15 20:22:22 +01:00
parent 6bd03ad564
commit b94fa5bfcf

26
main.c
View file

@ -12,7 +12,8 @@
pid_t pid_child; pid_t pid_child;
void drop_root(void) { void drop_root(void)
{
uid_t uid = getuid(); uid_t uid = getuid();
// Drop root privileges // Drop root privileges
if (setresuid(-1,uid,uid) == -1) if (setresuid(-1,uid,uid) == -1)
@ -31,7 +32,8 @@ void drop_root(void) {
} }
// sanity check // sanity check
if(seteuid(0) != -1) { if (seteuid(0) != -1)
{
printf("Sanity check failed. I was able to regain root.\n"); printf("Sanity check failed. I was able to regain root.\n");
exit(1); exit(1);
} }
@ -39,9 +41,10 @@ void drop_root(void) {
void forward_signal(int sig) void forward_signal(int sig)
{ {
if(kill(pid_child, sig) == -1) { if (kill(pid_child, sig) == -1)
{
printf("Unable to forward signal %d to child\n", sig); printf("Unable to forward signal %d to child\n", sig);
if(sig == SIGTERM) if (sig == SIGTERM)
exit(1); exit(1);
} }
} }
@ -49,7 +52,7 @@ void forward_signal(int sig)
char** argdup(int argc, const char** argv) char** argdup(int argc, const char** argv)
{ {
char** newargs = malloc(sizeof(char*) * (argc+1)); char** newargs = malloc(sizeof(char*) * (argc+1));
for(size_t i = 0; i < argc; i++) for (size_t i = 0; i < argc; i++)
{ {
newargs[i] = strdup(argv[i]); newargs[i] = strdup(argv[i]);
} }
@ -59,7 +62,8 @@ char** argdup(int argc, const char** argv)
int main(int argc, const char** argv) int main(int argc, const char** argv)
{ {
if(argc == 1) { if (argc == 1)
{
printf("Usage: pidjail PROGRAM ARGUMENTS...\n" printf("Usage: pidjail PROGRAM ARGUMENTS...\n"
"Run command within its own pid namespace. Integrated init process.\n"); "Run command within its own pid namespace. Integrated init process.\n");
return 0; return 0;
@ -90,12 +94,13 @@ int main(int argc, const char** argv)
// Setup signal handler to forward SIGTERM // Setup signal handler to forward SIGTERM
pid_child = pid; pid_child = pid;
if(signal(SIGTERM, forward_signal) == SIG_ERR) { if (signal(SIGTERM, forward_signal) == SIG_ERR)
{
printf("Unable to setup signal handler in head\n"); printf("Unable to setup signal handler in head\n");
} }
// parent waits for child then exits // parent waits for child then exits
int status; int status;
if(waitpid(pid, &status, 0) == -1) if (waitpid(pid, &status, 0) == -1)
{ {
int err = errno; int err = errno;
printf("Failed to wait (%d)\n", err); printf("Failed to wait (%d)\n", err);
@ -122,7 +127,8 @@ int main(int argc, const char** argv)
// Setup forward for SIGTERM // Setup forward for SIGTERM
pid_child = first_child; pid_child = first_child;
if(signal(SIGTERM, forward_signal) == SIG_ERR) { if (signal(SIGTERM, forward_signal) == SIG_ERR)
{
printf("Unable to setup signal forward in init. Aborting.\n"); printf("Unable to setup signal forward in init. Aborting.\n");
return 1; return 1;
} }
@ -130,7 +136,7 @@ int main(int argc, const char** argv)
do { do {
exited_child = wait(&child_status); exited_child = wait(&child_status);
err = errno; err = errno;
} while(exited_child != first_child && exited_child != -1); } while (exited_child != first_child && exited_child != -1);
if (exited_child == -1) if (exited_child == -1)
{ {