endgrent(关闭组文件)

相关函数: getgrent, setgrent

表头文件: #include "grp.h" #include "sys/types.h"

定义函数: void endgrent(void);

函数说明: endgrent()用来关闭由getgrent()所打开的密码文件。

范例: 请参考getgrent()与setgrent()。

endpwent(关闭密码文件)

相关函数: getpwent, setpwent

表头文件: #include "pwd.h" #include "sys/types.h"

定义函数: void endpwent(void);

函数说明: endpwent()用来关闭由getpwent()所打开的密码文件。

范例: 请参考getpwent()与setpwent()。

endpwent(关闭密码文件)

相关函数: getpwent, setpwent

表头文件: #include "pwd.h" #include "sys/types.h"

定义函数: void endpwent(void);

函数说明: endpwent()用来关闭由getpwent()所打开的密码文件。

范例: 请参考getpwent()与setpwent()。

endutent(关闭utmp文件)

相关函数: getutent, setutent

表头文件: #include "utmp.h"

定义函数: void endutent(void);

函数说明: endutent()用来关闭由getutent所打开的utmp文件。

范例: 请参考getutent()。

fgetgrent(从指定的文件来读取组格式)

相关函数: fgetpwent

表头文件: #include "grp.h" #include "stdio.h" #include "sys/types.h"

定义函数: struct group * getgrent(FILE * stream);

函数说明: fgetgrent()会从参数stream指定的文件读取一行数据,然后以group结构将该数据返回。参数stream所指定的文件必须和、etc/group相同的格式。group结构定义请参考getgrent()。

返回值: 返回group结构数据,如果返回NULL则表示已无数据,或有错误发生。

范例


                #include  "grp.h"
                #include "sys/types.h"
                #include "stdio.h"

                int main(int argc, char const *argv[])
                {
                    struct group *data;
                    FILE *stream;
                    int i;

                    stream = fopen("/etc/group", "r");

                    while ((data = fgetgrent(stream)) != 0)
                    {
                        i = 0;
                        printf("%s :%s:%d :", data ->gr_name, data->gr_passwd, data->gr_gid);
                        while (data->gr_mem[i])
                            printf("%s,", data->gr_mem[i++]);
                        printf("\n");
                    }
                    fclose(stream);

                    return 0;
                }
			

执行结果


                root :x:0 :
                daemon :x:1 :
                bin :x:2 :
                sys :x:3 :
                adm :x:4 :syslog,angela,
                tty :x:5 :
                disk :x:6 :
                lp :x:7 :
                mail :x:8 :
                news :x:9 :
                uucp :x:10 :
                man :x:12 :
                /*过多,省略*/
			

fgetpwent(从指定的文件来读取密码格式)

相关函数: fgetgrent

表头文件: #include "pwd.h" #include "stdio.h" #include "sys/types.h"

定义函数: struct passwd * fgetpwent(FILE *stream);

函数说明: fgetpwent()会从参数stream指定的文件读取一行数据,然后以passwd结构将该数据返回。参数stream所指定的文件必须和/etc/passwd相同的格式。passwd结构定义请参考getpwent()。

返回值: 返回passwd结构数据,如果返回NULL则表示已无数据,或有错误发生。

范例


                #include "pwd.h"
                #include "sys/types.h"
                #include "stdio.h"

                int main(int argc, char const *argv[])
                {
                    struct passwd *user;
                    FILE *stream;
                    stream = fopen("/etc/passwd", "r");
                    while ((user = fgetpwent(stream)) != 0)
                    {
                        printf("%s:%d:%d:%s:%s:%s\n", user->pw_name, user->pw_uid,
                                user->pw_gid, user->pw_gecos, user->pw_dir, user->pw_shell);
                    }
                    fclose(stream);
                    return 0;
                }
			

执行结果


            root:0:0:root:/root:/bin/bash
            daemon:1:1:daemon:/usr/sbin:/usr/sbin/nologin
            bin:2:2:bin:/bin:/usr/sbin/nologin
            sys:3:3:sys:/dev:/usr/sbin/nologin
            sync:4:65534:sync:/bin:/bin/sync
            /*过多,省略*/
			

getegid(取得有效的组识别码)

相关函数: getgid, setgid, setregid

