分布式系统的中间件选型和日志模块

项目软件结构

  • 网络层
    • 多线程+reactor+one loop per thread+socket api
    • 同步/异步/阻塞/非阻塞
    • select/poll/epoll
    • 池化技术
    • IO模型
    • 网络数据包的收发过程
  • 业务层
    • 状态机/MVC模式
  • 缓存层
    • 为了存储过程的高效性,使用内存型数据库 - redis/memcached,其中redis可以持久化
  • 存储层
    • 关系型数据库 - mysql/oracle/sql server/maridb/mongodb

环境配置

主要是云服务器的购买、远程登录配置、vscode ssh remote配置,git配置,远程仓库配置。

日志组件

1
[时间戳][带颜色 日志级别][文件名][函数名][行号] - [日志msg]

组成部分

分为两个部分,前面的部分封装为一个日志头部,记为Message类。后面的具体信息内容拼接在头部之后即可。

  • 日志级别

1、重要程度;2、日志级别越细,打印的信息就越多,会对磁盘IO造成影响,影响性能。一般来说,有这些类别:info、debug、error、fatal。我们分别设置为白色、绿色、黄色、红色。

  • 文件名、函数名、行号可以用C语言内置的宏。
1
2
3
4
int main()
{
printf("[file:%s] [function:%s] [lineNumber:%d]\n", __FILE__, __FUNCTION__, __LINE__);
}

时间戳类Timestamp

1
2
3
4
5
6
7
8
classDiagram
class Timestamp{
-secondSinceEpoch : uint64_t
+TimeStamp()
+TimeStamp(uint64_t)
+ToString() string
+Now()
}

日志类Logger

消息类Message

由两大部分组成,一部分是消息头,显示信息发生的位置,二是消息体,是具体内容。

1
2
3
4
5
6
7
8
9
10
11
classDiagram
class Message{
-m_coloredHeader //带颜色的日志头 输出到terminal
-m_commonHeader //不带颜色的日志头 输出到logfile
-m_msg 日志消息
-formatFieldWithColor(COLOR_NUMBER, bool dark, string delimiter, string fieldName, bool colored) string
+Message(LOG_LEVEL, string fileName, string functionName, int lineNumber)
+FormatHeader(LOG_LEVEL, string fileName, string functionName, int lineNumber, bool colored) string
+ToString(bool colored) string
+operator<<(constT& t)
}

日志类Logger

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classDiagram
class Logger{
-m_level : LOG_LEVEL
-m_logToTerminal : bool
-m_logToFile : bool
-m_logFile : std::fstream
-Logger()
+getInstance()
+SetLogToTerminal(bool logToTerminal)
+GetLogToTerminal() bool
+SetLogToFile(bool logToFile)
+GetLogToFile(const string& logFileName) bool
+SetLogFile(const std::string & logFileName)
+OffLogFile()
+SetLogLevel(LOG_LEVEL level)
+GetLogLevel() LOG_LEVEL
+operator+=(constMessage& msg)
}

日志类的设计模式为单例模式,把构造函数私有化,提供生成对象的静态方法。

Linux errno

系统调用抛出的错误信息首先是一个错误码,如果要和我们自己实现的日志类结合的话,需要知道Linux errno的含义。

Linux中系统调用的错误都存储于errno中,errno由操作系统维护,存储就近发生的错误,即下一次的错误码会覆盖掉上一次的错误。

对errno的测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
int main()
{
int res = open("./a.txt", O_RDONLY, 0664);
if(-1 == res)
{
perror("open()");
}
}
-------------
/* 输出结果
* open(): No such file or directory
*/
说明,perror括号里的内容只是错误信息前面的开头部分,后面的部分才是刚刚发生的错误的errno所对应的错误信息内容。

看open的帮助手册,在return value部分,手册写到:
open(), openat(), and creat() return the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately).

可见,返回值和errno是相互独立的,返回值为-1说明此系统调用出错,而具体的错误类型需要用errno来标识,由内核标识。

perror和strerror

  • perror

手册中对perror的描述(man 3 perror)
The perror() function produces a message on standard error describing the last error encountered during a call to a system or library function.

关键信息:perror描述的是最后一次错误,是由系统调用产生的错误,将输出到标准错误输出中。

  • strerror

我们想要的效果是:通过errno找到对应的msg消息。这个工作可以通过strerror找到。

1
2
3
4
5
6
7
8
9
#include<string.h>
int main()
{
int res = open("./a.txt", O_RDONLY, 0664);
if(-1 == res)
{
LOG_ERROR << strerror(errno);
}
}

