1 /* This file is part of Mailfromd.
2    Copyright (C) 2005-2021 Sergey Poznyakoff
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #ifndef __mfd_srvman_h
18 #define __mfd_srvman_h
19 
20 typedef struct mfd_server *mfd_server_t;
21 
22 typedef	int (*mfd_server_func_t) (const char *id,
23 				  int fd,
24 				  struct sockaddr const *sa, socklen_t len,
25 				  void *server_data, void *srvman_data);
26 typedef	int (*mfd_server_prefork_hook_t) (const char *id,
27 					  struct sockaddr const *sa,
28 					  socklen_t len,
29 					  void *server_data,
30 					  void *srvman_data);
31 typedef	int (*mfd_srvman_hook_t) (void *data);
32 typedef int (*mfd_srvman_prefork_hook_t) (struct sockaddr const *sa,
33 					  socklen_t len,
34 					  void *data);
35 
36 #define DEFAULT_PIDTAB_SIZE 64
37 #define DEFAULT_SHUTDOWN_TIMEOUT 5
38 
39 #define SRV_SINGLE_PROCESS 0x01
40 #define SRV_KEEP_EXISTING  0x02
41 
42 struct srvman_param {
43 	void *data;                 /* Server manager data */
44 	mu_acl_t acl;               /* Global Access Control List */
45 	time_t shutdown_timeout;
46 	mfd_srvman_hook_t idle_hook;             /* Idle function */
47 	mfd_srvman_prefork_hook_t prefork_hook;  /* Pre-fork function */
48 	mfd_srvman_hook_t free_hook;             /* Free function */
49 	size_t max_children;        /* Maximum number of sub-processes
50 				       to run. */
51 	int flags;                  /* Global flags */
52 	fd_set keepfds;             /* Keep these fds open when spawning
53 				       children */
54 };
55 
56 extern struct srvman_param srvman_param;
57 
58 void srvman_init(void);
59 
60 void mfd_server_shutdown(mfd_server_t srv);
61 mfd_server_t mfd_server_new(const char *id, mu_url_t url,
62 			    mfd_server_func_t conn, int flags);
63 void mfd_server_free(mfd_server_t srv);
64 void mfd_server_set_prefork_hook(mfd_server_t srv,
65 				 mfd_server_prefork_hook_t hook);
66 void mfd_server_set_data(mfd_server_t srv,
67 			 void *data,
68 			 mfd_srvman_hook_t free_hook);
69 void mfd_server_set_max_children(mfd_server_t srv, size_t n);
70 void mfd_server_set_acl(struct mfd_server *srv, mu_acl_t acl);
71 void mfd_server_set_backlog(struct mfd_server *srv, int value);
72 
73 void mfd_srvman_attach_server(mfd_server_t srv);
74 void mfd_srvman_run(sigset_t *);
75 int mfd_srvman_open(void);
76 void mfd_srvman_shutdown(void);
77 void mfd_srvman_free(void);
78 size_t mfd_srvman_count_servers(void);
79 void mfd_srvman_stop(void);
80 
81 struct sockaddr *srvman_url_to_sockaddr(mu_url_t url, socklen_t *psalen);
82 
83 #endif
84