Skip to content

Commit 3500865

Browse files
einval22wlallemand
authored andcommitted
REORG: startup: move mworker_apply_master_worker_mode in mworker.c
mworker_apply_master_worker_mode() is called only in master-worker mode, so let's move it mworker.c
1 parent 3899a7e commit 3500865

File tree

3 files changed

+111
-110
lines changed

3 files changed

+111
-110
lines changed

include/haproxy/mworker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ void mworker_create_master_cli(void);
5454

5555
void mworker_prepare_master(void);
5656
void mworker_run_master(void);
57+
void mworker_apply_master_worker_mode(void);
5758

5859
#endif /* _HAPROXY_MWORKER_H_ */

src/haproxy.c

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,116 +1794,6 @@ static void handle_pidfile()
17941794
DISGUISE(write(pidfd, pidstr, strlen(pidstr)));
17951795
}
17961796

1797-
/* This function at first does master-worker fork. It creates then GLOBAL and
1798-
* MASTER proxies, allocates listeners for these proxies and binds a GLOBAL
1799-
* proxy listener in worker process on ipc_fd[1] and MASTER proxy listener
1800-
* in master process on ipc_fd[0]. ipc_fd[0] and ipc_fd[1] are the "ends" of the
1801-
* sockpair, created in prepare_master(). This sockpair is copied via fork to
1802-
* each process and serves as communication channel between master and worker
1803-
* (master CLI applet is attached in master process to MASTER proxy). This
1804-
* function returns only if everything is OK. If something fails, it exits.
1805-
*/
1806-
static void mworker_apply_master_worker_mode()
1807-
{
1808-
int worker_pid;
1809-
struct mworker_proc *child;
1810-
char *sock_name = NULL;
1811-
char *errmsg = NULL;
1812-
1813-
worker_pid = fork();
1814-
switch (worker_pid) {
1815-
case -1:
1816-
ha_alert("[%s.main()] Cannot fork.\n", progname);
1817-
1818-
exit(EXIT_FAILURE);
1819-
case 0:
1820-
/* This one must not be exported, it's internal! */
1821-
unsetenv("HAPROXY_MWORKER_REEXEC");
1822-
ha_random_jump96(1);
1823-
1824-
list_for_each_entry(child, &proc_list, list) {
1825-
if ((child->options & PROC_O_TYPE_WORKER) && (child->options & PROC_O_INIT)) {
1826-
close(child->ipc_fd[0]);
1827-
child->ipc_fd[0] = -1;
1828-
/* proc_self needs to point to the new forked worker in
1829-
* worker's context, as it's dereferenced in
1830-
* mworker_sockpair_register_per_thread(), called for
1831-
* master and for worker.
1832-
*/
1833-
proc_self = child;
1834-
/* attach listener to GLOBAL proxy on child->ipc_fd[1] */
1835-
if (mworker_cli_global_proxy_new_listener(child) < 0)
1836-
exit(EXIT_FAILURE);
1837-
1838-
break;
1839-
}
1840-
1841-
/* need to close reload sockpair fds, inherited after master's execvp and fork(),
1842-
* we can't close these fds in master before the fork(), as ipc_fd[1] serves after
1843-
* the mworker_reexec to obtain the MCLI client connection fd, like this we can
1844-
* write to this connection fd the content of the startup_logs ring.
1845-
*/
1846-
if (child->options & PROC_O_TYPE_MASTER) {
1847-
if (child->ipc_fd[0] > 0)
1848-
close(child->ipc_fd[0]);
1849-
if (child->ipc_fd[1] > 0)
1850-
close(child->ipc_fd[1]);
1851-
}
1852-
}
1853-
break;
1854-
default:
1855-
/* in parent */
1856-
ha_notice("Initializing new worker (%d)\n", worker_pid);
1857-
master = 1;
1858-
1859-
/* in exec mode, there's always exactly one thread. Failure to
1860-
* set these ones now will result in nbthread being detected
1861-
* automatically.
1862-
*/
1863-
global.nbtgroups = 1;
1864-
global.nbthread = 1;
1865-
1866-
/* creates MASTER proxy */
1867-
if (mworker_cli_create_master_proxy(&errmsg) < 0) {
1868-
ha_alert("Can't create MASTER proxy: %s\n", errmsg);
1869-
free(errmsg);
1870-
exit(EXIT_FAILURE);
1871-
}
1872-
1873-
/* attaches servers to all existed workers on its shared MCLI sockpair ends, ipc_fd[0] */
1874-
if (mworker_cli_attach_server(&errmsg) < 0) {
1875-
ha_alert("Can't attach servers needed for master CLI %s\n", errmsg ? errmsg : "");
1876-
free(errmsg);
1877-
exit(EXIT_FAILURE);
1878-
}
1879-
1880-
/* creates reload sockpair and listeners for master CLI (-S) */
1881-
mworker_create_master_cli();
1882-
1883-
/* find the right mworker_proc */
1884-
list_for_each_entry(child, &proc_list, list) {
1885-
if ((child->options & PROC_O_TYPE_WORKER) && (child->options & PROC_O_INIT)) {
1886-
child->timestamp = date.tv_sec;
1887-
child->pid = worker_pid;
1888-
child->version = strdup(haproxy_version);
1889-
1890-
close(child->ipc_fd[1]);
1891-
child->ipc_fd[1] = -1;
1892-
1893-
/* attach listener to MASTER proxy on child->ipc_fd[0] */
1894-
memprintf(&sock_name, "sockpair@%d", child->ipc_fd[0]);
1895-
if (mworker_cli_master_proxy_new_listener(sock_name) == NULL) {
1896-
ha_free(&sock_name);
1897-
exit(EXIT_FAILURE);
1898-
}
1899-
ha_free(&sock_name);
1900-
1901-
break;
1902-
}
1903-
}
1904-
}
1905-
}
1906-
19071797
static void get_listeners_fd()
19081798
{
19091799
/* Try to get the listeners FD from the previous process using

src/mworker.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,116 @@ void mworker_run_master(void)
12271227
exit(0);
12281228
}
12291229

1230+
/* This function at first does master-worker fork. It creates then GLOBAL and
1231+
* MASTER proxies, allocates listeners for these proxies and binds a GLOBAL
1232+
* proxy listener in worker process on ipc_fd[1] and MASTER proxy listener
1233+
* in master process on ipc_fd[0]. ipc_fd[0] and ipc_fd[1] are the "ends" of the
1234+
* sockpair, created in prepare_master(). This sockpair is copied via fork to
1235+
* each process and serves as communication channel between master and worker
1236+
* (master CLI applet is attached in master process to MASTER proxy). This
1237+
* function returns only if everything is OK. If something fails, it exits.
1238+
*/
1239+
void mworker_apply_master_worker_mode(void)
1240+
{
1241+
int worker_pid;
1242+
struct mworker_proc *child;
1243+
char *sock_name = NULL;
1244+
char *errmsg = NULL;
1245+
1246+
worker_pid = fork();
1247+
switch (worker_pid) {
1248+
case -1:
1249+
ha_alert("[%s.main()] Cannot fork.\n", progname);
1250+
1251+
exit(EXIT_FAILURE);
1252+
case 0:
1253+
/* This one must not be exported, it's internal! */
1254+
unsetenv("HAPROXY_MWORKER_REEXEC");
1255+
ha_random_jump96(1);
1256+
1257+
list_for_each_entry(child, &proc_list, list) {
1258+
if ((child->options & PROC_O_TYPE_WORKER) && (child->options & PROC_O_INIT)) {
1259+
close(child->ipc_fd[0]);
1260+
child->ipc_fd[0] = -1;
1261+
/* proc_self needs to point to the new forked worker in
1262+
* worker's context, as it's dereferenced in
1263+
* mworker_sockpair_register_per_thread(), called for
1264+
* master and for worker.
1265+
*/
1266+
proc_self = child;
1267+
/* attach listener to GLOBAL proxy on child->ipc_fd[1] */
1268+
if (mworker_cli_global_proxy_new_listener(child) < 0)
1269+
exit(EXIT_FAILURE);
1270+
1271+
break;
1272+
}
1273+
1274+
/* need to close reload sockpair fds, inherited after master's execvp and fork(),
1275+
* we can't close these fds in master before the fork(), as ipc_fd[1] serves after
1276+
* the mworker_reexec to obtain the MCLI client connection fd, like this we can
1277+
* write to this connection fd the content of the startup_logs ring.
1278+
*/
1279+
if (child->options & PROC_O_TYPE_MASTER) {
1280+
if (child->ipc_fd[0] > 0)
1281+
close(child->ipc_fd[0]);
1282+
if (child->ipc_fd[1] > 0)
1283+
close(child->ipc_fd[1]);
1284+
}
1285+
}
1286+
break;
1287+
default:
1288+
/* in parent */
1289+
ha_notice("Initializing new worker (%d)\n", worker_pid);
1290+
master = 1;
1291+
1292+
/* in exec mode, there's always exactly one thread. Failure to
1293+
* set these ones now will result in nbthread being detected
1294+
* automatically.
1295+
*/
1296+
global.nbtgroups = 1;
1297+
global.nbthread = 1;
1298+
1299+
/* creates MASTER proxy */
1300+
if (mworker_cli_create_master_proxy(&errmsg) < 0) {
1301+
ha_alert("Can't create MASTER proxy: %s\n", errmsg);
1302+
free(errmsg);
1303+
exit(EXIT_FAILURE);
1304+
}
1305+
1306+
/* attaches servers to all existed workers on its shared MCLI sockpair ends, ipc_fd[0] */
1307+
if (mworker_cli_attach_server(&errmsg) < 0) {
1308+
ha_alert("Can't attach servers needed for master CLI %s\n", errmsg ? errmsg : "");
1309+
free(errmsg);
1310+
exit(EXIT_FAILURE);
1311+
}
1312+
1313+
/* creates reload sockpair and listeners for master CLI (-S) */
1314+
mworker_create_master_cli();
1315+
1316+
/* find the right mworker_proc */
1317+
list_for_each_entry(child, &proc_list, list) {
1318+
if ((child->options & PROC_O_TYPE_WORKER) && (child->options & PROC_O_INIT)) {
1319+
child->timestamp = date.tv_sec;
1320+
child->pid = worker_pid;
1321+
child->version = strdup(haproxy_version);
1322+
1323+
close(child->ipc_fd[1]);
1324+
child->ipc_fd[1] = -1;
1325+
1326+
/* attach listener to MASTER proxy on child->ipc_fd[0] */
1327+
memprintf(&sock_name, "sockpair@%d", child->ipc_fd[0]);
1328+
if (mworker_cli_master_proxy_new_listener(sock_name) == NULL) {
1329+
ha_free(&sock_name);
1330+
exit(EXIT_FAILURE);
1331+
}
1332+
ha_free(&sock_name);
1333+
1334+
break;
1335+
}
1336+
}
1337+
}
1338+
}
1339+
12301340
static struct cfg_kw_list mworker_kws = {{ }, {
12311341
{ CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads, KWF_DISCOVERY },
12321342
{ 0, NULL, NULL },

0 commit comments

Comments
 (0)