ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtd.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtd.C (file contents):
Revision 1.1 by pcg, Mon Nov 24 17:28:08 2003 UTC vs.
Revision 1.12 by pcg, Sun Apr 4 23:52:37 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines