zoukankan      html  css  js  c++  java
  • 【Linux系统编程】真实用户、有效用户

    用户API

    1. 函数原型:

    #include <unistd.h>
    uid_t getuid(void);
    uid_t geteuid(void);

    说明:1. getuid()返回调用进程的真实用户ID。

        2. geteuid()返回调用进程的有效用户ID。

    和系统数据相关的一个结构passwd定义如下:

    #include <sys/types.h>
    #include <pwd.h>
    struct passwd *getpwuid(uid_t uid);

    说明:查找用户的passwd数据

    
    
    /* The passwd structure.      */
    struct passwd
    {
        char *pw_name;     /* 用户名*/
        char *pw_passwd;   /* 密码.*/
        __uid_t pw_uid;     /* 用户ID.*/
        __gid_t pw_gid;     /*组ID.*/
        char *pw_gecos;     /*真实名*/
        char *pw_dir;        /* 主目录.*/
        char *pw_shell;     /*使用的shell*/
    };
     1 #include <pwd.h>
     2 #include <sys/types.h>
     3 #include <stdio.h>
     4 #include <pwd.h>
     5 #include <unistd.h>
     6 
     7 int main()
     8 {
     9     struct passwd *user;
    10     user = getpwuid(getuid());
    11     printf("name:%s\n", user->pw_name);
    12     printf("uid:%d\n", user->pw_uid);
    13     printf("home:%s\n", user->pw_dir);
    14 }

    输出:

    [root@kristopher01 kristopher]# g++ test.cpp
    [root@kristopher01 kristopher]# ./a.out
    name:root
    uid:0
    home:/root
    
  • 相关阅读:
    LeetCode | Divide Two Integers
    LeetCode | Pow(x, n)
    LeetCode | Sqrt (x)
    LeetCode | 3 Sum
    LeetCode | Two Sum
    LeetCode | Pascal's Triangle II
    nodejs eclipse
    CentOS: Make Command not Found and linux xinetd 服务不能启动
    jquery将form表单序列化常json
    VMware Mac OS补丁安装
  • 原文地址:https://www.cnblogs.com/sunbines/p/15746551.html
Copyright © 2011-2022 走看看