3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
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.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <lib/vserver.h>
26 #include <lib/internal.h>
30 #include <sys/socket.h>
33 #define ENSC_WRAPPERS_PREFIX "keep-ctx-alive: "
34 #define ENSC_WRAPPERS_VSERVER 1
35 #define ENSC_WRAPPERS_SOCKET 1
36 #define ENSC_WRAPPERS_UNISTD 1
37 #define ENSC_WRAPPERS_IOSOCK 1
38 #define ENSC_WRAPPERS_IO 1
42 #define CMD_HELP 0x1000
43 #define CMD_VERSION 0x1001
44 #define CMD_SOCKET 0x4000
45 #define CMD_TIMEOUT 0x4001
46 #define CMD_QUIT 0x4002
48 int wrapper_exit_code = 1;
52 { "help", no_argument, 0, CMD_HELP },
53 { "version", no_argument, 0, CMD_VERSION },
54 { "socket", required_argument, 0, CMD_SOCKET },
55 { "timeout", required_argument, 0, CMD_TIMEOUT },
56 { "quit", required_argument, 0, CMD_QUIT },
61 showHelp(int fd, char const *cmd, int res)
63 WRITE_MSG(fd, "Usage:\n ");
66 " --socket <filename> [--timeout <seconds>]\n"
68 "Please report bugs to " PACKAGE_BUGREPORT "\n");
77 "keep-ctx-alive " VERSION " -- keeps a context alive\n"
78 "This program is part of " PACKAGE_STRING "\n\n"
79 "Copyright (C) 2004 Enrico Scholz\n"
80 VERSION_COPYRIGHT_DISCLAIMER);
87 xid_t xid = Evc_get_task_xid(0);
88 char buf[sizeof(xid)*3 + 2];
91 l = utilvserver_fmt_long(buf, xid);
92 EwriteAll(fd, buf, l);
96 static void handleMessage(int fd)
100 struct sockaddr_un addr;
101 socklen_t addr_len = sizeof(addr);
102 int new_fd = Eaccept(fd, &addr, &addr_len);
104 len = Erecv(new_fd, buf, sizeof buf,0);
107 // TODO: handle message???
112 sendQuitSignal(char const *filename, char const *msg)
115 struct sockaddr_un addr;
117 ENSC_INIT_UNIX_SOCK(addr, filename);
118 fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
119 Econnect(fd, &addr, sizeof(addr));
121 if (msg) EsendAll(fd, msg, strlen(msg));
128 doit(char const *filename, struct timeval *timeout)
131 struct sockaddr_un sock;
135 ENSC_INIT_UNIX_SOCK(sock, filename);
137 fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
138 Ebind(fd, &sock, sizeof sock);
148 Eselect(fd+1, &fd_set, 0,0, timeout);
149 if (FD_ISSET(fd, &fd_set)) handleMessage(fd);
156 int main(int argc, char *argv[])
158 char const * socket_name = 0;
159 struct timeval timeout = { 0,0 };
160 struct timeval *timeout_ptr = 0;
161 char const * quit_msg = false;
164 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
168 case CMD_HELP : showHelp(1, argv[0], 0);
169 case CMD_VERSION : showVersion();
170 case CMD_SOCKET : socket_name = optarg; break;
171 case CMD_QUIT : quit_msg = optarg; break;
173 timeout.tv_sec = atoi(optarg);
174 timeout_ptr = &timeout;
177 WRITE_MSG(2, "Try '");
178 WRITE_STR(2, argv[0]);
179 WRITE_MSG(2, " --help\" for more information.\n");
186 WRITE_MSG(2, "reserve-context: No socketname specified\n");
188 return sendQuitSignal(socket_name, quit_msg);
190 return doit(socket_name, timeout_ptr);