4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.9 2008/02/20 18:49:11 staffcvs
14 * *** empty log message ***
16 * Revision 1.8 2008/02/20 18:33:37 james
17 * *** empty log message ***
19 * Revision 1.7 2008/02/20 18:31:44 james
20 * *** empty log message ***
22 * Revision 1.6 2008/02/20 17:18:33 james
23 * *** empty log message ***
25 * Revision 1.5 2008/02/20 15:50:14 james
26 * *** empty log message ***
28 * Revision 1.4 2008/02/20 02:11:35 james
29 * *** empty log message ***
31 * Revision 1.3 2008/02/14 02:46:44 james
32 * *** empty log message ***
34 * Revision 1.2 2008/02/14 00:57:58 james
35 * *** empty log message ***
37 * Revision 1.1 2008/02/05 14:25:49 james
38 * *** empty log message ***
42 #include <sys/types.h>
46 #include <sys/utsname.h>
55 extern void usage (void);
57 static char hostname[1024];
68 ret = (int) strtol (a, &end, 0);
77 fatal_moan (char *fmt, ...)
83 vfprintf (stderr, fmt, ap);
92 /*make the path in fmt from home (hence the name) */
105 home = getenv ("HOME");
109 homelen = strlen (home) + 1;
111 size = 1024 + homelen;
116 fatal_moan ("malloc failed");
126 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
129 if (n > -1 && n < (size - homelen))
132 if (n > -1) /* glibc 2.1 */
133 size = homelen + n + 1;
135 size *= 2; /* twice the old size */
137 buf = realloc (buf, size);
140 fatal_moan ("malloc failed");
152 char *sockdir = mome ("/.sympathy/");
153 DIR *dir = opendir (sockdir);
157 int hostname_len = strlen (hostname);
160 fatal_moan ("can't examine %s for sockets", sockdir);
165 while ((ent = readdir (dir)))
167 sn = mome (".sympathy/%s", ent->d_name);
168 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
174 s = socket_connect (sn);
178 printf ("\t%s (Active)\n", ent->d_name);
183 if (strncmp (ent->d_name, hostname, hostname_len))
185 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
189 printf ("\t%s (Dead, wiped)\n", ent->d_name);
210 strcpy (hostname, "unknown.");
215 strcpy (hostname, name.nodename);
216 strcat (hostname, ".");
221 main (int argc, char *argv[])
225 extern int optind, opterr, optopt;
230 Socket *server_socket = NULL, *client_socket = NULL;
240 memset (oflags, 0, sizeof (oflags));
241 memset (oargs, 0, sizeof (oargs));
243 "sympathy -t [-K] [-d serialdev|-p] [-b baud] [-f] [-L log]\n"
244 "sympathy -s [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-F] [-k skt]\n"
246 "sympathy [-s -c] [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-k skt]\n"
248 "sympathy -c [-H] -k skt\n"
249 "sympathy -r id [-H]\n" "sympathy {-l|-ls}\n"
251 while ((c = getopt (argc, argv, "tscr:lKHd:pb:fL:Fk:n:")) != EOF)
260 if ((c > 64) && (c < 91))
265 else if ((c > 96) && (c < 123))
272 fprintf (stderr, "unknown option %c\n", c);
279 /*Compatability for screen's ls */
284 if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r']
287 /*If no mode is specified behave like screen */
296 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
301 fatal_moan ("specifiy exactly one of ( -c and or -s ), -t, -r and -l");
306 return list_sockets ();
309 if (oflags['r'] && oflags['k'])
310 fatal_moan ("-k is incompatible with -r");
315 history = safe_atoi (oargs['n']);
317 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
320 fatal_moan ("agrument to -n must be greater than zero");
326 int id = safe_atoi (oargs['r']);
328 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
331 oargs['k'] = mome ("/.sympathy/%s%d", hostname, id);
336 if (oflags['p'] && oflags['d'])
337 fatal_moan ("-p incompatible with -d");
339 /*implement server and client by opening the server socket to prevent */
340 /*a race condition, and then forking and munging the cmd line options */
341 /*in the parent and child so that the child is the server and the */
342 /*parent becomes its client */
344 if (oflags['c'] && oflags['s'] && oflags['F'])
345 fatal_moan ("-F is incompatible with -c -s");
347 if (oflags['s'] && !oflags['k'])
350 path = mome ("/.sympathy");
354 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
360 server_socket = socket_listen (oargs['k']);
362 fatal_moan ("failed to create socket %s for listening", oargs['k']);
365 if (oflags['s'] && oflags['c'])
369 case 0: /*child becomes the server */
374 fatal_moan ("fork failed");
386 socket_free_parent (server_socket);
387 server_socket = NULL;
392 if (oflags['c'] && !oflags['k'])
393 fatal_moan ("-c requires a socket to be specified with -s or -k");
395 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
396 || oflags['L']) && oflags['c'])
397 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
399 if (oflags['t'] || oflags['s'])
401 if (!oflags['p'] && !oflags['d'])
405 if (oflags['s'] && !oflags['F'])
407 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
411 if (oflags['s'] || oflags['t'])
416 log = file_log_new (oargs['L']);
418 fatal_moan ("unable to access log file %s", oargs['L']);
423 tty = ptty_open (NULL, NULL);
425 fatal_moan ("unable to open a ptty");
430 serial_open (oargs['d'],
431 oflags['K'] ? SERIAL_LOCK_ACTIVE :
432 SERIAL_LOCK_PASSIVE);
434 fatal_moan ("unable to open serial port %s", oargs['d']);
439 int baud = safe_atoi (oargs['b']);
442 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
444 tty_set_baud (tty, baud);
448 tty_set_flow (tty, oflags['f'] ? 1 : 0);
455 client_socket = socket_connect (oargs['k']);
457 fatal_moan ("failed to connect to socket %s", oargs['k']);
463 if (oflags['c'] || oflags['t'])
468 fatal_moan ("fix a bug in HTML dispatcher before this works");
472 ansi = (ANSI *) malloc (sizeof (ANSI));
473 memset (ansi, 0, sizeof (ANSI));
475 terminal_register_handlers ();
476 ansi->terminal = terminal_open (0, 1);
477 ansi_reset (ansi, NULL);
481 mainloop (tty, server_socket, client_socket, ansi, log, history);
485 ansi_terminal_reset (ansi);
495 socket_free (server_socket);
497 socket_free (client_socket);
499 printf ("you have now exited sympathy\n");