… | |
… | |
6 | #include <cstdlib> |
6 | #include <cstdlib> |
7 | #include <cstdarg> |
7 | #include <cstdarg> |
8 | #include <cstring> |
8 | #include <cstring> |
9 | |
9 | |
10 | #include <unistd.h> |
10 | #include <unistd.h> |
|
|
11 | #include <sys/types.h> |
|
|
12 | #include <sys/stat.h> |
11 | #include <sys/socket.h> |
13 | #include <sys/socket.h> |
12 | #include <sys/un.h> |
14 | #include <sys/un.h> |
13 | |
15 | |
14 | #include <cerrno> |
16 | #include <cerrno> |
15 | |
17 | |
… | |
… | |
26 | } |
28 | } |
27 | |
29 | |
28 | void err (const char *format = 0, ...); |
30 | void err (const char *format = 0, ...); |
29 | }; |
31 | }; |
30 | |
32 | |
31 | struct listener { |
33 | struct unix_listener { |
32 | int fd; |
34 | int fd; |
33 | |
35 | |
34 | void accept_cb (io_watcher &w, short revents); io_watcher accept_ev; |
36 | void accept_cb (io_watcher &w, short revents); io_watcher accept_ev; |
35 | |
37 | |
36 | listener (); |
38 | unix_listener (const char *sockname); |
37 | }; |
39 | }; |
38 | |
40 | |
39 | listener::listener () |
41 | unix_listener::unix_listener (const char *sockname) |
40 | : accept_ev (this, &listener::accept_cb) |
42 | : accept_ev (this, &unix_listener::accept_cb) |
41 | { |
43 | { |
42 | if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) |
44 | if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) |
43 | { |
45 | { |
44 | perror ("unable to create listening socket"); |
46 | perror ("unable to create listening socket"); |
45 | exit (EXIT_FAILURE); |
47 | exit (EXIT_FAILURE); |
46 | } |
48 | } |
47 | |
49 | |
48 | sockaddr_un sa; |
50 | sockaddr_un sa; |
49 | |
51 | |
50 | sa.sun_family = AF_UNIX; |
52 | sa.sun_family = AF_UNIX; |
51 | strcpy (sa.sun_path, rxvt_connection::unix_sockname ()); |
53 | strcpy (sa.sun_path, sockname); |
52 | |
54 | |
53 | unlink (rxvt_connection::unix_sockname ()); |
55 | unlink (rxvt_connection::unix_sockname ()); |
|
|
56 | |
|
|
57 | mode_t omask = umask (0077); |
54 | |
58 | |
55 | if (bind (fd, (sockaddr *)&sa, sizeof (sa))) |
59 | if (bind (fd, (sockaddr *)&sa, sizeof (sa))) |
56 | { |
60 | { |
57 | perror ("unable to bind listening socket"); |
61 | perror ("unable to bind listening socket"); |
58 | exit (EXIT_FAILURE); |
62 | exit (EXIT_FAILURE); |
59 | } |
63 | } |
|
|
64 | |
|
|
65 | umask (omask); |
60 | |
66 | |
61 | if (listen (fd, 5)) |
67 | if (listen (fd, 5)) |
62 | { |
68 | { |
63 | perror ("unable to listen on socket"); |
69 | perror ("unable to listen on socket"); |
64 | exit (EXIT_FAILURE); |
70 | exit (EXIT_FAILURE); |
65 | } |
71 | } |
66 | |
72 | |
67 | accept_ev.start (fd, EVENT_READ); |
73 | accept_ev.start (fd, EVENT_READ); |
68 | } |
74 | } |
69 | |
75 | |
70 | void listener::accept_cb (io_watcher &w, short revents) |
76 | void unix_listener::accept_cb (io_watcher &w, short revents) |
71 | { |
77 | { |
72 | int fd2 = accept (fd, 0, 0); |
78 | int fd2 = accept (fd, 0, 0); |
73 | |
79 | |
74 | if (fd2 >= 0) |
80 | if (fd2 >= 0) |
75 | new server (fd2); |
81 | new server (fd2); |
… | |
… | |
130 | { |
136 | { |
131 | char **old_environ = environ; |
137 | char **old_environ = environ; |
132 | environ = envv.begin (); |
138 | environ = envv.begin (); |
133 | |
139 | |
134 | rxvt_init (argv.size (), argv.begin ()); |
140 | rxvt_init (argv.size (), argv.begin ()); |
135 | //dR; |
|
|
136 | //rxvt_main_loop (aR); |
|
|
137 | |
141 | |
138 | environ = old_environ; |
142 | environ = old_environ; |
|
|
143 | envv.clear (); // can't yet save the env 'cause rxvt modifies it : ( |
139 | } |
144 | } |
140 | } |
145 | } |
141 | else |
146 | else |
142 | return err ("protocol error: request '%s' unsupported.", (char *)tok); |
147 | return err ("protocol error: request '%s' unsupported.", (char *)tok); |
143 | } |
148 | } |
144 | else |
149 | else |
145 | return err (); |
150 | return err (); |
146 | } |
151 | } |
147 | |
152 | |
148 | int |
153 | int |
149 | main(int argc, const char *const *argv) |
154 | main (int argc, const char *const *argv) |
150 | { |
155 | { |
151 | listener l; |
156 | rxvt_init_signals (); |
152 | |
157 | |
153 | { |
158 | char *sockname = rxvt_connection::unix_sockname (); |
154 | sigset_t ss; |
159 | unix_listener l (sockname); |
|
|
160 | printf ("rxvtd listening on %s.\n", sockname); |
|
|
161 | free (sockname); |
155 | |
162 | |
156 | sigaddset (&ss, SIGHUP); |
|
|
157 | sigprocmask (SIG_BLOCK, &ss, 0); |
|
|
158 | } |
|
|
159 | |
|
|
160 | printf ("rxvtd running.\n"); |
|
|
161 | iom.loop (); |
163 | iom.loop (); |
162 | |
164 | |
163 | #if 0 |
165 | #if 0 |
164 | if (rxvt_init(argc, argv) == NULL) |
166 | if (rxvt_init (argc, argv) == NULL) |
165 | return EXIT_FAILURE; |
167 | return EXIT_FAILURE; |
166 | |
168 | |
167 | dR; |
169 | dR; |
168 | rxvt_main_loop(aR); /* main processing loop */ |
170 | rxvt_main_loop (aR); /* main processing loop */ |
169 | #endif |
171 | #endif |
170 | return EXIT_SUCCESS; |
172 | return EXIT_SUCCESS; |
171 | } |
173 | } |
|
|
174 | |