表头文件: #include "unistd.h" #include "sys/types.h"

定义函数: gid_t getegid(void);

函数说明: getegid()用来取得执行目前进程有效组识别码。有效的组识别码用来决定进程执行时组的权限。返回值返回有效的组识别码。

范例


          #include "unistd.h"
          #include "stdio.h"

          int main(int argc, char const *argv[])
          {
              printf("egid is %d\n",getegid());
              return 0;
          }
			

执行结果


            egid is 1000
			

geteuid(取得有效的用户识别码)

相关函数: getuid, setreuid, setuid

表头文件: #include "unistd.h" #include "sys/types.h"

定义函数: uid_t geteuid(void)

函数说明: geteuid()用来取得执行目前进程有效的用户识别码。有效的用户识别码用来决定进程执行的权限,借由此改变此值,进程可以获得额外的权限。 倘若执行文件的setID位已被设置,该文件执行时,其进程的euid值便会设成该文件所有者的uid。 例如,执行文件/usr/bin/passwd的权限为-r-s--x--x,其s位即为setID(SUID)位,而当任何用户在执行passwd 时其有效的用户识别码会被设成passwd 所有者的uid 值,即root的uid值(0)。

返回值: 返回有效的用户识别码。

范例


            #include "unistd.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                printf("euid is %d\n",geteuid());
                return 0;
            }
			

执行结果


            euid is 1000
			

getgid(取得真实的组识别码)

相关函数: getegid, setregid, setgid

表头文件: #include "unistd.h" #include "sys/types.h"

定义函数: gid_t getgid(void);

函数说明: getgid()用来取得执行目前进程的组识别码。

返回值: 返回组识别码

范例


            #include "unistd.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                printf("gid is %d\n", getgid());
                return 0;
            }
			

执行结果


            gid is 1000
			

getgrent(从组文件中取得账号的数据)

相关函数: setgrent, endgrent

表头文件: #include "grp.h" #include "sys/types.h"

定义函数: struct group *getgrent(void);

函数说明: getgrent()用来从组文件(/etc/group)中读取一项组数据,该数据以group结构返回。第一次调用时会取得第一项组数据,之后每调用一次就会返回下一项数据,直到已无任何数据时返回NULL。


            struct group
            {
                char *gr_name; /*组名称*/
                char *gr_passwd; /* 组密码*/
                gid_t gr_gid; /*组识别码*/
                char **gr_mem; /*组成员账号*/
            }
			

返回值: 返回group结构数据,如果返回NULL则表示已无数据,或有错误发生。

附加说明: getgrent()在第一次调用时会打开组文件,读取数据完毕后可使用endgrent()来关闭该组文件。

错误代码: ENOMEM 内存不足,无法配置group结构。

范例


            #include "grp.h"
            #include "sys/types.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                struct group *data;
                int i;
                while ((data = getgrent()) != 0)
                {
                    i = 0;
                    printf("%s: %s: %d: ", data->gr_name, data->gr_passwd, data->gr_gid);
                    while (data->gr_mem[i])
                        printf("%s,", data->gr_mem[i++]);
                    printf("\n");
                }
                endgrent();
                return 0;
            }
			

执行结果


            root: x: 0:
            daemon: x: 1:
            bin: x: 2:
            sys: x: 3:
            adm: x: 4: syslog,angela,
            tty: x: 5:
            disk: x: 6:
            lp: x: 7:
            mail: x: 8:
            news: x: 9:
            uucp: x: 10:
            man: x: 12:
            /*过多,省略*/
			

getgrgid(从组文件中取得指定gid 的数据)

相关函数: fgetgrent, getgrent, getgrnam

表头文件: #include "grp.h" #include "sys/types.h"

定义函数: strcut group *getgrgid(gid_t gid);

函数说明: getgrgid()用来依参数gid指定的组识别码逐一搜索组文件,找到时便将该组的数据以group结构返回。group结构请参考getgrent()。

返回值: 返回group结构数据,如果返回NULL则表示已无数据,或有错误发生。

范例


          /* 取得gid=1000的组数据*/
            #include "grp.h"
            #include "sys/types.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                struct group *data;
                int i = 0;

                data = getgrgid(1000);
                printf("%s:%s:%d:", data->gr_name, data->gr_passwd, data->gr_gid);
                while(data->gr_mem[i])
                    printf("%s, ", data->gr_mem[i++]);
                printf("\n");
                return 0;
            }
			

