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>
32 #include <sys/select.h>
34 #define ENSC_WRAPPERS_PREFIX "keep-ctx-alive: "
35 #define ENSC_WRAPPERS_VSERVER 1
36 #define ENSC_WRAPPERS_SOCKET 1
37 #define ENSC_WRAPPERS_UNISTD 1
38 #define ENSC_WRAPPERS_IOSOCK 1
39 #define ENSC_WRAPPERS_IO 1
43 #define CMD_HELP 0x1000
44 #define CMD_VERSION 0x1001
45 #define CMD_SOCKET 0x4000
46 #define CMD_TIMEOUT 0x4001
47 #define CMD_QUIT 0x4002
49 int wrapper_exit_code = 1;
53 { "help", no_argument, 0, CMD_HELP },
54 { "version", no_argument, 0, CMD_VERSION },
55 { "socket", required_argument, 0, CMD_SOCKET },
56 { "timeout", required_argument, 0, CMD_TIMEOUT },
57 { "quit", required_argument, 0, CMD_QUIT },
62 showHelp(int fd, char const *cmd, int res)
64 WRITE_MSG(fd, "Usage:\n ");
67 " --socket <filename> [--timeout <seconds>]\n"
69 "Please report bugs to " PACKAGE_BUGREPORT "\n");
78 "keep-ctx-alive " VERSION " -- keeps a context alive\n"
79 "This program is part of " PACKAGE_STRING "\n\n"
80 "Copyright (C) 2004 Enrico Scholz\n"
81 VERSION_COPYRIGHT_DISCLAIMER);
88 xid_t xid = Evc_get_task_xid(0);
89 char buf[sizeof(xid)*3 + 2];
92 l = utilvserver_fmt_long(buf, xid);
93 EwriteAll(fd, buf, l);
97 static void handleMessage(int fd)
101 struct sockaddr_un addr;
102 socklen_t addr_len = sizeof(addr);
103 int new_fd = Eaccept(fd, &addr, &addr_len);
105 len = Erecv(new_fd, buf, sizeof buf,0);
108 // TODO: handle message???
113 sendQuitSignal(char const *filename, char const *msg)
116 struct sockaddr_un addr;
118 ENSC_INIT_UNIX_SOCK(addr, filename);
119 fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
120 Econnect(fd, &addr, sizeof(addr));
122 if (msg) EsendAll(fd, msg, strlen(msg));
129 doit(char const *filename, struct timeval *timeout)
132 struct sockaddr_un sock;
136 ENSC_INIT_UNIX_SOCK(sock, filename);
138 fd = Esocket(PF_UNIX, SOCK_STREAM, 0);
139 Ebind(fd, &sock, sizeof sock);
149 Eselect(fd+1, &fdset, 0,0, timeout);
150 if (FD_ISSET(fd, &fdset)) handleMessage(fd);
157 int main(int argc, char *argv[])
159 char const * socket_name = 0;
160 struct timeval timeout = { 0,0 };
161 struct timeval *timeout_ptr = 0;
162 char const * quit_msg = false;
165 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
169 case CMD_HELP : showHelp(1, argv[0], 0);
170 case CMD_VERSION : showVersion();
171 case CMD_SOCKET : socket_name = optarg; break;
172 case CMD_QUIT : quit_msg = optarg; break;
174 timeout.tv_sec = atoi(optarg);
175 timeout_ptr = &timeout;
178 WRITE_MSG(2, "Try '");
179 WRITE_STR(2, argv[0]);
180 WRITE_MSG(2, " --help' for more information.\n");
187 WRITE_MSG(2, "reserve-context: No socketname specified\n");
189 return sendQuitSignal(socket_name, quit_msg);
191 return doit(socket_name, timeout_ptr);