1 /*	$NetBSD: master.h,v 1.2 2020/03/18 19:05:16 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	master 3h
6 /* SUMMARY
7 /*	Postfix master - data structures and prototypes
8 /* SYNOPSIS
9 /*	#include "master.h"
10 /* DESCRIPTION
11 /* .nf
12 
13  /*
14   * Server processes that provide the same service share a common "listen"
15   * socket to accept connection requests, and share a common pipe to the
16   * master process to send status reports. Server processes die voluntarily
17   * when idle for a configurable amount of time, or after servicing a
18   * configurable number of requests; the master process spawns new processes
19   * on demand up to a configurable concurrency limit and/or periodically.
20   *
21   * The canonical service name is what we use internally, so that we correctly
22   * handle a request to "reload" after someone changes "smtp" into "25".
23   *
24   * We use the external service name from master.cf when reporting problems, so
25   * that the user can figure out what we are talking about. Of course we also
26   * include the canonical service name so that the UNIX-domain smtp service
27   * can be distinguished from the Internet smtp service.
28   */
29 typedef struct MASTER_SERV {
30     int     flags;			/* status, features, etc. */
31     char   *ext_name;			/* service endpoint name (master.cf) */
32     char   *name;			/* service endpoint name (canonical) */
33     int     type;			/* UNIX-domain, INET, etc. */
34     time_t  busy_warn_time;		/* limit "all servers busy" warning */
35     int     wakeup_time;		/* wakeup interval */
36     int    *listen_fd;			/* incoming requests */
37     int     listen_fd_count;		/* nr of descriptors */
38     union {
39 	struct {
40 	    char   *port;		/* inet listen port */
41 	    struct INET_ADDR_LIST *addr;/* inet listen address */
42 	}       inet_ep;
43 #define MASTER_INET_ADDRLIST(s)	((s)->endpoint.inet_ep.addr)
44 #define MASTER_INET_PORT(s)	((s)->endpoint.inet_ep.port)
45     }       endpoint;
46     int     max_proc;			/* upper bound on # processes */
47     char   *path;			/* command pathname */
48     struct ARGV *args;			/* argument vector */
49     char   *stress_param_val;		/* stress value: "yes" or empty */
50     time_t  stress_expire_time;		/* stress pulse stretcher */
51     int     avail_proc;			/* idle processes */
52     int     total_proc;			/* number of processes */
53     int     throttle_delay;		/* failure recovery parameter */
54     int     status_fd[2];		/* child status reports */
55     struct BINHASH *children;		/* linkage */
56     struct MASTER_SERV *next;		/* linkage */
57 } MASTER_SERV;
58 
59  /*
60   * Per-service flag bits. We assume trouble when a child process terminates
61   * before completing its first request: either the program is defective,
62   * some configuration is wrong, or the system is out of resources.
63   */
64 #define MASTER_FLAG_THROTTLE	(1<<0)	/* we're having trouble */
65 #define MASTER_FLAG_MARK	(1<<1)	/* garbage collection support */
66 #define MASTER_FLAG_CONDWAKE	(1<<2)	/* wake up if actually used */
67 #define MASTER_FLAG_INETHOST	(1<<3)	/* endpoint name specifies host */
68 #define MASTER_FLAG_LOCAL_ONLY	(1<<4)	/* no remote clients */
69 #define MASTER_FLAG_LISTEN	(1<<5)	/* monitor this port */
70 
71 #define MASTER_THROTTLED(f)	((f)->flags & MASTER_FLAG_THROTTLE)
72 #define MASTER_MARKED_FOR_DELETION(f) ((f)->flags & MASTER_FLAG_MARK)
73 #define MASTER_LISTENING(f)	((f)->flags & MASTER_FLAG_LISTEN)
74 
75 #define MASTER_LIMIT_OK(limit, count) ((limit) == 0 || ((count) < (limit)))
76 
77  /*
78   * Service types, stream sockets unless indicated otherwise.
79   */
80 #define MASTER_SERV_TYPE_UNIX	1	/* AF_UNIX domain socket */
81 #define MASTER_SERV_TYPE_INET	2	/* AF_INET domain socket */
82 #define MASTER_SERV_TYPE_FIFO	3	/* fifo (named pipe) */
83 #define MASTER_SERV_TYPE_PASS	4	/* AF_UNIX domain socket */
84 #define MASTER_SERV_TYPE_UXDG	5	/* AF_UNIX domain datagram socket */
85 
86  /*
87   * Default process management policy values. This is only the bare minimum.
88   * Most policy management is delegated to child processes. The process
89   * manager runs at high privilege level and has to be kept simple.
90   */
91 #define MASTER_DEF_MIN_IDLE	1	/* preferred # of idle processes */
92 
93  /*
94   * Structure of child process.
95   */
96 typedef int MASTER_PID;			/* pid is key into binhash table */
97 
98 typedef struct MASTER_PROC {
99     MASTER_PID pid;			/* child process id */
100     unsigned gen;			/* child generation number */
101     int     avail;			/* availability */
102     MASTER_SERV *serv;			/* parent linkage */
103     int     use_count;			/* number of service requests */
104 } MASTER_PROC;
105 
106  /*
107   * Other manifest constants.
108   */
109 #define MASTER_BUF_LEN	2048		/* logical config line length */
110 
111  /*
112   * master.c
113   */
114 extern int master_detach;
115 extern int init_mode;
116 
117  /*
118   * master_ent.c
119   */
120 extern void fset_master_ent(char *);
121 extern void set_master_ent(void);
122 extern void end_master_ent(void);
123 extern void print_master_ent(MASTER_SERV *);
124 extern MASTER_SERV *get_master_ent(void);
125 extern void free_master_ent(MASTER_SERV *);
126 
127  /*
128   * master_conf.c
129   */
130 extern void master_config(void);
131 extern void master_refresh(void);
132 
133  /*
134   * master_vars.c
135   */
136 extern void master_vars_init(void);
137 
138  /*
139   * master_service.c
140   */
141 extern MASTER_SERV *master_head;
142 extern void master_start_service(MASTER_SERV *);
143 extern void master_stop_service(MASTER_SERV *);
144 extern void master_restart_service(MASTER_SERV *, int);
145 
146 #define DO_CONF_RELOAD	1	/* config files were reloaded */
147 #define NO_CONF_RELOAD	0	/* no config file was reloaded */
148 
149  /*
150   * master_events.c
151   */
152 extern int master_gotsighup;
153 extern int master_gotsigchld;
154 extern void master_sigsetup(void);
155 
156  /*
157   * master_status.c
158   */
159 extern void master_status_init(MASTER_SERV *);
160 extern void master_status_cleanup(MASTER_SERV *);
161 
162  /*
163   * master_wakeup.c
164   */
165 extern void master_wakeup_init(MASTER_SERV *);
166 extern void master_wakeup_cleanup(MASTER_SERV *);
167 
168 
169  /*
170   * master_listen.c
171   */
172 extern void master_listen_init(MASTER_SERV *);
173 extern void master_listen_cleanup(MASTER_SERV *);
174 
175  /*
176   * master_avail.c
177   */
178 extern void master_avail_listen(MASTER_SERV *);
179 extern void master_avail_cleanup(MASTER_SERV *);
180 extern void master_avail_more(MASTER_SERV *, MASTER_PROC *);
181 extern void master_avail_less(MASTER_SERV *, MASTER_PROC *);
182 
183  /*
184   * master_spawn.c
185   */
186 extern struct BINHASH *master_child_table;
187 extern void master_spawn(MASTER_SERV *);
188 extern void master_reap_child(void);
189 extern void master_delete_children(MASTER_SERV *);
190 
191  /*
192   * master_flow.c
193   */
194 extern void master_flow_init(void);
195 extern int master_flow_pipe[2];
196 
197  /*
198   * master_watch.c
199   *
200   * Support to warn about main.cf parameters that can only be initialized but
201   * not updated, and to initialize or update data structures that derive
202   * values from main.cf parameters.
203   */
204 typedef struct {
205     const char *name;			/* parameter name */
206     char  **value;			/* current main.cf value */
207     char  **backup;			/* actual value that is being used */
208     int     flags;			/* see below */
209     void    (*notify) (void);		/* init or update data structure */
210 } MASTER_STR_WATCH;
211 
212 typedef struct {
213     const char *name;			/* parameter name */
214     int    *value;			/* current main.cf value */
215     int     backup;			/* actual value that is being used */
216     int     flags;			/* see below */
217     void    (*notify) (void);		/* init or update data structure */
218 } MASTER_INT_WATCH;
219 
220 #define MASTER_WATCH_FLAG_UPDATABLE (1<<0)	/* support update after init */
221 #define MASTER_WATCH_FLAG_ISSET    (1<<1)	/* backup is initialized */
222 
223 extern void master_str_watch(const MASTER_STR_WATCH *);
224 extern void master_int_watch(MASTER_INT_WATCH *);
225 
226  /*
227   * master_monitor.c
228   */
229 extern int master_monitor(int);
230 
231 /* DIAGNOSTICS
232 /* BUGS
233 /* SEE ALSO
234 /* LICENSE
235 /* .ad
236 /* .fi
237 /*	The Secure Mailer license must be distributed with this software.
238 /* AUTHOR(S)
239 /*	Wietse Venema
240 /*	IBM T.J. Watson Research
241 /*	P.O. Box 704
242 /*	Yorktown Heights, NY 10598, USA
243 /*
244 /*	Wietse Venema
245 /*	Google, Inc.
246 /*	111 8th Avenue
247 /*	New York, NY 10011, USA
248 /*--*/
249