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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*
Test porting lib (common/system_*.c)
Copyright (C) Mathieu Parent 2013
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "replace.h"
#include "system/filesys.h"
#include "system/network.h"
#include <popt.h>
#include <talloc.h>
#include <tevent.h>
#include <tdb.h>
#include <assert.h>
#include "lib/util/debug.h"
#include "lib/util/blocking.h"
#include "protocol/protocol.h"
#include "common/system.h"
#include "common/logging.h"
static struct {
const char *socketname;
const char *debuglevel;
pid_t helper_pid;
int socket;
} globals = {
.socketname = "/tmp/test.sock"
};
/*
Socket functions
*/
/*
create a unix domain socket and bind it
return a file descriptor open on the socket
*/
static int socket_server_create(void)
{
struct sockaddr_un addr;
int ret;
globals.socket = socket(AF_UNIX, SOCK_STREAM, 0);
assert(globals.socket != -1);
set_close_on_exec(globals.socket);
//set_blocking(globals.socket, false);
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, globals.socketname, sizeof(addr.sun_path)-1);
ret = bind(globals.socket, (struct sockaddr *)&addr, sizeof(addr));
assert(ret == 0);
ret = chown(globals.socketname, geteuid(), getegid());
assert(ret == 0);
ret = chmod(globals.socketname, 0700);
assert(ret == 0);
ret = listen(globals.socket, 100);
assert(ret == 0);
return 0;
}
static int socket_server_wait_peer(void)
{
struct sockaddr_un addr;
socklen_t len;
int fd;
memset(&addr, 0, sizeof(addr));
len = sizeof(addr);
fd = accept(globals.socket, (struct sockaddr *)&addr, &len);
assert(fd != -1);
//set_blocking(fd, false);
set_close_on_exec(fd);
return fd;
}
static int socket_server_close(void)
{
int ret;
ret = close(globals.socket);
assert(ret == 0);
ret = unlink(globals.socketname);
assert(ret == 0);
return 0;
}
static int socket_client_connect(void)
{
struct sockaddr_un addr;
int client = 0;
int ret;
client = socket(AF_UNIX, SOCK_STREAM, 0);
assert(client != -1);
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, globals.socketname, sizeof(addr.sun_path)-1);
ret = connect(client, (struct sockaddr *)&addr, sizeof(addr));
assert(ret == 0);
return client;
}
static int socket_client_close(int client)
{
int ret;
ret = close(client);
assert(ret == 0);
return 0;
}
/*
forked program
*/
static int fork_helper(void)
{
pid_t pid;
int client;
pid = fork();
assert(pid != -1);
if (pid == 0) { // Child
pid = getppid();
client = socket_client_connect();
while (kill(pid, 0) == 0) {
sleep(1);
}
socket_client_close(client);
exit(0);
} else {
globals.helper_pid = pid;
}
return 0;
}
/*
tests
*/
static int test_ctdb_sys_check_iface_exists(void)
{
bool test1, test2;
test1 = ctdb_sys_check_iface_exists("unlikely123xyz");
assert(!test1);
/* Linux and others */
test1 = ctdb_sys_check_iface_exists("lo");
/* FreeBSD */
test2 = ctdb_sys_check_iface_exists("lo0");
assert(test1 || test2);
return 0;
}
static int test_ctdb_get_peer_pid(void)
{
int ret;
int fd;
pid_t peer_pid = 0;
fd = socket_server_wait_peer();
ret = ctdb_get_peer_pid(fd, &peer_pid);
assert(ret == 0 || ret == ENOSYS);
if (ret == 0) {
assert(peer_pid == globals.helper_pid);
kill(peer_pid, SIGTERM);
} else {
kill(globals.helper_pid, SIGTERM);
}
close(fd);
return 0;
}
/*
main program
*/
int main(int argc, const char *argv[])
{
struct poptOption popt_options[] = {
POPT_AUTOHELP
{ "socket", 0, POPT_ARG_STRING, &globals.socketname, 0, "local socket name", "filename" },
POPT_TABLEEND
};
int opt, ret;
const char **extra_argv;
int extra_argc = 0;
poptContext pc;
pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
while ((opt = poptGetNextOpt(pc)) != -1) {
switch (opt) {
default:
fprintf(stderr, "Invalid option %s: %s\n",
poptBadOption(pc, 0), poptStrerror(opt));
exit(1);
}
}
/* setup the remaining options for the main program to use */
extra_argv = poptGetArgs(pc);
if (extra_argv) {
extra_argv++;
while (extra_argv[extra_argc]) extra_argc++;
}
assert(globals.socketname != NULL);
ret = socket_server_create();
assert(ret == 0);
/* FIXME: Test tcp_checksum6, tcp_checksum */
/* FIXME: Test ctdb_sys_send_arp, ctdb_sys_send_tcp */
/* FIXME: Test ctdb_sys_{open,close}_capture_socket, ctdb_sys_read_tcp_packet */
test_ctdb_sys_check_iface_exists();
ret = fork_helper();
assert(ret == 0);
test_ctdb_get_peer_pid();
ret = socket_server_close();
assert(ret == 0);
return 0;
}
|