Unix系统数据文件和信息 - APUE


本文内容来源于Unix高级编程,欢迎评论。

口令文件 /etc/passwd

passwd结构包含用户名、密码、用户ID、组ID等信息,Linux在该结构中定义了下列结构:

char *pw_name;      
char *pw_passwd;    
uid_t pw_uid;       
gid_t pw_gid;
char *pw_gecos; //注释字段
char *pw_dir;
char *pw_shell;

如下函数通过uid或用户名来获取用户的passwd信息:

#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
                        // 成功,返回指针;出错,返回NULL

下列函数可以用来查看整个口令文件:

#include <pwd.h>
struct passwd *getpwent(void);
                        // 成功,返回口令文件下一个记录项指针;若出错或到达文件尾,返回NULL
void setpwen();         // 将getpwent指向口令文件开头
void endpwent();        // 关闭相关的文件

阴影口令 /etc/shadow

阴影口令包含了加密的用户登陆信息,相关结构体为struct spwd,包含如下成员:

struct spwd {
    char *sp_namp;		        // Login name.  
    char *sp_pwdp;		        // Encrypted password.  
    long int sp_lstchg;	   	    // Date of last change.  
    long int sp_min;		    // Minimum number of days between changes.  
    long int sp_max;		    // Maximum number of days between changes.  
    long int sp_warn;		    // Number of days to warn user to change the password.  
    long int sp_inact;		    // Number of days the account may be inactive.  
    long int sp_expire;		    // Number of days since 1970-01-01 until account expires.  
    unsigned long int sp_flag;	// Reserved.  
};

与口令文件类似,下列函数可用于访问阴影口令文件:

#include <shadow.h>
struct spwd *getspent (void);
struct spwd *getspnam (const char *__name);

void setspent (void);
void endspent (void);

组文件 /etc/group

组文件结构包含如下字段:

struct group{
    char *gr_name;		    // Group name.	
    char *gr_passwd;		// Password.	
    __gid_t gr_gid;		    // Group ID.	
    char **gr_mem;		    // Member list.	
};

其中gr_mem是一个指针数组,每一项都指向一个属于该组的用户名。与口令文件类似,下列函数可以用来获取组相关信息:

struct group *getgrgid (__gid_t __gid);
struct group *getgrnam (const char *__name);

void setgrent (void);
void endgrent (void);
struct group *getgrent (void);

附属组 ID

附属组,即每个进程不仅可以属于口令文件中规定的组,也可以属于最多16个其它组。下列函数用来获取附属组ID:

#include <unistd.h>
#include <grp.h>
int getgroups(int gidsetsize, git_t grouplist[]);
                        // 返回附属组ID数量,出错返回-1
int setgroups(int ngroups, const gid_t grouplist[]);
int initgroups(const char *username, gid_t basegid);
                        // 成功,返回0,出错,返回-1

其它数据文件

说明 数据文件 头文件 结构
口令 passwd pwd.h passwd
group grp.h group
阴影 shadow shadow.h spwd
主机 hosts netdb.h hostent
网络 networks netdb.h netent
协议 protocols netdb.h protoent
服务 services netdb.h servent

登陆账户记录

参考utmp(5),相关头文件为utmp.h,相关记录存放在/var/run/utmp/var/log/wtmp中。

系统标识

utsname结构中包含相关信息。

struct utsname{
    char sysname[_UTSNAME_SYSNAME_LENGTH];      // Name of the implementation of the operating system.  
    char nodename[_UTSNAME_NODENAME_LENGTH];    // Name of this node on the network.  
    char release[_UTSNAME_RELEASE_LENGTH];      // Current release level of this implementation.  
    char version[_UTSNAME_VERSION_LENGTH];      // Current version level of this release.  
    char machine[_UTSNAME_MACHINE_LENGTH];      // Name of the hardware type the system is running on.  
    char domainname[_UTSNAME_DOMAIN_LENGTH];
};

下列函数提供了获取系统标识的相关数据:

#include <sys/utsname.h>
int uname(struct utsname *name);
                        // 成功,返回非负值,出错,返回-1
#include <unistd.h>
int gethostname(char *name, int namelen);
                        // 成功,返回0,出错,返回-1

时间和日期

time函数返回当前时间

#include <time.h>
time_t time(time_t *calptr);
                        // 成功,返回时间,出错,返回-1
int clock_gettime(clockid_t clock_id, struct timespec *tsp);    // 获取指定时钟的时间
                        // 成功,返回0,出错,返回-1

下列函数用于转换时间,并将结果存放到tm结构中:

struct tm{
    int tm_sec;			    // Seconds.	[0-60] (1 leap second) 
    int tm_min;			    // Minutes.	[0-59] 
    int tm_hour;			    // Hours.	[0-23] 
    int tm_mday;			    // Day.		[1-31] 
    int tm_mon;			    // Month.	[0-11] 
    int tm_year;			    // Year	- 1900.  
    int tm_wday;			    // Day of week.	[0-6] 
    int tm_yday;			    // Days in year.[0-365]	
    int tm_isdst;			    // DST.		[-1/0/1]
    long int tm_gmtoff;		// Seconds east of UTC.  
    const char *tm_zone;		// Timezone abbreviation.
};

#include <time.h>
struct tm *gmtime (const time_t *__timer);      // UTC(Universal Time of Coordinate)协调统一时间
struct tm *localtime (const time_t *__timer);   // 本地时间
                        // 成功,返回指针,出错,返回NULL
time_t mktime(struct tm *tmptr);                // 将年月日等参数转换为time_t
                        // 成功,返回time_t,出错,返回-1

函数strftime类似于printf,通过参数来控制产生的字符串,参考strftime(3),而strptime将字符串转换为时间:

#include <time.h>
size_t strftime (char *__restrict __s, size_t __maxsize, const char *__restrict __format, const struct tm *__restrict __tp);
size_t strftime_l (char *__restrict __s, size_t __maxsize, const char *__restrict __format, const struct tm *__restrict __tp, locale_t __loc)
                        // 成功,返回存入数组的字符数,否则,返回0

// Parse S according to FORMAT and store binary time information in TP.
// The return value is a pointer to the first unparsed character in S.
// 根据 fmt 解析 s,并将结果存入 tp中 
char *strptime (const char *__restrict __s, const char *__restrict __fmt, struct tm *__tp);
                        // 成功,返回指向第一个未被解析字符的指针,否则,返回NULL

Linux设置时区

通过如下命令可以设置时区,其中GMT+6可以替换为其它的,另外也可以设置为/usr/share/zoneinfo/中的其它文件

sudo unlink /etc/localtime 
sudo ln -s /usr/share/zoneinfo/Etc/GMT+6 /etc/localtime
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章