APUE学习笔记(第六章 系统数据文件和信息)

UNIX系统的正常运作需要使用大量与系统有关的数据文件,例如,口令文件/etc/passwd和组文件/etc/group就是经常被多个程序频繁使用的两个文件。

口令文件

UNIX系统口令文件包含如下字段,这些字段包含在<pwd.h>中定义的passwd结构中

口令文件是/etc/passwd,每一行包含上面各字段,字段之间用冒号分隔。可以使用finger命令打印指定用户的有关信息:finger -p 用户名

POSIX.1定义了两个获取口令文件项的函数,在给定用户登录名或数值用户ID后,这两个函数就能查看相关项

#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);

下面两个函数可以实现查看整个口令文件

#include <pwd.h>
struct passwd *getpwent(void);
void setpwent(void);
void endpwent(void);

调用getpwent时,它返回口令文件中的下一个记录项。函数setpwent反绕它所使用的文件(定位到文件开始处),endpwent则关闭这些文件,下面给出使用这3个函数实现getpwnam函数的程序

#include <pwd.h>
#include <stddef.h>
#include <string.h>

struct passwd *
getpwnam(const char *name)
{
    struct passwd  *ptr;

    setpwent();
    while ((ptr = getpwent()) != NULL)
        if (strcmp(name, ptr->pw_name) == 0)
            break;        /* found a match */
    endpwent();
    return(ptr);    /* ptr is NULL if no match found */
}

阴影文件

现在,某些系统将加密口令存放在另一个通常称为阴影口令的文件中:/etc/shadow

与访问口令文件的一组函数相类似,有另一组函数可用于访问阴影口令文件

#include <shadow.h>
struct spwd *getspnam(cosnt char *name);
struct spwd *getspent(void);
void setspent(void);
void endspent(void);

组文件

UNIX组文件包含下图所示字段(/etc/group文件中的字段),这些字段包含在<grp.h>中所定义的group结构中。

字段gr_mem是一个指针数组,其中每一个指针指向一个属于该组的用户名。该数组以null指针结尾。

可以使用下列两个函数来查看组相关项

#include <grp.h>
struct group *getgrgid(gid_t gid);
struct group *getgrnam(const char *name);

类似于口令文件,如果需要搜索整个组文件,可以使用下面3个函数

#include <grp.h>
struct group *getgrent(void);
void setgrent(void);
void endgrent(void);

附属组ID

当用户登录时,系统就按口令文件记录项中的数值组ID,赋给它实际组ID。在任何时刻执行newgrp更改组ID

一个用户可能参与多个项目,因此也就要同时属于多个组,使用附属组ID的优点是不必再显式地经常更改组。

为了获取和设置附属组ID,提供了下列3个函数

#include <unistd.h>
int getgroups(int gidsetsize,gid_t grouplist[]);
int setgroups(int ngroups,const gid_t grouplist[]);
int init groups(const char *username,gid_t basegid);

getgroups将进程所属用户的各附属组ID填写到数组grouplist中,最多为gidsetsize个

setgroups可由超级用户调用以便为调用进程设置附属组ID表。

通常,只有initgroups函数调用setgroups,用来设置用户的附属组ID。

其他数据文件

下图给出访问系统数据文件的一些例程

对于这些数据文件的接口都都与上述对口令文件和组文件的相似,一般情况下,对于每个数据文件至少提供一下3个函数

1 get函数:读下一个记录

2 set函数:打开相应数据文件,然后反绕该文件

3 end函数:关闭相应数据文件

系统标识

POSIX.1定义了uname函数,它返回与主机和操作系统有关的信息

#include <sys/utsname.h>
int uname(struct utsname *name);

POSIX.1定义了该结构中最少需提供的字段

 struct utsname {
         char sysname[];    /* Operating system name (e.g., "Linux") */
         char nodename[];   /* Name within "some implementation-defined network" */
         char release[];    /* Operating system release (e.g., "2.6.28") */
         char version[];    /* Operating system version */
         char machine[];    /* Hardware identifier */
}

时间和日期例程

time函数返回当前时间和日期。它是自公元1970年1月1日00:00:00以来经过的秒数(日历时间)

#include <time.h>
time_t time(time_t *calptr);

如果参数非空,则时间值也存放在由calptr指向的单元内。

下面两个函数将日历时间转换成分解的时间,并将这些存放在一个tm结构中

#include <time.h>
struct tm *gmtime(const time_t *calptr);
struct tm *localtime(const time_t *calptr);
struct tm {
         int tm_sec;         /* seconds [0-60] */
         int tm_min;         /* minutes  [0-59] */
         int tm_hour;        /* hours [0-23] */
         int tm_mday;        /* day of the month [1-31] */
         int tm_mon;         /* month [0-11] */
         int tm_year;        /* year since 1900*/
         int tm_wday;        /* day of the week [0-6]*/
         int tm_yday;        /* day in the year [0-365]*/
         int tm_isdst;       /* daylight saving time */
};

mktime函数将tm结构转换成time_t值

#include <time.h>
time_t mktime(struct tm *tmptr);

strftime用于格式化时间值

#include <time.h>
size_t strftime(char *restrict buf,size_t maxsize,const char *restrict format,const struct tm *restrict tmptr);
size_t strftime_l(char *restrict buf,size_t maxsize,const char *restrict format,const struct tm *restrict tmptr,locale_t locale);

