7.2进程切换用户 calm2012 2019-04-08 星期一 linux / Linux高性能服务器编程 123456789101112131415161718192021222324252627282930313233343536373839#include <unistd.h>#include <stdio.h>static bool switch_to_user(uid_t user_id, gid_t gp_id){ if ((user_id == 0) && (gp_id == 0)) { return false; } gid_t gid = getgid(); uid_t uid = getuid(); if (((gid != 0) || (uid != 0)) && ((gid != gp_id) || (uid != user_id))) { return false; } if (uid != 0) { return true; } if ((setgid(gp_id) < 0) || (setuid(user_id) < 0)) { return false; } return true;}int main(){ printf("uid:%d gid:%d\n", getuid(), getgid()); switch_to_user(1000, 1000); printf("uid:%d gid:%d\n", getuid(), getgid()); return 0;} 2019-04-08 星期一 linux Linux高性能服务器编程 上一页 7.3以守护进程方式运行 linux Linux高性能服务器编程 下一页 7.1进程的UID和EUID linux Linux高性能服务器编程