linux用户组操作函数

1:getdid函数和setgid函数,获得,设置组识别码,用于获取设置目前进程组识别码

    #include <unistd.h>

    #include <sys/types.h>

    gid_t getgid(void);

    int setgid(gid_t gid).

2:getegid函数和setegid函数,获得设置有效的组识别码

    #include <unistd.h>

    #include <sys/types.h>

    gid_t getegid(void)

    int setegid(gid_t egid)

    getegid用来获取执行目前进程有效组识别码,有效的祖师倍吗用于决定进程执行时组的权限,setegid函数用来重新设置执行目前进程有效组识别码

3:getuid函数和setuid函数,获取设置真实的用户识别码

    #include <unistd.h>

    #include <sys/types.h>

    uid_t getuid (void)

    int seuid(uid_t uid)

    函数返回值,真实的用户识别码,setuid要是root权限才行

4:geteuid 函数和seteuid函数,获得设置有效的用户识别码

    #include <unistd.h>

    #include <sys/types.h>

    uid_t getduid(void)

    int seteuid(uid_t uid)

    一个是获得有效的用户识别码,一个是重新设置执行目前进程的有效用户识别码

5:getgroups函数和setgroupes函数,获得设置组代码

    #include <unistd.h>

    #include <sys/types.h>

    int gegroupes(int size,gid_t list[])

    int set groupes(size_t size,const gid_t *list)

    用于获取目前用户所属组代码,参数size为list【】所能容纳的gid_t数目,如果参数size值为零,此函数仅会返回用户所属组数

    如果执行成功就返回组识别码,错误就返回-1

    setgroupes函数用来将list数组中所表明的组加入到目前进程的组设置中,参数size的list的gid_t数目,最大值为32

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux内核的文件操作函数可以帮助我们在内核态对系统中的文件进行读取、写入、打开、关闭等许多操作。下面介绍几个常用的文件操作函数及其使用案例。 1. open()函数函数用于打开文件,可以根据需要进行读写和创建等不同的操作。其函数原型如下: ``` #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); ``` flags参数用来指定打开文件时的模式,常用的模式包括: * O_RDONLY:只读模式 * O_WRONLY:只写模式 * O_RDWR:读写模式 * O_CREAT:文件不存在时创建文件 * O_APPEND:追加模式 mode参数用来指定创建文件时的权限,常用的权限包括: * S_IRUSR:用户具有读取权限 * S_IWUSR:用户具有写入权限 * S_IXUSR:用户具有执行权限 * S_IRGRP:组具有读取权限 * S_IWGRP:组具有写入权限 * S_IXGRP:组具有执行权限 * S_IROTH:其他用户具有读取权限 * S_IWOTH:其他用户具有写入权限 * S_IXOTH:其他用户具有执行权限 下面是一个使用open()函数打开并读取文件的例子: ``` #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { int fd, n; char buf[1024]; fd = open("test.txt", O_RDONLY); if (fd < 0) { perror("open"); exit(EXIT_FAILURE); } n = read(fd, buf, 1024); if (n < 0) { perror("read"); exit(EXIT_FAILURE); } printf("The content of test.txt is:\n%s", buf); close(fd); return 0; } ``` 2. read()函数函数用于从文件中读取数据。其函数原型如下: ``` #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); ``` fd参数是文件描述符,buf参数是读取数据的缓冲区,count参数是要读取的字节数。 下面是一个使用read()函数读取文件的例子: ``` #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { int fd, n; char buf[1024]; fd = open("test.txt", O_RDONLY); if (fd < 0) { perror("open"); exit(EXIT_FAILURE); } n = read(fd, buf, 1024); if (n < 0) { perror("read"); exit(EXIT_FAILURE); } printf("The content of test.txt is:\n%s", buf); close(fd); return 0; } ``` 3. write()函数函数用于将数据写入文件中。其函数原型如下: ``` #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); ``` fd参数是文件描述符,buf参数是要写入的数据的缓冲区,count参数是要写入的字节数。 下面是一个使用write()函数写入文件的例子: ``` #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { int fd, n; char buf[1024] = "This is a test for write() function.\n"; fd = open("test.txt", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); if (fd < 0) { perror("open"); exit(EXIT_FAILURE); } n = write(fd, buf, sizeof(buf)); if (n < 0) { perror("write"); exit(EXIT_FAILURE); } close(fd); return 0; } ``` 4. close()函数函数用于关闭文件。其函数原型如下: ``` #include <unistd.h> int close(int fd); ``` fd参数是文件描述符。 下面是一个使用close()函数关闭文件的例子: ``` #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { int fd, n; char buf[1024]; fd = open("test.txt", O_RDONLY); if (fd < 0) { perror("open"); exit(EXIT_FAILURE); } /* do something */ close(fd); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值