1 /* vdagent.c xorg-client to vdagentd (daemon).
3 Copyright 2010 Red Hat, Inc.
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/>.
29 #include <sys/select.h>
31 #include <spice/vd_agent.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
;
44 void daemon_read_complete(struct udscs_connection
**connp
,
45 struct udscs_message_header
*header
, uint8_t *data
)
47 switch (header
->type
) {
48 case VDAGENTD_MONITORS_CONFIG
:
49 vdagent_x11_set_monitor_config(x11
, (VDAgentMonitorsConfig
*)data
);
52 case VDAGENTD_CLIPBOARD_REQUEST
:
53 vdagent_x11_clipboard_request(x11
, header
->arg1
, header
->arg2
);
56 case VDAGENTD_CLIPBOARD_GRAB
:
57 vdagent_x11_clipboard_grab(x11
, header
->arg1
, (uint32_t *)data
,
58 header
->size
/ sizeof(uint32_t));
61 case VDAGENTD_CLIPBOARD_DATA
:
62 vdagent_x11_clipboard_data(x11
, header
->arg1
, header
->arg2
,
64 /* vdagent_x11_clipboard_data takes ownership of the data (or frees
67 case VDAGENTD_CLIPBOARD_RELEASE
:
68 vdagent_x11_clipboard_release(x11
, header
->arg1
);
73 fprintf(logfile
, "Unknown message from vdagentd type: %d\n",
79 static void usage(FILE *fp
)
82 "vdagent -- spice agent xorg client\n"
84 " -h print this text\n"
85 " -d log debug messages\n"
86 " -x don't daemonize (and log to logfile)\n");
89 static void quit_handler(int sig
)
98 /* detach from terminal */
101 close(0); close(1); close(2);
103 x
= open("/dev/null", O_RDWR
); dup(x
); dup(x
);
106 fprintf(logfile
, "fork: %s\n", strerror(errno
));
109 udscs_destroy_connection(&client
);
110 if (logfile
!= stderr
)
116 int main(int argc
, char *argv
[])
118 fd_set readfds
, writefds
;
119 int c
, n
, nfds
, x11_fd
, retval
= 0;
120 int do_daemonize
= 1;
121 char *home
, filename
[1024];
122 struct sigaction act
;
125 if (-1 == (c
= getopt(argc
, argv
, "-dxh")))
143 memset(&act
, 0, sizeof(act
));
144 act
.sa_flags
= SA_RESTART
;
145 act
.sa_handler
= quit_handler
;
146 sigaction(SIGINT
, &act
, NULL
);
147 sigaction(SIGHUP
, &act
, NULL
);
148 sigaction(SIGTERM
, &act
, NULL
);
149 sigaction(SIGQUIT
, &act
, NULL
);
152 home
= getenv("HOME");
154 snprintf(filename
, sizeof(filename
), "%s/.spice-vdagent", home
);
155 n
= mkdir(filename
, 0755);
156 snprintf(filename
, sizeof(filename
), "%s/.spice-vdagent/log", home
);
158 logfile
= fopen(filename
, "w");
160 fprintf(stderr
, "Error opening %s: %s\n", filename
,
166 fprintf(stderr
, "Could not get home directory, logging to stderr\n");
169 client
= udscs_connect(VDAGENTD_SOCKET
, daemon_read_complete
, NULL
,
170 vdagentd_messages
, VDAGENTD_NO_MESSAGES
,
171 verbose
? logfile
:NULL
, logfile
);
173 if (logfile
!= stderr
)
181 x11
= vdagent_x11_create(client
, logfile
, verbose
);
183 udscs_destroy_connection(&client
);
184 if (logfile
!= stderr
)
189 while (client
&& !quit
) {
193 nfds
= udscs_client_fill_fds(client
, &readfds
, &writefds
);
194 x11_fd
= vdagent_x11_get_fd(x11
);
195 FD_SET(x11_fd
, &readfds
);
199 n
= select(nfds
, &readfds
, &writefds
, NULL
, NULL
);
203 fprintf(logfile
, "Fatal error select: %s\n", strerror(errno
));
208 if (FD_ISSET(x11_fd
, &readfds
))
209 vdagent_x11_do_read(x11
);
210 udscs_client_handle_fds(&client
, &readfds
, &writefds
);
214 vdagent_x11_destroy(x11
);
215 udscs_destroy_connection(&client
);
216 if (logfile
!= stderr
)