summaryrefslogtreecommitdiffstats
path: root/mgmtd/mgmt_be_server.c
blob: 6997fdcf81f8ce8faac5d0ef8edc0bcb30a0f905 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * MGMTD Backend Server
 *
 * Copyright (C) 2021  Vmware, Inc.
 *		       Pushpasis Sarkar <spushpasis@vmware.com>
 */

#include <zebra.h>
#include "network.h"
#include "libfrr.h"
#include "mgmtd/mgmt.h"
#include "mgmtd/mgmt_be_server.h"
#include "mgmtd/mgmt_be_adapter.h"

#ifdef REDIRECT_DEBUG_TO_STDERR
#define MGMTD_BE_SRVR_DBG(fmt, ...)                                         \
	fprintf(stderr, "%s: " fmt "\n", __func__, ##__VA_ARGS__)
#define MGMTD_BE_SRVR_ERR(fmt, ...)                                         \
	fprintf(stderr, "%s: ERROR, " fmt "\n", __func__, ##__VA_ARGS__)
#else /* REDIRECT_DEBUG_TO_STDERR */
#define MGMTD_BE_SRVR_DBG(fmt, ...)                                            \
	do {                                                                   \
		if (mgmt_debug_be)                                             \
			zlog_debug("%s: " fmt, __func__, ##__VA_ARGS__);       \
	} while (0)
#define MGMTD_BE_SRVR_ERR(fmt, ...)                                         \
	zlog_err("%s: ERROR: " fmt, __func__, ##__VA_ARGS__)
#endif /* REDIRECT_DEBUG_TO_STDERR */

static int mgmt_be_listen_fd = -1;
static struct thread_master *mgmt_be_listen_tm;
static struct thread *mgmt_be_listen_ev;
static void mgmt_be_server_register_event(enum mgmt_be_event event);

static void mgmt_be_conn_accept(struct thread *thread)
{
	int client_conn_fd;
	union sockunion su;

	if (mgmt_be_listen_fd < 0)
		return;

	/* We continue hearing server listen socket. */
	mgmt_be_server_register_event(MGMTD_BE_SERVER);

	memset(&su, 0, sizeof(union sockunion));

	/* We can handle IPv4 or IPv6 socket. */
	client_conn_fd = sockunion_accept(mgmt_be_listen_fd, &su);
	if (client_conn_fd < 0) {
		MGMTD_BE_SRVR_ERR(
			"Failed to accept MGMTD Backend client connection : %s",
			safe_strerror(errno));
		return;
	}
	set_nonblocking(client_conn_fd);
	set_cloexec(client_conn_fd);

	MGMTD_BE_SRVR_DBG("Got a new MGMTD Backend connection");

	mgmt_be_create_adapter(client_conn_fd, &su);
}

static void mgmt_be_server_register_event(enum mgmt_be_event event)
{
	if (event == MGMTD_BE_SERVER) {
		thread_add_read(mgmt_be_listen_tm, mgmt_be_conn_accept,
				NULL, mgmt_be_listen_fd,
				&mgmt_be_listen_ev);
		assert(mgmt_be_listen_ev);
	} else {
		assert(!"mgmt_be_server_post_event() called incorrectly");
	}
}

static void mgmt_be_server_start(const char *hostname)
{
	int ret;
	int sock;
	struct sockaddr_un addr;
	mode_t old_mask;

	/* Set umask */
	old_mask = umask(0077);

	sock = socket(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
	if (sock < 0) {
		MGMTD_BE_SRVR_ERR("Failed to create server socket: %s",
				     safe_strerror(errno));
		goto mgmt_be_server_start_failed;
	}

	addr.sun_family = AF_UNIX,
	strlcpy(addr.sun_path, MGMTD_BE_SERVER_PATH, sizeof(addr.sun_path));
	unlink(addr.sun_path);
	ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
	if (ret < 0) {
		MGMTD_BE_SRVR_ERR(
			"Failed to bind server socket to '%s'. Err: %s",
			addr.sun_path, safe_strerror(errno));
		goto mgmt_be_server_start_failed;
	}

	ret = listen(sock, MGMTD_BE_MAX_CONN);
	if (ret < 0) {
		MGMTD_BE_SRVR_ERR("Failed to listen on server socket: %s",
				     safe_strerror(errno));
		goto mgmt_be_server_start_failed;
	}

	/* Restore umask */
	umask(old_mask);

	mgmt_be_listen_fd = sock;
	mgmt_be_server_register_event(MGMTD_BE_SERVER);

	MGMTD_BE_SRVR_DBG("Started MGMTD Backend Server!");
	return;

mgmt_be_server_start_failed:
	if (sock)
		close(sock);

	mgmt_be_listen_fd = -1;
	exit(-1);
}

int mgmt_be_server_init(struct thread_master *master)
{
	if (mgmt_be_listen_tm) {
		MGMTD_BE_SRVR_DBG("MGMTD Backend Server already running!");
		return 0;
	}

	mgmt_be_listen_tm = master;

	mgmt_be_server_start("localhost");

	return 0;
}

void mgmt_be_server_destroy(void)
{
	if (mgmt_be_listen_tm) {
		MGMTD_BE_SRVR_DBG("Closing MGMTD Backend Server!");

		if (mgmt_be_listen_ev) {
			THREAD_OFF(mgmt_be_listen_ev);
			mgmt_be_listen_ev = NULL;
		}

		if (mgmt_be_listen_fd >= 0) {
			close(mgmt_be_listen_fd);
			mgmt_be_listen_fd = -1;
		}

		mgmt_be_listen_tm = NULL;
	}
}