4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.13 2008/02/24 00:42:53 james
15 * *** empty log message ***
17 * Revision 1.12 2008/02/23 11:48:52 james
18 * *** empty log message ***
20 * Revision 1.11 2008/02/20 20:16:07 james
21 * *** empty log message ***
23 * Revision 1.10 2008/02/20 19:44:37 james
26 * Revision 1.9 2008/02/20 18:49:11 staffcvs
27 * *** empty log message ***
29 * Revision 1.8 2008/02/20 18:33:37 james
30 * *** empty log message ***
32 * Revision 1.7 2008/02/20 18:31:44 james
33 * *** empty log message ***
35 * Revision 1.6 2008/02/20 17:18:33 james
36 * *** empty log message ***
38 * Revision 1.5 2008/02/20 15:50:14 james
39 * *** empty log message ***
41 * Revision 1.4 2008/02/20 02:11:35 james
42 * *** empty log message ***
44 * Revision 1.3 2008/02/14 02:46:44 james
45 * *** empty log message ***
47 * Revision 1.2 2008/02/14 00:57:58 james
48 * *** empty log message ***
50 * Revision 1.1 2008/02/05 14:25:49 james
51 * *** empty log message ***
55 #include <sys/types.h>
59 #include <sys/utsname.h>
68 extern void usage (void);
70 static char hostname[1024];
81 ret = (int) strtol (a, &end, 0);
90 fatal_moan (char *fmt, ...)
96 vfprintf (stderr, fmt, ap);
105 /*make the path in fmt from home (hence the name) */
108 mome (char *fmt, ...)
118 home = getenv ("HOME");
122 homelen = strlen (home) + 1;
124 size = 1024 + homelen;
129 fatal_moan ("malloc failed");
139 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
142 if (n > -1 && n < (size - homelen))
145 if (n > -1) /* glibc 2.1 */
146 size = homelen + n + 1;
148 size *= 2; /* twice the old size */
150 buf = realloc (buf, size);
153 fatal_moan ("malloc failed");
165 char *sockdir = mome ("/.sympathy/");
166 DIR *dir = opendir (sockdir);
170 int hostname_len = strlen (hostname);
173 fatal_moan ("can't examine %s for sockets", sockdir);
178 while ((ent = readdir (dir)))
180 sn = mome (".sympathy/%s", ent->d_name);
181 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
187 s = socket_connect (sn);
191 printf ("\t%s (Active)\n", ent->d_name);
196 if (strncmp (ent->d_name, hostname, hostname_len))
198 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
202 printf ("\t%s (Dead, wiped)\n", ent->d_name);
223 strcpy (hostname, "unknown.");
228 strcpy (hostname, name.nodename);
229 strcat (hostname, ".");
234 main (int argc, char *argv[])
238 extern int optind, opterr, optopt;
243 Socket *server_socket = NULL, *client_socket = NULL;
253 memset (oflags, 0, sizeof (oflags));
254 memset (oargs, 0, sizeof (oargs));
256 "sympathy -t [-K] [-d serialdev|-p] [-b baud] [-f] [-L log]\n"
257 "sympathy -s [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-F] [-k skt]\n"
259 "sympathy [-s -c] [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-k skt]\n"
261 "sympathy -c [-H] -k skt\n"
262 "sympathy -r id [-H]\n" "sympathy {-l|-ls}\n"
264 while ((c = getopt (argc, argv, "utscr:lKHd:pb:fL:Fk:n:")) != EOF)
273 if ((c > 64) && (c < 91))
278 else if ((c > 96) && (c < 123))
285 fprintf (stderr, "unknown option %c\n", c);
292 /*Compatability for screen's ls */
297 if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r']
300 /*If no mode is specified behave like screen */
309 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
314 fatal_moan ("specifiy exactly one of ( -c and or -s ), -t, -r and -l");
319 return list_sockets ();
322 if (oflags['r'] && oflags['k'])
323 fatal_moan ("-k is incompatible with -r");
328 history = safe_atoi (oargs['n']);
330 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
333 fatal_moan ("agrument to -n must be greater than zero");
339 int id = safe_atoi (oargs['r']);
341 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
344 oargs['k'] = mome ("/.sympathy/%s%d", hostname, id);
349 if (oflags['p'] && oflags['d'])
350 fatal_moan ("-p incompatible with -d");
352 /*implement server and client by opening the server socket to prevent */
353 /*a race condition, and then forking and munging the cmd line options */
354 /*in the parent and child so that the child is the server and the */
355 /*parent becomes its client */
357 if (oflags['c'] && oflags['s'] && oflags['F'])
358 fatal_moan ("-F is incompatible with -c -s");
360 if (oflags['s'] && !oflags['k'])
363 path = mome ("/.sympathy");
367 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
373 server_socket = socket_listen (oargs['k']);
375 fatal_moan ("failed to create socket %s for listening", oargs['k']);
378 if (oflags['s'] && oflags['c'])
382 case 0: /*child becomes the server */
387 fatal_moan ("fork failed");
399 socket_free_parent (server_socket);
400 server_socket = NULL;
405 if (oflags['c'] && !oflags['k'])
406 fatal_moan ("-c requires a socket to be specified with -s or -k");
408 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
409 || oflags['L']) && oflags['c'])
410 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
412 if (oflags['t'] || oflags['s'])
414 if (!oflags['p'] && !oflags['d'])
418 if (oflags['s'] && !oflags['F'])
420 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
424 if (oflags['s'] || oflags['t'])
429 log = file_log_new (oargs['L']);
431 fatal_moan ("unable to access log file %s", oargs['L']);
436 tty = ptty_open (NULL, NULL);
438 fatal_moan ("unable to open a ptty");
443 serial_open (oargs['d'],
444 oflags['K'] ? SERIAL_LOCK_ACTIVE :
445 SERIAL_LOCK_PASSIVE);
447 fatal_moan ("unable to open serial port %s", oargs['d']);
452 int baud = safe_atoi (oargs['b']);
455 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
457 tty_set_baud (tty, baud);
461 tty_set_flow (tty, oflags['f'] ? 1 : 0);
468 client_socket = socket_connect (oargs['k']);
470 fatal_moan ("failed to connect to socket %s", oargs['k']);
476 if (oflags['c'] || oflags['t'])
481 ansi = ansi_new_html (stdout);
485 terminal_register_handlers ();
486 ansi = ansi_new_from_terminal (terminal_open (0, 1),oflags['u'] ? 0:1);
487 ansi->reset (ansi, NULL);
491 mainloop (tty, server_socket, client_socket, ansi, log, history);
505 socket_free (server_socket);
507 socket_free (client_socket);
510 printf ("you have now exited sympathy\n");