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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines