Apue Reading Notes-sixth chapter system data files and information

Source: Internet
Author: User
Tags session id

I saw it yesterday, so let's look at the sixth chapter today. It is not very important to feel the contents of the sixth chapter. Just take a look.

6.2 Password File

The password file is actually the passwd file in the/etc folder, but in security considerations, we cannot read it directly. is to protect it directly by restricting permissions, passwd file specific permissions are as follows:

-rw-r--r--1 root root

You can see that only the root user has read and write permissions, and that the user with root is the only reader with access to other users.

However, in order to solve these problems, Linux has given a series of data structures and functions to help us manipulate the password file, first of all, the key data structure, defined in the/include/pwd.h

struct passwd{  char *pw_name;/* Username.  */  char *pw_passwd;/* Password.  */  __uid_t pw_uid;/* User ID.  */  __gid_t pw_gid;/* Group ID.  */  char *pw_gecos;/* Real name.  */  char *pw_dir;/* Home directory.  */  char *pw_shell;/* shell program  . */};

Given the user login and numeric user ID, the passwd structure can be obtained through the following two functions, which contain the information we need.

#include <pwd.h>extern struct passwd *getpwuid (__uid_t __uid), extern struct passwd *getpwnam (const char *__name);

The Getpwuid function is used by the LS program. The Getpwnam function is used by the login program. The passwd structure is usually a static variable inside a function, and its contents are rewritten whenever any of the related functions are called.

The above two functions can only view the password information of one user, but cannot obtain this information if they do not know the login name or numeric ID of another user. Therefore, the following three functions are given in Linux to view the entire password file.

#include <pwd.h>extern struct passwd *getpwent (void); If successful, returns a pointer to the next record entry in the password file. The first time it is called, it opens the individual files used. extern void setpwent (void); Back to the beginning of the password file extern void endpwent (void); Close Password file

6.3 section of the shadow password file feeling and the content of the password file does not have much of a gap, in this first not in-depth study, and later encountered.

6.4 Set of files

Still the same idea, if the user program wants to access the group information, although the information is stored in the/etc/group, but also because of the restrictions on the permissions of the general user can not directly access the problem. The method given in Linux is accessed through the relevant functions and data structures, first of all the key data structures.

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

This data structure can be obtained by group name and numeric ID.

#include <grp.h>extern struct Group *getgrgid (__gid_t __gid); extern struct Group *getgrnam (const char *__name);

The following three functions are required to search the entire password file, and the idea is the same.

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

6.5 Subsidiary groups

One reason to introduce a subordinate group is that one user participates in multiple projects, and therefore it is also necessary to belong to more than one group at a time. To address these issues, 4.2BSD introduces the concept of a satellite group, which no longer simply belongs to a group, or can belong to as many as 16 additional groups.

The user program can get and set the satellite group ID through the following functions.

extern int getgrouplist (const char *__user, __gid_t __group, __gid_t *__groups, int *__ngroups); extern int SE Tgroups (size_t __n, const __gid_t *__groups) __throw;extern int initgroups (const char *__user, __gid_t __group);

Where setgroups requires superuser privilege calls, and because initgroups needs to call Setgroups, initgroups can be called only by super users.

6.7 Other data files

In addition to the password and group files mentioned above, Linux has several files with similar concepts. The book gives a table to summarize the above content.

Description Data files Header file Structure Additional Subtraction search function
Password /etc/passwd <pwd.h> passwd Getpwnam/getpwuid
Group /etc/group <grp.h> Group Getgrnam/getgrgid
Shadow password /etc/shadow <shadow.h> Spwd Getspnam
Host /etc/hosts <netdb.h> Hostnet Getnameinfo/getaddrinfo
Internet /etc/networks <netdb.h> Netent Getnetbyname/getnetbyaddr
Agreement /etc/protocols <netdb.h> Protonet Getprotobyname/getprotobynumber
Service /etc/services <netdb.h> Servent Getserbyname/getserbyport

6.8 Login Account Record

Linux records individual users who are currently logged on to the system through "/var/run/utmp", and tracks individual logon and logoff events through the "/var/log/wtmp" file. The above two files are binary files and cannot be opened directly.

Linux uses the following functions and data structures to modify the above two files. First, take a look at the key data structures, defined in/usr/include/x86_64-linux-gnu/bits/utmp.h

