xref: /qemu/qga/channel-posix.c (revision 6c1fdcf9)
1 #include <glib.h>
2 #include <termios.h>
3 #include "qemu_socket.h"
4 #include "qga/channel.h"
5 
6 #define GA_CHANNEL_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */
7 
8 struct GAChannel {
9     GIOChannel *listen_channel;
10     GIOChannel *client_channel;
11     GAChannelMethod method;
12     GAChannelCallback event_cb;
13     gpointer user_data;
14 };
15 
16 static int ga_channel_client_add(GAChannel *c, int fd);
17 
18 static gboolean ga_channel_listen_accept(GIOChannel *channel,
19                                          GIOCondition condition, gpointer data)
20 {
21     GAChannel *c = data;
22     int ret, client_fd;
23     bool accepted = false;
24     struct sockaddr_un addr;
25     socklen_t addrlen = sizeof(addr);
26 
27     g_assert(channel != NULL);
28 
29     client_fd = qemu_accept(g_io_channel_unix_get_fd(channel),
30                             (struct sockaddr *)&addr, &addrlen);
31     if (client_fd == -1) {
32         g_warning("error converting fd to gsocket: %s", strerror(errno));
33         goto out;
34     }
35     fcntl(client_fd, F_SETFL, O_NONBLOCK);
36     ret = ga_channel_client_add(c, client_fd);
37     if (ret) {
38         g_warning("error setting up connection");
39         goto out;
40     }
41     accepted = true;
42 
43 out:
44     /* only accept 1 connection at a time */
45     return !accepted;
46 }
47 
48 /* start polling for readable events on listen fd, new==true
49  * indicates we should use the existing s->listen_channel
50  */
51 static void ga_channel_listen_add(GAChannel *c, int listen_fd, bool create)
52 {
53     if (create) {
54         c->listen_channel = g_io_channel_unix_new(listen_fd);
55     }
56     g_io_add_watch(c->listen_channel, G_IO_IN, ga_channel_listen_accept, c);
57 }
58 
59 static void ga_channel_listen_close(GAChannel *c)
60 {
61     g_assert(c->method == GA_CHANNEL_UNIX_LISTEN);
62     g_assert(c->listen_channel);
63     g_io_channel_shutdown(c->listen_channel, true, NULL);
64     g_io_channel_unref(c->listen_channel);
65     c->listen_channel = NULL;
66 }
67 
68 /* cleanup state for closed connection/session, start accepting new
69  * connections if we're in listening mode
70  */
71 static void ga_channel_client_close(GAChannel *c)
72 {
73     g_assert(c->client_channel);
74     g_io_channel_shutdown(c->client_channel, true, NULL);
75     g_io_channel_unref(c->client_channel);
76     c->client_channel = NULL;
77     if (c->method == GA_CHANNEL_UNIX_LISTEN && c->listen_channel) {
78         ga_channel_listen_add(c, 0, false);
79     }
80 }
81 
82 static gboolean ga_channel_client_event(GIOChannel *channel,
83                                         GIOCondition condition, gpointer data)
84 {
85     GAChannel *c = data;
86     gboolean client_cont;
87 
88     g_assert(c);
89     if (c->event_cb) {
90         client_cont = c->event_cb(condition, c->user_data);
91         if (!client_cont) {
92             ga_channel_client_close(c);
93             return false;
94         }
95     }
96     return true;
97 }
98 
99 static int ga_channel_client_add(GAChannel *c, int fd)
100 {
101     GIOChannel *client_channel;
102     GError *err = NULL;
103 
104     g_assert(c && !c->client_channel);
105     client_channel = g_io_channel_unix_new(fd);
106     g_assert(client_channel);
107     g_io_channel_set_encoding(client_channel, NULL, &err);
108     if (err != NULL) {
109         g_warning("error setting channel encoding to binary");
110         g_error_free(err);
111         return -1;
112     }
113     g_io_add_watch(client_channel, G_IO_IN | G_IO_HUP,
114                    ga_channel_client_event, c);
115     c->client_channel = client_channel;
116     return 0;
117 }
118 
119 static gboolean ga_channel_open(GAChannel *c, const gchar *path, GAChannelMethod method)
120 {
121     int ret;
122     c->method = method;
123 
124     switch (c->method) {
125     case GA_CHANNEL_VIRTIO_SERIAL: {
126         int fd = qemu_open(path, O_RDWR | O_NONBLOCK | O_ASYNC);
127         if (fd == -1) {
128             g_critical("error opening channel: %s", strerror(errno));
129             exit(EXIT_FAILURE);
130         }
131         ret = ga_channel_client_add(c, fd);
132         if (ret) {
133             g_critical("error adding channel to main loop");
134             return false;
135         }
136         break;
137     }
138     case GA_CHANNEL_ISA_SERIAL: {
139         struct termios tio;
140         int fd = qemu_open(path, O_RDWR | O_NOCTTY | O_NONBLOCK);
141         if (fd == -1) {
142             g_critical("error opening channel: %s", strerror(errno));
143             exit(EXIT_FAILURE);
144         }
145         tcgetattr(fd, &tio);
146         /* set up serial port for non-canonical, dumb byte streaming */
147         tio.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP |
148                          INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY |
149                          IMAXBEL);
150         tio.c_oflag = 0;
151         tio.c_lflag = 0;
152         tio.c_cflag |= GA_CHANNEL_BAUDRATE_DEFAULT;
153         /* 1 available byte min or reads will block (we'll set non-blocking
154          * elsewhere, else we have to deal with read()=0 instead)
155          */
156         tio.c_cc[VMIN] = 1;
157         tio.c_cc[VTIME] = 0;
158         /* flush everything waiting for read/xmit, it's garbage at this point */
159         tcflush(fd, TCIFLUSH);
160         tcsetattr(fd, TCSANOW, &tio);
161         ret = ga_channel_client_add(c, fd);
162         if (ret) {
163             g_error("error adding channel to main loop");
164         }
165         break;
166     }
167     case GA_CHANNEL_UNIX_LISTEN: {
168         int fd = unix_listen(path, NULL, strlen(path));
169         if (fd == -1) {
170             g_critical("error opening path: %s", strerror(errno));
171             return false;
172         }
173         ga_channel_listen_add(c, fd, true);
174         break;
175     }
176     default:
177         g_critical("error binding/listening to specified socket");
178         return false;
179     }
180 
181     return true;
182 }
183 
184 GIOStatus ga_channel_write_all(GAChannel *c, const gchar *buf, gsize size)
185 {
186     GError *err = NULL;
187     gsize written = 0;
188     GIOStatus status = G_IO_STATUS_NORMAL;
189 
190     while (size) {
191         status = g_io_channel_write_chars(c->client_channel, buf, size,
192                                           &written, &err);
193         g_debug("sending data, count: %d", (int)size);
194         if (err != NULL) {
195             g_warning("error writing to channel: %s", err->message);
196             return G_IO_STATUS_ERROR;
197         }
198         if (status != G_IO_STATUS_NORMAL) {
199             break;
200         }
201         size -= written;
202     }
203 
204     if (status == G_IO_STATUS_NORMAL) {
205         status = g_io_channel_flush(c->client_channel, &err);
206         if (err != NULL) {
207             g_warning("error flushing channel: %s", err->message);
208             return G_IO_STATUS_ERROR;
209         }
210     }
211 
212     return status;
213 }
214 
215 GIOStatus ga_channel_read(GAChannel *c, gchar *buf, gsize size, gsize *count)
216 {
217     return g_io_channel_read_chars(c->client_channel, buf, size, count, NULL);
218 }
219 
220 GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
221                           GAChannelCallback cb, gpointer opaque)
222 {
223     GAChannel *c = g_malloc0(sizeof(GAChannel));
224     c->event_cb = cb;
225     c->user_data = opaque;
226 
227     if (!ga_channel_open(c, path, method)) {
228         g_critical("error opening channel");
229         ga_channel_free(c);
230         return NULL;
231     }
232 
233     return c;
234 }
235 
236 void ga_channel_free(GAChannel *c)
237 {
238     if (c->method == GA_CHANNEL_UNIX_LISTEN
239         && c->listen_channel) {
240         ga_channel_listen_close(c);
241     }
242     if (c->client_channel) {
243         ga_channel_client_close(c);
244     }
245     g_free(c);
246 }
247