errno与Logger类的结合使用

1
2
3
4
5
6
7
8
int main()
{
int res = open("./a.txt", O_RDONLY, 0664);
if(-1 == res)
{
LOG_ERROR << strerror(errno);
}
}

系统中errno的位置及含义

查看系统中所有的errno所代表的含义,可以采用如下的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h> //for strerror()
//#include <errno.h>
int main()
{
for(int tmp = 0; tmp <=256; tmp++)
{
printf("errno: %2d\t%s\n", tmp, strerror(tmp));
}
return 0;
}
/* 最后的效果
* [2022/04/17 09:42:40][ERROR][logger.cc][main][61]: No such file or directory
*/

Linux中,在头文件 /usr/include/asm-generic/errno-base.h 对基础常用errno进行了宏定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
ifndef _ASM_GENERIC_ERRNO_BASE_H
#define _ASM_GENERIC_ERRNO_BASE_H

#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */

#endif

其他错误码定义在 /usr/include/asm-generic/errno.h中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef _ASM_GENERIC_ERRNO_H
#define _ASM_GENERIC_ERRNO_H

#include <asm-generic/errno-base.h>

#define EDEADLK 35 /* Resource deadlock would occur */
#define ENAMETOOLONG 36 /* File name too long */
#define ENOLCK 37 /* No record locks available */
#define ENOSYS 38 /* Function not implemented */
#define ENOTEMPTY 39 /* Directory not empty */
#define ELOOP 40 /* Too many symbolic links encountered */
#define EWOULDBLOCK EAGAIN /* Operation would block */
#define ENOMSG 42 /* No message of desired type */
#define EIDRM 43 /* Identifier removed */
#define ECHRNG 44 /* Channel number out of range */
#define EL2NSYNC 45 /* Level 2 not synchronized */
#define EL3HLT 46 /* Level 3 halted */
#define EL3RST 47 /* Level 3 reset */
#define ELNRNG 48 /* Link number out of range */
#define EUNATCH 49 /* Protocol driver not attached */
#define ENOCSI 50 /* No CSI structure available */
#define EL2HLT 51 /* Level 2 halted */
#define EBADE 52 /* Invalid exchange */
#define EBADR 53 /* Invalid request descriptor */
#define EXFULL 54 /* Exchange full */
#define ENOANO 55 /* No anode */
#define EBADRQC 56 /* Invalid request code */
#define EBADSLT 57 /* Invalid slot */

#define EDEADLOCK EDEADLK

#define EBFONT 59 /* Bad font file format */
#define ENOSTR 60 /* Device not a stream */
#define ENODATA 61 /* No data available */
#define ETIME 62 /* Timer expired */
#define ENOSR 63 /* Out of streams resources */
#define ENONET 64 /* Machine is not on the network */
#define ENOPKG 65 /* Package not installed */
#define EREMOTE 66 /* Object is remote */
#define ENOLINK 67 /* Link has been severed */
#define EADV 68 /* Advertise error */
#define ESRMNT 69 /* Srmount error */
#define ECOMM 70 /* Communication error on send */
#define EPROTO 71 /* Protocol error */
#define EMULTIHOP 72 /* Multihop attempted */
#define EDOTDOT 73 /* RFS specific error */
#define EBADMSG 74 /* Not a data message */
#define EOVERFLOW 75 /* Value too large for defined data type */
#define ENOTUNIQ 76 /* Name not unique on network */
#define EBADFD 77 /* File descriptor in bad state */
#define EREMCHG 78 /* Remote address changed */
#define ELIBACC 79 /* Can not access a needed shared library */
#define ELIBBAD 80 /* Accessing a corrupted shared library */
#define ELIBSCN 81 /* .lib section in a.out corrupted */
#define ELIBMAX 82 /* Attempting to link in too many shared libraries */
#define ELIBEXEC 83 /* Cannot exec a shared library directly */
#define EILSEQ 84 /* Illegal byte sequence */
#define ERESTART 85 /* Interrupted system call should be restarted */
#define ESTRPIPE 86 /* Streams pipe error */
#define EUSERS 87 /* Too many users */
#define ENOTSOCK 88 /* Socket operation on non-socket */
#define EDESTADDRREQ 89 /* Destination address required */
#define EMSGSIZE 90 /* Message too long */
#define EPROTOTYPE 91 /* Protocol wrong type for socket */
#define ENOPROTOOPT 92 /* Protocol not available */
#define EPROTONOSUPPORT 93 /* Protocol not supported */
#define ESOCKTNOSUPPORT 94 /* Socket type not supported */
#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */
#define EPFNOSUPPORT 96 /* Protocol family not supported */
#define EAFNOSUPPORT 97 /* Address family not supported by protocol */
#define EADDRINUSE 98 /* Address already in use */
#define EADDRNOTAVAIL 99 /* Cannot assign requested address */
#define ENETDOWN 100 /* Network is down */
#define ENETUNREACH 101 /* Network is unreachable */
#define ENETRESET 102 /* Network dropped connection because of reset */
#define ECONNABORTED 103 /* Software caused connection abort */
#define ECONNRESET 104 /* Connection reset by peer */
#define ENOBUFS 105 /* No buffer space available */
#define EISCONN 106 /* Transport endpoint is already connected */
#define ENOTCONN 107 /* Transport endpoint is not connected */
#define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */
#define ETOOMANYREFS 109 /* Too many references: cannot splice */
#define ETIMEDOUT 110 /* Connection timed out */
#define ECONNREFUSED 111 /* Connection refused */
#define EHOSTDOWN 112 /* Host is down */
#define EHOSTUNREACH 113 /* No route to host */
#define EALREADY 114 /* Operation already in progress */
#define EINPROGRESS 115 /* Operation now in progress */
#define ESTALE 116 /* Stale file handle */
#define EUCLEAN 117 /* Structure needs cleaning */
#define ENOTNAM 118 /* Not a XENIX named type file */
#define ENAVAIL 119 /* No XENIX semaphores available */
#define EISNAM 120 /* Is a named type file */
#define EREMOTEIO 121 /* Remote I/O error */
#define EDQUOT 122 /* Quota exceeded */

