mirror of
https://github.com/RubyMetric/chsrc.git
synced 2025-03-13 07:55:12 +08:00
Ensure root
Gitee close #I81YOX
This commit is contained in:
parent
2038b84d57
commit
094f1ed7d2
5
chsrc.c
5
chsrc.c
@ -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
23
chsrc.h
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user