4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.8 2008/02/20 18:33:37 james
14 * *** empty log message ***
16 * Revision 1.7 2008/02/20 18:31:44 james
17 * *** empty log message ***
19 * Revision 1.6 2008/02/20 17:18:33 james
20 * *** empty log message ***
22 * Revision 1.5 2008/02/20 15:50:14 james
23 * *** empty log message ***
25 * Revision 1.4 2008/02/20 02:11:35 james
26 * *** empty log message ***
28 * Revision 1.3 2008/02/14 02:46:44 james
29 * *** empty log message ***
31 * Revision 1.2 2008/02/14 00:57:58 james
32 * *** empty log message ***
34 * Revision 1.1 2008/02/05 14:25:49 james
35 * *** empty log message ***
39 #include <sys/types.h>
43 #include <sys/utsname.h>
49 extern void usage (void);
51 static char hostname[1024];
62 ret = (int) strtol (a, &end, 0);
71 fatal_moan (char *fmt, ...)
77 vfprintf (stderr, fmt, ap);
86 /*make the path in fmt from home (hence the name) */
99 home = getenv ("HOME");
103 homelen = strlen (home) + 1;
105 size = 1024 + homelen;
110 fatal_moan ("malloc failed");
120 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
123 if (n > -1 && n < (size - homelen))
126 if (n > -1) /* glibc 2.1 */
127 size = homelen + n + 1;
129 size *= 2; /* twice the old size */
131 buf = realloc (buf, size);
134 fatal_moan ("malloc failed");
146 char *sockdir = mome ("/.sympathy/");
147 DIR *dir = opendir (sockdir);
151 int hostname_len = strlen (hostname);
154 fatal_moan ("can't examine %s for sockets", sockdir);
159 while ((ent = readdir (dir)))
161 sn = mome (".sympathy/%s", ent->d_name);
162 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
168 s = socket_connect (sn);
172 printf ("\t%s (Active)\n", ent->d_name);
177 if (strncmp (ent->d_name, hostname, hostname_len))
179 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
183 printf ("\t%s (Dead, wiped)\n", ent->d_name);
204 strcpy (hostname, "unknown.");
209 strcpy (hostname, name.nodename);
210 strcat (hostname, ".");
215 main (int argc, char *argv[])
219 extern int optind, opterr, optopt;
224 Socket *server_socket = NULL, *client_socket = NULL;
234 memset (oflags, 0, sizeof (oflags));
235 memset (oargs, 0, sizeof (oargs));
237 "sympathy -t [-K] [-d serialdev|-p] [-b baud] [-f] [-L log]\n"
238 "sympathy -s [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-F] [-k skt]\n"
240 "sympathy [-s -c] [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-k skt]\n"
242 "sympathy -c [-H] -k skt\n"
243 "sympathy -r id [-H]\n" "sympathy {-l|-ls}\n"
245 while ((c = getopt (argc, argv, "tscr:lKHd:pb:fL:Fk:n:")) != EOF)
254 if ((c > 64) && (c < 91))
259 else if ((c > 96) && (c < 123))
266 fprintf (stderr, "unknown option %c\n", c);
273 /*Compatability for screen's ls */
278 if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r']
281 /*If no mode is specified behave like screen */
290 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
295 fatal_moan ("specifiy exactly one of ( -c and or -s ), -t, -r and -l");
300 return list_sockets ();
303 if (oflags['r'] && oflags['k'])
304 fatal_moan ("-k is incompatible with -r");
309 history = safe_atoi (oargs['n']);
311 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
314 fatal_moan ("agrument to -n must be greater than zero");
320 int id = safe_atoi (oargs['r']);
322 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
325 oargs['k'] = mome ("/.sympathy/%s%d", hostname, id);
330 if (oflags['p'] && oflags['d'])
331 fatal_moan ("-p incompatible with -d");
333 /*implement server and client by opening the server socket to prevent */
334 /*a race condition, and then forking and munging the cmd line options */
335 /*in the parent and child so that the child is the server and the */
336 /*parent becomes its client */
338 if (oflags['c'] && oflags['s'] && oflags['F'])
339 fatal_moan ("-F is incompatible with -c -s");
341 if (oflags['s'] && !oflags['k'])
344 path = mome ("/.sympathy");
348 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
354 server_socket = socket_listen (oargs['k']);
356 fatal_moan ("failed to create socket %s for listening", oargs['k']);
359 if (oflags['s'] && oflags['c'])
363 case 0: /*child becomes the server */
368 fatal_moan ("fork failed");
380 socket_free_parent (server_socket);
381 server_socket = NULL;
386 if (oflags['c'] && !oflags['k'])
387 fatal_moan ("-c requires a socket to be specified with -s or -k");
389 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
390 || oflags['L']) && oflags['c'])
391 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
393 if (oflags['t'] || oflags['s'])
395 if (!oflags['p'] && !oflags['d'])
400 if (oflags['s'] || oflags['t'])
405 log = file_log_new (oargs['L']);
407 fatal_moan ("unable to access log file %s", oargs['L']);
412 tty = ptty_open (NULL, NULL);
414 fatal_moan ("unable to open a ptty");
419 serial_open (oargs['d'],
420 oflags['K'] ? SERIAL_LOCK_ACTIVE :
421 SERIAL_LOCK_PASSIVE);
423 fatal_moan ("unable to open serial port %s", oargs['d']);
428 int baud = safe_atoi (oargs['b']);
431 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
433 tty_set_baud (tty, baud);
437 tty_set_flow (tty, oflags['f'] ? 1 : 0);
444 client_socket = socket_connect (oargs['k']);
446 fatal_moan ("failed to connect to socket %s", oargs['k']);
451 if (oflags['s'] && !oflags['F'])
453 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
456 if (oflags['c'] || oflags['t'])
461 fatal_moan ("fix a bug in HTML dispatcher before this works");
465 ansi = (ANSI *) malloc (sizeof (ANSI));
466 memset (ansi, 0, sizeof (ANSI));
468 terminal_register_handlers ();
469 ansi->terminal = terminal_open (0, 1);
470 ansi_reset (ansi, NULL);
474 mainloop (tty, server_socket, client_socket, ansi, log, history);
478 ansi_terminal_reset (ansi);
488 socket_free (server_socket);
490 socket_free (client_socket);
492 printf ("you have now exited sympathy\n");