Luna/apps/su.cpp

33 lines
568 B
C++

#include <os/ArgumentParser.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv)
{
StringView id;
if (geteuid() != 0)
{
fprintf(stderr, "su must be run as root!\n");
return 1;
}
os::ArgumentParser parser;
parser.add_positional_argument(id, "id"_sv, true);
parser.parse(argc, argv);
int uid = atoi(id.chars());
if (uid == 0)
{
fprintf(stderr, "Already root!\n");
return 1;
}
setgid(uid);
setuid(uid);
execl("/bin/sh", "sh", NULL);
}