下面列出format参数的格式

strptime是strftime的反过来的版本,将字符串时间转换成分解时间

#include <time.h>
char *strptime(const char *restrict buf,const char *restrict format,struct tm *restrict tmptr);
时间: 2024-08-02 06:52:40

APUE学习笔记(第六章 系统数据文件和信息)的相关文章

APUE读书笔记-第六章 系统数据文件和信息

昨天看完了,今天来看看第六章.感觉第六章的内容不是非常重要.简单看看吧 6.2 口令文件 口令文件其实就是/etc文件夹下的passwd文件,但处于安全性的考虑,我们无法直接读取它.就是通过直接限制权限的方式对其进行保护,passwd文件具体权限如下: -rw-r--r-- 1 root root 可以看到只有root用户具有读写权限,与root同组的用户与其他用户仅具有读权限. 不过为了解决以上问题,Linux中给出了一系列数据结构与函数帮助我们操纵口令文件,首先是关键数据结构,定义位于/in

APUE学习笔记:第六章 系统数据文件和信息

6.1 引言 UNIX系统的正常运行需要使用大量与系统有关的数据文件,针对这些数据文件的可移植接口是本章的主题.本章还介绍了系统标识函数.时间和日期函数 6.2 口令文件 UNIX系统的口令文件包含了下列各字段,这些字段包含在<pwd.h>中定义的passwd结构中 用户名 char *pw_name 加密口令 char *pw_passwd 数值用户ID uid_t pw_uid 数值组ID gid_t pw_gid 注释字段 char *pw_gecos 初始工作目录 char *pw_d

第6章系统数据文件和信息总结

1 口令文件的shell字段 如果是空,则使用系统默认的shell,一般是/bin/sh /dev/null:阻止对应的用户名登陆系统 /bin/false:同样是阻止特定用户登录,以不成功状态终止 /bin/true:阻止特定用户登录,以成功状态终止 2 获取口令文件内容的函数 通过用户ID或用户名可以获取该用户在口令文件中的对应项 struct passwd* getpwuid(uid_t uid) struct passwd* getpwnam(const char *name) 获取整个

APUE学习笔记:第九章 进程关系

9.1 引言 本章将更详尽地说明进程组以及POSIX.1引入的会话的概念.还将介绍登陆shell(登录时所调用的)和所有从登陆shell启动的进程之间的关系. 9.1 终端登陆 系统管理员创建通常名为/etc/ttys的文件,其中每个终端设备都有一行,每一行说明设备名传递给getty程序的参数.当系统自举时,内核创建进程ID为1的进程,依旧是init进程.init进程使系统进入多用户状态.init进程读文件/etc/ttys,对每一个允许登陆的终端设备,init调用一次fork,所生成的子进程则

APUE学习笔记:第一章 UNUX基础知识

1.2 UNIX体系结构 从严格意义上,可将操作系统定义为一种软件(内核),它控制计算机硬件资源,提供程序运行环境.内核的接口被称为系统调用.公用函数库构建在系统调用接口之上,应用软件即可使用公用函数库,也可使用系统调用.shell是一种特殊的应用程序,它为运行其他应用程序提供了一个接口 从广义上,操作系统包括了内核和一些其他软件,这些软件使得计算机能够发挥作用,并给予计算机以独有的特性(软件包括系统实用程序,应用软件,shell以及公用函数库等) 1.3  shell shell是一个命令行解

APUE学习笔记:第二章 UNIX标准化及实现

2.2UNIX标准化 2.2.1 ISO C 国际标准化组织(International Organization for Standardization,ISO) 国际电子技术委员会(International Electrotechnical Commission,IEC) ISO C标准的意图是提供C程序的可移植性,使其能适合于大量不同的操作系统,而不只是UNIX系统.此标准不仅定义了C程序设计语言的语法和语义,还定义了其标准库.因为所有现今的UNIX系统都提供C标准中定义的库例程,所以该

读书笔记-APUE第三版-(6)系统数据文件和信息

常见系统数据文件 下表列出了常见的系统数据文件及其查找函数. 以/etc/passwd文件为例,读取数据的程序基本框架如下: void get_pw_entry() { struct passwd *ptr; setpwent(); while ((ptr = getpwent()) != 0) { -- } endpwent(); return ptr; } 每个数据文件都提供了一个get方法返回文件下一个记录项. set方法充值当前位置到文件开始处. end方法关闭数据文件. 表格中的get

Unix环境高级编程学习笔记(三):标准I/O , 系统数据文件和信息

1 标准I/O函数不同于read,write函数,是其在流上进行操作, 当首次调用标准I/O函数时,系统会首先调用malloc,为流创造缓冲区, 2 fopen函数 #include<stdio.h> file * fopen(const char* pathname, const char * restrict name); 打开返回指针,出错返回NULL, type的取指有r(读),w(写),a(追加),r+/w+(读+写),a+(读+写+追加) int fclose(file* fp)

(四) 一起学 APUE 之 系统数据文件和信息

. . . . . 目录 (一) 一起学 APUE 之 标准 IO (二) 一起学 APUE 之 文件 IO (三) 一起学 APUE 之 文件和目录 (四) 一起学 APUE 之 系统数据文件和信息 1.getpwnam(3).getpwuid(3) 1 getpwnam, getpwuid - get password file entry 2 3 #include <sys/types.h> 4 #include <pwd.h> 5 6 struct passwd *getp