How can my C/C++ application determine if the root user is executing the command?

前端 未结 4 1177
借酒劲吻你
借酒劲吻你 2021-01-05 11:34

I am writing an application that requires root user privileges to execute. If executed by a non root user, it exits and terminates with a perror message such as:

<         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-05 11:56

    #include  // getuid
    #include  // printf
    
    int main()
    {
        if (getuid()) printf("%s", "You are not root!\n");
        else printf("%s", "OK, you are root.\n");
        return 0;
    }
    

提交回复
热议问题