4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.7 2008/02/20 18:31:44 james
14 * *** empty log message ***
16 * Revision 1.6 2008/02/20 17:18:33 james
17 * *** empty log message ***
19 * Revision 1.5 2008/02/20 15:50:14 james
20 * *** empty log message ***
22 * Revision 1.4 2008/02/20 02:11:35 james
23 * *** empty log message ***
25 * Revision 1.3 2008/02/14 02:46:44 james
26 * *** empty log message ***
28 * Revision 1.2 2008/02/14 00:57:58 james
29 * *** empty log message ***
31 * Revision 1.1 2008/02/05 14:25:49 james
32 * *** empty log message ***
36 #include <sys/types.h>
40 #include <sys/utsname.h>
46 extern void usage (void);
48 static char hostname[1024];
59 ret = (int) strtol (a, &end, 0);
68 fatal_moan (char *fmt, ...)
74 vfprintf (stderr, fmt, ap);
83 /*make the path in fmt from home (hence the name) */
96 home = getenv ("HOME");
100 homelen = strlen (home) + 1;
102 size = 1024 + homelen;
107 fatal_moan ("malloc failed");
117 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
120 if (n > -1 && n < (size - homelen))
123 if (n > -1) /* glibc 2.1 */
124 size = homelen + n + 1;
126 size *= 2; /* twice the old size */
128 buf = realloc (buf, size);
131 fatal_moan ("malloc failed");
143 char *sockdir = mome ("/.sympathy/");
144 DIR *dir = opendir (sockdir);
148 int hostname_len = strlen (hostname);
151 fatal_moan ("can't examine %s for sockets", sockdir);
156 while ((ent = readdir (dir)))
158 sn = mome (".sympathy/%s", ent->d_name);
159 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
165 s = socket_connect (sn);
169 printf ("\t%s (Active)\n", ent->d_name);
174 if (strncmp (ent->d_name, hostname, hostname_len))
176 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
180 printf ("\t%s (Dead, wiped)\n", ent->d_name);
201 strcpy (hostname, "unknown.");
206 strcpy (hostname, name.nodename);
207 strcat (hostname, ".");
212 main (int argc, char *argv[])
216 extern int optind, opterr, optopt;
221 Socket *server_socket = NULL, *client_socket = NULL;
231 memset (oflags, 0, sizeof (oflags));
232 memset (oargs, 0, sizeof (oargs));
234 "sympathy -t [-K] [-d serialdev|-p] [-b baud] [-f] [-L log]\n"
235 "sympathy -s [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-F] [-k skt]\n"
237 "sympathy [-s -c] [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-k skt]\n"
239 "sympathy -c [-H] -k skt\n"
240 "sympathy -r id [-H]\n" "sympathy {-l|-ls}\n"
242 while ((c = getopt (argc, argv, "tscr:lKHd:pb:fL:Fk:n:")) != EOF)
251 if ((c > 64) && (c < 91))
256 else if ((c > 96) && (c < 123))
263 fprintf (stderr, "unknown option %c\n", c);
270 /*Compatability for screen's ls */
275 if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r']
278 /*If no mode is specified behave like screen */
287 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
292 fatal_moan ("specifiy exactly one of ( -c and or -s ), -t, -r and -l");
297 return list_sockets ();
300 if (oflags['r'] && oflags['k'])
301 fatal_moan ("-k is incompatible with -r");
306 nhistory = safe_atoi (oargs['n']);
308 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
311 fatal_moan ("agrument to -n must be greater than zero");
317 int id = safe_atoi (oargs['r']);
319 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
322 oargs['k'] = mome ("/.sympathy/%s%d", hostname, id);
327 if (oflags['p'] && oflags['d'])
328 fatal_moan ("-p incompatible with -d");
330 /*implement server and client by opening the server socket to prevent */
331 /*a race condition, and then forking and munging the cmd line options */
332 /*in the parent and child so that the child is the server and the */
333 /*parent becomes its client */
335 if (oflags['c'] && oflags['s'] && oflags['F'])
336 fatal_moan ("-F is incompatible with -c -s");
338 if (oflags['s'] && !oflags['k'])
341 path = mome ("/.sympathy");
345 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
351 server_socket = socket_listen (oargs['k']);
353 fatal_moan ("failed to create socket %s for listening", oargs['k']);
356 if (oflags['s'] && oflags['c'])
360 case 0: /*child becomes the server */
365 fatal_moan ("fork failed");
377 socket_free_parent (server_socket);
378 server_socket = NULL;
383 if (oflags['c'] && !oflags['k'])
384 fatal_moan ("-c requires a socket to be specified with -s or -k");
386 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
387 || oflags['L']) && oflags['c'])
388 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
390 if (oflags['t'] || oflags['s'])
392 if (!oflags['p'] && !oflags['d'])
397 if (oflags['s'] || oflags['t'])
402 log = file_log_new (oargs['L']);
404 fatal_moan ("unable to access log file %s", oargs['L']);
409 tty = ptty_open (NULL, NULL);
411 fatal_moan ("unable to open a ptty");
416 serial_open (oargs['d'],
417 oflags['K'] ? SERIAL_LOCK_ACTIVE :
418 SERIAL_LOCK_PASSIVE);
420 fatal_moan ("unable to open serial port %s", oargs['d']);
425 int baud = safe_atoi (oargs['b']);
428 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
430 tty_set_baud (tty, baud);
434 tty_set_flow (tty, oflags['f'] ? 1 : 0);
441 client_socket = socket_connect (oargs['k']);
443 fatal_moan ("failed to connect to socket %s", oargs['k']);
448 if (oflags['s'] && !oflags['F'])
450 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
453 if (oflags['c'] || oflags['t'])
458 fatal_moan ("fix a bug in HTML dispatcher before this works");
462 ansi = (ANSI *) malloc (sizeof (ANSI));
463 memset (ansi, 0, sizeof (ANSI));
465 terminal_register_handlers ();
466 ansi->terminal = terminal_open (0, 1);
467 ansi_reset (ansi, NULL);
471 mainloop (tty, server_socket, client_socket, ansi, log, nhistory);
475 ansi_terminal_reset (ansi);
485 socket_free (server_socket);
487 socket_free (client_socket);
489 printf ("you have now exited sympathy\n");