System data files and information

Source: Internet
Author: User
Tags posix time and date

The system needs to use a large number of system data files, wherein all UNIX systems are: Password files, group files, most systems provide: login account records, system identification, time and date routines, At the same time, there are some other common system data files such as: BSD network software has a record of the services provided by the network server data Files (/etc/services), recording protocol residence data Files (etc/protocols) and so on.

1. password file

The password file includes the following fields, which are contained in the PASSWD structure defined in <pwd.h>:

struct passwd

char* Pw_name; User name

char* pw_passwd; Encrypt password

uid_t Pw_uid; Numeric User ID

gid_t Pw_gid; Numeric Group ID

char* Pw_gecos; Comment Field

char* Pw_dir; Initial working directory

char* Pw_shell; Initial shell (user program)

char* Pw_class; User Access Class

time_t Pw_change; Next Change Password time

time_t Pw_expire; Account Expiry time

};

(1) posix.1 defines only two functions to get the password file phase. Given a user login or numeric user ID, these two functions allow us to find the relevant item

#include <pwd.h>struct passwd *getpwuid (uid_t uid);  struct passwd *getpwnam (char *name);  Both success returns a pointer, failure returns NULL      

The Getpwuid function is used by the LS program, which maps the numeric user ID values in the I node to a user login name, and the Getpwnam function is used by the login (1) function when typing the login name.

(2) The following three functions can be used for the program to view the entire password file

#include <pwd.h>struct passwd *getpwent (void);  The pointer returned successfully, either failed or the end of the file returned null. void Setpwent (void);  void Endpwent (void);         

When calling Getpwent, it returns the next record entry in the password file, the function setpwent the file used by it, Endpwent closes the files, and after using getpwent to check the password file, make sure to call Endpwent to close the files.

2. Shadow password

To make it harder to get raw data (encrypted passwords), some systems have encrypted passwords in a file that is often called a shadow password, which must contain at least a user name and an encrypted password. Other password-related information also exists in the file, see the following table:

The domain of the file/etc/shadow
Describe struct SPWD Member
User Login Name Char *sp_namp
Encrypt password Char *SP_PWDP
Number of days since the epoch when the password was last modified int Sp_lstchg
Until you change the number of days allowed int Sp_min
The number of days before you need to change int Sp_max
Warning password expires in days int Sp_warn
Number of days before the account expires int Sp_inact
Number of days from epoch since the account expires int Sp_expire
Keep unsigned int sp_flag

Similar to a set of functions that access a password file, there is another set of functions that can be used to access the shadow password file

Similar to a set of functions that access a password file, there is another set of functions that can be used to access the shadow password file

#include <shadow.h>struct spwd *getspnam (char *name);  struct spwd *getspent (void);  both returns a pointer when both are successful, otherwise null is returned. void Setspent (void);  void Endspent (void);            

3. Group files

1). You can view the group name or group ID with the following two functions defined by posix.1

#include <grp.h>struct Group *Getgrgid (gid_t gid);  struct Group *getgrnam (char *name);  both of the successes return pointers, otherwise null is returned. 

Like the password file function, these functions typically return a pointer to a static variable that is overridden each time it is called.

2). If you need to find the entire group file, you need to use a few other functions, the following three functions are similar to the three functions for the password file:

#include <grp.h>struct group *getgrent (void);  a successful return pointer, either failed or the end of the file returns NULL. void Setgrent (void);  void Endgrent (void);         

4. Additional Group ID

The advantage of using additional group IDs is that we no longer need to explicitly change the group

#include <unistd.h>int GetGroups (IntGidsetsize, gid_t grouplist[]);//The number of supplemental group IDs was returned successfully, and the error returned-1.#inlcude <grp.h>/*On Linux*/#inlcude <unistd.h>/* on FreeBSD, Mac OS X, and Solaris */int setgroups (const gid_t grouplist[]); #inlcude <grp.h> /* on Linux and Solaris */ #inlcude <unistd.h> /* on FreeBSD and Mac OS X */ int initgroups (const  Char *username, gid_t Basegid); // Both return 0 for success, or 1.  

5. Other data files

There are at least three functions for each data file

1). Get function, read the next record, open the file if necessary. These functions usually return a pointer to a struct body. A null pointer is returned when the end of the file is reached. Most get functions return a pointer to a static struct, so we always need to copy it if we want to save it.

2). Set function, if the file is not open, open the file and return the file. This function is used when we know we want to start again from the beginning of the file.

3). End key to close the data file. As we mentioned earlier, we always need to call it when we're done, to close all the files.

The following table has the get, set, and end functions for all data files:

Similar functions for accessing system data files
Describe Data files Header file Structural body Supplementary keyword Lookup function
Password /etc/passwd <pwd.h> passwd Getpwnam, Getpwuid
Group /etc/group <grp.h> Group Getgrnam, Getgrgid
Shadow /etc/shadow <shadow.h> Spwd Getspanam
Host /etc/hosts/ <netdb.h> Hostent gethostbyname, gethostbyaddr
Internet /etc/networks <netdb.h> Netent Getnetbyname, GETNETBYADDR
Agreement /etc/protocols <netdb.h> Protoent Getprotobyname, Getprotobynumber
Service /etc/services <netdb.h> Servent Getservbyname, Getservbyport

6. Login Account Record

The two data files provided by most Unix systems are: utmp files, which record the individual users of the system currently logged on, and the Wtmp file, which records all log-in and logoff events. In V7, a record type is written by these two files, a binary record that is consistent with the following structure

struct utmp {  char ut_line[/* Char ut_name[/* ** *     

7. System identification

1). POSIX.1 defines the uname function, which returns information about the current host and operating system

#include <sys/utsname.h>int uname (struct utsname *name);  a non-negative value was returned successfully, and the failure returned-1. 

A UTSNAME structure:

structUtsname {Char sysname[];/* name of the operating system */char nodename[]; /* name of hits node */ Span style= "color: #0000ff;" >char release[]; /* current release of operating system */char version[]; /* current version of this release */char machine[]; /* name of hardware type };  

2). The BSD-based system provides the GetHostName function to return only the host's name. This name is usually the host name on the TCP/IP network.

#include <unistd.h>int gethostname (int namelen);  successful return 0, Failure returns-1. 

The Namelen parameter specifies the length of the name buffer. If sufficient space is provided, the returned string ends with a null. If you do not provide enough space, you do not specify whether the string ends with a null.

8. Time and Date routines

1). The time function returns the current times and dates.

#include <time.h>time_t time (time_t *calptr);  the time value is returned successfully, and the error returns-1. 

2). The Gettimeofday function provides better accuracy than the time function (accurate to microseconds)

#include <sys/time.h>int gettimeofday (void *restrict tzp);  return value: Always returns 0. 

Timeval Structural Body

struct timeval {  time_t tv_sec;  /**/  /**/};

3). LocalTime and Gmtime

#include <time.h>struct TM *gmtime (const time_t *calptr);  struct TM *localtime (const time_t *calptr);  both return a pointer to the decomposition time. 

LocalTime and Gmtime These two functions convert the calendar time to a structure TM called decomposition time (broken-down times):

struct TM {/*A broken-down time*/int tm_sec;/*Seconds after the minute: [0-60]*/int tm_min;/*Minutes after the hour: [0-59]*/int tm_hour;/*Hours after midnight:[0-23]*/int tm_mday;/*Day of the Month: [1-31]*/int Tm_mon;/* months since January: [0-11] */int tm_year; /* years since 1900 */ Span style= "color: #0000ff;" >int Tm_wday; /* days since Sunday: [0-6] */int Tm_yday; /* days since January 1: [0-365] */int tm_isdst; /* Daylight saving Time flag: <0, 0, >0 */};  

The difference between localtime and Gmtime is that the first converts the calendar time to local time, depending on the time zone and the daylight saving date flag, which converts the calendar time to a decomposition time expressed as UTC.

4). The function mktime accepts a decomposition time represented as a local time and converts it to a time_t value

#include <time.h>time_t mktime (struct TM *tmptr);  return Calendar time successfully, error returned-1    

5). Asctime and CTime functions produce familiar 26-byte strings

#include <time.h>char *asctime (struct TM *tmptr);  Char *ctime (const time_t *calptr);  both return a null-terminated string. 

6). Strftime, is the most complex. It is a function that is similar to printf for time values

#include <time.h>size_t strftime (struct TM *restrict tmptr);  returns 0 if the space is sufficient to return the number of characters stored in the array. 

The final argument is the time value required for the format, specified by a pointer to the decomposition time value. The formatted structure is stored in a buf array of size maxsize. If the size of the result including terminating null can be put into this buffer, then the function returns the number of characters stored in the BUF, excluding terminating null. Otherwise, the function returns 0.

Transferred from: http://www.cnblogs.com/biyeymyhjob/archive/2012/08/03/2621445.html

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.