4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.27 2008/03/02 09:55:37 james
15 * *** empty log message ***
17 * Revision 1.26 2008/02/29 14:55:09 james
18 * *** empty log message ***
20 * Revision 1.25 2008/02/28 22:43:25 james
21 * *** empty log message ***
23 * Revision 1.24 2008/02/28 22:00:42 james
24 * *** empty log message ***
26 * Revision 1.23 2008/02/28 16:57:51 james
27 * *** empty log message ***
29 * Revision 1.22 2008/02/28 01:47:44 james
30 * *** empty log message ***
32 * Revision 1.21 2008/02/27 16:01:24 james
33 * *** empty log message ***
35 * Revision 1.20 2008/02/27 10:00:34 james
36 * *** empty log message ***
38 * Revision 1.19 2008/02/27 09:47:05 james
39 * *** empty log message ***
41 * Revision 1.18 2008/02/27 09:42:53 james
42 * *** empty log message ***
44 * Revision 1.17 2008/02/27 09:42:21 james
45 * *** empty log message ***
47 * Revision 1.16 2008/02/27 01:31:38 james
48 * *** empty log message ***
50 * Revision 1.15 2008/02/27 01:31:14 james
51 * *** empty log message ***
53 * Revision 1.14 2008/02/24 00:43:55 james
54 * *** empty log message ***
56 * Revision 1.13 2008/02/24 00:42:53 james
57 * *** empty log message ***
59 * Revision 1.12 2008/02/23 11:48:52 james
60 * *** empty log message ***
62 * Revision 1.11 2008/02/20 20:16:07 james
63 * *** empty log message ***
65 * Revision 1.10 2008/02/20 19:44:37 james
68 * Revision 1.9 2008/02/20 18:49:11 staffcvs
69 * *** empty log message ***
71 * Revision 1.8 2008/02/20 18:33:37 james
72 * *** empty log message ***
74 * Revision 1.7 2008/02/20 18:31:44 james
75 * *** empty log message ***
77 * Revision 1.6 2008/02/20 17:18:33 james
78 * *** empty log message ***
80 * Revision 1.5 2008/02/20 15:50:14 james
81 * *** empty log message ***
83 * Revision 1.4 2008/02/20 02:11:35 james
84 * *** empty log message ***
86 * Revision 1.3 2008/02/14 02:46:44 james
87 * *** empty log message ***
89 * Revision 1.2 2008/02/14 00:57:58 james
90 * *** empty log message ***
92 * Revision 1.1 2008/02/05 14:25:49 james
93 * *** empty log message ***
97 #include <sys/types.h>
101 #include <sys/utsname.h>
102 #include <sys/stat.h>
109 #include "mainloop.h"
111 extern void usage (void);
112 static char hostname[1024];
114 char *socket_dirs[] =
115 { ".sympathy", "sympathy", "/etc/sympathy", "/var/sympathy", NULL };
126 ret = (int) strtol (a, &end, 0);
135 fatal_moan (char *fmt, ...)
141 vfprintf (stderr, fmt, ap);
150 /*make the path in fmt from home (hence the name) */
153 mome (char *fmt, ...)
163 home = getenv ("HOME");
167 homelen = strlen (home) + 1;
169 size = 1024 + homelen;
174 fatal_moan ("malloc failed");
187 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
190 if (n > -1 && n < (size - homelen))
193 if (n > -1) /* glibc 2.1 */
194 size = homelen + n + 1;
196 size *= 2; /* twice the old size */
198 buf = realloc (buf, size);
201 fatal_moan ("malloc failed");
206 gloo_paths (char *dir, char *leaf)
210 ret = ptr = malloc (strlen (dir) + strlen (leaf) + 2);
216 *(ptr++) = *(leaf++);
225 list_sockets_in_dir (char *sockdir)
231 DIR *dir = opendir (sockdir);
235 int hostname_len = strlen (hostname);
238 fatal_moan ("can't examine %s for sockets", sockdir);
243 while ((ent = readdir (dir)))
245 sn = gloo_paths (sockdir, ent->d_name);
246 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
252 s = socket_connect (sn);
256 printf ("\t%s (Active)\n", sn);
261 if (strncmp (ent->d_name, hostname, hostname_len))
263 printf ("\t%s (Unknown - not this host)\n", sn);
267 printf ("\t%s (Dead, wiped)\n", sn);
286 for (ptr = socket_dirs; *ptr; ptr++)
292 list_sockets_in_dir (h);
309 strcpy (hostname, "unknown.");
314 strcpy (hostname, name.nodename);
315 strcat (hostname, ".");
320 main (int argc, char *argv[])
324 extern int optind, opterr, optopt;
325 CRT_Pos size = { VT102_COLS_80, VT102_ROWS_24 };
327 int csnok_pipe[2] = { 0 };
333 Socket *server_socket = NULL, *client_socket = NULL;
343 memset (oflags, 0, sizeof (oflags));
344 memset (oargs, 0, sizeof (oargs));
345 while ((c = getopt (argc, argv, "vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF)
354 if ((c > 64) && (c < 91))
359 else if ((c > 96) && (c < 123))
366 fprintf (stderr, "unknown option %c\n", c);
373 /*Compatability for screen's ls */
381 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
388 /*If no mode is specified behave like screen */
396 ("specifiy exactly one of ( -c and or -s ), -t, -r, -l and -v");
401 fprintf (stderr, "Version: %s\n", libsympathy_version ());
402 fprintf (stderr, "Version: %s\n", rcsid);
407 return list_sockets ();
410 if (oflags['r'] && oflags['k'])
411 fatal_moan ("-k is incompatible with -r");
416 history = safe_atoi (oargs['n']);
418 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
421 fatal_moan ("agrument to -n must be greater than zero");
424 /*Fold -r implies -c */
428 if (oflags['p'] && oflags['d'])
429 fatal_moan ("-p incompatible with -d");
432 if (oflags['c'] && oflags['s'] && oflags['F'])
433 fatal_moan ("-F is incompatible with -c -s");
435 /*implement server and client: this process forks. The parent */
436 /*becomes the client and the child forks again to become the */
437 /*server. If there's no -k argument the client (parent) needs */
438 /*to find out the pid of the server, we use a pipe */
440 if (oflags['s'] && oflags['c'])
448 switch (pid = fork ())
450 case 0: /*child becomes the server */
455 close (csnok_pipe[0]);
458 fatal_moan ("fork failed");
459 default: /*parent becomes client */
470 /*Collect the child */
471 waitpid (pid, NULL, 0);
473 /*if there was no k argument we need to find the */
474 /*pid of the server process so that we can work out */
475 /*what the socket is called. The server tells us on */
480 close (csnok_pipe[1]);
482 if (read (csnok_pipe[0], &pid, sizeof (pid)) != sizeof (pid))
483 fatal_moan ("Failed to receive pid of server process");
485 close (csnok_pipe[0]);
487 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
494 if (oflags['c'] && !oflags['k'] && !oflags['r'])
495 fatal_moan ("-c requires a socket to be specified with -s or -k or -r");
497 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
498 || oflags['L']) && oflags['c'])
499 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
501 if (oflags['t'] || oflags['s'])
503 if (!oflags['p'] && !oflags['d'])
510 strcpy (buf, oargs['w']);
511 ptr = index (buf, 'x');
516 size.y = safe_atoi (ptr);
518 size.x = safe_atoi (buf);
520 if ((size.x > VT102_MAX_COLS) || (size.x < 1))
521 fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS);
523 if ((size.y > VT102_MAX_ROWS) || (size.y < 1))
524 fatal_moan ("-w requires a height between 1 and %d\n",
529 if (oflags['s'] && !oflags['F'])
531 /*nochdir incase socket is relative path, unlink then will fail */
540 path = mome ("/.sympathy");
549 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
553 server_socket = socket_listen (oargs['k']);
555 /*Tell our parent's parent what our pid is */
560 write (csnok_pipe[1], &pid, sizeof (pid));
561 close (csnok_pipe[1]);
565 fatal_moan ("failed to create socket %s for listening", oargs['k']);
569 if (oflags['s'] || oflags['t'])
574 log = file_log_new (oargs['L']);
576 fatal_moan ("unable to access log file %s", oargs['L']);
581 tty = ptty_open (NULL, NULL, &size);
583 fatal_moan ("unable to open a ptty");
587 /*HACK-- check that console=device does not occur in */
590 fatal_moan ("no argument to -d");
593 char kernel_cmdline[4096] = { 0 };
594 char search_string[1024] = "console=";
595 char *ptr = oargs['d'];
598 if (!strncmp ("/dev/", ptr, 5))
601 strcat (search_string, ptr);
603 fd = open ("/proc/cmdline", O_RDONLY);
604 read (fd, kernel_cmdline, sizeof (kernel_cmdline));
607 kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0;
609 if (strstr (kernel_cmdline, search_string))
610 fatal_moan ("/proc/cmdline contains %s", search_string);
614 serial_open (oargs['d'],
615 oflags['K'] ? SERIAL_LOCK_ACTIVE :
616 SERIAL_LOCK_PASSIVE);
618 fatal_moan ("unable to open serial port %s", oargs['d']);
623 int baud = safe_atoi (oargs['b']);
626 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
628 tty_set_baud (tty, baud);
632 tty_set_flow (tty, oflags['f'] ? 1 : 0);
639 char *id = oargs['r'];
641 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
644 if (safe_atoi (id) > 0)
646 oargs['k'] = find_socket ("%s%d", hostname, safe_atoi (id));
650 oargs['k'] = find_socket ("%s", id);
659 client_socket = socket_connect (oargs['k']);
661 fatal_moan ("failed to connect to socket %s", oargs['k']);
667 if (oflags['c'] || oflags['t'])
672 ansi = ansi_new_html (stdout);
676 terminal_register_handlers ();
678 ansi_new_from_terminal (terminal_open (0, 1),
679 oflags['u'] ? 0 : 1);
680 ansi->reset (ansi, NULL);
684 mainloop (tty, server_socket, client_socket, ansi, log, history, &size);
698 socket_free (server_socket);
700 socket_free (client_socket);
703 printf ("you have now exited sympathy\n");