执行结果


            angela:x:1000:
			

getgrnam(从组文件中取得指定组的数据)

相关函数: fgetgrent, getgrent, getgruid

表头文件: #include "grp.h" #include "sys/types.h"

定义函数: strcut group * getgrnam(const char * name);

函数说明: getgrnam()用来逐一搜索参数那么指定的组名称,找到时便将该组的数据以group结构返回。group结构请参考getgrent()。

返回值: 返回group结构数据,如果返回NULL则表示已无数据,或有错误发生。

范例


					/* 取得adm的组数据*/
            #include "grp.h"
            #include "sys/types.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                struct group * data;
                int i = 0;
                data = getgrnam("angela");
                printf("%s:%s:%d:", data->gr_name, data->gr_passwd, data->gr_gid);
                while(data->gr_mem[i])
                    printf("%s,", data->gr_mem[i++]);
                printf("\n");

                return 0;
            }
			

执行结果


            angela:x:1000:
			

getgroups(取得组代码)

相关函数: initgroups, setgroup, getgid, setgid,

表头文件: #include "unistd.h" #include "sys/types.h"

定义函数: int getgroups(int size, gid_t list[]);

函数说明: getgroup()用来取得目前用户所属的组代码。参数size为list[]所能容纳的gid_t数目。如果参数size值为零,此函数仅会返回用户所属的组数。

返回值: 返回组识别码,如有错误则返回-1。

错误代码: EFAULT 参数list数组地址不合法。EINVAL 参数size值不足以容纳所有的组。

范例


            #include "unistd.h"
            #include "sys/types.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                gid_t list[500];
                int x, i;
                x = getgroups(0, list);
                getgroups(x, list);
                for (i = 0; i < x; i++)
                    printf("%d:%d\n", i, list[i]);

                return 0;
            }
			


            0:4
            1:24
            2:27
            3:30
            4:46
            5:108
            6:111
            7:1000
			

getpw(取得指定用户的密码文件数据)

相关函数: getpwent

表头文件: #include "pwd.h" #include "sys/types.h"

定义函数: int getpw(uid_t uid,char *buf);

函数说明: getpw()会从/etc/passwd中查找符合参数uid所指定的用户账号数据,找不到相关数据就返回-1。所返回的buf字符串格式如下:账号:密码:用户识别码(uid):组识别码(gid):全名:根目录:shell

返回值: 返回0表示成功,有错误发生时返回-1。

附加说明: 1. getpw()会有潜在的安全性问题,请尽量使用别的函数取代。2. 使用shadow的系统已把用户密码抽出/etc/passwd,因此使用getpw()取得的密码将为"x"。

范例


            #include "pwd.h"
            #include "sys/types.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                char buffer[80];
                getpw(0, buffer);
                printf("%s\n", buffer);
                return 0;
            }
			

执行结果


            root:x:0:0:root:/root:/bin/bash
			

getpwent(从密码文件中取得账号的数据)

相关函数: getpw, fgetpwent, getpwnam, getpwuid, setpwent, endpwent

表头文件: #include "pwd.h" #include "sys/types.h"

定义函数: strcut passwd * getpwent(void);

函数说明: getpwent()用来从密码文件(/etc/passwd)中读取一项用户数据,该用户的数据以passwd结构返回。第一次调用时会取得第一位用户数据,之后每调用一次就会返回下一项数据,直到已无任何数据时返回NULL。passwd 结构定义如下


            struct passwd
            {
                char *pw_name; /*用户账号*/
                char *pw_passwd; /*用户密码*/
                uid_t pw_uid; /*用户识别码*/
                gid_t pw_gid; /*组识别码*/
                char *pw_gecos; /*用户全名*/
                char *pw_dir; /*家目录*/
                char *pw_shell; /* 所使用的shell路径*/
            };
			

范例


            #include "pwd.h"
            #include "sys/types.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                struct passwd *user;
                while((user = getpwent()) != 0) {
                    printf("%s:%d:%d:%s:%s:%s\n", user->pw_name, user->pw_uid, user->pw_gid,
                           user->pw_gecos, user->pw_dir, user->pw_shell);
                }
                endpwent();
                return 0;
            }
			