#define ENOMEDIUM 123 /* No medium found */
#define EMEDIUMTYPE 124 /* Wrong medium type */
#define ECANCELED 125 /* Operation Canceled */
#define ENOKEY 126 /* Required key not available */
#define EKEYEXPIRED 127 /* Key has expired */
#define EKEYREVOKED 128 /* Key has been revoked */
#define EKEYREJECTED 129 /* Key was rejected by service */

/* for robust mutexes */
#define EOWNERDEAD 130 /* Owner died */
#define ENOTRECOVERABLE 131 /* State not recoverable */

#define ERFKILL 132 /* Operation not possible due to RF-kill */
#define EHWPOISON 133 /* Memory page has hardware error */

#endif

TcpC/S

TcpClient/TcpServer类对资源的利用一定要遵循RAII规则。即构造时申请系统资源,析构时归还系统资源。

TcpClient

1
2
3
4
5
6
7
8
classDiagram
class TcpClient{
-m_sfd : int //serverfd
+TcpClient(const IpAddressPort &)
+~TcpClient()
+SendMsg(const string& msg) int
+RecvMsg() Msg
}

TcpServer

1
2
3
4
5
6
7
8
9
classDiagram
class TcpServer{
-m_lfd : int //listenfd
+TcpServer(const IpAddressPort&)
+~TcpServer()
+SendMsg(const string& msg) int
+RecvMsg() Msg
+GetLfd() int
}

RAII

RAII: Resource Acquisition is Initialization。

希望C++对象的生命周期和资源的生命周期是一致的。

堆内存:易失性

业务层 - MVC模式

View

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<string>
/* 接口类 */
class View
{
public:
virtual void process(int fd, std::string &data) = 0;
};
class LoginView : public View
{
public:
void process(int fd, std::string &data)
{

}
}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<unordered_map>
#include<view.h>
class Controller
{
private:
std::unordered_map<BIZTYPE, View*> bizTypeMap =

public:
Controller();
~Controller();
void ControllerProcess(int fd, std::string &data)
{
/* string -> json */
bizTypeMap[data["biztype"]]
}
};
1
2
3
4
5
# public.h
enum BIZTYPE
{

};

MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// mysqldb.h
#ifndef __MYSQLDB_H__
#define __MYSQLDB_H__
#include <mysql/mysql.h>
#include <string>
// 设计成一个线程安全的mysql单例类 因为mysql是可以被多线程看到的
// raii
class MySQLDB
{
private:
MYSQL* m_mysqlClient;
MySQLDB();
public:
static MySQLDB* GetInstance();
// mysql -h x.x.x.x -P 3307 -u root -p
void Connect(const std::string& ip,
unsigned short port,
const std::string& user,
const std::string& password,
const std::string& db);
int Query(std::string&& sql);
bool Insert(std::string&& sql);
~MySQLDB();
};
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
MySQLDB::MySQLDB()
{
m_mysqlClient = NULL;
}
MySQLDB::~MySQLDB()
{
if(m_mysqlClient != nullptr)
{
mysql_close(m_mysqlClient);
}
}
MySQLDB::MySQLDB* GetInstance()
{
static MySQLDB res;
return &res;
}
void MySQLDB::Connect(const std::string & ip,
unsigned short port,
const std::string& user,
const std::string& password,
const std::string& db)
{
if(!mysql_real_connect(m_mysqlClient,
ip.c_str(), user.c_str(), password.c_str(), db.c_str(), 3306, nullptr, 0))
{
LOG_ERROR << "mysql_real_connect() failed!";
}
LOG_INFO << "mysql connect success!";
}
int MySQLDB::Query(std::string&& sql)
{
LOG_INFO << "sql:" << sql;
if(mysql_query(m_mysqlClient, sql.c_str()))
{
LOG_ERROR << mysql_error(m_mysqlClient);
return 0;
}
return mysql_num_rows(mysql_store_result(m_mysqlClient));
}
bool MySQLDB::Insert(std::string&& sql)
{
LOG_INFO << "sql:" << sql;
if(mysql_query(m_mysqlClient, sql.c_str()))
{
LOG_ERROR << mysql_error(m_mysqlClient);
return false;
}
return true;
}

Redis

安装

1
2
3
4
5
6
wget https://github.com/redis/hiredis/archive/refs/tags/v1.0.2.tar.gz
tar -zxvf v1.0.2.tar.gz
cd hiredis-1.0.2
make
make install
ldconfig

Linux_守护进程

内容

本节将说明守护进程结构,以及如何编写守护进程程序。因为守护进程没有控制终端,我们需要了解在出现问题时,守护进程如何报告出错情况。

  1. 守护进程的特点
  2. 进程组、控制终端、会话
  3. 守护进程编程流程

守护进程

守护进程(daemon)是生存期长的一种进程。常常在系统引导装入时启动,仅在系统关闭时才终止。因为它们没有控制终端,所以说它们是在后台运行的。UNIX系统有很多守护进程,它们执行日常事务活动。

在基于BSD的系统下执行ps -axj

选项-a显示由其他用户所拥有的进程的状态,-x显示没有控制终端的进程状态,-j显示与作业有关的信息:会话ID、进程组ID、控制终端以及终端进程组ID。

类似的命令,在基于System V的系统中,与此相类似的命令是ps -efj(为了提高安全性,某些UNIX系统不允许用户使用ps命令查看不属于自己的进程)。

有以下比较关键的列:UID(用户ID)、PID(进程ID)、PPID(父进程ID)、PGID(进程组ID)、SID(session ID, 会话ID)、终端名称(TTY)、命令字符串(CMD)。

ps命令在支持会话ID的系统上运行(Linux 3.2.0),setsid函数中的sid即会话ID。简单地说,它就是会话首进程的进程ID。

系统进程历程

系统进程依赖于操作系统实现。父进程ID为0的各进程通常是内核进程,它们作为系统引导装入过程的一部分而启动。(init是个例外,它是一个由内核在引导装入时启动的用户层次的命令。)内核进程是特殊的,通常存在于系统的整个生命期中。它们以超级用户特权运行,无控制终端,无命令行。

ps的输出实例中,内核守护进程的名字出现在方括号中,如[sshd]该版本的Linux使用一个名为kthreadd的特殊内核进程来创建其他内核进程,所以kthreadd表现为其他内核进程的父进程。对于需要在进程上下文执行工作但却不被用户层进程上下文调用的每一个内核组件,通常有它自己的内核守护进程。例如,在Linux中:

  1. kswapd守护进程,内存换页守护进程。它支持虚拟内存子系统在经过一段时间后将脏页面慢慢地写回磁盘来回收这些页面。
  2. flush守护进程在可用内存达到设置的最小阈值时将脏页面冲洗至磁盘。它也定期地将脏页面冲洗回磁盘来减少在系统出现故障时发生的数据丢失。多个冲洗守护进程可以同时存在,每个写回的设备都有一个冲洗守护进程。比如一个名为flush-8:0的冲洗守护进程。从名字中可以看出,写回设备是通过主设备号(8)和副设备号(0)来识别的。
  3. sync_supers守护进程定期将文件系统元数据冲洗至磁盘。
  4. jbd守护进程帮助实现了ext4文件系统中的日志功能。

进程1通常是init,它是一个系统守护进程,除了其他工作外,主要负责启动各运行层次特定的系统服务。这些服务通常是在它们自己拥有的守护进程的帮助下实现的。

netd守护进程。它侦听系统网络接口,以便取得来自网络的对各种网络服务进程的请求。nfsdnfsiodlockdrpciodrpe.idmapdrpc.statdrpc.mountd守护进程提供对网络文件系统(Network File System,NFS)的支持。注意,前4个是内核守护进程,后3个是用户级守护进程。

注意,大多数守护进程都以超级用户(root)特权运行。所有的守护进程都没有控制终端,其终端名设置为问号内核守护进程以无控制终端方式启动。用户层守护进程缺少控制终端可能是守护进程调用了setsid的结果。大多数用户层守护进程都是进程组的组长进程以及会话的首进程,而且是这些进程组和会话中的唯一进程rsyslogd是一个例外)。最后,应当引起注意的是用户层守护进程的父进程是init进程。

特点

运行周期长,在后台执行,不需要和用户交互。

概念

  1. 会话:每当打开一个终端,就相当于和内核建立了一个会话。再打开一个终端就会创建一个新会话,与第一个会话不同。即会话是伴随终端的打开而建立的。用会话首进程的pid来作为整个会话的sid。即使该进程结束了,会话id依然不变。
  2. 会话首进程:在该会话中运行的第一个进程。一般来讲会话首进程就是bash。
  3. 进程组:在会话中每运行一个命令,比如执行ls,会创建一个进程组,同时会创建一个进程。用该进程组的首进程pid作为整个进程组的pgid,即组长进程。进程组中有可能会有多个进程,是组内进程fork产生的。如果父进程(组长进程)结束了,pgid不会改变。只有进程组中一个进程都没有时该进程组才消失。进程组可以便于系统进行管理,比如给组长进程发一个信号,其他人都可以收到。
  4. 组长进程

如果关闭终端,则会话里的进程会全部结束。

我们要想办法使会话中的进程与终端断绝关系,独立起来。

思路:可以新建另一个会话,把终端中的进程组挪出来到这个会话里,然后就可以放心地关闭终端了。这就叫做守护进程。

创建新会话可调用setsid。但是是有条件的,调用该函数的进程不能是一个组长进程,因为这会引起组长id冲突。即必须是非组长才可创建新会话。

编程流程

  1. fork():父进程创建子进程,退出父进程,这样就保证了子进程不是组长id。
  2. setsid()
  3. fork():子进程再创建孙子进程,保证孙子进程不是组长id,保证不和终端再关联。但不必要。
  4. chdir("/"):把工作目录改变到根目录,保险,为长时间的运行做好准备。但不一定能用的到。这么做是防止被卸载。
  5. unmask():清空掩码,保证创建文件时创建出应有的权限。
  6. close():关闭用不到的文件描述符,如0、1、2
  7. 如果可能产生子进程,还需要注意处理僵死进程。

