Ensure root

Gitee close #I81YOX
This commit is contained in:
Aoran Zeng 2023-09-22 13:07:49 +08:00
parent 2038b84d57
commit 094f1ed7d2
2 changed files with 26 additions and 2 deletions

View File

@ -846,6 +846,8 @@ os_ubuntu_getsrc(char* option)
void
os_ubuntu_setsrc (char* option)
{
ensure_root();
int index = 0;
if (NULL!=option) {
@ -879,8 +881,7 @@ os_ubuntu_setsrc (char* option)
"-ports/@\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list");
}
chsrc_logcmd(cmd);
system(cmd);
chsrc_runcmd(cmd);
// char* rm = "rm -rf /etc/apt/source.list.bak";
// system(rm);

23
chsrc.h
View File

@ -261,6 +261,29 @@ chsrc_say_thanks (source_info* source)
}
void
ensure_root ()
{
char* euid = getenv("$EUID");
if (NULL==euid) {
FILE* fp = popen("id -u", "r");
char buf[10] = {0};
fgets(buf, 10, fp);
fclose(fp);
if (0!=atoi(buf)) goto not_root;
else return;
} else {
if (0!=atoi(euid)) goto not_root;
else return;
}
not_root:
xy_error("chsrc: 请在命令前使用 sudo 来保证必要的权限");
exit(1);
}
/* Target Info */
typedef struct {
void (*setfn)(char* option);