执行结果


            root:0:0:root:/root:/bin/bash
            daemon:1:1:daemon:/usr/sbin:/usr/sbin/nologin
            bin:2:2:bin:/bin:/usr/sbin/nologin
            sys:3:3:sys:/dev:/usr/sbin/nologin
            sync:4:65534:sync:/bin:/bin/sync
            games:5:60:games:/usr/games:/usr/sbin/nologin
            man:6:12:man:/var/cache/man:/usr/sbin/nologin
            lp:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
            mail:8:8:mail:/var/mail:/usr/sbin/nologin
            /*过多,省略*/
			

getpwnam(从密码文件中取得指定账号的数据)

相关函数: getpw, fgetpwent, getpwent, getpwuid,

表头文件: #include "pwd.h" #include "sys/types.h"

定义函数: struct passwd *getpwnam(const char * name);

函数说明: getpwnam()用来逐一搜索参数name 指定的账号名称,找到时便将该用户的数据以passwd结构返回。passwd结构请参考getpwent()。

返回值: 返回passwd 结构数据,如果返回NULL 则表示已无数据,或有错误发生。

范例


	/*取得root账号的识别码和根目录*/
            #include "pwd.h"
            #include "sys/types.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                struct passwd *user;
                user = getpwnam("root");
                printf("name:%s\n", user->pw_name);
                printf("uid:%d\n", user->pw_uid);
                printf("home:%s\n", user->pw_dir);
                return 0;
            }
			

执行结果


            name:root
            uid:0
            home:/root
			

getpwuid(从密码文件中取得指定uid 的数据)

相关函数: getpw, fgetpwent, getpwent, getpwnam

表头文件: #include "pwd.h" #include "sys/types.h"

定义函数: struct passwd *getpwuid(uid_t uid);

函数说明: getpwuid()用来逐一搜索参数uid 指定的用户识别码,找到时便将该用户的数据以结构返回结构请参考将该用户的数据以passwd结构返回。passwd 结构请参考getpwent()。

返回值: 返回passwd 结构数据,如果返回NULL 则表示已无数据,或者有错误发生。

范例


            #include "pwd.h"
            #include "sys/types.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                struct passwd *user;
                user = getpwuid(6);
                printf("name:%s\n", user->pw_name);
                printf("uid:%d\n", user->pw_uid);
                printf("home:%s\n", user->pw_dir);
                return 0;
            }
			

执行结果


            name:man
            uid:6
            home:/var/cache/man
			

getuid(取得真实的用户识别码)

相关函数: geteuid, setreuid, setuid

表头文件: #include "unistd.h" #include "sys/types.h"

定义函数: uid_t getuid(void);

函数说明: getuid()用来取得执行目前进程的用户识别码。

返回值: 用户识别码

范例


            #include "unistd.h"
            #include "sys/types.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                printf("uid is %d\n",getuid());
                return 0;
            }
			

执行结果


            uid is 1000
			

getutent(从utmp 文件中取得账号登录数据)

相关函数: getutent, getutid, getutline, setutent, endutent, pututline, utmpname

表头文件: #include "utmp.h"

定义函数: struct utmp *getutent(void);

函数说明: getutent()用来从utmp 文件(/var/run/utmp)中读取一项登录数据,该数据以utmp结构返回。第一次调用时会取得第一位用户数据,之后每调用一次就会返回下一项数据,直到已无任何数据时返回NULL。utmp结构定义如下:


            struct utmp
            {
                short int ut_type; /*登录类型*/
                pid_t ut_pid; /*login进程的pid*/
                char ut_line[UT_LINESIZE];/*登录装置名,省略了"/dev/"*/
                char ut_id[4]; /* Inittab ID*/
                char ut_user[UT_NAMESIZE];/*登录账号*/
                char ut_host[UT_HOSTSIZE];/*登录账号的远程主机名称*/
                struxt exit_status ut_exit;/* 当类型为DEAD_PROCESS时进程的结
                    束状态*/
                long int ut_session; /*Sessioc ID*/
                struct timeval ut_tv; /*时间记录*/
                int32_t ut_addr_v6[4]; /*远程主机的网络地址*/
                char __unused[20]; /* 保留未使用*/
            };
			

