1 /*----------------------------------------------------------------------*
3 *----------------------------------------------------------------------*
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2003-2007 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"
31 #include <sys/types.h>
33 #include <sys/socket.h>
36 #if defined(ENABLE_FRILLS) && defined(_POSIX_MEMLOCK) && _POSIX_MEMLOCK > 0
37 # define ENABLE_MLOCK 1
41 # include <sys/mman.h>
47 #include "rxvtdaemon.h"
48 #include "libptytty.h"
50 struct server : rxvt_connection {
52 getfd_callback getfd_cb;
54 void read_cb (ev::io &w, int revents); ev::io read_ev;
55 void log_msg (const char *msg);
56 int getfd (int remote_fd);
60 read_ev.set <server, &server::read_cb> (this);
61 log_cb.set <server, &server::log_msg> (this);
62 getfd_cb.set<server, &server::getfd> (this);
65 fcntl (fd, F_SETFD, FD_CLOEXEC);
66 fcntl (fd, F_SETFL, 0);
67 read_ev.start (fd, ev::READ);
70 void err (const char *format = 0, ...);
73 struct unix_listener {
76 void accept_cb (ev::io &w, int revents); ev::io accept_ev;
78 unix_listener (const char *sockname);
81 unix_listener::unix_listener (const char *sockname)
83 accept_ev.set<unix_listener, &unix_listener::accept_cb> (this);
87 if (strlen (sockname) >= sizeof(sa.sun_path))
89 fputs ("socket name too long, aborting.\n", stderr);
93 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
95 perror ("unable to create listening socket");
99 fcntl (fd, F_SETFD, FD_CLOEXEC);
100 fcntl (fd, F_SETFL, O_NONBLOCK);
102 sa.sun_family = AF_UNIX;
103 strcpy (sa.sun_path, sockname);
105 unlink (rxvt_connection::unix_sockname ());
107 mode_t omask = umask (0077);
109 if (bind (fd, (sockaddr *)&sa, sizeof (sa)))
111 perror ("unable to bind listening socket");
119 perror ("unable to listen on socket");
123 accept_ev.start (fd, ev::READ);
126 void unix_listener::accept_cb (ev::io &w, int revents)
128 int fd2 = accept (fd, 0, 0);
134 int server::getfd (int remote_fd)
138 return ptytty::recv_fd (fd);
141 void server::log_msg (const char *msg)
143 send ("MSG"), send (msg);
146 void server::err (const char *format, ...)
153 va_start (ap, format);
154 vsnprintf (err, 1024, format, ap);
160 send ("END"), send (0);
165 void server::read_cb (ev::io &w, int revents)
171 if (!strcmp (tok, "NEW"))
173 stringvec *argv = new stringvec;
174 stringvec *envv = new stringvec;
181 if (!strcmp (tok, "END"))
183 else if (!strcmp (tok, "ENV") && recv (tok))
184 envv->push_back (strdup (tok));
185 else if (!strcmp (tok, "ARG") && recv (tok))
186 argv->push_back (strdup (tok));
188 return err ("protocol error: unexpected NEW token.\n");
194 rxvt_term *term = new rxvt_term;
196 term->log_hook = &log_cb;
197 term->getfd_hook = &getfd_cb;
203 term->init (argv, envv);
205 catch (const class rxvt_failure_exception &e)
212 chdir ("/"); // init might change to different working directory
217 send ("END"); send (success ? 1 : 0);
221 return err ("protocol error: request '%s' unsupported.\n", (char *)tok);
227 int opt_fork, opt_opendisplay, opt_quiet;
233 main (int argc, const char *const *argv)
237 for (int i = 1; i < argc; i++)
239 if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork"))
241 else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay"))
243 else if (!strcmp (argv [i], "-q") || !strcmp (argv [i], "--quiet"))
246 else if (!strcmp (argv [i], "-m") || !strcmp (argv [i], "--mlock"))
251 rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]);
256 // optionally open display and never release it.
258 if (const char *dpy = getenv ("DISPLAY"))
259 displays.get (dpy ? dpy : ":0"); // move string logic into rxvt_display maybe?
261 char *sockname = rxvt_connection::unix_sockname ();
262 unix_listener l (sockname);
268 printf ("rxvt-unicode daemon listening on %s.\n", sockname);
281 // Optionally perform an mlockall so this process does not get swapped out.
282 if (opt_lock && !pid)
283 if (mlockall (MCL_CURRENT | MCL_FUTURE) < 0)
284 perror ("unable to lock into ram");
291 rxvt_log ("unable to fork daemon, aborting.\n");
295 _exit (EXIT_SUCCESS);