4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.28 2008/03/02 10:27:24 james
15 * *** empty log message ***
17 * Revision 1.27 2008/03/02 09:55:37 james
18 * *** empty log message ***
20 * Revision 1.26 2008/02/29 14:55:09 james
21 * *** empty log message ***
23 * Revision 1.25 2008/02/28 22:43:25 james
24 * *** empty log message ***
26 * Revision 1.24 2008/02/28 22:00:42 james
27 * *** empty log message ***
29 * Revision 1.23 2008/02/28 16:57:51 james
30 * *** empty log message ***
32 * Revision 1.22 2008/02/28 01:47:44 james
33 * *** empty log message ***
35 * Revision 1.21 2008/02/27 16:01:24 james
36 * *** empty log message ***
38 * Revision 1.20 2008/02/27 10:00:34 james
39 * *** empty log message ***
41 * Revision 1.19 2008/02/27 09:47:05 james
42 * *** empty log message ***
44 * Revision 1.18 2008/02/27 09:42:53 james
45 * *** empty log message ***
47 * Revision 1.17 2008/02/27 09:42:21 james
48 * *** empty log message ***
50 * Revision 1.16 2008/02/27 01:31:38 james
51 * *** empty log message ***
53 * Revision 1.15 2008/02/27 01:31:14 james
54 * *** empty log message ***
56 * Revision 1.14 2008/02/24 00:43:55 james
57 * *** empty log message ***
59 * Revision 1.13 2008/02/24 00:42:53 james
60 * *** empty log message ***
62 * Revision 1.12 2008/02/23 11:48:52 james
63 * *** empty log message ***
65 * Revision 1.11 2008/02/20 20:16:07 james
66 * *** empty log message ***
68 * Revision 1.10 2008/02/20 19:44:37 james
71 * Revision 1.9 2008/02/20 18:49:11 staffcvs
72 * *** empty log message ***
74 * Revision 1.8 2008/02/20 18:33:37 james
75 * *** empty log message ***
77 * Revision 1.7 2008/02/20 18:31:44 james
78 * *** empty log message ***
80 * Revision 1.6 2008/02/20 17:18:33 james
81 * *** empty log message ***
83 * Revision 1.5 2008/02/20 15:50:14 james
84 * *** empty log message ***
86 * Revision 1.4 2008/02/20 02:11:35 james
87 * *** empty log message ***
89 * Revision 1.3 2008/02/14 02:46:44 james
90 * *** empty log message ***
92 * Revision 1.2 2008/02/14 00:57:58 james
93 * *** empty log message ***
95 * Revision 1.1 2008/02/05 14:25:49 james
96 * *** empty log message ***
100 #include <sys/types.h>
102 #include <sympathy.h>
104 #include <sys/utsname.h>
105 #include <sys/stat.h>
112 #include "mainloop.h"
114 extern void usage (void);
115 static char hostname[1024];
117 char *socket_dirs[] =
118 { "~/.sympathy", "~/sympathy", "/etc/sympathy", "/var/sympathy", NULL };
129 ret = (int) strtol (a, &end, 0);
138 fatal_moan (char *fmt, ...)
144 vfprintf (stderr, fmt, ap);
153 teedious_snprint (char *fmt, va_alist * ap)
157 char *buf = malloc (size);
160 fatal_moan ("malloc failed");
165 n = vsnprintf (buf, size, fmt, aq);
168 if (n > -1 && n < (size))
171 if (n > -1) /* glibc 2.1 */
174 size *= 2; /* twice the old size */
176 buf = realloc (buf, size);
179 fatal_moan ("malloc failed");
185 gloo_paths (char *dir, char *leaf)
193 ret = ptr = malloc (strlen (dir) + strlen (leaf) + 2);
199 *(ptr++) = *(leaf++);
206 /*make the path in fmt from home (hence the name) */
208 mome (char *fmt, ...)
211 char *rt, *home, *leaf;
215 home = getenv ("HOME");
221 leaf = teedious_snprintf (fmt, ap);
227 ret = gloo_paths (home, leaf);
236 find_socket (char *fmt, ...)
239 char *path, *leaf, *h, **ptr;
245 leaf = teedious_snprintf (fmt, ap);
252 for (ptr = socket_dirs; *ptr; ptr++) {
263 path = gloo_paths (h, leaf);
267 ret = socket_connect (path);
286 list_sockets_in_dir (char *sockdir)
292 DIR *dir = opendir (sockdir);
296 int hostname_len = strlen (hostname);
299 fatal_moan ("can't examine %s for sockets", sockdir);
304 while ((ent = readdir (dir))) {
305 sn = gloo_paths (sockdir, ent->d_name);
306 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode))) {
311 s = socket_connect (sn);
314 printf ("\t%s (Active)\n", sn);
317 if (strncmp (ent->d_name, hostname, hostname_len)) {
318 printf ("\t%s (Unknown - not this host)\n", sn);
320 printf ("\t%s (Dead, wiped)\n", sn);
339 for (ptr = socket_dirs; *ptr; ptr++) {
348 list_sockets_in_dir (h);
363 strcpy (hostname, "unknown.");
368 strcpy (hostname, name.nodename);
369 strcat (hostname, ".");
374 main (int argc, char *argv[])
378 extern int optind, opterr, optopt;
379 CRT_Pos size = { VT102_COLS_80, VT102_ROWS_24 };
381 int csnok_pipe[2] = { 0 };
387 Socket *server_socket = NULL, *client_socket = NULL;
397 memset (oflags, 0, sizeof (oflags));
398 memset (oargs, 0, sizeof (oargs));
399 while ((c = getopt (argc, argv, "vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF) {
406 if ((c > 64) && (c < 91)) {
409 } else if ((c > 96) && (c < 123)) {
413 fprintf (stderr, "unknown option %c\n", c);
420 /*Compatability for screen's ls */
428 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
434 /*If no mode is specified behave like screen */
442 ("specifiy exactly one of ( -c and or -s ), -t, -r, -l and -v");
446 fprintf (stderr, "Version: %s\n", libsympathy_version ());
447 fprintf (stderr, "Version: %s\n", rcsid);
452 return list_sockets ();
455 if (oflags['r'] && oflags['k'])
456 fatal_moan ("-k is incompatible with -r");
460 history = safe_atoi (oargs['n']);
462 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
465 fatal_moan ("agrument to -n must be greater than zero");
468 /*Fold -r implies -c */
472 if (oflags['p'] && oflags['d'])
473 fatal_moan ("-p incompatible with -d");
476 if (oflags['c'] && oflags['s'] && oflags['F'])
477 fatal_moan ("-F is incompatible with -c -s");
479 /*implement server and client: this process forks. The parent */
480 /*becomes the client and the child forks again to become the */
481 /*server. If there's no -k argument the client (parent) needs */
482 /*to find out the pid of the server, we use a pipe */
484 if (oflags['s'] && oflags['c']) {
490 switch (pid = fork ()) {
491 case 0: /*child becomes the server */
496 close (csnok_pipe[0]);
499 fatal_moan ("fork failed");
500 default: /*parent becomes client */
511 /*Collect the child */
512 waitpid (pid, NULL, 0);
514 /*if there was no k argument we need to find the */
515 /*pid of the server process so that we can work out */
516 /*what the socket is called. The server tells us on */
520 close (csnok_pipe[1]);
522 if (read (csnok_pipe[0], &pid, sizeof (pid)) != sizeof (pid))
523 fatal_moan ("Failed to receive pid of server process");
525 close (csnok_pipe[0]);
527 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
534 if (oflags['c'] && !oflags['k'] && !oflags['r'])
535 fatal_moan ("-c requires a socket to be specified with -s or -k or -r");
537 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
538 || oflags['L']) && oflags['c'])
539 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
541 if (oflags['t'] || oflags['s']) {
542 if (!oflags['p'] && !oflags['d'])
548 strcpy (buf, oargs['w']);
549 ptr = index (buf, 'x');
553 size.y = safe_atoi (ptr);
555 size.x = safe_atoi (buf);
557 if ((size.x > VT102_MAX_COLS) || (size.x < 1))
558 fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS);
560 if ((size.y > VT102_MAX_ROWS) || (size.y < 1))
561 fatal_moan ("-w requires a height between 1 and %d\n", VT102_MAX_ROWS);
565 if (oflags['s'] && !oflags['F']) {
566 /*nochdir incase socket is relative path, unlink then will fail */
574 path = mome ("/.sympathy");
582 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
586 server_socket = socket_listen (oargs['k']);
588 /*Tell our parent's parent what our pid is */
592 write (csnok_pipe[1], &pid, sizeof (pid));
593 close (csnok_pipe[1]);
597 fatal_moan ("failed to create socket %s for listening", oargs['k']);
601 if (oflags['s'] || oflags['t']) {
604 log = file_log_new (oargs['L']);
606 fatal_moan ("unable to access log file %s", oargs['L']);
610 tty = ptty_open (NULL, NULL, &size);
612 fatal_moan ("unable to open a ptty");
614 /*HACK-- check that console=device does not occur in */
617 fatal_moan ("no argument to -d");
620 char kernel_cmdline[4096] = { 0 };
621 char search_string[1024] = "console=";
622 char *ptr = oargs['d'];
625 if (!strncmp ("/dev/", ptr, 5))
628 strcat (search_string, ptr);
630 fd = open ("/proc/cmdline", O_RDONLY);
631 read (fd, kernel_cmdline, sizeof (kernel_cmdline));
634 kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0;
636 if (strstr (kernel_cmdline, search_string))
637 fatal_moan ("/proc/cmdline contains %s", search_string);
641 serial_open (oargs['d'],
642 oflags['K'] ? SERIAL_LOCK_ACTIVE : SERIAL_LOCK_PASSIVE);
644 fatal_moan ("unable to open serial port %s", oargs['d']);
648 int baud = safe_atoi (oargs['b']);
651 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
653 tty_set_baud (tty, baud);
657 tty_set_flow (tty, oflags['f'] ? 1 : 0);
662 char *id = oargs['r'];
665 fatal_moan ("-r requires an argument");
667 if (safe_atoi (id) > 0) {
668 client_socket = find_socket ("%s%d", hostname, safe_atoi (id));
670 client_socket = find_socket ("%s", id);
674 fatal_moan ("failed to find socket %s", oargs['r']);
676 } else if (oflags['c']) {
677 client_socket = socket_connect (oargs['k']);
680 fatal_moan ("failed to connect to socket %s", oargs['k']);
685 if (oflags['c'] || oflags['t']) {
688 ansi = ansi_new_html (stdout);
690 terminal_register_handlers ();
692 ansi_new_from_terminal (terminal_open (0, 1), oflags['u'] ? 0 : 1);
693 ansi->reset (ansi, NULL);
697 mainloop (tty, server_socket, client_socket, ansi, log, history, &size);
710 socket_free (server_socket);
712 socket_free (client_socket);
715 printf ("you have now exited sympathy\n");