1 /*--------------------------------*-C-*---------------------------------*
3 *----------------------------------------------------------------------*
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *----------------------------------------------------------------------*/
23 #include "../config.h"
25 #include "rxvtdaemon.h"
35 #include <sys/types.h>
37 #include <sys/socket.h>
42 extern char **environ;
44 struct server : rxvt_connection {
47 void read_cb (io_watcher &w, short revents); io_watcher read_ev;
48 void log_msg (const char *msg);
51 : read_ev (this, &server::read_cb),
52 log_cb (this, &server::log_msg)
55 read_ev.start (fd, EVENT_READ);
58 void err (const char *format = 0, ...);
61 struct unix_listener {
64 void accept_cb (io_watcher &w, short revents); io_watcher accept_ev;
66 unix_listener (const char *sockname);
69 unix_listener::unix_listener (const char *sockname)
70 : accept_ev (this, &unix_listener::accept_cb)
72 if ((fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0)
74 perror ("unable to create listening socket");
78 fcntl (fd, F_SETFD, FD_CLOEXEC);
82 sa.sun_family = AF_UNIX;
83 strcpy (sa.sun_path, sockname);
85 unlink (rxvt_connection::unix_sockname ());
87 mode_t omask = umask (0077);
89 if (bind (fd, (sockaddr *)&sa, sizeof (sa)))
91 perror ("unable to bind listening socket");
99 perror ("unable to listen on socket");
103 accept_ev.start (fd, EVENT_READ);
106 void unix_listener::accept_cb (io_watcher &w, short revents)
108 int fd2 = accept (fd, 0, 0);
112 fcntl (fd2, F_SETFD, FD_CLOEXEC);
117 void server::log_msg (const char *msg)
119 send ("MSG"), send (msg);
122 void server::err (const char *format, ...)
129 va_start (ap, format);
130 vsnprintf (err, 1024, format, ap);
133 send ("MSG"), send (err);
141 void server::read_cb (io_watcher &w, short revents)
147 if (!strcmp (tok, "NEW"))
149 stringvec *argv = new stringvec;
150 stringvec *envv = new stringvec;
157 if (!strcmp (tok, "END"))
159 else if (!strcmp (tok, "ENV") && recv (tok))
160 envv->push_back (tok.get ());
161 else if (!strcmp (tok, "CWD") && recv (tok))
164 err ("unable to change to working directory to '%s': %s",
165 (char *)tok, strerror (errno));
167 else if (!strcmp (tok, "ARG") && recv (tok))
168 argv->push_back (tok.get ());
170 return err ("protocol error: unexpected NEW token");
176 char **old_environ = environ;
177 environ = envv->begin ();
179 rxvt_term *term = new rxvt_term;
181 term->log_hook = &log_cb;
189 success = term->init (argv->size (), argv->begin ());
191 catch (const class rxvt_failure_exception &e)
198 environ = old_environ;
203 send ("END"); send (success ? 1 : 0);
207 return err ("protocol error: request '%s' unsupported.", (char *)tok);
214 main (int argc, const char *const *argv)
216 rxvt_init_signals ();
218 char *sockname = rxvt_connection::unix_sockname ();
219 unix_listener l (sockname);
220 printf ("rxvtd listening on %s.\n", sockname);