ut_type有以下几种类型: EMPTY 此为空的记录。 RUN_LVL 记录系统run-level的改变 BOOT_TIME 记录系统开机时间 NEW_TIME 记录系统时间改变后的时间 OLD_TINE 记录当改变系统时间时的时间。 INIT_PROCESS 记录一个由init衍生出来的进程。 LOGIN_PROCESS 记录login进程。 USER_PROCESS 记录一般进程。 DEAD_PROCESS 记录一结束的进程。 ACCOUNTING 目前尚未使用。 exit_status结构定义:


            struct exit_status
            {
                short int e_termination; /*进程结束状态*/
                short int e_exit; /*进程退出状态*/
            };
			

timeval的结构定义请参考gettimeofday()。 相关常数定义如下: UT_LINESIZE 32 UT_NAMESIZE 32 UT_HOSTSIZE 256

返回值: 返回utmp 结构数据,如果返回NULL 则表示已无数据,或有错误发生。

附加说明: getutent()在第一次调用时会打开utmp 文件,读取数据完毕后可使用endutent()来关闭该utmp文件。

范例


            #include "utmp.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                struct utmp *u;
                while((u = getutent()))
                {
                    if(u->ut_type == USER_PROCESS)
                        printf("%d %s %s %s \n", u->ut_type, u->ut_user, u->ut_line, u->ut_host);
                }
                endutent();
                return 0;
            }
			

执行结果


            7 root tty8 :0
            7 root pts/1 :0
            7 root pts/2 :0
			

getutid(从utmp文件中查找特定的记录)

相关函数: getutent, getutline

表头文件: #include "utmp.h"

定义函数: strcut utmp *getutid(strcut utmp *ut);

函数说明: getutid()用来从目前utmp 文件的读写位置逐一往后搜索参数ut指定的记录,如果ut->ut_type为RUN_LVL,BOOT_TIME,NEW_TIME,OLD_TIME 其中之一则查找与ut->ut_type 相符的记录;若ut->ut_type为INIT_PROCESS,LOGIN_PROCESS,USER_PROCESS或DEAD_PROCESS其中之一,则查找与ut->ut_id相符的记录。找到相符的记录便将该数据以utmp结构返回。utmp结构请参考getutent()。

返回值: 返回utmp 结构数据,如果返回NULL 则表示已无数据,或有错误发生。

范例


            #include "utmp.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                struct utmp ut, *u;
                ut.ut_type = RUN_LVL;
                while ((u = getutid(&ut)))
                {
                    printf("%d %s %s %s\n", u->ut_type, u->ut_user, u->ut_line, u->ut_host);
                }
                return 0;
            }
			

执行结果


            1 runlevel ~ 3.13.0-37-generic
			

getutline(从utmp 文件中查找特定的记录)

相关函数: getutent, getutid, pututline

表头文件: #Include "utmp.h"

定义函数: struct utmp *getutline (struct utmp *ut);

函数说明: getutline()用来从目前utmp文件的读写位置逐一往后搜索ut_type为USER_PROCESS或LOGIN_PROCESS 的记录,而且ut_line 和ut-"ut_line 相符。找到相符的记录便将该数据以utmp 结构返回,utmp结构请参getutent()。

返回值: 返回utmp 结构数据,如果返回NULL 则表示已无数据,或有错误发生。

范例


            #include "utmp.h"
            #include "string.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                struct utmp ut, *u;
                strcpy (ut.ut_line, "pts/1");
                while((u = getutline(&ut)))
                {
                    printf("%d %s %s %s \n", u->ut_type, u->ut_user, u->ut_line, u->ut_host);
                }
                return 0;
            }
			

执行结果


            7 angela pts/1 :0
			

initgroups(初始化组清单)

相关函数: setgrent, endgrent

表头文件: #include "grp.h" #include "sys/types.h"

定义函数: int initgroups(const char *user,gid_t group);

函数说明: initgroups()用来从组文件(/etc/group)中读取一项组数据,若该组数据的成员中有参数user时,便将参数group组识别码加入到此数据中。

返回值: 执行成功则返回0,失败则返回-1,错误码存于errno。

