|
|
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 | |
|
|
19 | extern char **environ; |
11 | |
20 | |
12 | struct server : rxvt_connection { |
21 | struct 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 | |
25 | struct listener { |
34 | struct 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 | |
33 | listener::listener () |
42 | unix_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 | |
64 | void listener::accept_cb (io_watcher &w, short revents) |
77 | void 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 | |
72 | void server::err () |
85 | void 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 | |
78 | void server::read_cb (io_watcher &w, short revents) |
103 | void server::read_cb (io_watcher &w, short revents) |
… | |
… | |
81 | |
106 | |
82 | if (recv (tok)) |
107 | if (recv (tok)) |
83 | { |
108 | { |
84 | if (!strcmp (tok, "NEW")) |
109 | if (!strcmp (tok, "NEW")) |
85 | { |
110 | { |
86 | auto_str display, cwd; |
111 | stringvec *argv = new stringvec; |
87 | simplevec<auto_str> argv; |
112 | stringvec *envv = new stringvec; |
88 | |
113 | |
89 | for (;;) |
114 | for (;;) |
90 | { |
115 | { |
91 | if (!recv (tok)) |
116 | if (!recv (tok)) |
92 | return err (); |
117 | return err (); |
93 | |
118 | |
94 | if (!strcmp (tok, "END")) |
119 | if (!strcmp (tok, "END")) |
95 | break; |
120 | break; |
96 | else if (!strcmp (tok, "DISPLAY") && recv (display)) |
121 | else if (!strcmp (tok, "ENV") && recv (tok)) |
97 | ; |
122 | envv->push_back (tok.get ()); |
98 | else if (!strcmp (tok, "CWD") && recv (cwd)) |
123 | else if (!strcmp (tok, "CWD") && recv (tok)) |
99 | ; |
124 | { |
|
|
125 | if (chdir (tok)) |
|
|
126 | err ("unable to change to working directory to '%s': %s", |
|
|
127 | (char *)tok, strerror (errno)); |
|
|
128 | } |
100 | else if (!strcmp (tok, "ARG") && recv (tok)) |
129 | else if (!strcmp (tok, "ARG") && recv (tok)) |
101 | argv.push_back (tok); |
130 | argv->push_back (tok.get ()); |
102 | else |
131 | else |
103 | return err (); |
132 | return err ("protocol error: unexpected NEW token"); |
104 | } |
133 | } |
105 | |
134 | |
106 | // TODO: no setenv, please |
135 | envv->push_back (0); |
107 | setenv ("DISPLAY", display.get (), 1); |
|
|
108 | |
136 | |
109 | rxvt_init (argv.size (), reinterpret_cast<char **>(argv.begin ())); |
|
|
110 | dR; |
137 | { |
111 | rxvt_main_loop (aR); |
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 | } |
112 | } |
148 | } |
113 | else |
149 | else |
114 | return err (); |
150 | return err ("protocol error: request '%s' unsupported.", (char *)tok); |
115 | } |
151 | } |
116 | else |
152 | else |
117 | return err (); |
153 | return err (); |
118 | } |
154 | } |
119 | |
155 | |
120 | int |
156 | int |
121 | main(int argc, const char *const *argv) |
157 | main (int argc, const char *const *argv) |
122 | { |
158 | { |
123 | 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 | |
124 | iom.loop (); |
166 | iom.loop (); |
125 | |
167 | |
126 | #if 0 |
168 | #if 0 |
127 | if (rxvt_init(argc, argv) == NULL) |
169 | if (rxvt_init (argc, argv) == NULL) |
128 | return EXIT_FAILURE; |
170 | return EXIT_FAILURE; |
129 | |
171 | |
130 | dR; |
172 | dR; |
131 | rxvt_main_loop(aR); /* main processing loop */ |
173 | rxvt_main_loop (aR); /* main processing loop */ |
132 | #endif |
174 | #endif |
133 | return EXIT_SUCCESS; |
175 | return EXIT_SUCCESS; |
134 | } |
176 | } |
|
|
177 | |