在编写守护进程程序时需遵循一些基本规则,以防止产生不必要的交互作用。 下面先说明这些规则,然后给出一个按照这些规则编写的函数daemonize

  1. 首先要做的是调用umask将文件模式创建屏蔽字设置为一个已知值(通常是0)。由继承得来的文件模式创建屏蔽字可能会被设置为拒绝某些权限。如果守护进程要创建文件,那么它可能要设置特定的权限。例如,若守护进程要创建组可读、组可写的文件,继承的文件模式创建屏蔽字可能会屏蔽上述两种权限中的一种,而使其无法发挥作用。另一方面,如果守护进程调用的库函数创建了文件,那么将文件模式创建屏蔽字设置为一个限制性更强的值(如007)可能会更明智,因为库函数可能不允许调用者通过一个显式的函数参数来设置权限。

  2. 调用fork,然后使父进程exit。这样做实现了下面几点。第一,如果该守护进程是作为一条简单的shell命令启动的,那么父进程终止会让shell认为这条命令已经执行完毕。第二,虽然子进程继承了父进程的进程组ID,但获得了一个新的进程ID,这就保证了子进程不是一个进程组的组长进程。这是下面将要进行的setsid调用的先决条件。

  3. 调用setsid创建一个新会话。然后执行9.5节中列出的3个步骤,使调用进程∶(a)成为新会话的首进程,(b)成为一个新进程组的组长进程,(c)没有控制终端。

    在基于System V的系统中,有些人建议在此时再次调用fork,终止父进程,继续使用子进程中的守护进程。这就保证了该守护进程不是会话首进程,于是按照System V规则(见UNIX环境高级编程9.6节)可以防止它取得控制终端。为了避免取得控制终端的另一种方法是,无论何时打开一个终端设备,都一定要指定O_NOCTTY

  4. 将当前工作目录更改为根日录。从父进程处继承过来的当前工作目录可能在一个挂载的文件系统中。因为守护进程通常在系统再引导之前是一直存在的,所以如果守护进程的当前工作目录在一个挂载文件系统中,那么该文件系统就不能被卸载。或者,某些守护进程还可能会把当前工作目录更改到某个指定位置,并在此位置进行它们的全部工作。例如,行式打印机假脱机守护进程就可能将其工作目录更改到它们的spool目录上。

  5. 关闭不再需要的文件描述符。这使守护进程不再持有从其父进程继承来的任何文件描述符(父进程可能是shell进程,或某个其他进程)。可以使用open_max函数(见2.17节)或getrlimit函数(见 7.11节)来判定最高文件描述符值,并关闭直到该值的所有描述符。

  6. 某些守护进程打开/dev/null使其具有文件描述符0、1和2,这样,任何一个试图读标准输入、写标准输出或标准错误的库例程都不会产生任何效果。因为守护进程并不与终端设备相关联,所以其输出无处显示,也无处从交互式用户那里接收输入。即使守护讲程是从交互式会话启动的,但是守护进程是在后台运行的,所以登录会话的终止并不影响守护进程。如果其他用户在同一终端设备上登录,我们不希望在该终端上见到守护进程的输出,用户也不期望他们在终端上的输入被守护进程读取。

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include"apue.h"
#include<syslog.h>
#include<fcntl.h>
#include<sys/resource.h>
void daemonize(const char * cmd)
{
int i, fd0, fd1, fd2;
pid_t pid;
struct rlimit rl;
struct sigaction sa;

/* Clear file creation mask. */
umask(0);

/* Get maximum number of file descriptions. */
if(getrlimit(RLIMIT_NOFILE, &rl) < 0)
{
err_quit("%s: can't get file limit", cmd);
}
/* Become a session leader to lose controlling TTY. */
if((pid = fork()) < 0)
{
err_quit("%s: can't fork", cmd);
}
else if(pid != 0)//parent
{
exit(0);
}
setsid();
/* Ensure future opens won't allocate controlling TTYs. */
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if(sigaction(SIGHUP, &sa, NULL) < 0)
{
err_quit("%s: can't ignore SIGHUP", cmd);
}
if((pid = fork()) < 0)
{
err_quit("%s: can't fork", cmd);
}
else if(pid != 0)//parent
{
exit(0);
}
/* change the current working directory to the root,
so we won't prevent file systems from being unmounted. */
if(chdir("/") < 0)
{
err_quit("%s: can't change directory to /", cmd);
}
/* close all open file descriptors */
if(rl.rlim_max == RLIM_INFINTY)
{
rl.rlim_max = 1024;
}
for(i = 0; i < rl.rlim_max; ++i)
{
close(i);
}
/* attach file descriptors 0, 1, and 2 to /dev/null */
fd0 = open("/dev/null", O_RDWR);
fd1 = dup(0);
fd2 = dup(0);
/* initialize the log file. */
openlog(cmd, LOG_CONS, LOG_DAEMON);
if(fd0 != 0 || fd1 != 1 || fd2 != 2)
{
syslog(LOG_ERR, "unexpected file descriptors %d %d %d", fd0, fd1, fd2);
exit(1);
}
}

daemonize函数由main程序调用,然后main程序进入休眠状态,那么可以用ps命令检查该守护进程的状态:

1
2
3
4
5
6
$ ./a.out
$ ps -efj
UID PID PPID PGID SID TTY CMD
sar 13800 1 13799 13799 ? ./a.out
$ ps -efj | grep 13799
sar 13800 1 13799 13799 ? ./a.out

我们也可用ps命令验证,没有活动进程存在的ID是13799。这意味着,守护进程在一个孤儿进程组中(见9.10节),它不是会话首进程,因此没有机会被分配到一个控制终端。这一结果是在daemonize函数中执行第二个fork造成的。可以看出,守护进程已经被正确地初始化了。