4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.23 2008/02/28 16:57:51 james
15 * *** empty log message ***
17 * Revision 1.22 2008/02/28 01:47:44 james
18 * *** empty log message ***
20 * Revision 1.21 2008/02/27 16:01:24 james
21 * *** empty log message ***
23 * Revision 1.20 2008/02/27 10:00:34 james
24 * *** empty log message ***
26 * Revision 1.19 2008/02/27 09:47:05 james
27 * *** empty log message ***
29 * Revision 1.18 2008/02/27 09:42:53 james
30 * *** empty log message ***
32 * Revision 1.17 2008/02/27 09:42:21 james
33 * *** empty log message ***
35 * Revision 1.16 2008/02/27 01:31:38 james
36 * *** empty log message ***
38 * Revision 1.15 2008/02/27 01:31:14 james
39 * *** empty log message ***
41 * Revision 1.14 2008/02/24 00:43:55 james
42 * *** empty log message ***
44 * Revision 1.13 2008/02/24 00:42:53 james
45 * *** empty log message ***
47 * Revision 1.12 2008/02/23 11:48:52 james
48 * *** empty log message ***
50 * Revision 1.11 2008/02/20 20:16:07 james
51 * *** empty log message ***
53 * Revision 1.10 2008/02/20 19:44:37 james
56 * Revision 1.9 2008/02/20 18:49:11 staffcvs
57 * *** empty log message ***
59 * Revision 1.8 2008/02/20 18:33:37 james
60 * *** empty log message ***
62 * Revision 1.7 2008/02/20 18:31:44 james
63 * *** empty log message ***
65 * Revision 1.6 2008/02/20 17:18:33 james
66 * *** empty log message ***
68 * Revision 1.5 2008/02/20 15:50:14 james
69 * *** empty log message ***
71 * Revision 1.4 2008/02/20 02:11:35 james
72 * *** empty log message ***
74 * Revision 1.3 2008/02/14 02:46:44 james
75 * *** empty log message ***
77 * Revision 1.2 2008/02/14 00:57:58 james
78 * *** empty log message ***
80 * Revision 1.1 2008/02/05 14:25:49 james
81 * *** empty log message ***
85 #include <sys/types.h>
89 #include <sys/utsname.h>
99 extern void usage (void);
101 static char hostname[1024];
112 ret = (int) strtol (a, &end, 0);
121 fatal_moan (char *fmt, ...)
127 vfprintf (stderr, fmt, ap);
136 /*make the path in fmt from home (hence the name) */
139 mome (char *fmt, ...)
149 home = getenv ("HOME");
153 homelen = strlen (home) + 1;
155 size = 1024 + homelen;
160 fatal_moan ("malloc failed");
170 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
173 if (n > -1 && n < (size - homelen))
176 if (n > -1) /* glibc 2.1 */
177 size = homelen + n + 1;
179 size *= 2; /* twice the old size */
181 buf = realloc (buf, size);
184 fatal_moan ("malloc failed");
196 char *sockdir = mome ("/.sympathy/");
197 DIR *dir = opendir (sockdir);
201 int hostname_len = strlen (hostname);
204 fatal_moan ("can't examine %s for sockets", sockdir);
209 while ((ent = readdir (dir)))
211 sn = mome (".sympathy/%s", ent->d_name);
212 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
218 s = socket_connect (sn);
222 printf ("\t%s (Active)\n", ent->d_name);
227 if (strncmp (ent->d_name, hostname, hostname_len))
229 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
233 printf ("\t%s (Dead, wiped)\n", ent->d_name);
254 strcpy (hostname, "unknown.");
259 strcpy (hostname, name.nodename);
260 strcat (hostname, ".");
265 main (int argc, char *argv[])
269 extern int optind, opterr, optopt;
270 CRT_Pos size = { VT102_COLS_80, VT102_ROWS_24 };
275 Socket *server_socket = NULL, *client_socket = NULL;
285 memset (oflags, 0, sizeof (oflags));
286 memset (oargs, 0, sizeof (oargs));
287 while ((c = getopt (argc, argv, "vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF)
296 if ((c > 64) && (c < 91))
301 else if ((c > 96) && (c < 123))
308 fprintf (stderr, "unknown option %c\n", c);
315 /*Compatability for screen's ls */
323 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
330 /*If no mode is specified behave like screen */
338 ("specifiy exactly one of ( -c and or -s ), -t, -r, -l and -v");
343 fprintf (stderr, "Version: %s\n", libsympathy_version ());
344 fprintf (stderr, "Version: %s\n", rcsid);
349 return list_sockets ();
352 if (oflags['r'] && oflags['k'])
353 fatal_moan ("-k is incompatible with -r");
358 history = safe_atoi (oargs['n']);
360 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
363 fatal_moan ("agrument to -n must be greater than zero");
369 char *id = oargs['r'];
371 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
374 if (safe_atoi (id) > 0)
376 oargs['k'] = mome ("/.sympathy/%s%d", hostname, safe_atoi (id));
380 oargs['k'] = mome ("/.sympathy/%s", id);
386 if (oflags['p'] && oflags['d'])
387 fatal_moan ("-p incompatible with -d");
389 /*implement server and client by opening the server socket to prevent */
390 /*a race condition, and then forking and munging the cmd line options */
391 /*in the parent and child so that the child is the server and the */
392 /*parent becomes its client */
394 if (oflags['c'] && oflags['s'] && oflags['F'])
395 fatal_moan ("-F is incompatible with -c -s");
397 if (oflags['s'] && !oflags['k'])
400 path = mome ("/.sympathy");
404 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
410 server_socket = socket_listen (oargs['k']);
412 fatal_moan ("failed to create socket %s for listening", oargs['k']);
415 if (oflags['s'] && oflags['c'])
419 case 0: /*child becomes the server */
424 fatal_moan ("fork failed");
437 socket_free_parent (server_socket);
438 server_socket = NULL;
444 if (oflags['c'] && !oflags['k'])
445 fatal_moan ("-c requires a socket to be specified with -s or -k");
447 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
448 || oflags['L']) && oflags['c'])
449 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
451 if (oflags['t'] || oflags['s'])
453 if (!oflags['p'] && !oflags['d'])
460 strcpy (buf, oargs['w']);
461 ptr = index (buf, 'x');
466 size.y = safe_atoi (ptr);
468 size.x = safe_atoi (buf);
470 if ((size.x > VT102_MAX_COLS) || (size.x < 1))
471 fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS);
473 if ((size.y > VT102_MAX_ROWS) || (size.y < 1))
474 fatal_moan ("-w requires a height between 1 and %d\n",
479 if (oflags['s'] && !oflags['F'])
481 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
485 if (oflags['s'] || oflags['t'])
490 log = file_log_new (oargs['L']);
492 fatal_moan ("unable to access log file %s", oargs['L']);
497 tty = ptty_open (NULL, NULL, &size);
499 fatal_moan ("unable to open a ptty");
503 /*HACK-- check that console=device does not occur in */
506 fatal_moan ("no argument to -d");
509 char kernel_cmdline[4096] = { 0 };
510 char search_string[1024] = "console=";
511 char *ptr = oargs['d'];
514 if (!strncmp ("/dev/", ptr, 5))
517 strcat (search_string, ptr);
519 fd = open ("/proc/cmdline", O_RDONLY);
520 read (fd, kernel_cmdline, sizeof (kernel_cmdline));
523 kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0;
525 if (strstr (kernel_cmdline, search_string))
526 fatal_moan ("/proc/cmdline contains %s", search_string);
530 serial_open (oargs['d'],
531 oflags['K'] ? SERIAL_LOCK_ACTIVE :
532 SERIAL_LOCK_PASSIVE);
534 fatal_moan ("unable to open serial port %s", oargs['d']);
539 int baud = safe_atoi (oargs['b']);
542 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
544 tty_set_baud (tty, baud);
548 tty_set_flow (tty, oflags['f'] ? 1 : 0);
555 client_socket = socket_connect (oargs['k']);
557 fatal_moan ("failed to connect to socket %s", oargs['k']);
563 if (oflags['c'] || oflags['t'])
568 ansi = ansi_new_html (stdout);
572 terminal_register_handlers ();
574 ansi_new_from_terminal (terminal_open (0, 1),
575 oflags['u'] ? 0 : 1);
576 ansi->reset (ansi, NULL);
580 mainloop (tty, server_socket, client_socket, ansi, log, history, &size);
594 socket_free (server_socket);
596 socket_free (client_socket);
599 printf ("you have now exited sympathy\n");