Clean up code style
This commit is contained in:
parent
6bd03ad564
commit
b94fa5bfcf
1 changed files with 31 additions and 25 deletions
56
main.c
56
main.c
|
@ -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,25 +32,27 @@ 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");
|
{
|
||||||
exit(1);
|
printf("Sanity check failed. I was able to regain root.\n");
|
||||||
}
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
{
|
||||||
if(sig == SIGTERM)
|
printf("Unable to forward signal %d to child\n", sig);
|
||||||
exit(1);
|
if (sig == SIGTERM)
|
||||||
}
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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,11 +62,12 @@ 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"
|
{
|
||||||
"Run command within its own pid namespace. Integrated init process.\n");
|
printf("Usage: pidjail PROGRAM ARGUMENTS...\n"
|
||||||
return 0;
|
"Run command within its own pid namespace. Integrated init process.\n");
|
||||||
}
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// next fork shall be in a new pid namespace
|
// next fork shall be in a new pid namespace
|
||||||
if (unshare(CLONE_NEWPID) != 0)
|
if (unshare(CLONE_NEWPID) != 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,15 +127,16 @@ 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");
|
{
|
||||||
return 1;
|
printf("Unable to setup signal forward in init. Aborting.\n");
|
||||||
}
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue