4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.10 2008/02/20 19:44:37 james
16 * Revision 1.9 2008/02/20 18:49:11 staffcvs
17 * *** empty log message ***
19 * Revision 1.8 2008/02/20 18:33:37 james
20 * *** empty log message ***
22 * Revision 1.7 2008/02/20 18:31:44 james
23 * *** empty log message ***
25 * Revision 1.6 2008/02/20 17:18:33 james
26 * *** empty log message ***
28 * Revision 1.5 2008/02/20 15:50:14 james
29 * *** empty log message ***
31 * Revision 1.4 2008/02/20 02:11:35 james
32 * *** empty log message ***
34 * Revision 1.3 2008/02/14 02:46:44 james
35 * *** empty log message ***
37 * Revision 1.2 2008/02/14 00:57:58 james
38 * *** empty log message ***
40 * Revision 1.1 2008/02/05 14:25:49 james
41 * *** empty log message ***
45 #include <sys/types.h>
49 #include <sys/utsname.h>
58 extern void usage (void);
60 static char hostname[1024];
71 ret = (int) strtol (a, &end, 0);
80 fatal_moan (char *fmt, ...)
86 vfprintf (stderr, fmt, ap);
95 /*make the path in fmt from home (hence the name) */
108 home = getenv ("HOME");
112 homelen = strlen (home) + 1;
114 size = 1024 + homelen;
119 fatal_moan ("malloc failed");
129 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
132 if (n > -1 && n < (size - homelen))
135 if (n > -1) /* glibc 2.1 */
136 size = homelen + n + 1;
138 size *= 2; /* twice the old size */
140 buf = realloc (buf, size);
143 fatal_moan ("malloc failed");
155 char *sockdir = mome ("/.sympathy/");
156 DIR *dir = opendir (sockdir);
160 int hostname_len = strlen (hostname);
163 fatal_moan ("can't examine %s for sockets", sockdir);
168 while ((ent = readdir (dir)))
170 sn = mome (".sympathy/%s", ent->d_name);
171 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
177 s = socket_connect (sn);
181 printf ("\t%s (Active)\n", ent->d_name);
186 if (strncmp (ent->d_name, hostname, hostname_len))
188 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
192 printf ("\t%s (Dead, wiped)\n", ent->d_name);
213 strcpy (hostname, "unknown.");
218 strcpy (hostname, name.nodename);
219 strcat (hostname, ".");
224 main (int argc, char *argv[])
228 extern int optind, opterr, optopt;
233 Socket *server_socket = NULL, *client_socket = NULL;
243 memset (oflags, 0, sizeof (oflags));
244 memset (oargs, 0, sizeof (oargs));
246 "sympathy -t [-K] [-d serialdev|-p] [-b baud] [-f] [-L log]\n"
247 "sympathy -s [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-F] [-k skt]\n"
249 "sympathy [-s -c] [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-k skt]\n"
251 "sympathy -c [-H] -k skt\n"
252 "sympathy -r id [-H]\n" "sympathy {-l|-ls}\n"
254 while ((c = getopt (argc, argv, "tscr:lKHd:pb:fL:Fk:n:")) != EOF)
263 if ((c > 64) && (c < 91))
268 else if ((c > 96) && (c < 123))
275 fprintf (stderr, "unknown option %c\n", c);
282 /*Compatability for screen's ls */
287 if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r']
290 /*If no mode is specified behave like screen */
299 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
304 fatal_moan ("specifiy exactly one of ( -c and or -s ), -t, -r and -l");
309 return list_sockets ();
312 if (oflags['r'] && oflags['k'])
313 fatal_moan ("-k is incompatible with -r");
318 history = safe_atoi (oargs['n']);
320 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
323 fatal_moan ("agrument to -n must be greater than zero");
329 int id = safe_atoi (oargs['r']);
331 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
334 oargs['k'] = mome ("/.sympathy/%s%d", hostname, id);
339 if (oflags['p'] && oflags['d'])
340 fatal_moan ("-p incompatible with -d");
342 /*implement server and client by opening the server socket to prevent */
343 /*a race condition, and then forking and munging the cmd line options */
344 /*in the parent and child so that the child is the server and the */
345 /*parent becomes its client */
347 if (oflags['c'] && oflags['s'] && oflags['F'])
348 fatal_moan ("-F is incompatible with -c -s");
350 if (oflags['s'] && !oflags['k'])
353 path = mome ("/.sympathy");
357 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
363 server_socket = socket_listen (oargs['k']);
365 fatal_moan ("failed to create socket %s for listening", oargs['k']);
368 if (oflags['s'] && oflags['c'])
372 case 0: /*child becomes the server */
377 fatal_moan ("fork failed");
389 socket_free_parent (server_socket);
390 server_socket = NULL;
395 if (oflags['c'] && !oflags['k'])
396 fatal_moan ("-c requires a socket to be specified with -s or -k");
398 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
399 || oflags['L']) && oflags['c'])
400 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
402 if (oflags['t'] || oflags['s'])
404 if (!oflags['p'] && !oflags['d'])
408 if (oflags['s'] && !oflags['F'])
410 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
414 if (oflags['s'] || oflags['t'])
419 log = file_log_new (oargs['L']);
421 fatal_moan ("unable to access log file %s", oargs['L']);
426 tty = ptty_open (NULL, NULL);
428 fatal_moan ("unable to open a ptty");
433 serial_open (oargs['d'],
434 oflags['K'] ? SERIAL_LOCK_ACTIVE :
435 SERIAL_LOCK_PASSIVE);
437 fatal_moan ("unable to open serial port %s", oargs['d']);
442 int baud = safe_atoi (oargs['b']);
445 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
447 tty_set_baud (tty, baud);
451 tty_set_flow (tty, oflags['f'] ? 1 : 0);
458 client_socket = socket_connect (oargs['k']);
460 fatal_moan ("failed to connect to socket %s", oargs['k']);
466 if (oflags['c'] || oflags['t'])
471 fatal_moan ("fix a bug in HTML dispatcher before this works");
475 ansi = (ANSI *) malloc (sizeof (ANSI));
476 memset (ansi, 0, sizeof (ANSI));
478 terminal_register_handlers ();
479 ansi->terminal = terminal_open (0, 1);
480 ansi->reset(ansi, NULL);
484 mainloop (tty, server_socket, client_socket, ansi, log, history);
498 socket_free (server_socket);
500 socket_free (client_socket);
502 printf ("you have now exited sympathy\n");