|
|
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 | |
… | |
… | |
26 | } |
29 | } |
27 | |
30 | |
28 | void err (const char *format = 0, ...); |
31 | void err (const char *format = 0, ...); |
29 | }; |
32 | }; |
30 | |
33 | |
31 | struct listener { |
34 | struct unix_listener { |
32 | int fd; |
35 | int fd; |
33 | |
36 | |
34 | 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; |
35 | |
38 | |
36 | listener (); |
39 | unix_listener (const char *sockname); |
37 | }; |
40 | }; |
38 | |
41 | |
39 | listener::listener () |
42 | unix_listener::unix_listener (const char *sockname) |
40 | : accept_ev (this, &listener::accept_cb) |
43 | : accept_ev (this, &unix_listener::accept_cb) |
41 | { |
44 | { |
42 | if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) |
45 | if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) |
43 | { |
46 | { |
44 | perror ("unable to create listening socket"); |
47 | perror ("unable to create listening socket"); |
45 | exit (EXIT_FAILURE); |
48 | exit (EXIT_FAILURE); |
46 | } |
49 | } |
47 | |
50 | |
48 | sockaddr_un sa; |
51 | sockaddr_un sa; |
49 | |
52 | |
50 | sa.sun_family = AF_UNIX; |
53 | sa.sun_family = AF_UNIX; |
51 | strcpy (sa.sun_path, rxvt_connection::unix_sockname ()); |
54 | strcpy (sa.sun_path, sockname); |
52 | |
55 | |
53 | unlink (rxvt_connection::unix_sockname ()); |
56 | unlink (rxvt_connection::unix_sockname ()); |
|
|
57 | |
|
|
58 | mode_t omask = umask (0077); |
54 | |
59 | |
55 | if (bind (fd, (sockaddr *)&sa, sizeof (sa))) |
60 | if (bind (fd, (sockaddr *)&sa, sizeof (sa))) |
56 | { |
61 | { |
57 | perror ("unable to bind listening socket"); |
62 | perror ("unable to bind listening socket"); |
58 | exit (EXIT_FAILURE); |
63 | exit (EXIT_FAILURE); |
59 | } |
64 | } |
|
|
65 | |
|
|
66 | umask (omask); |
60 | |
67 | |
61 | if (listen (fd, 5)) |
68 | if (listen (fd, 5)) |
62 | { |
69 | { |
63 | perror ("unable to listen on socket"); |
70 | perror ("unable to listen on socket"); |
64 | exit (EXIT_FAILURE); |
71 | exit (EXIT_FAILURE); |
65 | } |
72 | } |
66 | |
73 | |
67 | accept_ev.start (fd, EVENT_READ); |
74 | accept_ev.start (fd, EVENT_READ); |
68 | } |
75 | } |
69 | |
76 | |
70 | void listener::accept_cb (io_watcher &w, short revents) |
77 | void unix_listener::accept_cb (io_watcher &w, short revents) |
71 | { |
78 | { |
72 | int fd2 = accept (fd, 0, 0); |
79 | int fd2 = accept (fd, 0, 0); |
73 | |
80 | |
74 | if (fd2 >= 0) |
81 | if (fd2 >= 0) |
75 | new server (fd2); |
82 | new server (fd2); |
… | |
… | |
99 | |
106 | |
100 | if (recv (tok)) |
107 | if (recv (tok)) |
101 | { |
108 | { |
102 | if (!strcmp (tok, "NEW")) |
109 | if (!strcmp (tok, "NEW")) |
103 | { |
110 | { |
104 | stringvec argv; |
111 | stringvec *argv = new stringvec; |
105 | stringvec envv; |
112 | stringvec *envv = new stringvec; |
106 | |
113 | |
107 | for (;;) |
114 | for (;;) |
108 | { |
115 | { |
109 | if (!recv (tok)) |
116 | if (!recv (tok)) |
110 | return err (); |
117 | return err (); |
111 | |
118 | |
112 | if (!strcmp (tok, "END")) |
119 | if (!strcmp (tok, "END")) |
113 | break; |
120 | break; |
114 | else if (!strcmp (tok, "ENV") && recv (tok)) |
121 | else if (!strcmp (tok, "ENV") && recv (tok)) |
115 | envv.push_back (tok.get ()); |
122 | envv->push_back (tok.get ()); |
116 | else if (!strcmp (tok, "CWD") && recv (tok)) |
123 | else if (!strcmp (tok, "CWD") && recv (tok)) |
117 | { |
124 | { |
118 | if (chdir (tok)) |
125 | if (chdir (tok)) |
119 | err ("unable to change to working directory to '%s': %s", |
126 | err ("unable to change to working directory to '%s': %s", |
120 | (char *)tok, strerror (errno)); |
127 | (char *)tok, strerror (errno)); |
121 | } |
128 | } |
122 | else if (!strcmp (tok, "ARG") && recv (tok)) |
129 | else if (!strcmp (tok, "ARG") && recv (tok)) |
123 | argv.push_back (tok.get ()); |
130 | argv->push_back (tok.get ()); |
124 | else |
131 | else |
125 | return err ("protocol error: unexpected NEW token"); |
132 | return err ("protocol error: unexpected NEW token"); |
126 | } |
133 | } |
127 | |
134 | |
128 | envv.push_back (0); |
135 | envv->push_back (0); |
129 | |
136 | |
130 | { |
137 | { |
131 | char **old_environ = environ; |
138 | char **old_environ = environ; |
132 | environ = envv.begin (); |
139 | environ = envv->begin (); |
133 | |
140 | |
134 | rxvt_init (argv.size (), argv.begin ()); |
141 | rxvt_term *term = rxvt_init (argv->size (), argv->begin ()); |
135 | //dR; |
142 | |
136 | //rxvt_main_loop (aR); |
143 | term->argv = argv; |
|
|
144 | term->envv = envv; |
137 | |
145 | |
138 | environ = old_environ; |
146 | environ = old_environ; |
139 | } |
147 | } |
140 | } |
148 | } |
141 | else |
149 | else |
… | |
… | |
144 | else |
152 | else |
145 | return err (); |
153 | return err (); |
146 | } |
154 | } |
147 | |
155 | |
148 | int |
156 | int |
149 | main(int argc, const char *const *argv) |
157 | main (int argc, const char *const *argv) |
150 | { |
158 | { |
151 | listener l; |
159 | rxvt_init_signals (); |
152 | |
160 | |
153 | { |
161 | char *sockname = rxvt_connection::unix_sockname (); |
154 | sigset_t ss; |
162 | unix_listener l (sockname); |
|
|
163 | printf ("rxvtd listening on %s.\n", sockname); |
|
|
164 | free (sockname); |
155 | |
165 | |
156 | sigaddset (&ss, SIGHUP); |
|
|
157 | sigprocmask (SIG_BLOCK, &ss, 0); |
|
|
158 | } |
|
|
159 | |
|
|
160 | printf ("rxvtd running.\n"); |
|
|
161 | iom.loop (); |
166 | iom.loop (); |
162 | |
167 | |
163 | #if 0 |
168 | #if 0 |
164 | if (rxvt_init(argc, argv) == NULL) |
169 | if (rxvt_init (argc, argv) == NULL) |
165 | return EXIT_FAILURE; |
170 | return EXIT_FAILURE; |
166 | |
171 | |
167 | dR; |
172 | dR; |
168 | rxvt_main_loop(aR); /* main processing loop */ |
173 | rxvt_main_loop (aR); /* main processing loop */ |
169 | #endif |
174 | #endif |
170 | return EXIT_SUCCESS; |
175 | return EXIT_SUCCESS; |
171 | } |
176 | } |
|
|
177 | |