1 |
#include "rxvtlib.h" |
2 |
#include "rxvtdaemon.h" |
3 |
#include "iom.h" |
4 |
|
5 |
#include <cstdio> |
6 |
#include <cstdlib> |
7 |
|
8 |
#include <unistd.h> |
9 |
#include <sys/socket.h> |
10 |
#include <sys/un.h> |
11 |
|
12 |
struct server : rxvt_connection { |
13 |
void read_cb (io_watcher &w, short revents); io_watcher read_ev; |
14 |
|
15 |
server (int fd) |
16 |
: read_ev (this, &server::read_cb) |
17 |
{ |
18 |
this->fd = fd; |
19 |
read_ev.start (fd, EVENT_READ); |
20 |
} |
21 |
|
22 |
void err (); |
23 |
}; |
24 |
|
25 |
struct listener { |
26 |
int fd; |
27 |
|
28 |
void accept_cb (io_watcher &w, short revents); io_watcher accept_ev; |
29 |
|
30 |
listener (); |
31 |
}; |
32 |
|
33 |
listener::listener () |
34 |
: accept_ev (this, &listener::accept_cb) |
35 |
{ |
36 |
if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) |
37 |
{ |
38 |
perror ("unable to create listening socket"); |
39 |
exit (EXIT_FAILURE); |
40 |
} |
41 |
|
42 |
sockaddr_un sa; |
43 |
|
44 |
sa.sun_family = AF_UNIX; |
45 |
strcpy (sa.sun_path, rxvt_connection::unix_sockname ()); |
46 |
|
47 |
unlink (rxvt_connection::unix_sockname ()); |
48 |
|
49 |
if (bind (fd, (sockaddr *)&sa, sizeof (sa))) |
50 |
{ |
51 |
perror ("unable to bind listening socket"); |
52 |
exit (EXIT_FAILURE); |
53 |
} |
54 |
|
55 |
if (listen (fd, 5)) |
56 |
{ |
57 |
perror ("unable to listen on socket"); |
58 |
exit (EXIT_FAILURE); |
59 |
} |
60 |
|
61 |
accept_ev.start (fd, EVENT_READ); |
62 |
} |
63 |
|
64 |
void listener::accept_cb (io_watcher &w, short revents) |
65 |
{ |
66 |
int fd2 = accept (fd, 0, 0); |
67 |
|
68 |
if (fd2 >= 0) |
69 |
new server (fd2); |
70 |
} |
71 |
|
72 |
void server::err () |
73 |
{ |
74 |
close (fd); |
75 |
delete this; |
76 |
} |
77 |
|
78 |
void server::read_cb (io_watcher &w, short revents) |
79 |
{ |
80 |
token cmd; |
81 |
|
82 |
if (recv (cmd)) |
83 |
{ |
84 |
if (!strcmp (cmd, "NEW")) |
85 |
{ |
86 |
} |
87 |
else |
88 |
err (); |
89 |
} |
90 |
else |
91 |
err (); |
92 |
} |
93 |
|
94 |
int |
95 |
main(int argc, const char *const *argv) |
96 |
{ |
97 |
listener l; |
98 |
iom.loop (); |
99 |
|
100 |
#if 0 |
101 |
if (rxvt_init(argc, argv) == NULL) |
102 |
return EXIT_FAILURE; |
103 |
|
104 |
dR; |
105 |
rxvt_main_loop(aR); /* main processing loop */ |
106 |
#endif |
107 |
return EXIT_SUCCESS; |
108 |
} |