• 沒有找到結果。

System Data Files and Info

N/A
N/A
Protected

Academic year: 2022

Share "System Data Files and Info"

Copied!
10
0
0

加載中.... (立即查看全文)

全文

(1)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Contents

1. Preface/Introduction

2. Standardization and Implementation 3. File I/O

4. Standard I/O Library 5. Files and Directories

6. System Data Files and Information 7. Environment of a Unix Process 8. Process Control

9. Signals

10.Inter-process Communication

System Data Files and Info

ƒ Objective

ƒ System Data Files

ƒE.g., /etc/passwd and /etc/group

ƒ Portable Interfaces to System Data Files

ƒFormats other than ASCII text files

ƒ /etc/passwd – local machine or NIS DB

ƒ User database in POSIX.1 <pwd.h> (Fig.6.1)

ƒ root:jhexh38fhajck:0:1:Super-User:/root:/bin/tcsh

ƒ Login-name, encrypted passwd, numeric user-ID, numeric group ID, comment, home dir, shell program

(2)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

System Data Files and Info

ƒ /etc/passwd (continue)

ƒ Superuser root – UID = 0

ƒ One-way encryption algorithm for passwd Æ 13 printable chars from 64- char sets [a-zA-Z0-9./].

ƒ nobody:*:65534:65534:SunOS 4.x Nobody:/

ƒ /bin/sh for default

ƒ Command finger

System Data Files and Info

#include <sys/types.h>

#include <pwd.h>

struct passwd *getpwuid(uid_t uid);

struct passwd *getpwnam(const char

*name);

ƒ getpwuid() used by Command ls

ƒ getpwnam() used by the login program

ƒ Both use a static variable for returning

ƒ POSIX.1

(3)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

System Data Files and Info

#include <sys/types.h>

#include <pwd.h>

struct passwd *getpwent(void);

void setpwent(void);

void endpwent(void);

ƒ No order in the returned pwd entries.

ƒ setpwent()/endpwent rewind/close these files.

ƒ Non-POSIX.1

ƒ Program 6.1 – Page 148

ƒ getpwnam

System Data Files and Info

ƒ /etc/shadow – shadow passwd file

ƒ /etc/passwd

ƒroot:x:0:1:Super-User:/root:/bin/tcsh

ƒwith “x” indicated for passwd

ƒ Contents

ƒUsername, passwd, passwd aging

ƒ SVR4: /etc/shadow, 4.3+BSD: /etc/master.passwd

ƒReadable by set-usr-ID login/passwd programs

ƒ Rationale: avoid a brute force approach in

(4)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

System Data Files and Info

ƒ /etc/group – the group database

ƒ nuucp::9:root,nuucp

ƒ Figure 6.2 – Page 149

ƒgr_passwd not in POSIX.1 (in SVR4 &

4.3+BSD)

#include <sys/types.h>

#include <grp.h>

struct group *getgrgid(gid_t gid);

struct group *getgrnam(const char *name);

ƒ A static variable for returned values.

System Data Files and Info

#include <sys/types.h>

#include <grp.h>

struct group *getgrent(void);

void setgrent(void);

void endgrent(void);

ƒ setgrent() open (if not) and rewind the group file.

ƒ endgrent() close the group file.

(5)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

System Data Files and Info – Supplementary Group IDs

ƒ Introduction of supplementary group ID’s – 4.2BSD

ƒ newgrp is the way to change gid since Version 7

ƒ They all can be used to check for file access permissions

ƒ Optional in POSIX.1,

NGROUP_MAX (16 in common)

System Data Files and Info – Supplementary Group IDs

#include <sys/types.h>

#include <unistd.h>

int getgroups(int gidsetsize, gid_t grouplist[]);

int setgroups(int ngroups, const gid_t grouplist[]);

int initgroups(const char *usrname, gid_t basegid);

ƒ gidsetsize = 0 Æ only number is returned.

ƒ Only superusers can call setgroups() and initgroups() – SVR4&4.3BSD

(6)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

System Data Files and Info

ƒ BSD Networking Software

ƒ /etc/services – network services

ƒ /etc/protocols – protocols

ƒ /etc/networks – networks

ƒ General Principle to the Interfaces

ƒ A get function to read the next record

ƒ A set function to rewind the file

ƒ An end function to close the file

ƒ Keyed lookup functions if needed.