pututline(将utmp记录写入文件)

相关函数: getutent, getutid, getutline

表头文件: #include "utmp.h"

定义函数: void pututline(struct utmp *ut);

函数说明: pututline()用来将参数ut的utmp结构记录到utmp文件中。此函数会先用getutid()来取得正确的写入位置,如果没有找到相符的记录则会加入到utmp文件尾,utmp结构请参考getutent()。

附加说明: 需要有写入/var/run/utmp 的权限

范例


            #include "utmp.h"
            #include "stdio.h"
            #include "string.h"
            #include "unistd.h"

            int main(int argc, char const *argv[])
            {
                struct utmp ut;
                ut.ut_type = USER_PROCESS;
                ut.ut_pid = getpid();
                strcpy(ut.ut_user, "angela");
                strcpy(ut.ut_line, "pts/1");
                strcpy(ut.ut_host, "www.gnu.org");
                pututline(&ut);

                return 0;
            }
			

执行结果


            root pts/0 dec9 19:20
            kids pts/1 dec12 10:31(www.gnu.org)
            root pts/2 dec12 13:33
            /*具体可能有差别*/
			

seteuid(设置有效的用户识别码)

相关函数: setuid, setreuid, setfsuid

表头文件: #include "unistd.h"

定义函数: int seteuid(uid_t euid);

函数说明: seteuid()用来重新设置执行目前进程的有效用户识别码。在Linux下,seteuid(euid)相当于setreuid(-1,euid)。

返回值: 执行成功则返回0,失败则返回-1,错误代码:存于errno

附加说明: 请参考setuid

setfsgid(设置文件系统的组识别码)

相关函数: setuid, setreuid, seteuid, setfsuid

表头文件: #include "unistd.h"

定义函数: int setfsgid(uid_t fsgid);

函数说明: setfsgid()用来重新设置目前进程的文件系统的组识别码。一般情况下,文件系统的组识别码(fsgid)与有效的组识别码(egid)是相同的。如果是超级用户调用此函数,参数fsgid可以为任何值,否则参数fsgid必须为real/effective/saved的组识别码之一。

返回值: 执行成功则返回0,失败则返回-1,错误代码:存于errno。

附加说明: 此函数为Linux特有。

错误代码: EPERM 权限不够,无法完成设置。

setfsuid(设置文件系统的用户识别码)

相关函数: setuid, setreuid, seteuid, setfsgid

表头文件: #include "unistd.h"

定义函数: int setfsuid(uid_t fsuid);

函数说明: setfsuid()用来重新设置目前进程的文件系统的用户识别码。一般情况下,文件系统的用户识别码(fsuid)与有效的用户识别码(euid)是相同的。如果是超级用户调用此函数,参数fsuid可以为任何值,否则参数fsuid必须为real/effective/saved的用户识别码之一。

返回值: 执行成功则返回0,失败则返回-1,错误代码:存于errno

附加说明: 此函数为Linux特有

错误代码: EPERM 权限不够,无法完成设置。

setgid(设置真实的组识别码)

相关函数: getgid, setregid, getegid, setegid

表头文件: #include "unistd.h"

定义函数: int setgid(gid_t gid);

函数说明: setgid()用来将目前进程的真实组识别码(real gid)设成参数gid值。如果是以超级用户身份执行此调用,则real、effective与savedgid都会设成参数gid。

返回值: 设置成功则返回0,失败则返回-1,错误代码:存于errno中。

错误代码: EPERM 并非以超级用户身份调用,而且参数gid 并非进程的effective gid或saved gid值之一。

setgrent(从头读取组文件中的组数据)

相关函数: getgrent, endgrent,

表头文件: #include "grp.h" #include "sys/types.h"

定义函数: void setgrent(void);

函数说明: setgrent()用来将getgrent()的读写地址指回组文件开头。

附加说明: 请参考setpwent()。

setgroups(设置组代码)

相关函数: initgroups, getgroup, getgid, setgid

表头文件: #include "grp.h"

定义函数: int setgroups(size_t size,const gid_t * list);

函数说明: setgroups()用来将list 数组中所标明的组加入到目前进程的组设置中。参数size为list()的gid_t数目,最大值为NGROUP(32)。

