ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtd.C
Revision: 1.19
Committed: Wed Feb 9 08:32:57 2005 UTC (19 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-5_0
Changes since 1.18: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.14 /*--------------------------------*-C-*---------------------------------*
2     * File: rxvtd.C
3     *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6     * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
7     *
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.
12     *
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.
17     *
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     *----------------------------------------------------------------------*/
22    
23 pcg 1.9 #include "../config.h"
24     #include "rxvt.h"
25 pcg 1.1 #include "rxvtdaemon.h"
26     #include "iom.h"
27    
28     #include <cstdio>
29     #include <cstdlib>
30 pcg 1.3 #include <cstdarg>
31     #include <cstring>
32 pcg 1.1
33     #include <unistd.h>
34 pcg 1.12 #include <fcntl.h>
35 pcg 1.5 #include <sys/types.h>
36     #include <sys/stat.h>
37 pcg 1.1 #include <sys/socket.h>
38     #include <sys/un.h>
39    
40 pcg 1.3 #include <cerrno>
41    
42     extern char **environ;
43    
44 pcg 1.1 struct server : rxvt_connection {
45 pcg 1.10 log_callback log_cb;
46    
47 pcg 1.1 void read_cb (io_watcher &w, short revents); io_watcher read_ev;
48 pcg 1.10 void log_msg (const char *msg);
49 pcg 1.1
50     server (int fd)
51 pcg 1.10 : read_ev (this, &server::read_cb),
52     log_cb (this, &server::log_msg)
53 pcg 1.1 {
54     this->fd = fd;
55     read_ev.start (fd, EVENT_READ);
56     }
57    
58 pcg 1.3 void err (const char *format = 0, ...);
59 pcg 1.1 };
60    
61 pcg 1.5 struct unix_listener {
62 pcg 1.1 int fd;
63    
64     void accept_cb (io_watcher &w, short revents); io_watcher accept_ev;
65    
66 pcg 1.5 unix_listener (const char *sockname);
67 pcg 1.1 };
68    
69 pcg 1.5 unix_listener::unix_listener (const char *sockname)
70     : accept_ev (this, &unix_listener::accept_cb)
71 pcg 1.1 {
72 pcg 1.14 if ((fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0)
73 pcg 1.1 {
74     perror ("unable to create listening socket");
75     exit (EXIT_FAILURE);
76     }
77    
78 pcg 1.12 fcntl (fd, F_SETFD, FD_CLOEXEC);
79    
80 pcg 1.1 sockaddr_un sa;
81    
82     sa.sun_family = AF_UNIX;
83 pcg 1.5 strcpy (sa.sun_path, sockname);
84 pcg 1.1
85     unlink (rxvt_connection::unix_sockname ());
86    
87 pcg 1.5 mode_t omask = umask (0077);
88    
89 pcg 1.1 if (bind (fd, (sockaddr *)&sa, sizeof (sa)))
90     {
91     perror ("unable to bind listening socket");
92     exit (EXIT_FAILURE);
93     }
94    
95 pcg 1.5 umask (omask);
96    
97 pcg 1.1 if (listen (fd, 5))
98     {
99     perror ("unable to listen on socket");
100     exit (EXIT_FAILURE);
101     }
102    
103     accept_ev.start (fd, EVENT_READ);
104     }
105    
106 pcg 1.5 void unix_listener::accept_cb (io_watcher &w, short revents)
107 pcg 1.1 {
108     int fd2 = accept (fd, 0, 0);
109    
110     if (fd2 >= 0)
111 pcg 1.12 {
112     fcntl (fd2, F_SETFD, FD_CLOEXEC);
113     new server (fd2);
114     }
115 pcg 1.1 }
116    
117 pcg 1.10 void server::log_msg (const char *msg)
118     {
119     send ("MSG"), send (msg);
120     }
121    
122 pcg 1.3 void server::err (const char *format, ...)
123 pcg 1.1 {
124 pcg 1.3 if (format)
125     {
126     char err[1024];
127    
128     va_list ap;
129     va_start (ap, format);
130     vsnprintf (err, 1024, format, ap);
131     va_end (ap);
132    
133 pcg 1.10 send ("MSG"), send (err);
134 pcg 1.3 }
135    
136 pcg 1.10 send ("END", 0);
137 pcg 1.1 close (fd);
138     delete this;
139     }
140    
141     void server::read_cb (io_watcher &w, short revents)
142     {
143 pcg 1.2 auto_str tok;
144 pcg 1.1
145 pcg 1.2 if (recv (tok))
146 pcg 1.1 {
147 pcg 1.2 if (!strcmp (tok, "NEW"))
148 pcg 1.1 {
149 pcg 1.9 stringvec *argv = new stringvec;
150     stringvec *envv = new stringvec;
151 pcg 1.2
152     for (;;)
153     {
154     if (!recv (tok))
155     return err ();
156    
157     if (!strcmp (tok, "END"))
158     break;
159 pcg 1.3 else if (!strcmp (tok, "ENV") && recv (tok))
160 pcg 1.9 envv->push_back (tok.get ());
161 pcg 1.3 else if (!strcmp (tok, "CWD") && recv (tok))
162     {
163     if (chdir (tok))
164     err ("unable to change to working directory to '%s': %s",
165     (char *)tok, strerror (errno));
166     }
167 pcg 1.2 else if (!strcmp (tok, "ARG") && recv (tok))
168 pcg 1.9 argv->push_back (tok.get ());
169 pcg 1.2 else
170 pcg 1.3 return err ("protocol error: unexpected NEW token");
171 pcg 1.2 }
172    
173 pcg 1.9 envv->push_back (0);
174 pcg 1.3
175     {
176     char **old_environ = environ;
177 pcg 1.9 environ = envv->begin ();
178 pcg 1.3
179 pcg 1.10 rxvt_term *term = new rxvt_term;
180    
181     term->log_hook = &log_cb;
182 pcg 1.9 term->argv = argv;
183     term->envv = envv;
184 pcg 1.2
185 pcg 1.10 bool success;
186    
187     try
188     {
189     success = term->init (argv->size (), argv->begin ());
190     }
191     catch (const class rxvt_failure_exception &e)
192     {
193     success = false;
194     }
195    
196 root 1.15 term->log_hook = 0;
197    
198 pcg 1.3 environ = old_environ;
199 pcg 1.10
200     if (!success)
201     term->destroy ();
202    
203     send ("END"); send (success ? 1 : 0);
204 pcg 1.3 }
205 pcg 1.1 }
206     else
207 pcg 1.3 return err ("protocol error: request '%s' unsupported.", (char *)tok);
208 pcg 1.1 }
209     else
210 pcg 1.2 return err ();
211 pcg 1.1 }
212    
213     int
214 pcg 1.8 main (int argc, const char *const *argv)
215 pcg 1.1 {
216 root 1.16 rxvt_init ();
217 pcg 1.5
218     char *sockname = rxvt_connection::unix_sockname ();
219     unix_listener l (sockname);
220 root 1.17 printf ("rxvt-unicode daemon listening on %s.\n", sockname);
221 root 1.19 fflush (stdout);
222 pcg 1.5 free (sockname);
223    
224 root 1.18 io_manager::loop ();
225 pcg 1.1
226     return EXIT_SUCCESS;
227     }
228 pcg 1.7