vdagentd: fix potentially copying more data then a chunk holds when reading the header
[vd_agent.git] / vdagent.c
blob233fd2cf12ec5ae0b9cbb626071fd14d1003b429
1 /* vdagent.c xorg-client to vdagentd (daemon).
3 Copyright 2010 Red Hat, Inc.
5 Red Hat Authors:
6 Hans de Goede <hdegoede@redhat.com>
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <signal.h>
29 #include <sys/select.h>
30 #include <sys/stat.h>
31 #include <spice/vd_agent.h>
33 #include "udscs.h"
34 #include "vdagentd-proto.h"
35 #include "vdagentd-proto-strings.h"
36 #include "vdagent-x11.h"
38 static int verbose = 0;
39 static struct vdagent_x11 *x11 = NULL;
40 static struct udscs_connection *client = NULL;
41 static FILE *logfile = NULL;
42 static int quit = 0;
44 void daemon_read_complete(struct udscs_connection **connp,
45 struct udscs_message_header *header, const uint8_t *data)
47 switch (header->type) {
48 case VDAGENTD_MONITORS_CONFIG:
49 vdagent_x11_set_monitor_config(x11, (VDAgentMonitorsConfig *)data);
50 break;
51 case VDAGENTD_CLIPBOARD_REQUEST:
52 vdagent_x11_clipboard_request(x11, header->opaque);
53 break;
54 case VDAGENTD_CLIPBOARD_GRAB:
55 vdagent_x11_clipboard_grab(x11, (uint32_t *)data,
56 header->size / sizeof(uint32_t));
57 break;
58 case VDAGENTD_CLIPBOARD_DATA:
59 vdagent_x11_clipboard_data(x11, header->opaque, data, header->size);
60 break;
61 case VDAGENTD_CLIPBOARD_RELEASE:
62 vdagent_x11_clipboard_release(x11);
63 break;
64 default:
65 if (verbose)
66 fprintf(logfile, "Unknown message from vdagentd type: %d\n",
67 header->type);
71 static void usage(FILE *fp)
73 fprintf(fp,
74 "vdagent -- spice agent xorg client\n"
75 "options:\n"
76 " -h print this text\n"
77 " -d log debug messages\n"
78 " -x don't daemonize (and log to logfile)\n");
81 static void quit_handler(int sig)
83 quit = 1;
86 void daemonize(void)
88 int x, retval = 0;
90 /* detach from terminal */
91 switch (fork()) {
92 case 0:
93 close(0); close(1); close(2);
94 setsid();
95 x = open("/dev/null", O_RDWR); dup(x); dup(x);
96 break;
97 case -1:
98 fprintf(logfile, "fork: %s\n", strerror(errno));
99 retval = 1;
100 default:
101 udscs_destroy_connection(&client);
102 if (logfile != stderr)
103 fclose(logfile);
104 exit(retval);
108 int main(int argc, char *argv[])
110 fd_set readfds, writefds;
111 int c, n, nfds, x11_fd, retval = 0;
112 int do_daemonize = 1;
113 char *home, filename[1024];
114 struct sigaction act;
116 for (;;) {
117 if (-1 == (c = getopt(argc, argv, "-dxh")))
118 break;
119 switch (c) {
120 case 'd':
121 verbose++;
122 break;
123 case 'x':
124 do_daemonize = 0;
125 break;
126 case 'h':
127 usage(stdout);
128 return 0;
129 default:
130 usage(stderr);
131 return 1;
135 memset(&act, 0, sizeof(act));
136 act.sa_flags = SA_RESTART;
137 act.sa_handler = quit_handler;
138 sigaction(SIGINT, &act, NULL);
139 sigaction(SIGHUP, &act, NULL);
140 sigaction(SIGTERM, &act, NULL);
141 sigaction(SIGQUIT, &act, NULL);
143 logfile = stderr;
144 home = getenv("HOME");
145 if (home) {
146 snprintf(filename, sizeof(filename), "%s/.spice-vdagent", home);
147 n = mkdir(filename, 0755);
148 snprintf(filename, sizeof(filename), "%s/.spice-vdagent/log", home);
149 if (do_daemonize) {
150 logfile = fopen(filename, "w");
151 if (!logfile) {
152 fprintf(stderr, "Error opening %s: %s\n", filename,
153 strerror(errno));
154 logfile = stderr;
157 } else {
158 fprintf(stderr, "Could not get home directory, logging to stderr\n");
161 client = udscs_connect(VDAGENTD_SOCKET, daemon_read_complete, NULL,
162 vdagentd_messages, VDAGENTD_NO_MESSAGES,
163 verbose? logfile:NULL, logfile);
164 if (!client) {
165 if (logfile != stderr)
166 fclose(logfile);
167 return 1;
170 if (do_daemonize)
171 daemonize();
173 x11 = vdagent_x11_create(client, logfile, verbose);
174 if (!x11) {
175 udscs_destroy_connection(&client);
176 if (logfile != stderr)
177 fclose(logfile);
178 return 1;
181 while (client && !quit) {
182 FD_ZERO(&readfds);
183 FD_ZERO(&writefds);
185 nfds = udscs_client_fill_fds(client, &readfds, &writefds);
186 x11_fd = vdagent_x11_get_fd(x11);
187 FD_SET(x11_fd, &readfds);
188 if (x11_fd >= nfds)
189 nfds = x11_fd + 1;
191 n = select(nfds, &readfds, &writefds, NULL, NULL);
192 if (n == -1) {
193 if (errno == EINTR)
194 continue;
195 fprintf(logfile, "Fatal error select: %s\n", strerror(errno));
196 retval = 1;
197 break;
200 if (FD_ISSET(x11_fd, &readfds))
201 vdagent_x11_do_read(x11);
202 udscs_client_handle_fds(&client, &readfds, &writefds);
203 fflush(logfile);
206 vdagent_x11_destroy(x11);
207 udscs_destroy_connection(&client);
208 if (logfile != stderr)
209 fclose(logfile);
211 return retval;