返回值: 设置成功则返回0,如有错误则返回-1。

错误代码: EFAULT 参数list数组地址不合法。 EPERM 权限不足,必须是root权限 EINVAL 参数size值大于NGROUP(32)。

setpwent(从头读取密码文件中的账号数据)

相关函数: getpwent, endpwent

表头文件: #include "pwd.h" #include "sys/types.h"

定义函数: void setpwent(void);

函数说明: setpwent()用来将getpwent()的读写地址指回密码文件开头。

范例


            #include "pwd.h"
            #include "sys/types.h"
            #include "stdio.h"

            int main(int argc, char const *argv[])
            {
                struct passwd *user;
                int i;
                for (i = 0; i < 4; i++)
                {
                    user = getpwent();
                    printf("%s :%d :%d :%s:%s:%s\n", user->pw_name, user->pw_uid,
                            user->pw_gid, user->pw_gecos, user->pw_dir, user->pw_shell);
                }
                setpwent();
                user = getpwent();
                printf("%s :%d :%d :%s:%s:%s\n", user->pw_name, user->pw_uid,
                        user->pw_gid, user->pw_gecos, user->pw_dir, user->pw_shell);
                endpwent();
                return 0;
            }
			


            root :0 :0 :root:/root:/bin/bash
            daemon :1 :1 :daemon:/usr/sbin:/usr/sbin/nologin
            bin :2 :2 :bin:/bin:/usr/sbin/nologin
            sys :3 :3 :sys:/dev:/usr/sbin/nologin
            root :0 :0 :root:/root:/bin/bash
			

setregid(设置真实及有效的组识别码)

相关函数: setgid, setegid, setfsgid

表头文件: #include "unistd.h"

定义函数: int setregid(gid_t rgid,gid_t egid);

函数说明: setregid()用来将参数rgid设为目前进程的真实组识别码,将参数egid设置为目前进程的有效组识别码。如果参数rgid或egid值为-1,则对应的识别码不会改变。

返回值: 执行成功则返回0,失败则返回-1,错误代码:存于errno。

setreuid(设置真实及有效的用户识别码)

相关函数: setuid, seteuid, setfsuid

表头文件: #include "unistd.h"

定义函数: int setreuid(uid_t ruid,uid_t euid);

函数说明: setreuid()用来将参数ruid 设为目前进程的真实用户识别码,将参数euid设置为目前进程的有效用户识别码。如果参数ruid 或euid值为-1,则对应的识别码不会改变。

返回值: 执行成功则返回0,失败则返回-1,错误代码:存于errno。

附加说明: 请参考setuid()。

setuid(设置真实的用户识别码)

相关函数: getuid, setreuid, seteuid, setfsuid

表头文件: #include "unistd.h"

定义函数: int setuid(uid_t uid)

函数说明: setuid()用来重新设置执行目前进程的用户识别码。不过,要让此函数有作用,其有效的用户识别码必须为0(root)。 在Linux下,当root使用setuid()来变换成其他用户识别码时,root权限会被抛弃,完全转换成该用户身份,也就是说, 该进程往后将不再具有可setuid()的权利,如果只是向暂时抛弃root权限,稍后想重新取回权限,则必须使用seteuid()。

返回值: 执行成功则返回0,失败则返回-1,错误代码:存于errno。

附加说明: 一般在编写具setuid root的程序时,为减少此类程序带来的系统安全风险,在使用完root权限后建议马上执行setuid(getuid());来抛弃root权限。此外,进程uid和euid不一致时Linux系统将不会产生core dump。

setutent(从头读取utmp 文件中的登录数据)

相关函数: getutent, endutent

表头文件: #include "utmp.h"

定义函数: void setutent(void);

函数说明: setutent()用来将getutent()的读写地址指回utmp文件开头。

附加说明: 请参考setpwent()或setgrent()。

utmpname(设置utmp 文件路径)

相关函数: getutent, getutid, getutline, setutent, endutent, pututline,

表头文件: #include "utmp.h"

定义函数: void utmpname(const char * file);

函数说明: utmpname()用来设置utmp文件的路径,以提供utmp

相关函数:的存取路径。如果没有使用utmpname()则默认utmp文件路径为/var/run/utmp。