4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.11 2008/02/20 20:16:07 james
14 * *** empty log message ***
16 * Revision 1.10 2008/02/20 19:44:37 james
19 * Revision 1.9 2008/02/20 18:49:11 staffcvs
20 * *** empty log message ***
22 * Revision 1.8 2008/02/20 18:33:37 james
23 * *** empty log message ***
25 * Revision 1.7 2008/02/20 18:31:44 james
26 * *** empty log message ***
28 * Revision 1.6 2008/02/20 17:18:33 james
29 * *** empty log message ***
31 * Revision 1.5 2008/02/20 15:50:14 james
32 * *** empty log message ***
34 * Revision 1.4 2008/02/20 02:11:35 james
35 * *** empty log message ***
37 * Revision 1.3 2008/02/14 02:46:44 james
38 * *** empty log message ***
40 * Revision 1.2 2008/02/14 00:57:58 james
41 * *** empty log message ***
43 * Revision 1.1 2008/02/05 14:25:49 james
44 * *** empty log message ***
48 #include <sys/types.h>
52 #include <sys/utsname.h>
61 extern void usage (void);
63 static char hostname[1024];
74 ret = (int) strtol (a, &end, 0);
83 fatal_moan (char *fmt, ...)
89 vfprintf (stderr, fmt, ap);
98 /*make the path in fmt from home (hence the name) */
101 mome (char *fmt, ...)
111 home = getenv ("HOME");
115 homelen = strlen (home) + 1;
117 size = 1024 + homelen;
122 fatal_moan ("malloc failed");
132 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
135 if (n > -1 && n < (size - homelen))
138 if (n > -1) /* glibc 2.1 */
139 size = homelen + n + 1;
141 size *= 2; /* twice the old size */
143 buf = realloc (buf, size);
146 fatal_moan ("malloc failed");
158 char *sockdir = mome ("/.sympathy/");
159 DIR *dir = opendir (sockdir);
163 int hostname_len = strlen (hostname);
166 fatal_moan ("can't examine %s for sockets", sockdir);
171 while ((ent = readdir (dir)))
173 sn = mome (".sympathy/%s", ent->d_name);
174 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
180 s = socket_connect (sn);
184 printf ("\t%s (Active)\n", ent->d_name);
189 if (strncmp (ent->d_name, hostname, hostname_len))
191 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
195 printf ("\t%s (Dead, wiped)\n", ent->d_name);
216 strcpy (hostname, "unknown.");
221 strcpy (hostname, name.nodename);
222 strcat (hostname, ".");
227 main (int argc, char *argv[])
231 extern int optind, opterr, optopt;
236 Socket *server_socket = NULL, *client_socket = NULL;
246 memset (oflags, 0, sizeof (oflags));
247 memset (oargs, 0, sizeof (oargs));
249 "sympathy -t [-K] [-d serialdev|-p] [-b baud] [-f] [-L log]\n"
250 "sympathy -s [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-F] [-k skt]\n"
252 "sympathy [-s -c] [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-k skt]\n"
254 "sympathy -c [-H] -k skt\n"
255 "sympathy -r id [-H]\n" "sympathy {-l|-ls}\n"
257 while ((c = getopt (argc, argv, "tscr:lKHd:pb:fL:Fk:n:")) != EOF)
266 if ((c > 64) && (c < 91))
271 else if ((c > 96) && (c < 123))
278 fprintf (stderr, "unknown option %c\n", c);
285 /*Compatability for screen's ls */
290 if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r']
293 /*If no mode is specified behave like screen */
302 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
307 fatal_moan ("specifiy exactly one of ( -c and or -s ), -t, -r and -l");
312 return list_sockets ();
315 if (oflags['r'] && oflags['k'])
316 fatal_moan ("-k is incompatible with -r");
321 history = safe_atoi (oargs['n']);
323 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
326 fatal_moan ("agrument to -n must be greater than zero");
332 int id = safe_atoi (oargs['r']);
334 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
337 oargs['k'] = mome ("/.sympathy/%s%d", hostname, id);
342 if (oflags['p'] && oflags['d'])
343 fatal_moan ("-p incompatible with -d");
345 /*implement server and client by opening the server socket to prevent */
346 /*a race condition, and then forking and munging the cmd line options */
347 /*in the parent and child so that the child is the server and the */
348 /*parent becomes its client */
350 if (oflags['c'] && oflags['s'] && oflags['F'])
351 fatal_moan ("-F is incompatible with -c -s");
353 if (oflags['s'] && !oflags['k'])
356 path = mome ("/.sympathy");
360 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
366 server_socket = socket_listen (oargs['k']);
368 fatal_moan ("failed to create socket %s for listening", oargs['k']);
371 if (oflags['s'] && oflags['c'])
375 case 0: /*child becomes the server */
380 fatal_moan ("fork failed");
392 socket_free_parent (server_socket);
393 server_socket = NULL;
398 if (oflags['c'] && !oflags['k'])
399 fatal_moan ("-c requires a socket to be specified with -s or -k");
401 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
402 || oflags['L']) && oflags['c'])
403 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
405 if (oflags['t'] || oflags['s'])
407 if (!oflags['p'] && !oflags['d'])
411 if (oflags['s'] && !oflags['F'])
413 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
417 if (oflags['s'] || oflags['t'])
422 log = file_log_new (oargs['L']);
424 fatal_moan ("unable to access log file %s", oargs['L']);
429 tty = ptty_open (NULL, NULL);
431 fatal_moan ("unable to open a ptty");
436 serial_open (oargs['d'],
437 oflags['K'] ? SERIAL_LOCK_ACTIVE :
438 SERIAL_LOCK_PASSIVE);
440 fatal_moan ("unable to open serial port %s", oargs['d']);
445 int baud = safe_atoi (oargs['b']);
448 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
450 tty_set_baud (tty, baud);
454 tty_set_flow (tty, oflags['f'] ? 1 : 0);
461 client_socket = socket_connect (oargs['k']);
463 fatal_moan ("failed to connect to socket %s", oargs['k']);
469 if (oflags['c'] || oflags['t'])
474 ansi=ansi_new_html(stdout);
478 terminal_register_handlers ();
479 ansi=ansi_new_from_terminal(terminal_open(0,1));
480 ansi->reset(ansi, NULL);
484 mainloop (tty, server_socket, client_socket, ansi, log, history);
498 socket_free (server_socket);
500 socket_free (client_socket);
503 printf ("you have now exited sympathy\n");