4 * Copyright (c) 2008 James McKenzie <sympathy@madingley.org>,
10 "$Id: sympathy.c,v 1.52 2010/07/27 14:49:34 james Exp $";
13 * $Log: sympathy.c,v $
14 * Revision 1.52 2010/07/27 14:49:34 james
15 * add support for byte logging
17 * Revision 1.51 2010/07/16 11:04:10 james
18 * ignore tedious return values
20 * Revision 1.50 2008/05/09 12:56:28 staffcvs
21 * *** empty log message ***
23 * Revision 1.49 2008/05/09 12:43:49 staffcvs
24 * *** empty log message ***
26 * Revision 1.48 2008/05/09 12:35:57 james
27 * *** empty log message ***
29 * Revision 1.47 2008/05/09 12:26:58 staffcvs
30 * *** empty log message ***
32 * Revision 1.46 2008/05/09 12:19:18 james
33 * *** empty log message ***
35 * Revision 1.45 2008/03/15 01:44:31 james
36 * *** empty log message ***
38 * Revision 1.44 2008/03/12 01:30:23 james
39 * *** empty log message ***
41 * Revision 1.43 2008/03/12 01:26:56 james
42 * *** empty log message ***
44 * Revision 1.42 2008/03/11 15:02:52 james
45 * *** empty log message ***
47 * Revision 1.41 2008/03/10 11:49:32 james
48 * *** empty log message ***
50 * Revision 1.40 2008/03/07 14:16:44 james
51 * *** empty log message ***
53 * Revision 1.39 2008/03/07 14:13:40 james
54 * *** empty log message ***
56 * Revision 1.38 2008/03/07 13:56:39 james
57 * *** empty log message ***
59 * Revision 1.37 2008/03/07 13:16:02 james
60 * *** empty log message ***
62 * Revision 1.36 2008/03/06 21:34:09 james
63 * *** empty log message ***
65 * Revision 1.35 2008/03/06 21:33:02 james
66 * *** empty log message ***
68 * Revision 1.34 2008/03/06 16:49:39 james
69 * *** empty log message ***
71 * Revision 1.33 2008/03/06 16:49:05 james
72 * *** empty log message ***
74 * Revision 1.32 2008/03/03 06:04:42 james
75 * *** empty log message ***
77 * Revision 1.31 2008/03/03 06:04:18 james
78 * *** empty log message ***
80 * Revision 1.30 2008/03/02 10:38:18 james
81 * *** empty log message ***
83 * Revision 1.29 2008/03/02 10:37:56 james
84 * *** empty log message ***
86 * Revision 1.28 2008/03/02 10:27:24 james
87 * *** empty log message ***
89 * Revision 1.27 2008/03/02 09:55:37 james
90 * *** empty log message ***
92 * Revision 1.26 2008/02/29 14:55:09 james
93 * *** empty log message ***
95 * Revision 1.25 2008/02/28 22:43:25 james
96 * *** empty log message ***
98 * Revision 1.24 2008/02/28 22:00:42 james
99 * *** empty log message ***
101 * Revision 1.23 2008/02/28 16:57:51 james
102 * *** empty log message ***
104 * Revision 1.22 2008/02/28 01:47:44 james
105 * *** empty log message ***
107 * Revision 1.21 2008/02/27 16:01:24 james
108 * *** empty log message ***
110 * Revision 1.20 2008/02/27 10:00:34 james
111 * *** empty log message ***
113 * Revision 1.19 2008/02/27 09:47:05 james
114 * *** empty log message ***
116 * Revision 1.18 2008/02/27 09:42:53 james
117 * *** empty log message ***
119 * Revision 1.17 2008/02/27 09:42:21 james
120 * *** empty log message ***
122 * Revision 1.16 2008/02/27 01:31:38 james
123 * *** empty log message ***
125 * Revision 1.15 2008/02/27 01:31:14 james
126 * *** empty log message ***
128 * Revision 1.14 2008/02/24 00:43:55 james
129 * *** empty log message ***
131 * Revision 1.13 2008/02/24 00:42:53 james
132 * *** empty log message ***
134 * Revision 1.12 2008/02/23 11:48:52 james
135 * *** empty log message ***
137 * Revision 1.11 2008/02/20 20:16:07 james
138 * *** empty log message ***
140 * Revision 1.10 2008/02/20 19:44:37 james
143 * Revision 1.9 2008/02/20 18:49:11 staffcvs
144 * *** empty log message ***
146 * Revision 1.8 2008/02/20 18:33:37 james
147 * *** empty log message ***
149 * Revision 1.7 2008/02/20 18:31:44 james
150 * *** empty log message ***
152 * Revision 1.6 2008/02/20 17:18:33 james
153 * *** empty log message ***
155 * Revision 1.5 2008/02/20 15:50:14 james
156 * *** empty log message ***
158 * Revision 1.4 2008/02/20 02:11:35 james
159 * *** empty log message ***
161 * Revision 1.3 2008/02/14 02:46:44 james
162 * *** empty log message ***
164 * Revision 1.2 2008/02/14 00:57:58 james
165 * *** empty log message ***
167 * Revision 1.1 2008/02/05 14:25:49 james
168 * *** empty log message ***
172 #include <sys/types.h>
174 #include <sympathy.h>
176 #include <sys/utsname.h>
177 #include <sys/stat.h>
183 #include <sys/resource.h>
184 #include <sys/wait.h>
188 #include "mainloop.h"
192 extern void usage (void);
193 extern char *expand (const char *, int *);
194 static char hostname[1024];
196 char *socket_dirs[] =
197 { "~/.sympathy", "~/sympathy", "/etc/sympathy", "/var/sympathy", NULL };
208 ret = (int) strtol (a, &end, 0);
217 fatal_moan (char *fmt, ...)
226 vsyslog (LOG_ERR, fmt, ap);
230 vfprintf (stderr, fmt, ap);
242 wait3 (&status, WNOHANG, NULL);
245 /* Dispell zombies, from, for example, log compression */
251 sa.sa_handler = sigchld;
252 sa.sa_flags = SA_RESTART;
253 sigaction (SIGCHLD, &sa, NULL);
257 teedious_snprintf (char *fmt, va_list ap)
261 char *buf = malloc (size);
265 fatal_moan ("malloc failed");
271 n = vsnprintf (buf, size, fmt, aq);
274 if (n > -1 && n < (size))
277 if (n > -1) /* glibc 2.1 */
280 size *= 2; /* twice the old size */
282 buf = realloc (buf, size);
285 fatal_moan ("malloc failed");
291 gloo_paths (char *dir, char *leaf)
299 ret = ptr = xmalloc (strlen (dir) + strlen (leaf) + 2);
305 *(ptr++) = *(leaf++);
312 /* make the path in fmt from home (hence the name) */
314 mome (char *fmt, ...)
316 char *ret, *home, *leaf;
320 home = getenv ("HOME");
327 leaf = teedious_snprintf (fmt, ap);
335 ret = gloo_paths (home, leaf);
345 find_socket (char **retpath, char *fmt, ...)
348 char *path, *leaf, *h, **ptr;
355 leaf = teedious_snprintf (fmt, ap);
364 for (ptr = socket_dirs; *ptr; ptr++)
379 path = gloo_paths (h, leaf);
383 ret = socket_connect (path);
411 list_sockets_in_dir (char *sockdir)
417 DIR *dir = opendir (sockdir);
421 int hostname_len = strlen (hostname);
429 while ((ent = readdir (dir)))
431 sn = gloo_paths (sockdir, ent->d_name);
432 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
438 s = socket_connect (sn);
442 printf ("\t%s (Active)\n", sn);
447 if (strncmp (ent->d_name, hostname, hostname_len))
449 printf ("\t%s (Unknown - not this host)\n", sn);
453 printf ("\t%s (Dead, wiped)\n", sn);
472 for (ptr = socket_dirs; *ptr; ptr++)
486 list_sockets_in_dir (h);
503 strcpy (hostname, "unknown.");
508 strcpy (hostname, name.nodename);
509 strcat (hostname, ".");
513 send_to_server (Socket * c, char *s)
524 ipc_msg_send_key (c, *(uint8_t *) s);
527 ipc_msg_send_killme (c);
531 main (int argc, char *argv[])
533 Context ctx_store = { 0 }, *ctx = &ctx_store;
536 extern int optind, opterr, optopt;
537 CRT_Pos size = { VT102_COLS_80, VT102_ROWS_24 };
541 int cs_pipe[2] = { 0 };
547 Socket *server_socket = NULL, *client_socket = NULL;
552 char *pid_file = NULL;
556 memset (oflags, 0, sizeof (oflags));
557 memset (oargs, 0, sizeof (oargs));
559 while ((c = getopt (argc, argv, "BI:NCSRP:vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF)
569 openlog ("sympathy", LOG_PID | LOG_CONS, LOG_DAEMON);
572 if ((c >= 'A') && (c <= 'Z'))
577 else if ((c >= 'a') && (c <= 'z'))
586 syslog (LOG_ERR, "unknown option %c\n", c);
590 fprintf (stderr, "unknown option %c\n", c);
599 /* Compatability for screen's ls */
607 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
615 /* If no mode is specified behave like screen */
623 ("specifiy exactly one of ( -c and or -s ), -t, -r, -C, -l and -v");
628 fprintf (stderr, "Version: %s\n", libsympathy_version ());
629 fprintf (stderr, "Version: %s\n", rcsid);
634 return list_sockets ();
637 if (oflags['r'] && oflags['k'])
638 fatal_moan ("-k is incompatible with -r");
643 history = safe_atoi (oargs['n']);
645 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
648 fatal_moan ("agrument to -n must be greater than zero");
651 /* Fold -r implies -c */
655 if (oflags['p'] && oflags['d'])
656 fatal_moan ("-p incompatible with -d");
659 if (oflags['c'] && oflags['s'] && oflags['F'])
660 fatal_moan ("-F is incompatible with -c -s");
662 if (oflags['H'] && oflags['N'])
663 fatal_moan ("-H is incompatible with -N");
665 /* implement server and client: this process forks. The parent */
666 /* becomes the client and the child forks again to become the */
667 /* server. If there's no -k argument the client (parent) needs */
668 /* to find out the pid of the server, we use a pipe */
670 if (oflags['s'] && oflags['c'])
674 result = pipe (cs_pipe);
676 switch (pid = fork ())
678 case 0: /* child becomes the server */
687 fatal_moan ("fork failed");
688 default: /* parent becomes client */
702 /* Collect the child */
703 waitpid (pid, NULL, 0);
705 /* if there was no k argument we need to find the */
706 /* pid of the server process so that we can work out */
707 /* what the socket is called. The server tells us on */
708 /* a pipe. We do this even if k is specified to avoid */
709 /* a race, the server writes to the pipe when the socket */
714 if (read (cs_pipe[0], &pid, sizeof (pid)) != sizeof (pid))
715 fatal_moan ("Failed to receive pid of server process");
721 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
729 if (oflags['c'] && !oflags['k'] && !oflags['r'])
730 fatal_moan ("-c requires a socket to be specified with -s or -k or -r");
732 if ((oflags['H'] || oflags['N'] || oflags['I']) && oflags['s'])
733 fatal_moan ("-s is incompatible with -H, -N and -I");
735 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
736 || oflags['L'] || oflags['R'] || oflags['B'] || oflags['P']) && oflags['c'])
738 ("-c or -r are incompatible with -p, -d, -K, -b, -f, -R, -P, -B or -L");
740 if (oflags['C'] && (!oflags['d']))
741 fatal_moan ("-C requires -d");
744 if (oflags['t'] || oflags['s'])
746 if (!oflags['p'] && !oflags['d'])
753 strcpy (buf, oargs['w']);
754 ptr = index (buf, 'x');
759 size.y = safe_atoi (ptr);
761 size.x = safe_atoi (buf);
763 if ((size.x > VT102_MAX_COLS) || (size.x < 1))
764 fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS);
766 if ((size.y > VT102_MAX_ROWS) || (size.y < 1))
767 fatal_moan ("-w requires a height between 1 and %d\n",
772 if (oflags['s'] && !oflags['F'])
774 /* nochdir incase socket is relative path, unlink then will fail */
776 fish = daemon (1, 0);
785 path = mome ("/.sympathy");
794 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
798 server_socket = socket_listen (oargs['k']);
800 /* Tell our parent's parent what our pid is */
801 /* and that we've opened the server socket */
807 fish = write (cs_pipe[1], &pid, sizeof (pid));
812 fatal_moan ("failed to create socket %s for listening", oargs['k']);
816 if (oflags['s'] || oflags['t'] || oflags['C'])
821 pid_file = oargs['P'];
822 fp = fopen (pid_file, "w");
825 fprintf (fp, "%d", getpid ());
832 ctx->l = file_log_new (oargs['L'], oflags['R']);
833 ctx->byte_logging=oflags['B'];
835 fatal_moan ("unable to access log file %s", oargs['L']);
842 ctx->t = ptty_open (argv[optind], &argv[optind], &size);
846 ctx->t = ptty_open (NULL, NULL, &size);
850 fatal_moan ("unable to open a ptty");
854 /* HACK-- check that console=device does not occur in */
857 fatal_moan ("no argument to -d");
860 char kernel_cmdline[4096] = { 0 };
861 char search_string[1024] = "console=";
862 char *ptr = oargs['d'];
865 if (!strncmp ("/dev/", ptr, 5))
868 strcat (search_string, ptr);
870 fd = open ("/proc/cmdline", O_RDONLY);
872 fish = read (fd, kernel_cmdline, sizeof (kernel_cmdline));
875 kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0;
877 if (strstr (kernel_cmdline, search_string))
878 fatal_moan ("/proc/cmdline contains %s", search_string);
882 serial_open (oargs['d'],
883 oflags['K'] ? SERIAL_LOCK_ACTIVE :
884 SERIAL_LOCK_PASSIVE);
886 fatal_moan ("unable to open serial port %s", oargs['d']);
890 ctx->t->close (ctx->t);
897 int baud = safe_atoi (oargs['b']);
900 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
902 tty_set_baud (ctx->t, baud);
906 tty_set_flow (ctx->t, oflags['f'] ? 1 : 0);
912 char *id = oargs['r'];
915 fatal_moan ("-r requires an argument");
917 if (safe_atoi (id) > 0)
920 find_socket (&oargs['k'], "%s%d", hostname, safe_atoi (id));
924 client_socket = find_socket (&oargs['k'], "%s", id);
928 fatal_moan ("failed to find socket %s", oargs['r']);
931 else if (oflags['c'])
933 client_socket = socket_connect (oargs['k']);
936 fatal_moan ("failed to connect to socket %s", oargs['k']);
943 fatal_moan ("-I requires either -c or -r", oargs['k']);
945 fatal_moan ("-I requires an arugment");
946 send_to_server (client_socket, oargs['I']);
951 ipc_msg_send_initialize (client_socket);
953 if (oflags['c'] || oflags['t'])
957 ctx->r = rx_new_raw (0, 1);
958 ansi = ansi_new_raw (0, 1);
960 else if (oflags['H'])
962 ansi = ansi_new_html (stdout);
966 terminal_register_handlers ();
968 ansi_new_from_terminal (terminal_open (0, 1),
969 oflags['u'] ? 0 : 1);
970 ansi->reset (ansi, NULL);
976 if (oflags['c'] && oargs['k'])
978 ansi->set_title (ansi, oargs['k']);
980 else if ((ctx->t) && (ctx->t->name))
982 ansi->set_title (ansi, ctx->t->name);
989 ctx->v = vt102_new (&size);
990 ctx->h = history_new (history);
992 mainloop (ctx, ansi, server_socket, client_socket);
1001 ctx->t->close (ctx->t);
1004 ctx->l->close (ctx->l);
1006 socket_free (server_socket);
1008 socket_free (client_socket);
1013 if (!oflags['H'] && !oflags['I'])
1014 printf ("you have now exited sympathy\n");