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.7 by pcg, Fri Jan 16 22:11:09 2004 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>
11#include <sys/types.h> 13#include <sys/types.h>
12#include <sys/stat.h> 14#include <sys/stat.h>
13#include <sys/socket.h> 15#include <sys/socket.h>
14#include <sys/un.h> 16#include <sys/un.h>
15 17
16#include <cerrno> 18#include <cerrno>
17 19
18extern char **environ; 20extern char **environ;
19 21
20struct server : rxvt_connection { 22struct server : rxvt_connection {
23 log_callback log_cb;
24
21 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);
22 27
23 server (int fd) 28 server (int fd)
24 : read_ev (this, &server::read_cb) 29 : read_ev (this, &server::read_cb),
30 log_cb (this, &server::log_msg)
25 { 31 {
26 this->fd = fd; 32 this->fd = fd;
27 read_ev.start (fd, EVENT_READ); 33 read_ev.start (fd, EVENT_READ);
28 } 34 }
29 35
45 { 51 {
46 perror ("unable to create listening socket"); 52 perror ("unable to create listening socket");
47 exit (EXIT_FAILURE); 53 exit (EXIT_FAILURE);
48 } 54 }
49 55
56 fcntl (fd, F_SETFD, FD_CLOEXEC);
57
50 sockaddr_un sa; 58 sockaddr_un sa;
51 59
52 sa.sun_family = AF_UNIX; 60 sa.sun_family = AF_UNIX;
53 strcpy (sa.sun_path, sockname); 61 strcpy (sa.sun_path, sockname);
54 62
76void unix_listener::accept_cb (io_watcher &w, short revents) 84void unix_listener::accept_cb (io_watcher &w, short revents)
77{ 85{
78 int fd2 = accept (fd, 0, 0); 86 int fd2 = accept (fd, 0, 0);
79 87
80 if (fd2 >= 0) 88 if (fd2 >= 0)
89 {
90 fcntl (fd2, F_SETFD, FD_CLOEXEC);
81 new server (fd2); 91 new server (fd2);
92 }
93}
94
95void server::log_msg (const char *msg)
96{
97 send ("MSG"), send (msg);
82} 98}
83 99
84void server::err (const char *format, ...) 100void server::err (const char *format, ...)
85{ 101{
86 if (format) 102 if (format)
90 va_list ap; 106 va_list ap;
91 va_start (ap, format); 107 va_start (ap, format);
92 vsnprintf (err, 1024, format, ap); 108 vsnprintf (err, 1024, format, ap);
93 va_end (ap); 109 va_end (ap);
94 110
95 send ("ERR"), send (err); 111 send ("MSG"), send (err);
96 } 112 }
97 113
114 send ("END", 0);
98 close (fd); 115 close (fd);
99 delete this; 116 delete this;
100} 117}
101 118
102void server::read_cb (io_watcher &w, short revents) 119void server::read_cb (io_watcher &w, short revents)
105 122
106 if (recv (tok)) 123 if (recv (tok))
107 { 124 {
108 if (!strcmp (tok, "NEW")) 125 if (!strcmp (tok, "NEW"))
109 { 126 {
110 stringvec argv; 127 stringvec *argv = new stringvec;
111 stringvec envv; 128 stringvec *envv = new stringvec;
112 129
113 for (;;) 130 for (;;)
114 { 131 {
115 if (!recv (tok)) 132 if (!recv (tok))
116 return err (); 133 return err ();
117 134
118 if (!strcmp (tok, "END")) 135 if (!strcmp (tok, "END"))
119 break; 136 break;
120 else if (!strcmp (tok, "ENV") && recv (tok)) 137 else if (!strcmp (tok, "ENV") && recv (tok))
121 envv.push_back (tok.get ()); 138 envv->push_back (tok.get ());
122 else if (!strcmp (tok, "CWD") && recv (tok)) 139 else if (!strcmp (tok, "CWD") && recv (tok))
123 { 140 {
124 if (chdir (tok)) 141 if (chdir (tok))
125 err ("unable to change to working directory to '%s': %s", 142 err ("unable to change to working directory to '%s': %s",
126 (char *)tok, strerror (errno)); 143 (char *)tok, strerror (errno));
127 } 144 }
128 else if (!strcmp (tok, "ARG") && recv (tok)) 145 else if (!strcmp (tok, "ARG") && recv (tok))
129 argv.push_back (tok.get ()); 146 argv->push_back (tok.get ());
130 else 147 else
131 return err ("protocol error: unexpected NEW token"); 148 return err ("protocol error: unexpected NEW token");
132 } 149 }
133 150
134 envv.push_back (0); 151 envv->push_back (0);
135 152
136 { 153 {
137 char **old_environ = environ; 154 char **old_environ = environ;
138 environ = envv.begin (); 155 environ = envv->begin ();
139 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 {
140 rxvt_init (argv.size (), argv.begin ()); 167 success = term->init (argv->size (), argv->begin ());
168 }
169 catch (const class rxvt_failure_exception &e)
170 {
171 success = false;
172 }
141 173
142 environ = old_environ; 174 environ = old_environ;
143 envv.clear (); // can't yet save the env 'cause rxvt modifies it :( 175
176 if (!success)
177 term->destroy ();
178
179 send ("END"); send (success ? 1 : 0);
144 } 180 }
145 } 181 }
146 else 182 else
147 return err ("protocol error: request '%s' unsupported.", (char *)tok); 183 return err ("protocol error: request '%s' unsupported.", (char *)tok);
148 } 184 }
149 else 185 else
150 return err (); 186 return err ();
151} 187}
152 188
153int 189int
154main(int argc, const char *const *argv) 190main (int argc, const char *const *argv)
155{ 191{
156 rxvt_init_signals (); 192 rxvt_init_signals ();
157 193
158 char *sockname = rxvt_connection::unix_sockname (); 194 char *sockname = rxvt_connection::unix_sockname ();
159 unix_listener l (sockname); 195 unix_listener l (sockname);
161 free (sockname); 197 free (sockname);
162 198
163 iom.loop (); 199 iom.loop ();
164 200
165#if 0 201#if 0
166 if (rxvt_init(argc, argv) == NULL) 202 if (rxvt_init (argc, argv) == NULL)
167 return EXIT_FAILURE; 203 return EXIT_FAILURE;
168 204
169 dR; 205 dR;
170 rxvt_main_loop(aR); /* main processing loop */ 206 rxvt_main_loop (aR); /* main processing loop */
171#endif 207#endif
172 return EXIT_SUCCESS; 208 return EXIT_SUCCESS;
173} 209}
174 210

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines