170 likes | 245 Views
System Files and Process Environment. Password file Group file System identification Time Process environment. Password File. doej:x:293:105:John Doe:/home/doej:/bin/sh passwd struct char *pw_name char *pw_passwd uid_t pw_uid gid_t pw_gid char *pw_gecos char *pw_dir char *pw_shell.
E N D
System Files and Process Environment • Password file • Group file • System identification • Time • Process environment
Password File doej:x:293:105:John Doe:/home/doej:/bin/sh • passwd struct • char *pw_name • char *pw_passwd • uid_t pw_uid • gid_t pw_gid • char *pw_gecos • char *pw_dir • char *pw_shell
Password File struct passwd *getpwnam(const char *name); • Gets a pointer to a passwd struct based on the login name struct passwd *getpwuid(uid_t uid); • Gets a pointer to a passwd struct based on the user ID • Both return a pointer if OK and NULL on error • Requires <pwd.h>
Shadow Passwords • Encrypted passwords kept in separate file /etc/shadow • Minimally contains the username and encrypted password • Password field of /etc/passwd replaced with ‘x’ • Only root can open and read the shadow file
Group File • Located in /etc/group • struct group • char *gr_name • char *gr_passwd • int gr_gid • char **gr_mem
Group File struct group *getgrnam(const char *name); • Gets a pointer to a group struct based on group name struct group *getgrgid(gid_t gid); • Gets a pointer to a group struct based on group ID
System Identification int uname(struct utsname *buf); • Stores system information in the structure pointed to by buf • char sysname[] • char nodename[] • char release[] • car version[] • char machine[]
System Identification int gethostname(char *name, size_t len); • Copies hostname into the buffer of size len pointed to by name. • If the buffer is not large enough, hostname is truncated and may not be null terminated • Returns 0 on success or -1 on error
Date and Time time_t time(time_t *t); • Stores an instance of the time_t at the location pointed to by t • Holds both time and date • Returns a time_t value if ok, -1 on error struct tm *gmtime(const time_t *timep); struct tm *localtime(const time_t *timep); • These functions break down a raw time_t into a struct tm representation
Date and Time • struct tm • tm_sec 0-60 • tm_min 0-59 • tm_hour 0-23 • tm_mday 1-31 • tm_mon 0-11 • tm_year • tm_wday 0-6 • tm_yday 0-365 • tm_isdst <0, 0, >0
Date and Time • More functions • See man pages for • gettimeofday • settimeofday • mktime • asctime • ctime • strftime • See page 174 fig 6.8
Process Environment • main function • When a C program is started by the kernel, a start up routine is called before main is called. • Added in by the linker automatically • Start up routine is given as the starting address of the executable program
Process Termination • Normal • Return from main • A call to exit • A call to _exit or _Exit • Abnormal • A call to abort (SIGABRT) • Receiving a kill signal (SIGTERM)
Exit Functions • _exit and _Exit functions return to Kernel immediately • exit function performs cleanup and then returns to Kernel • Start up routine returns to the Kernel the value returned from mainexit(main(argc, argv));
Environment List • Name=value pairs • Passed to every process • Global variable environ. Pointer to an array of strings • Functions char *getenv(const char *name); int putenv(char *string); int setenv(const char *name, const char *value, int overwrite);
Memory Layout of a C Program • Text segment – read only machine instructions • Initialized data segment • Uninitialized data segment • Stack • Heap • See fig 7.6 on page 188 • size command can be used to find the sizes of the various sections
Allocate and Free Memory void *calloc(size_t nmemb, size_t size); void *malloc(size_t size); void *realloc(void *ptr, size_t size); void free(void *ptr); • calloc clears (sets to zero) the memory it allocates • realloc changes the size of the allocated memory (usually to allocate more)