xref: /netbsd-src/external/ibm-public/postfix/dist/src/master/master_proto.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: master_proto.c,v 1.2 2017/02/14 01:16:45 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	master_proto 3
6 /* SUMMARY
7 /*	Postfix master - status notification protocol
8 /* SYNOPSIS
9 /*	#include <master_proto.h>
10 /*
11 /*	int	master_notify(pid, generation, status)
12 /*	int	pid;
13 /*	unsigned generation;
14 /*	int	status;
15 /* DESCRIPTION
16 /*	The master process provides a standard environment for its
17 /*	child processes. Part of this environment is a pair of file
18 /*	descriptors that the master process shares with all child
19 /*	processes that provide the same service.
20 /* .IP MASTER_LISTEN_FD
21 /*	The shared file descriptor for accepting client connection
22 /*	requests. The master process listens on this socket or FIFO
23 /*	when all child processes are busy.
24 /* .IP MASTER_STATUS_FD
25 /*	The shared file descriptor for sending child status updates to
26 /*	the master process.
27 /* .PP
28 /*	A child process uses master_notify() to send a status notification
29 /*	message to the master process.
30 /* .IP MASTER_STAT_AVAIL
31 /*	The child process is ready to accept client connections.
32 /* .IP MASTER_STAT_TAKEN
33 /*	Until further notice, the child process is unavailable for
34 /*	accepting client connections.
35 /* .PP
36 /*	When a child process terminates without sending a status update,
37 /*	the master process will figure out that the child is no longer
38 /*	available.
39 /* DIAGNOSTICS
40 /*	The result is -1 in case of problems. This usually means that
41 /*	the parent disconnected after a reload request, in order to
42 /*	force children to commit suicide.
43 /* LICENSE
44 /* .ad
45 /* .fi
46 /*	The Secure Mailer license must be distributed with this software.
47 /* AUTHOR(S)
48 /*	Wietse Venema
49 /*	IBM T.J. Watson Research
50 /*	P.O. Box 704
51 /*	Yorktown Heights, NY 10598, USA
52 /*--*/
53 
54 /* System library. */
55 
56 #include <sys_defs.h>
57 #include <unistd.h>
58 
59 /* Utility library. */
60 
61 #include <msg.h>
62 
63 /* Global library. */
64 
65 #include "master_proto.h"
66 
master_notify(int pid,unsigned generation,int status)67 int     master_notify(int pid, unsigned generation, int status)
68 {
69     const char *myname = "master_notify";
70     MASTER_STATUS stat;
71 
72     /*
73      * We use a simple binary protocol to minimize security risks. Since this
74      * is local IPC, there are no byte order or word length issues. The
75      * server treats this information as gossip, so sending a bad PID or a
76      * bad status code will only have amusement value.
77      */
78     stat.pid = pid;
79     stat.gen = generation;
80     stat.avail = status;
81 
82     if (write(MASTER_STATUS_FD, (void *) &stat, sizeof(stat)) != sizeof(stat)) {
83 	if (msg_verbose)
84 	    msg_info("%s: status %d: %m", myname, status);
85 	return (-1);
86     } else {
87 	if (msg_verbose)
88 	    msg_info("%s: status %d", myname, status);
89 	return (0);
90     }
91 }
92