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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines