4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.12 2008/02/23 11:48:52 james
15 * *** empty log message ***
17 * Revision 1.11 2008/02/20 20:16:07 james
18 * *** empty log message ***
20 * Revision 1.10 2008/02/20 19:44:37 james
23 * Revision 1.9 2008/02/20 18:49:11 staffcvs
24 * *** empty log message ***
26 * Revision 1.8 2008/02/20 18:33:37 james
27 * *** empty log message ***
29 * Revision 1.7 2008/02/20 18:31:44 james
30 * *** empty log message ***
32 * Revision 1.6 2008/02/20 17:18:33 james
33 * *** empty log message ***
35 * Revision 1.5 2008/02/20 15:50:14 james
36 * *** empty log message ***
38 * Revision 1.4 2008/02/20 02:11:35 james
39 * *** empty log message ***
41 * Revision 1.3 2008/02/14 02:46:44 james
42 * *** empty log message ***
44 * Revision 1.2 2008/02/14 00:57:58 james
45 * *** empty log message ***
47 * Revision 1.1 2008/02/05 14:25:49 james
48 * *** empty log message ***
52 #include <sys/types.h>
56 #include <sys/utsname.h>
65 extern void usage (void);
67 static char hostname[1024];
78 ret = (int) strtol (a, &end, 0);
87 fatal_moan (char *fmt, ...)
93 vfprintf (stderr, fmt, ap);
102 /*make the path in fmt from home (hence the name) */
105 mome (char *fmt, ...)
115 home = getenv ("HOME");
119 homelen = strlen (home) + 1;
121 size = 1024 + homelen;
126 fatal_moan ("malloc failed");
136 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
139 if (n > -1 && n < (size - homelen))
142 if (n > -1) /* glibc 2.1 */
143 size = homelen + n + 1;
145 size *= 2; /* twice the old size */
147 buf = realloc (buf, size);
150 fatal_moan ("malloc failed");
162 char *sockdir = mome ("/.sympathy/");
163 DIR *dir = opendir (sockdir);
167 int hostname_len = strlen (hostname);
170 fatal_moan ("can't examine %s for sockets", sockdir);
175 while ((ent = readdir (dir)))
177 sn = mome (".sympathy/%s", ent->d_name);
178 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
184 s = socket_connect (sn);
188 printf ("\t%s (Active)\n", ent->d_name);
193 if (strncmp (ent->d_name, hostname, hostname_len))
195 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
199 printf ("\t%s (Dead, wiped)\n", ent->d_name);
220 strcpy (hostname, "unknown.");
225 strcpy (hostname, name.nodename);
226 strcat (hostname, ".");
231 main (int argc, char *argv[])
235 extern int optind, opterr, optopt;
240 Socket *server_socket = NULL, *client_socket = NULL;
250 memset (oflags, 0, sizeof (oflags));
251 memset (oargs, 0, sizeof (oargs));
253 "sympathy -t [-K] [-d serialdev|-p] [-b baud] [-f] [-L log]\n"
254 "sympathy -s [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-F] [-k skt]\n"
256 "sympathy [-s -c] [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-k skt]\n"
258 "sympathy -c [-H] -k skt\n"
259 "sympathy -r id [-H]\n" "sympathy {-l|-ls}\n"
261 while ((c = getopt (argc, argv, "tscr:lKHd:pb:fL:Fk:n:")) != EOF)
270 if ((c > 64) && (c < 91))
275 else if ((c > 96) && (c < 123))
282 fprintf (stderr, "unknown option %c\n", c);
289 /*Compatability for screen's ls */
294 if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r']
297 /*If no mode is specified behave like screen */
306 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
311 fatal_moan ("specifiy exactly one of ( -c and or -s ), -t, -r and -l");
316 return list_sockets ();
319 if (oflags['r'] && oflags['k'])
320 fatal_moan ("-k is incompatible with -r");
325 history = safe_atoi (oargs['n']);
327 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
330 fatal_moan ("agrument to -n must be greater than zero");
336 int id = safe_atoi (oargs['r']);
338 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
341 oargs['k'] = mome ("/.sympathy/%s%d", hostname, id);
346 if (oflags['p'] && oflags['d'])
347 fatal_moan ("-p incompatible with -d");
349 /*implement server and client by opening the server socket to prevent */
350 /*a race condition, and then forking and munging the cmd line options */
351 /*in the parent and child so that the child is the server and the */
352 /*parent becomes its client */
354 if (oflags['c'] && oflags['s'] && oflags['F'])
355 fatal_moan ("-F is incompatible with -c -s");
357 if (oflags['s'] && !oflags['k'])
360 path = mome ("/.sympathy");
364 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
370 server_socket = socket_listen (oargs['k']);
372 fatal_moan ("failed to create socket %s for listening", oargs['k']);
375 if (oflags['s'] && oflags['c'])
379 case 0: /*child becomes the server */
384 fatal_moan ("fork failed");
396 socket_free_parent (server_socket);
397 server_socket = NULL;
402 if (oflags['c'] && !oflags['k'])
403 fatal_moan ("-c requires a socket to be specified with -s or -k");
405 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
406 || oflags['L']) && oflags['c'])
407 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
409 if (oflags['t'] || oflags['s'])
411 if (!oflags['p'] && !oflags['d'])
415 if (oflags['s'] && !oflags['F'])
417 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
421 if (oflags['s'] || oflags['t'])
426 log = file_log_new (oargs['L']);
428 fatal_moan ("unable to access log file %s", oargs['L']);
433 tty = ptty_open (NULL, NULL);
435 fatal_moan ("unable to open a ptty");
440 serial_open (oargs['d'],
441 oflags['K'] ? SERIAL_LOCK_ACTIVE :
442 SERIAL_LOCK_PASSIVE);
444 fatal_moan ("unable to open serial port %s", oargs['d']);
449 int baud = safe_atoi (oargs['b']);
452 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
454 tty_set_baud (tty, baud);
458 tty_set_flow (tty, oflags['f'] ? 1 : 0);
465 client_socket = socket_connect (oargs['k']);
467 fatal_moan ("failed to connect to socket %s", oargs['k']);
473 if (oflags['c'] || oflags['t'])
478 ansi = ansi_new_html (stdout);
482 terminal_register_handlers ();
483 ansi = ansi_new_from_terminal (terminal_open (0, 1));
484 ansi->reset (ansi, NULL);
488 mainloop (tty, server_socket, client_socket, ansi, log, history);
502 socket_free (server_socket);
504 socket_free (client_socket);
507 printf ("you have now exited sympathy\n");