4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.26 2008/02/29 14:55:09 james
15 * *** empty log message ***
17 * Revision 1.25 2008/02/28 22:43:25 james
18 * *** empty log message ***
20 * Revision 1.24 2008/02/28 22:00:42 james
21 * *** empty log message ***
23 * Revision 1.23 2008/02/28 16:57:51 james
24 * *** empty log message ***
26 * Revision 1.22 2008/02/28 01:47:44 james
27 * *** empty log message ***
29 * Revision 1.21 2008/02/27 16:01:24 james
30 * *** empty log message ***
32 * Revision 1.20 2008/02/27 10:00:34 james
33 * *** empty log message ***
35 * Revision 1.19 2008/02/27 09:47:05 james
36 * *** empty log message ***
38 * Revision 1.18 2008/02/27 09:42:53 james
39 * *** empty log message ***
41 * Revision 1.17 2008/02/27 09:42:21 james
42 * *** empty log message ***
44 * Revision 1.16 2008/02/27 01:31:38 james
45 * *** empty log message ***
47 * Revision 1.15 2008/02/27 01:31:14 james
48 * *** empty log message ***
50 * Revision 1.14 2008/02/24 00:43:55 james
51 * *** empty log message ***
53 * Revision 1.13 2008/02/24 00:42:53 james
54 * *** empty log message ***
56 * Revision 1.12 2008/02/23 11:48:52 james
57 * *** empty log message ***
59 * Revision 1.11 2008/02/20 20:16:07 james
60 * *** empty log message ***
62 * Revision 1.10 2008/02/20 19:44:37 james
65 * Revision 1.9 2008/02/20 18:49:11 staffcvs
66 * *** empty log message ***
68 * Revision 1.8 2008/02/20 18:33:37 james
69 * *** empty log message ***
71 * Revision 1.7 2008/02/20 18:31:44 james
72 * *** empty log message ***
74 * Revision 1.6 2008/02/20 17:18:33 james
75 * *** empty log message ***
77 * Revision 1.5 2008/02/20 15:50:14 james
78 * *** empty log message ***
80 * Revision 1.4 2008/02/20 02:11:35 james
81 * *** empty log message ***
83 * Revision 1.3 2008/02/14 02:46:44 james
84 * *** empty log message ***
86 * Revision 1.2 2008/02/14 00:57:58 james
87 * *** empty log message ***
89 * Revision 1.1 2008/02/05 14:25:49 james
90 * *** empty log message ***
94 #include <sys/types.h>
98 #include <sys/utsname.h>
106 #include "mainloop.h"
108 extern void usage (void);
110 static char hostname[1024];
121 ret = (int) strtol (a, &end, 0);
130 fatal_moan (char *fmt, ...)
136 vfprintf (stderr, fmt, ap);
145 /*make the path in fmt from home (hence the name) */
148 mome (char *fmt, ...)
158 home = getenv ("HOME");
162 homelen = strlen (home) + 1;
164 size = 1024 + homelen;
169 fatal_moan ("malloc failed");
179 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
182 if (n > -1 && n < (size - homelen))
185 if (n > -1) /* glibc 2.1 */
186 size = homelen + n + 1;
188 size *= 2; /* twice the old size */
190 buf = realloc (buf, size);
193 fatal_moan ("malloc failed");
205 char *sockdir = mome ("/.sympathy/");
206 DIR *dir = opendir (sockdir);
210 int hostname_len = strlen (hostname);
213 fatal_moan ("can't examine %s for sockets", sockdir);
218 while ((ent = readdir (dir)))
220 sn = mome (".sympathy/%s", ent->d_name);
221 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
227 s = socket_connect (sn);
231 printf ("\t%s (Active)\n", ent->d_name);
236 if (strncmp (ent->d_name, hostname, hostname_len))
238 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
242 printf ("\t%s (Dead, wiped)\n", ent->d_name);
263 strcpy (hostname, "unknown.");
268 strcpy (hostname, name.nodename);
269 strcat (hostname, ".");
274 main (int argc, char *argv[])
278 extern int optind, opterr, optopt;
279 CRT_Pos size = { VT102_COLS_80, VT102_ROWS_24 };
281 int csnok_pipe[2] = { 0 };
287 Socket *server_socket = NULL, *client_socket = NULL;
297 memset (oflags, 0, sizeof (oflags));
298 memset (oargs, 0, sizeof (oargs));
299 while ((c = getopt (argc, argv, "vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF)
308 if ((c > 64) && (c < 91))
313 else if ((c > 96) && (c < 123))
320 fprintf (stderr, "unknown option %c\n", c);
327 /*Compatability for screen's ls */
335 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
342 /*If no mode is specified behave like screen */
350 ("specifiy exactly one of ( -c and or -s ), -t, -r, -l and -v");
355 fprintf (stderr, "Version: %s\n", libsympathy_version ());
356 fprintf (stderr, "Version: %s\n", rcsid);
361 return list_sockets ();
364 if (oflags['r'] && oflags['k'])
365 fatal_moan ("-k is incompatible with -r");
370 history = safe_atoi (oargs['n']);
372 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
375 fatal_moan ("agrument to -n must be greater than zero");
381 char *id = oargs['r'];
383 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
386 if (safe_atoi (id) > 0)
388 oargs['k'] = mome ("/.sympathy/%s%d", hostname, safe_atoi (id));
392 oargs['k'] = mome ("/.sympathy/%s", id);
398 if (oflags['p'] && oflags['d'])
399 fatal_moan ("-p incompatible with -d");
402 if (oflags['c'] && oflags['s'] && oflags['F'])
403 fatal_moan ("-F is incompatible with -c -s");
405 /*implement server and client: this process forks. The parent */
406 /*becomes the client and the child forks again to become the */
407 /*server. If there's no -k argument the client (parent) needs */
408 /*to find out the pid of the server, we use a pipe */
410 if (oflags['s'] && oflags['c'])
418 switch (pid = fork ())
420 case 0: /*child becomes the server */
425 close (csnok_pipe[0]);
428 fatal_moan ("fork failed");
429 default: /*parent becomes client*/
440 /*Collect the child*/
441 waitpid (pid, NULL, 0);
443 /*if there was no k argument we need to find the*/
444 /*pid of the server process so that we can work out*/
445 /*what the socket is called. The server tells us on*/
450 close (csnok_pipe[1]);
452 if (read (csnok_pipe[0], &pid, sizeof (pid)) != sizeof (pid))
453 fatal_moan ("Failed to receive pid of server process");
455 close (csnok_pipe[0]);
457 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
464 if (oflags['c'] && !oflags['k'])
465 fatal_moan ("-c requires a socket to be specified with -s or -k");
467 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
468 || oflags['L']) && oflags['c'])
469 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
471 if (oflags['t'] || oflags['s'])
473 if (!oflags['p'] && !oflags['d'])
480 strcpy (buf, oargs['w']);
481 ptr = index (buf, 'x');
486 size.y = safe_atoi (ptr);
488 size.x = safe_atoi (buf);
490 if ((size.x > VT102_MAX_COLS) || (size.x < 1))
491 fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS);
493 if ((size.y > VT102_MAX_ROWS) || (size.y < 1))
494 fatal_moan ("-w requires a height between 1 and %d\n",
499 if (oflags['s'] && !oflags['F'])
501 /*nochdir incase socket is relative path, unlink then will fail */
510 path = mome ("/.sympathy");
518 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
522 server_socket = socket_listen (oargs['k']);
524 /*Tell our parent's parent what our pid is */
529 write (csnok_pipe[1], &pid, sizeof (pid));
530 close (csnok_pipe[1]);
534 fatal_moan ("failed to create socket %s for listening", oargs['k']);
538 if (oflags['s'] || oflags['t'])
543 log = file_log_new (oargs['L']);
545 fatal_moan ("unable to access log file %s", oargs['L']);
550 tty = ptty_open (NULL, NULL, &size);
552 fatal_moan ("unable to open a ptty");
556 /*HACK-- check that console=device does not occur in */
559 fatal_moan ("no argument to -d");
562 char kernel_cmdline[4096] = { 0 };
563 char search_string[1024] = "console=";
564 char *ptr = oargs['d'];
567 if (!strncmp ("/dev/", ptr, 5))
570 strcat (search_string, ptr);
572 fd = open ("/proc/cmdline", O_RDONLY);
573 read (fd, kernel_cmdline, sizeof (kernel_cmdline));
576 kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0;
578 if (strstr (kernel_cmdline, search_string))
579 fatal_moan ("/proc/cmdline contains %s", search_string);
583 serial_open (oargs['d'],
584 oflags['K'] ? SERIAL_LOCK_ACTIVE :
585 SERIAL_LOCK_PASSIVE);
587 fatal_moan ("unable to open serial port %s", oargs['d']);
592 int baud = safe_atoi (oargs['b']);
595 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
597 tty_set_baud (tty, baud);
601 tty_set_flow (tty, oflags['f'] ? 1 : 0);
608 client_socket = socket_connect (oargs['k']);
610 fatal_moan ("failed to connect to socket %s", oargs['k']);
616 if (oflags['c'] || oflags['t'])
621 ansi = ansi_new_html (stdout);
625 terminal_register_handlers ();
627 ansi_new_from_terminal (terminal_open (0, 1),
628 oflags['u'] ? 0 : 1);
629 ansi->reset (ansi, NULL);
633 mainloop (tty, server_socket, client_socket, ansi, log, history, &size);
647 socket_free (server_socket);
649 socket_free (client_socket);
652 printf ("you have now exited sympathy\n");