struct utmp{short int ut_type;/* type of login.  */pid_t ut_pid;/* process ID of login process.  */char ut_line[ut_linesize];/* devicename.  */char ut_id[4];/* Inittab ID.  */char ut_user[ut_namesize];/* Username.  */char ut_host[ut_hostsize];/* Hostname for remote login.  */struct Exit_status ut_exit;/* exit status of a process marked as dead_process.  *//* the Ut_session and Ut_tv fields must is the same size when compiled 32-and 64-bit.  This allows data files and the shared memory to be shared between 32-and 64-bit applications.  */#ifdef __wordsize_time64_compat32 int32_t ut_session;/* session ID, used for windowing.  */struct {int32_t tv_sec;/* Seconds.  */int32_t tv_usec;/* microseconds.  */} ut_tv;/* time entry was made.  */#else Long int ut_session;/* session ID, used for windowing.  */struct Timeval ut_tv;/* time entry was made.  */#endif int32_t ut_addr_v6[4];/* Internet address of remote host. */char __glibc_reserved[20];/* reserved foR future use. */};

Take a look at the related functions:

extern void Login (const struct UTMP *__entry) __throw;

When logged in, the login program fills in the type structure and writes it to the Utmp file, and also adds it to the wtmp file. This can also be verified through the parameters of the login function.

When you log off, the init process erases the corresponding record in the Utmp file and adds a new record to the Wtmp file.

6.9 System Identification

POSIX.1 defines the uname function, which returns information about the host and the operating system. The definition is located in/usr/include/x86_64-linux-gnu/sys/utsname.h.

extern int uname (struct utsname *__name) __throw; __name is the input parameter and the output parameter. If the program successfully returns a non-negative value, return-1 if an error occurs.

The parameters are defined as follows:

struct Utsname  {/* Name of the implementation of the    operating system.  */    char sysname[_utsname_sysname_length];    /* Name of this node on the network.  */    char nodename[_utsname_nodename_length];    /* Current release level of this implementation.  */    char release[_utsname_release_length];    /* Current version level of this release.  */    char version[_utsname_version_length];    /* Name of the hardware type the system is running on.  */    char machine[_utsname_machine_length]; #if _utsname_domain_length-0/* Name of the DOMAIN of this    node on th E Network.  */# ifdef __USE_GNU    Char domainname[_utsname_domain_length];# else    Char __domainname[_utsname_domain_ length];# Endif#endif  };

The information in the Utsname structure is usually printed using the uname command. The results of uname on my machine are very easy to return.

Linux

That's it, nothing more.

There is also a function that returns the host name, which is usually the name of the host on the TCP/IP network.

extern int GetHostName (char *__name, size_t __len) __throw __nonnull ((1));

Where name is the input parameter and the output parameter.

The hostname command can be used to get and set the host name. The hostname is usually set at System bootstrap, which is taken from a boot file by/ETC/RC or init.

6.10 Time and date routines

The basic time service provided by the UNIX kernel is the number of seconds that have elapsed since the time of the coordinated World (coordinated Universal TIME,UTC) January 1, 1970 00:00:00 AD. This number of seconds is obtained through the following functions:

#include <time.h>extern time_t time (time_t *__timer) __throw;

This number of seconds can also be obtained through clock_gettime, but with a higher time accuracy:

#include <time.h>extern int clock_gettime (clockid_t __clock_id, struct timespec *__tp) __throw;

The results of time and Clock_gettime functions can be converted directly to each other.

The parameters of the time_t type can be transformed into the year, month, day, hour, minute, second, and Sunday decomposition of the harmonized time by the Gmtime function. The time in this format is represented by the following structure.

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]*/# ifdef__use_misc  Long int tm_gmtoff;/* Seconds east of UTC.  */  const char *tm_zone;/* Timezone abbreviation.  */# else  long int __tm_gmtoff;/* Seconds east of UTC.  */  const char *__tm_zone;/* Timezone abbreviation.  */# endif};

This type needs to be converted to a string pattern by strftime and output.

extern size_t strftime (char *__restrict __s, size_t __maxsize,const char *__restrict __format,const struct TM *__restrict __TP) __throw;

A formatted time string can be converted to struct struct TM by the following function.

extern char *strptime (const char *__restrict __s,       const char *__restrict __fmt, struct TM *__tp)     __throw;

struct TM structure can be converted to time_t type parameter by Mktime function.

extern time_t mktime (struct TM *__tp) __throw;



Apue Reading Notes-sixth chapter system data files and information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.