apps: Add a new su utility

This commit is contained in:
apio 2022-10-28 17:52:46 +02:00
parent 8f0e358360
commit 68d0d0b759
2 changed files with 33 additions and 1 deletions

View File

@ -1,4 +1,4 @@
APPS := init sym sh crash uname uptime hello ps ls args cat stat
APPS := init sym sh crash uname uptime hello ps ls args cat stat su
APPS_DIR := $(LUNA_ROOT)/apps
APPS_SRC := $(APPS_DIR)/src

32
apps/src/su.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char* default_argv[] = {"/bin/sh", NULL};
void run_program(char** argv)
{
execv(argv[0], argv);
perror("execv");
exit(EXIT_FAILURE);
}
int main(int argc, char** argv)
{
if (argc == 1)
{
fprintf(stderr, "Usage: %s [uid] [command | sh]\n", argv[0]);
return EXIT_FAILURE;
}
if (setuid(atoi(argv[1])) < 0)
{
perror("setuid");
return EXIT_FAILURE;
}
if (argc == 2) run_program(default_argv);
else
run_program(argv + 2);
}