2 Test porting lib (common/system_*.c)
4 Copyright (C) Mathieu Parent 2013
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "system/network.h"
30 #include "lib/util/debug.h"
31 #include "lib/util/blocking.h"
33 #include "protocol/protocol.h"
34 #include "common/system.h"
35 #include "common/logging.h"
39 const char *socketname;
40 const char *debuglevel;
44 .socketname = "/tmp/test.sock"
53 create a unix domain socket and bind it
54 return a file descriptor open on the socket
56 static int socket_server_create(void)
58 struct sockaddr_un addr;
61 globals.socket = socket(AF_UNIX, SOCK_STREAM, 0);
62 assert(globals.socket != -1);
64 set_close_on_exec(globals.socket);
65 //set_blocking(globals.socket, false);
67 memset(&addr, 0, sizeof(addr));
68 addr.sun_family = AF_UNIX;
69 strncpy(addr.sun_path, globals.socketname, sizeof(addr.sun_path)-1);
71 ret = bind(globals.socket, (struct sockaddr *)&addr, sizeof(addr));
74 ret = chown(globals.socketname, geteuid(), getegid());
77 ret = chmod(globals.socketname, 0700);
80 ret = listen(globals.socket, 100);
86 static int socket_server_wait_peer(void)
88 struct sockaddr_un addr;
92 memset(&addr, 0, sizeof(addr));
94 fd = accept(globals.socket, (struct sockaddr *)&addr, &len);
97 //set_blocking(fd, false);
98 set_close_on_exec(fd);
102 static int socket_server_close(void)
106 ret = close(globals.socket);
109 ret = unlink(globals.socketname);
115 static int socket_client_connect(void)
117 struct sockaddr_un addr;
121 client = socket(AF_UNIX, SOCK_STREAM, 0);
122 assert(client != -1);
124 memset(&addr, 0, sizeof(addr));
125 addr.sun_family = AF_UNIX;
126 strncpy(addr.sun_path, globals.socketname, sizeof(addr.sun_path)-1);
128 ret = connect(client, (struct sockaddr *)&addr, sizeof(addr));
134 static int socket_client_close(int client)
147 static int fork_helper(void)
155 if (pid == 0) { // Child
157 client = socket_client_connect();
158 while (kill(pid, 0) == 0) {
161 socket_client_close(client);
164 globals.helper_pid = pid;
172 static int test_ctdb_sys_check_iface_exists(void)
174 const char *fakename = "fake";
177 test = ctdb_sys_check_iface_exists(fakename);
178 if (geteuid() == 0) {
179 assert(test == false);
181 assert(test == true);
186 static int test_ctdb_get_peer_pid(void)
192 fd = socket_server_wait_peer();
194 ret = ctdb_get_peer_pid(fd, &peer_pid);
197 assert(peer_pid == globals.helper_pid);
199 kill(peer_pid, SIGTERM);
208 int main(int argc, const char *argv[])
210 struct poptOption popt_options[] = {
212 { "socket", 0, POPT_ARG_STRING, &globals.socketname, 0, "local socket name", "filename" },
216 const char **extra_argv;
220 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
222 while ((opt = poptGetNextOpt(pc)) != -1) {
225 fprintf(stderr, "Invalid option %s: %s\n",
226 poptBadOption(pc, 0), poptStrerror(opt));
231 /* setup the remaining options for the main program to use */
232 extra_argv = poptGetArgs(pc);
235 while (extra_argv[extra_argc]) extra_argc++;
238 assert(globals.socketname != NULL);
240 ret = socket_server_create();
243 /* FIXME: Test tcp_checksum6, tcp_checksum */
244 /* FIXME: Test ctdb_sys_send_arp, ctdb_sys_send_tcp */
245 /* FIXME: Test ctdb_sys_{open,close}_capture_socket, ctdb_sys_read_tcp_packet */
246 test_ctdb_sys_check_iface_exists();
250 test_ctdb_get_peer_pid();
252 ret = socket_server_close();