4.5 User Information This article for the Linux Environment, code is downloaded in the Linux environment code. In addition to the famous INIT program, all Linux programs are initiated by other programs or users. The boot process is completed by starting a user-level program init, after the kernel has booted itself (it has been loaded into memory, started running, and has initialized all device drivers and data structures). So Init is always the first process (its process number is always 1).
When a user logs into a Linux system, the user has a user name and password, and once the user name and password are authenticated, the user can enter a shell. From an internal mechanism, the user also has a unique UID for the user identifier,
every program that runs Linux actually runs on the name of a user, so there is an associated UID.
Programs can be set up so that they run as if they were started by another user, and when a program's suid bit is placed, it runs as if it were initiated by the owner of the executable file. When the SU command is executed, the program runs as if it were initiated by a superuser, which then verifies the user's access rights, changes the UID to the target account's UID value, and makes the login shell for that account.
The UID is-uid_t by its own type, usually a small integer.
#include <sys/types.h>
#include <unistd.h>
uid_t getuid (void);
char* getlogin (void);
The Getuid function returns the UID associated with the program, which is usually the UID of the user who initiates the program.
The GetLogin function returns the logon name associated with the current user.
System file/etc/passwd contains a database of user accounts. It consists of rows with one user per line, including user name, encrypted password, user identifier (UID), group identifier (GID), full name, home directory, and default shell. For example
Liu:x:1000:1000:liu:/home/liu:/bin/bash
People define a set of functions that provide a standard and efficient programming interface for obtaining user information:
#include <sys/type.h>
#include <pwd.h>
struct passwd *getpwuid (uid_t uid);
struct passwd *getpwnam (const char *name);
The password database structure passwd defined in the header file Pwd.h, which contains the following members:
PASSWD member Description
char* pw_name User Login name
uid_t pw_uid UID Number
gid_t pw_gid GID number
char* Pw_dir User Home Directory
char* Pw_gecos User's full name
char* Pw_shell User Default shell
Both the Getpwuid and Getpwnam functions return a pointer to the PASSWD structure that corresponds to a user. This user is determined by the UID parameter of the Getpwuid or by the Getpwnam user login parameter. When they are out, they all return a null pointer.
Write the USER.C program to extract some user information from the password database.
4.6 Host information just as the program can look up user information, the program can also get details about the computer that is running it. This type of information is provided by the uname command.
Host information is useful in many cases, and it may be necessary to customize the program's behavior based on the name of the machine that the program runs on the network.
If the system has a network component installed, you can get its network name through the GetHostName function:
#include <unistd.h>
int GetHostName (char* name, size_t Namelen);
The GetHostName function writes the machine's network name to the name string, which is at least namelen characters in length. On success, returns 0, otherwise returns-1.
More detailed information about the host can be obtained by uname system calls:
#include <sys/utsname.h>
int uname (struct ustname* name);
The Uname function writes the host information to the structure that the name parameter points to.
Write the program hostget.c put forward some host information
4.7 Logs many applications need to record their activity, and system programs often need to write messages to the console or log files. These messages may indicate errors, warnings, or general information about the state of the system.
Usually these log information is recorded in the system files, and these system files are stored in a directory dedicated to this purpose, possibly/usr/adm or/var/log directory.
Although system messages are formatted and stored differently, the way messages are generated is standard. The UNIX specification provides an interface for generating log information for all programs through the Syslog function:
#include <syslog.h>
void syslog (int priority, const char *message, arguments ...);
The Syslog function sends a log message to the system's log facility. Each message has a priority parameter, which is a bitwise OR of a severity level with a facility value.
Writing a program syslog.c
You can use the Setlongmask function to set a log mask that controls the priority of the log information. Subsequent syslog calls that have precedence placed in the log mask are discarded.
Writing a program LOGMASK.C
LOGMASK.C also uses the Getpid function, which is defined as Getppid:
#include <sys/types.h>
#include <unistd.h>
pid_t getpid (void);
pid_t getppid (void);
This
two functions return the process identifier PID of the calling process and the parent process of the calling process, respectively。
4.8 Resources and restricting programs running on Linux systems are affected by resource constraints. They can be physical limitations of hardware (such as memory), restrictions on system policies (such as CPU time allowed), or specific implementations (such as the length of an integer or the maximum number of characters allowed in a file name).
Header file Sys/resource.h provides a definition of resource operation, including the function of the length of the program, the execution priority, and the file resources to restrict queries and settings:
#include <sys/resource.h>
int getpriority (int which, id_t who);
int setpriority (int which, id_t who, int.);
int getrlimit (int resource, struct rlimit *r_limit);
int setrlimit (int resource, const struct RLIMIT *r_limit);
int getrusage (int who, struct rusage* r_usage);
id_t is an integer type that is used for user and group identifiers, and the rusage structure defined in header file sys/resource.h is used to determine how much CPU time the current program has consumed, and it contains at least two members:
Rusage member Description
User time used by struct Timeval ru_utime
The system time used by the struct timeval ru_stime
One
CPU time consumed by the program can be divided into user time(Time spent by the program executing its own designation) and
System Time(The amount of time the operating system spends executing a program, that is, the time it takes for a system call or other system function to perform an input-output operation).
Each running program has a priority associated with it, and the higher the priority, the more CPU available time is allocated to the program.
A normal user can only lower the priority of its program, but not the higher.
Applications can use the getpriority and SetPriority functions to determine and change their precedence.
Processes that are checked or changed by a priority function can be determined by a process identifier, a group identifier, or a user.
The which parameter specifies how to treat the WHO parameter
which parameter description
Prio_process who parameter is the process identifier
Prio_pgrp who parameter is a process group
Prio_user who parameter is a user identifier
Therefore, to determine the priority of the current process, you can call the
proprity = GetPriority (prio_process, Getpid ());
the default priority is 0, and the positive priority is used for background services, which are only executed when no other higher-priority tasks are ready to run. A negative priority causes a program to run more frequently and to get more CPU time available. The valid range for the priority is -20~20. The higher the value, the lower the priority level.
Writing a program LIMITS.C
4.9 Summary In this chapter, we understand the Linux environment, and study the conditions of the program running, learning command-line parameters and environment variables, which can be used to change the default behavior of the program, and provide useful program options
It also describes how the program uses library functions to handle date and time values, and to obtain information about itself, the user, and the computer on which it runs.
Because Linux programs often share valuable resources on the host, they also introduce how to identify and manage resources.
Linux Programming--linux Environment (fourth chapter)