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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines