4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.25 2008/02/28 22:43:25 james
15 * *** empty log message ***
17 * Revision 1.24 2008/02/28 22:00:42 james
18 * *** empty log message ***
20 * Revision 1.23 2008/02/28 16:57:51 james
21 * *** empty log message ***
23 * Revision 1.22 2008/02/28 01:47:44 james
24 * *** empty log message ***
26 * Revision 1.21 2008/02/27 16:01:24 james
27 * *** empty log message ***
29 * Revision 1.20 2008/02/27 10:00:34 james
30 * *** empty log message ***
32 * Revision 1.19 2008/02/27 09:47:05 james
33 * *** empty log message ***
35 * Revision 1.18 2008/02/27 09:42:53 james
36 * *** empty log message ***
38 * Revision 1.17 2008/02/27 09:42:21 james
39 * *** empty log message ***
41 * Revision 1.16 2008/02/27 01:31:38 james
42 * *** empty log message ***
44 * Revision 1.15 2008/02/27 01:31:14 james
45 * *** empty log message ***
47 * Revision 1.14 2008/02/24 00:43:55 james
48 * *** empty log message ***
50 * Revision 1.13 2008/02/24 00:42:53 james
51 * *** empty log message ***
53 * Revision 1.12 2008/02/23 11:48:52 james
54 * *** empty log message ***
56 * Revision 1.11 2008/02/20 20:16:07 james
57 * *** empty log message ***
59 * Revision 1.10 2008/02/20 19:44:37 james
62 * Revision 1.9 2008/02/20 18:49:11 staffcvs
63 * *** empty log message ***
65 * Revision 1.8 2008/02/20 18:33:37 james
66 * *** empty log message ***
68 * Revision 1.7 2008/02/20 18:31:44 james
69 * *** empty log message ***
71 * Revision 1.6 2008/02/20 17:18:33 james
72 * *** empty log message ***
74 * Revision 1.5 2008/02/20 15:50:14 james
75 * *** empty log message ***
77 * Revision 1.4 2008/02/20 02:11:35 james
78 * *** empty log message ***
80 * Revision 1.3 2008/02/14 02:46:44 james
81 * *** empty log message ***
83 * Revision 1.2 2008/02/14 00:57:58 james
84 * *** empty log message ***
86 * Revision 1.1 2008/02/05 14:25:49 james
87 * *** empty log message ***
91 #include <sys/types.h>
95 #include <sys/utsname.h>
103 #include "mainloop.h"
105 extern void usage (void);
107 static char hostname[1024];
118 ret = (int) strtol (a, &end, 0);
127 fatal_moan (char *fmt, ...)
133 vfprintf (stderr, fmt, ap);
142 /*make the path in fmt from home (hence the name) */
145 mome (char *fmt, ...)
155 home = getenv ("HOME");
159 homelen = strlen (home) + 1;
161 size = 1024 + homelen;
166 fatal_moan ("malloc failed");
176 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
179 if (n > -1 && n < (size - homelen))
182 if (n > -1) /* glibc 2.1 */
183 size = homelen + n + 1;
185 size *= 2; /* twice the old size */
187 buf = realloc (buf, size);
190 fatal_moan ("malloc failed");
202 char *sockdir = mome ("/.sympathy/");
203 DIR *dir = opendir (sockdir);
207 int hostname_len = strlen (hostname);
210 fatal_moan ("can't examine %s for sockets", sockdir);
215 while ((ent = readdir (dir)))
217 sn = mome (".sympathy/%s", ent->d_name);
218 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
224 s = socket_connect (sn);
228 printf ("\t%s (Active)\n", ent->d_name);
233 if (strncmp (ent->d_name, hostname, hostname_len))
235 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
239 printf ("\t%s (Dead, wiped)\n", ent->d_name);
260 strcpy (hostname, "unknown.");
265 strcpy (hostname, name.nodename);
266 strcat (hostname, ".");
271 main (int argc, char *argv[])
275 extern int optind, opterr, optopt;
276 CRT_Pos size = { VT102_COLS_80, VT102_ROWS_24 };
278 int csnok_pipe[2] = { 0 };
284 Socket *server_socket = NULL, *client_socket = NULL;
294 memset (oflags, 0, sizeof (oflags));
295 memset (oargs, 0, sizeof (oargs));
296 while ((c = getopt (argc, argv, "vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF)
305 if ((c > 64) && (c < 91))
310 else if ((c > 96) && (c < 123))
317 fprintf (stderr, "unknown option %c\n", c);
324 /*Compatability for screen's ls */
332 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
339 /*If no mode is specified behave like screen */
347 ("specifiy exactly one of ( -c and or -s ), -t, -r, -l and -v");
352 fprintf (stderr, "Version: %s\n", libsympathy_version ());
353 fprintf (stderr, "Version: %s\n", rcsid);
358 return list_sockets ();
361 if (oflags['r'] && oflags['k'])
362 fatal_moan ("-k is incompatible with -r");
367 history = safe_atoi (oargs['n']);
369 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
372 fatal_moan ("agrument to -n must be greater than zero");
378 char *id = oargs['r'];
380 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
383 if (safe_atoi (id) > 0)
385 oargs['k'] = mome ("/.sympathy/%s%d", hostname, safe_atoi (id));
389 oargs['k'] = mome ("/.sympathy/%s", id);
395 if (oflags['p'] && oflags['d'])
396 fatal_moan ("-p incompatible with -d");
399 if (oflags['c'] && oflags['s'] && oflags['F'])
400 fatal_moan ("-F is incompatible with -c -s");
402 /*implement server and client: this process forks. The parent */
403 /*becomes the client and the child forks again to become the */
404 /*server. If there's no -k argument the client (parent) needs */
405 /*to find out the pid of the server, we use a pipe */
407 if (oflags['s'] && oflags['c'])
415 switch (pid = fork ())
417 case 0: /*child becomes the server */
422 close (csnok_pipe[0]);
425 fatal_moan ("fork failed");
426 default: /*parent becomes client*/
437 /*Collect the child*/
438 waitpid (pid, NULL, 0);
440 /*if there was no k argument we need to find the*/
441 /*pid of the server process so that we can work out*/
442 /*what the socket is called. The server tells us on*/
447 close (csnok_pipe[1]);
449 if (read (csnok_pipe[0], &pid, sizeof (pid)) != sizeof (pid))
450 fatal_moan ("Failed to receive pid of server process");
452 close (csnok_pipe[0]);
454 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
461 if (oflags['c'] && !oflags['k'])
462 fatal_moan ("-c requires a socket to be specified with -s or -k");
464 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
465 || oflags['L']) && oflags['c'])
466 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
468 if (oflags['t'] || oflags['s'])
470 if (!oflags['p'] && !oflags['d'])
477 strcpy (buf, oargs['w']);
478 ptr = index (buf, 'x');
483 size.y = safe_atoi (ptr);
485 size.x = safe_atoi (buf);
487 if ((size.x > VT102_MAX_COLS) || (size.x < 1))
488 fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS);
490 if ((size.y > VT102_MAX_ROWS) || (size.y < 1))
491 fatal_moan ("-w requires a height between 1 and %d\n",
496 if (oflags['s'] && !oflags['F'])
498 /*nochdir incase socket is relative path, unlink then will fail */
507 path = mome ("/.sympathy");
515 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
519 server_socket = socket_listen (oargs['k']);
521 /*Tell our parent's parent what our pid is */
526 write (csnok_pipe[1], &pid, sizeof (pid));
527 close (csnok_pipe[1]);
531 fatal_moan ("failed to create socket %s for listening", oargs['k']);
535 if (oflags['s'] || oflags['t'])
540 log = file_log_new (oargs['L']);
542 fatal_moan ("unable to access log file %s", oargs['L']);
547 tty = ptty_open (NULL, NULL, &size);
549 fatal_moan ("unable to open a ptty");
553 /*HACK-- check that console=device does not occur in */
556 fatal_moan ("no argument to -d");
559 char kernel_cmdline[4096] = { 0 };
560 char search_string[1024] = "console=";
561 char *ptr = oargs['d'];
564 if (!strncmp ("/dev/", ptr, 5))
567 strcat (search_string, ptr);
569 fd = open ("/proc/cmdline", O_RDONLY);
570 read (fd, kernel_cmdline, sizeof (kernel_cmdline));
573 kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0;
575 if (strstr (kernel_cmdline, search_string))
576 fatal_moan ("/proc/cmdline contains %s", search_string);
580 serial_open (oargs['d'],
581 oflags['K'] ? SERIAL_LOCK_ACTIVE :
582 SERIAL_LOCK_PASSIVE);
584 fatal_moan ("unable to open serial port %s", oargs['d']);
589 int baud = safe_atoi (oargs['b']);
592 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
594 tty_set_baud (tty, baud);
598 tty_set_flow (tty, oflags['f'] ? 1 : 0);
605 client_socket = socket_connect (oargs['k']);
607 fatal_moan ("failed to connect to socket %s", oargs['k']);
613 if (oflags['c'] || oflags['t'])
618 ansi = ansi_new_html (stdout);
622 terminal_register_handlers ();
624 ansi_new_from_terminal (terminal_open (0, 1),
625 oflags['u'] ? 0 : 1);
626 ansi->reset (ansi, NULL);
630 mainloop (tty, server_socket, client_socket, ansi, log, history, &size);
644 socket_free (server_socket);
646 socket_free (client_socket);
649 printf ("you have now exited sympathy\n");