ƒ Figure 6.3 – Page 153

ƒRoutines for System File Access

Login Accounting

ƒ /etc/utmp Æ /var/adm/utmp in SVR4 (or /var/run/utmp in 4.3+BSD)

ƒ ut_line, ut_name, ut_time (in sec since Epoch)

ƒ Updated by the login program, erased by init

ƒ /etc/wtmp Æ /var/adm/wtmp in SVR4 (or /var/log/wtmp in 4.3+BSD)

ƒ Updated by the login and init programs, reboot

ƒ Related Commands: last, who, etc

> last | grep ktw

ktw pts/26 austin.csie.ntu. Fri Apr 12 18:22 still logged in ktw pts/5 pc210.ice.ntnu.e Thu Apr 11 10:25 - 10:36 (00:10)

ƒ /etc/?tmp entries: 20 bytes (Ver 7) Æ 350 bytes (SVR4)

(7)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

System Identification

#include <sys/utsname.h>

int uname(struct utsname *name);

struct utsname {

char sysname[9] /* name of OS */

char nodename[9]; /* name of the node */

char release[9]; /* current release of the OS */

char version[9]; /* current ver of the release */

char machine[9]; /* name of the HW type */ }

ƒ sysconf()

#include <sys/utsname.h>

int gethostname(char *name, int namelen);

ƒ Domain name of the host on a TCP/IP network – BSD systems

ƒ MAXHOSTNAMELEN in <sys/param.h>

System Identification

ƒ Commands:

hostname, sethostname (/etc/rc), uname

ƒ $ uname –a

SunOS ntucsa 5.6 Generic_105181-26 sun4u sparc sun4u

ƒ Nodename is not good to reference on a network.

ƒ Most versions of System V has this info compiled into the kernel when the kernel is built.

(8)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Time and Date Routines

ƒ Time Values

ƒ Calendar time

ƒIn seconds since the Epoch (00:00:00 January 1, 1970, Coordinated Universal Time, i.e., UTC)

ƒtype time_t

ƒ Remark: Times in Unix

ƒKeeping time in UTC

ƒAutomatic handling of conversions, such as daylight saving time

ƒKeeping of time and date as a single quantity.

Time and Date Routines

ƒ Time Values (continued)

ƒ Process time

ƒIn clock ticks (divided by CLK_TCK -

> secs)

ƒtype clock_t

ƒClock time, user/system CPU time

> time grep _POSIX_SOURCE */*.h >

/dev/null

0.25u 0.25s 0:03.51 14.2%

(9)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Time and Date Routines

#include <time.h>

time_t time(time_t *calptr);

ƒ As a func in BSD, call gettimeofday (1us)

ƒ Time initialization: settimeofday (BSD, 1us), stime (SVR4)

time_t kernel

time

(calendar time)

struct tm

mktime

localtime

gmtime

string

(broken-down time)

formatted string strftime

asctime ctime

Affected by env var TZ

Time and Date Routines

#include <time.h>

struct tm *gmtime(const time_t *calptr);

struct tm *localtime(const time_t *calptr);

struct tm { /* borken-down time */

int tm_sec; /* [0, 61], >= 59 for leap seconds*/

int tm_min; /* [0, 59] */

int tm_hour; /* [0, 23] */

int tm_mday; /* [1, 31] */

int tm_mon; /* [0, 11] */

int tm_year; /* years since 1900 */

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 (not available) */

};

ƒ localtime() Æ local time, gmtime() Æ

(10)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Time and Date Routines

#include <time.h>

time_t mktime(struct tm *tmptr);

char *asctime(const struct tm *tmptr);

char *ctime(const time_t *calptr);

size_t strftime(char *buf, size_t maxsize, const char *format, const struct tm *tmptr);

ƒ strftime() returns 0 if the size of the buf does not fit!

ƒ Figure 6.5 – Page 158 (Conversion Specifiers)

ƒ Tue Jan 14 19:40:30 MST 1992

ƒ Remark: env var TZ= Æ UTC is normally used for

參考文獻

相關文件

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005!.

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

Tei-Wei Kuo, Embedded System and Wireless Networking Lab, National Taiwan University.. Real-Time

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

* All rights reserved, Tei-Wei Kuo, National Taiwan University,

* All rights reserved, Tei-Wei Kuo, National Taiwan University,