diff options
author | Hans de Goede <hdegoede@redhat.com> | 2012-08-24 17:31:07 +0200 |
---|---|---|
committer | Hans de Goede <hdegoede@redhat.com> | 2012-08-24 17:31:07 +0200 |
commit | 01f5a910ab2d66a3fb39403ea5ca4bc32a1e9631 (patch) | |
tree | ad34098f1fa9f9efea0e268c8d9570ca328c3e4b /src | |
parent | b2a77aa5169305d961d16046d043d3128893ecde (diff) | |
download | vd_agent-01f5a910ab2d66a3fb39403ea5ca4bc32a1e9631.tar.gz vd_agent-01f5a910ab2d66a3fb39403ea5ca4bc32a1e9631.tar.xz vd_agent-01f5a910ab2d66a3fb39403ea5ca4bc32a1e9631.zip |
Replace file-logging with syslog
This resolves:
http://bugzilla.redhat.com/show_bug.cgi?id=747894
http://bugzilla.freedesktop.org/show_bug.cgi?id=49092
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/console-kit.c | 68 | ||||
-rw-r--r-- | src/session-info.h | 2 | ||||
-rw-r--r-- | src/systemd-login.c | 15 | ||||
-rw-r--r-- | src/udscs.c | 102 | ||||
-rw-r--r-- | src/udscs.h | 6 | ||||
-rw-r--r-- | src/vdagent-virtio-port.c | 31 | ||||
-rw-r--r-- | src/vdagent-virtio-port.h | 3 | ||||
-rw-r--r-- | src/vdagent-x11-priv.h | 9 | ||||
-rw-r--r-- | src/vdagent-x11-randr.c | 97 | ||||
-rw-r--r-- | src/vdagent-x11.c | 106 | ||||
-rw-r--r-- | src/vdagent-x11.h | 2 | ||||
-rw-r--r-- | src/vdagent.c | 73 | ||||
-rw-r--r-- | src/vdagentd-uinput.c | 50 | ||||
-rw-r--r-- | src/vdagentd-uinput.h | 4 | ||||
-rw-r--r-- | src/vdagentd-xorg-conf.c | 29 | ||||
-rw-r--r-- | src/vdagentd-xorg-conf.h | 5 | ||||
-rw-r--r-- | src/vdagentd.c | 140 |
17 files changed, 321 insertions, 421 deletions
diff --git a/src/console-kit.c b/src/console-kit.c index f1a5d5a..759a81e 100644 --- a/src/console-kit.c +++ b/src/console-kit.c @@ -25,19 +25,19 @@ #include <unistd.h> #include <stdlib.h> #include <string.h> +#include <syslog.h> struct session_info { DBusConnection *connection; int fd; char *seat; char *active_session; - FILE *logfile; }; static char *console_kit_get_first_seat(struct session_info *ck); static char *console_kit_check_active_session_change(struct session_info *ck); -struct session_info *session_info_create(FILE *logfile, int verbose) +struct session_info *session_info_create(int verbose) { struct session_info *ck; DBusError error; @@ -47,23 +47,21 @@ struct session_info *session_info_create(FILE *logfile, int verbose) if (!ck) return NULL; - ck->logfile = logfile; - dbus_error_init(&error); ck->connection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); if (ck->connection == NULL || dbus_error_is_set(&error)) { if (dbus_error_is_set(&error)) { - fprintf(ck->logfile, "Unable to connect to system bus: %s\n", - error.message); + syslog(LOG_ERR, "Unable to connect to system bus: %s", + error.message); dbus_error_free(&error); } else - fprintf(ck->logfile, "Unable to connect to system bus\n"); + syslog(LOG_ERR, "Unable to connect to system bus"); free(ck); return NULL; } if (!dbus_connection_get_unix_fd(ck->connection, &ck->fd)) { - fprintf(ck->logfile, "Unable to get connection fd\n"); + syslog(LOG_ERR, "Unable to get connection fd"); session_info_destroy(ck); return NULL; } @@ -80,7 +78,7 @@ struct session_info *session_info_create(FILE *logfile, int verbose) dbus_error_init(&error); dbus_bus_add_match(ck->connection, match, &error); if (dbus_error_is_set(&error)) { - fprintf(ck->logfile, "Match Error (%s)\n", error.message); + syslog(LOG_ERR, "Match Error (%s)", error.message); session_info_destroy(ck); return NULL; } @@ -118,7 +116,7 @@ static char *console_kit_get_first_seat(struct session_info *ck) "org.freedesktop.ConsoleKit.Manager", "GetSeats"); if (message == NULL) { - fprintf(ck->logfile, "Unable to create dbus message\n"); + syslog(LOG_ERR, "Unable to create dbus message"); goto exit; } @@ -129,27 +127,26 @@ static char *console_kit_get_first_seat(struct session_info *ck) &error); if (reply == NULL || dbus_error_is_set(&error)) { if (dbus_error_is_set(&error)) { - fprintf(ck->logfile, "GetSeats failed: %s\n", - error.message); + syslog(LOG_ERR, "GetSeats failed: %s", error.message); dbus_error_free(&error); } else - fprintf(ck->logfile, "GetSeats failed\n"); + syslog(LOG_ERR, "GetSeats failed"); goto exit; } dbus_message_iter_init(reply, &iter); type = dbus_message_iter_get_arg_type(&iter); if (type != DBUS_TYPE_ARRAY) { - fprintf(ck->logfile, - "expected an array return value, got a '%c' instead\n", type); + syslog(LOG_ERR, + "expected an array return value, got a '%c' instead", type); goto exit; } dbus_message_iter_recurse(&iter, &subiter); type = dbus_message_iter_get_arg_type(&subiter); if (type != DBUS_TYPE_OBJECT_PATH) { - fprintf(ck->logfile, - "expected an object path element, got a '%c' instead\n", type); + syslog(LOG_ERR, + "expected an object path element, got a '%c' instead", type); goto exit; } @@ -186,7 +183,7 @@ const char *session_info_get_active_session(struct session_info *ck) "org.freedesktop.ConsoleKit.Seat", "GetActiveSession"); if (message == NULL) { - fprintf(ck->logfile, "Unable to create dbus message\n"); + syslog(LOG_ERR, "Unable to create dbus message"); goto exit; } @@ -197,11 +194,10 @@ const char *session_info_get_active_session(struct session_info *ck) &error); if (reply == NULL || dbus_error_is_set(&error)) { if (dbus_error_is_set(&error)) { - fprintf(ck->logfile, "GetActiveSession failed: %s\n", - error.message); + syslog(LOG_ERR, "GetActiveSession failed: %s", error.message); dbus_error_free(&error); } else - fprintf(ck->logfile, "GetActiveSession failed\n"); + syslog(LOG_ERR, "GetActiveSession failed"); goto exit; } @@ -211,11 +207,10 @@ const char *session_info_get_active_session(struct session_info *ck) DBUS_TYPE_OBJECT_PATH, &session, DBUS_TYPE_INVALID)) { if (dbus_error_is_set(&error)) { - fprintf(ck->logfile, "error getting ssid from reply: %s\n", - error.message); + syslog(LOG_ERR, "error get ssid from reply: %s", error.message); dbus_error_free(&error); } else - fprintf(ck->logfile, "error getting ssid from reply\n"); + syslog(LOG_ERR, "error getting ssid from reply"); session = NULL; goto exit; } @@ -251,13 +246,13 @@ char *session_info_session_for_pid(struct session_info *ck, uint32_t pid) "org.freedesktop.ConsoleKit.Manager", "GetSessionForUnixProcess"); if (message == NULL) { - fprintf(ck->logfile, "Unable to create dbus message\n"); + syslog(LOG_ERR, "Unable to create dbus message"); goto exit; } dbus_message_iter_init_append(message, &args); if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &pid)) { - fprintf(ck->logfile, "Unable to append dbus message args\n"); + syslog(LOG_ERR, "Unable to append dbus message args"); goto exit; } @@ -268,11 +263,11 @@ char *session_info_session_for_pid(struct session_info *ck, uint32_t pid) &error); if (reply == NULL || dbus_error_is_set(&error)) { if (dbus_error_is_set(&error)) { - fprintf(ck->logfile, "GetSessionForUnixProcess failed: %s\n", - error.message); + syslog(LOG_ERR, "GetSessionForUnixProcess failed: %s", + error.message); dbus_error_free(&error); } else - fprintf(ck->logfile, "GetSessionForUnixProces failed\n"); + syslog(LOG_ERR, "GetSessionForUnixProces failed"); goto exit; } @@ -282,11 +277,10 @@ char *session_info_session_for_pid(struct session_info *ck, uint32_t pid) DBUS_TYPE_OBJECT_PATH, &ssid, DBUS_TYPE_INVALID)) { if (dbus_error_is_set(&error)) { - fprintf(ck->logfile, "error getting ssid from reply: %s\n", - error.message); + syslog(LOG_ERR, "error get ssid from reply: %s", error.message); dbus_error_free(&error); } else - fprintf(ck->logfile, "error getting ssid from reply\n"); + syslog(LOG_ERR, "error getting ssid from reply"); ssid = NULL; goto exit; } @@ -322,12 +316,12 @@ static char *console_kit_check_active_session_change(struct session_info *ck) continue; } if (strcmp(member, "ActiveSessionChanged")) { - fprintf(ck->logfile, "unexpected signal member: %s\n", member); + syslog(LOG_ERR, "unexpected signal member: %s", member); dbus_message_unref(message); continue; } } else { - fprintf(ck->logfile, "received non signal message!\n"); + syslog(LOG_ERR, "received non signal message!"); dbus_message_unref(message); continue; } @@ -341,9 +335,9 @@ static char *console_kit_check_active_session_change(struct session_info *ck) ConsoleKit where it sends a string rather then an object_path accept object_path too in case the bug ever gets fixed */ if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_OBJECT_PATH) { - fprintf(ck->logfile, - "ActiveSessionChanged message has unexpected type: '%c'\n", - type); + syslog(LOG_ERR, + "ActiveSessionChanged message has unexpected type: '%c'", + type); dbus_message_unref(message); continue; } diff --git a/src/session-info.h b/src/session-info.h index 054aa6c..5a1d140 100644 --- a/src/session-info.h +++ b/src/session-info.h @@ -31,7 +31,7 @@ struct session_info; -struct session_info *session_info_create(FILE *logfile, int verbose); +struct session_info *session_info_create(int verbose); void session_info_destroy(struct session_info *ck); int session_info_get_fd(struct session_info *ck); diff --git a/src/systemd-login.c b/src/systemd-login.c index 277cb03..7907fe0 100644 --- a/src/systemd-login.c +++ b/src/systemd-login.c @@ -23,16 +23,16 @@ #include <errno.h> #include <stdlib.h> #include <string.h> +#include <syslog.h> #include <systemd/sd-login.h> struct session_info { - FILE *logfile; int verbose; sd_login_monitor *mon; char *session; }; -struct session_info *session_info_create(FILE *logfile, int verbose) +struct session_info *session_info_create(int verbose) { struct session_info *si; int r; @@ -41,12 +41,11 @@ struct session_info *session_info_create(FILE *logfile, int verbose) if (!si) return NULL; - si->logfile = logfile; si->verbose = verbose; r = sd_login_monitor_new("session", &si->mon); if (r < 0) { - fprintf(logfile, "Error creating login monitor: %s\n", strerror(-r)); + syslog(LOG_ERR, "Error creating login monitor: %s", strerror(-r)); free(si); return NULL; } @@ -75,12 +74,12 @@ const char *session_info_get_active_session(struct session_info *si) r = sd_seat_get_active("seat0", &si->session, NULL); /* ENOENT happens when a seat is switching from one session to another */ if (r < 0 && r != ENOENT) - fprintf(si->logfile, "Error getting active session: %s\n", + syslog(LOG_ERR, "Error getting active session: %s", strerror(-r)); if (si->verbose && si->session && (!old_session || strcmp(old_session, si->session))) - fprintf(si->logfile, "Active session: %s\n", si->session); + syslog(LOG_INFO, "Active session: %s", si->session); sd_login_monitor_flush(si->mon); free(old_session); @@ -95,10 +94,10 @@ char *session_info_session_for_pid(struct session_info *si, uint32_t pid) r = sd_pid_get_session(pid, &session); if (r < 0) - fprintf(si->logfile, "Error getting session for pid %u: %s\n", + syslog(LOG_ERR, "Error getting session for pid %u: %s", pid, strerror(-r)); else if (si->verbose) - fprintf(si->logfile, "Session for pid %u: %s\n", pid, session); + syslog(LOG_INFO, "Session for pid %u: %s", pid, session); return session; } diff --git a/src/udscs.c b/src/udscs.c index 6f1328e..288aca2 100644 --- a/src/udscs.c +++ b/src/udscs.c @@ -26,7 +26,7 @@ #include <stdio.h> #include <stdlib.h> -#include <string.h> +#include <syslog.h> #include <unistd.h> #include <errno.h> #include <sys/socket.h> @@ -45,8 +45,7 @@ struct udscs_connection { int fd; const char * const *type_to_string; int no_types; - FILE *logfile; - FILE *errfile; + int debug; struct ucred peer_cred; void *user_data; @@ -71,8 +70,7 @@ struct udscs_server { int fd; const char * const *type_to_string; int no_types; - FILE *logfile; - FILE *errfile; + int debug; struct udscs_connection connections_head; udscs_connect_callback connect_callback; udscs_read_callback read_callback; @@ -87,8 +85,7 @@ struct udscs_server *udscs_create_server(const char *socketname, udscs_connect_callback connect_callback, udscs_read_callback read_callback, udscs_disconnect_callback disconnect_callback, - const char * const type_to_string[], int no_types, - FILE *logfile, FILE *errfile) + const char * const type_to_string[], int no_types, int debug) { int c; struct sockaddr_un address; @@ -98,15 +95,13 @@ struct udscs_server *udscs_create_server(const char *socketname, if (!server) return NULL; - server->logfile = logfile; - server->errfile = errfile; server->type_to_string = type_to_string; server->no_types = no_types; + server->debug = debug; server->fd = socket(PF_UNIX, SOCK_STREAM, 0); if (server->fd == -1) { - fprintf(server->errfile, "creating unix domain socket: %s\n", - strerror(errno)); + syslog(LOG_ERR, "creating unix domain socket: %m"); free(server); return NULL; } @@ -115,14 +110,14 @@ struct udscs_server *udscs_create_server(const char *socketname, snprintf(address.sun_path, sizeof(address.sun_path), "%s", socketname); c = bind(server->fd, (struct sockaddr *)&address, sizeof(address)); if (c != 0) { - fprintf(server->errfile, "bind %s: %s\n", socketname, strerror(errno)); + syslog(LOG_ERR, "bind %s: %m", socketname); free(server); return NULL; } c = listen(server->fd, 5); if (c != 0) { - fprintf(server->errfile, "listen: %s\n", strerror(errno)); + syslog(LOG_ERR, "listen: %m"); free(server); return NULL; } @@ -154,8 +149,7 @@ void udscs_destroy_server(struct udscs_server *server) struct udscs_connection *udscs_connect(const char *socketname, udscs_read_callback read_callback, udscs_disconnect_callback disconnect_callback, - const char * const type_to_string[], int no_types, - FILE *logfile, FILE *errfile) + const char * const type_to_string[], int no_types, int debug) { int c; struct sockaddr_un address; @@ -165,15 +159,13 @@ struct udscs_connection *udscs_connect(const char *socketname, if (!conn) return NULL; - conn->logfile = logfile; - conn->errfile = errfile; conn->type_to_string = type_to_string; conn->no_types = no_types; + conn->debug = debug; conn->fd = socket(PF_UNIX, SOCK_STREAM, 0); if (conn->fd == -1) { - fprintf(conn->errfile, "creating unix domain socket: %s\n", - strerror(errno)); + syslog(LOG_ERR, "creating unix domain socket: %m"); free(conn); return NULL; } @@ -182,9 +174,8 @@ struct udscs_connection *udscs_connect(const char *socketname, snprintf(address.sun_path, sizeof(address.sun_path), "%s", socketname); c = connect(conn->fd, (struct sockaddr *)&address, sizeof(address)); if (c != 0) { - if (conn->logfile) { - fprintf(conn->logfile, "connect %s: %s\n", socketname, - strerror(errno)); + if (conn->debug) { + syslog(LOG_DEBUG, "connect %s: %m", socketname); } free(conn); return NULL; @@ -193,8 +184,8 @@ struct udscs_connection *udscs_connect(const char *socketname, conn->read_callback = read_callback; conn->disconnect_callback = disconnect_callback; - if (conn->logfile) - fprintf(conn->logfile, "%p connected to %s\n", conn, socketname); + if (conn->debug) + syslog(LOG_DEBUG, "%p connected to %s", conn, socketname); return conn; } @@ -227,8 +218,8 @@ void udscs_destroy_connection(struct udscs_connection **connp) close(conn->fd); - if (conn->logfile) - fprintf(conn->logfile, "%p disconnected\n", conn); + if (conn->debug) + syslog(LOG_DEBUG, "%p disconnected", conn); free(conn); *connp = NULL; @@ -285,30 +276,28 @@ static void udscs_server_accept(struct udscs_server *server) { if (fd == -1) { if (errno == EINTR) return; - fprintf(server->errfile, "accept: %s\n", strerror(errno)); + syslog(LOG_ERR, "accept: %m"); return; } new_conn = calloc(1, sizeof(*conn)); if (!new_conn) { - fprintf(server->errfile, "out of memory, disconnecting new client\n"); + syslog(LOG_ERR, "out of memory, disconnecting new client"); close(fd); return; } new_conn->fd = fd; - new_conn->logfile = server->logfile; - new_conn->errfile = server->errfile; new_conn->type_to_string = server->type_to_string; new_conn->no_types = server->no_types; + new_conn->debug = server->debug; new_conn->read_callback = server->read_callback; new_conn->disconnect_callback = server->disconnect_callback; length = sizeof(new_conn->peer_cred); r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &new_conn->peer_cred, &length); if (r != 0) { - fprintf(server->errfile, - "Could not get peercred, disconnecting new client\n"); + syslog(LOG_ERR, "Could not get peercred, disconnecting new client"); close(fd); free(new_conn); return; @@ -321,9 +310,9 @@ static void udscs_server_accept(struct udscs_server *server) { new_conn->prev = conn; conn->next = new_conn; - if (server->logfile) - fprintf(server->logfile, "new client accepted: %p, pid: %d\n", - new_conn, (int)new_conn->peer_cred.pid); + if (server->debug) + syslog(LOG_DEBUG, "new client accepted: %p, pid: %d", + new_conn, (int)new_conn->peer_cred.pid); if (server->connect_callback) server->connect_callback(new_conn); @@ -390,13 +379,13 @@ int udscs_write(struct udscs_connection *conn, uint32_t type, uint32_t arg1, memcpy(new_wbuf->buf, &header, sizeof(header)); memcpy(new_wbuf->buf + sizeof(header), data, size); - if (conn->logfile) { + if (conn->debug) { if (type < conn->no_types) - fprintf(conn->logfile, "%p sent %s, arg1: %u, arg2: %u, size %u\n", - conn, conn->type_to_string[type], arg1, arg2, size); + syslog(LOG_DEBUG, "%p sent %s, arg1: %u, arg2: %u, size %u", + conn, conn->type_to_string[type], arg1, arg2, size); else - fprintf(conn->logfile, - "%p sent invalid message %u, arg1: %u, arg2: %u, size %u\n", + syslog(LOG_DEBUG, + "%p sent invalid message %u, arg1: %u, arg2: %u, size %u", conn, type, arg1, arg2, size); } @@ -454,15 +443,15 @@ static void udscs_read_complete(struct udscs_connection **connp) { struct udscs_connection *conn = *connp; - if (conn->logfile) { + if (conn->debug) { if (conn->header.type < conn->no_types) - fprintf(conn->logfile, - "%p received %s, arg1: %u, arg2: %u, size %u\n", - conn, conn->type_to_string[conn->header.type], - conn->header.arg1, conn->header.arg2, conn->header.size); + syslog(LOG_DEBUG, + "%p received %s, arg1: %u, arg2: %u, size %u", + conn, conn->type_to_string[conn->header.type], + conn->header.arg1, conn->header.arg2, conn->header.size); else - fprintf(conn->logfile, - "%p received invalid message %u, arg1: %u, arg2: %u, size %u\n", + syslog(LOG_DEBUG, + "%p received invalid message %u, arg1: %u, arg2: %u, size %u", conn, conn->header.type, conn->header.arg1, conn->header.arg2, conn->header.size); } @@ -496,9 +485,8 @@ static void udscs_do_read(struct udscs_connection **connp) if (n < 0) { if (errno == EINTR) return; - fprintf(conn->errfile, - "reading unix domain socket: %s, disconnecting %p\n", - strerror(errno), conn); + syslog(LOG_ERR, "reading unix domain socket: %m, disconnecting %p", + conn); } if (n <= 0) { udscs_destroy_connection(connp); @@ -516,8 +504,7 @@ static void udscs_do_read(struct udscs_connection **connp) conn->data.size = conn->header.size; conn->data.buf = malloc(conn->data.size); if (!conn->data.buf) { - fprintf(conn->errfile, "out of memory, disconnecting %p\n", - conn); + syslog(LOG_ERR, "out of memory, disconnecting %p", conn); udscs_destroy_connection(connp); return; } @@ -537,9 +524,9 @@ static void udscs_do_write(struct udscs_connection **connp) struct udscs_buf* wbuf = conn->write_buf; if (!wbuf) { - fprintf(conn->errfile, - "%p do_write called on a connection without a write buf ?!\n", - conn); + syslog(LOG_ERR, + "%p do_write called on a connection without a write buf ?!", + conn); return; } @@ -548,9 +535,8 @@ static void udscs_do_write(struct udscs_connection **connp) if (n < 0) { if (errno == EINTR) return; - fprintf(conn->errfile, - "writing to unix domain socket: %s, disconnecting %p\n", - strerror(errno), conn); + syslog(LOG_ERR, "writing to unix domain socket: %m, disconnecting %p", + conn); udscs_destroy_connection(connp); return; } diff --git a/src/udscs.h b/src/udscs.h index 251e2d0..89ab617 100644 --- a/src/udscs.h +++ b/src/udscs.h @@ -62,8 +62,7 @@ struct udscs_server *udscs_create_server(const char *socketname, udscs_connect_callback connect_callback, udscs_read_callback read_callback, udscs_disconnect_callback disconnect_callback, - const char * const type_to_string[], int no_types, - FILE *logfile, FILE *errfile); + const char * const type_to_string[], int no_types, int debug); void udscs_destroy_server(struct udscs_server *server); @@ -71,8 +70,7 @@ void udscs_destroy_server(struct udscs_server *server); struct udscs_connection *udscs_connect(const char *socketname, udscs_read_callback read_callback, udscs_disconnect_callback disconnect_callback, - const char * const type_to_string[], int no_types, - FILE *logfile, FILE *errfile); + const char * const type_to_string[], int no_types, int debug); /* The contents of connp will be made NULL */ void udscs_destroy_connection(struct udscs_connection **connp); diff --git a/src/vdagent-virtio-port.c b/src/vdagent-virtio-port.c index 47de48d..de4b748 100644 --- a/src/vdagent-virtio-port.c +++ b/src/vdagent-virtio-port.c @@ -22,6 +22,7 @@ #include <errno.h> #include <stdlib.h> #include <string.h> +#include <syslog.h> #include <unistd.h> #include <fcntl.h> #include <sys/select.h> @@ -50,7 +51,6 @@ struct vdagent_virtio_port_chunk_port_data { struct vdagent_virtio_port { int fd; int opening; - FILE *errfile; /* Chunk read stuff, single buffer, separate header and data buffer */ int chunk_header_read; @@ -75,8 +75,7 @@ static void vdagent_virtio_port_do_read(struct vdagent_virtio_port **vportp); struct vdagent_virtio_port *vdagent_virtio_port_create(const char *portname, vdagent_virtio_port_read_callback read_callback, - vdagent_virtio_port_disconnect_callback disconnect_callback, - FILE *errfile) + vdagent_virtio_port_disconnect_callback disconnect_callback) { struct vdagent_virtio_port *vport; @@ -84,10 +83,9 @@ struct vdagent_virtio_port *vdagent_virtio_port_create(const char *portname, if (!vport) return 0; - vport->errfile = errfile; vport->fd = open(portname, O_RDWR); if (vport->fd == -1) { - fprintf(vport->errfile, "open %s: %s\n", portname, strerror(errno)); + syslog(LOG_ERR, "open %s: %m", portname); free(vport); return NULL; } @@ -226,12 +224,12 @@ int vdagent_virtio_port_write_append(struct vdagent_virtio_port *vport, wbuf = vdagent_virtio_port_get_last_wbuf(vport); if (!wbuf) { - fprintf(vport->errfile, "can't append without a buffer"); + syslog(LOG_ERR, "can't append without a buffer"); return -1; } if (wbuf->size - wbuf->write_pos < size) { - fprintf(vport->errfile, "can't append to full buffer"); + syslog(LOG_ERR, "can't append to full buffer"); return -1; } @@ -281,7 +279,7 @@ static void vdagent_virtio_port_do_chunk(struct vdagent_virtio_port **vportp) port->message_header.size) { port->message_data = malloc(port->message_header.size); if (!port->message_data) { - fprintf(vport->errfile, "out of memory, disconnecting virtio\n"); + syslog(LOG_ERR, "out of memory, disconnecting virtio"); vdagent_virtio_port_destroy(vportp); return; } @@ -294,7 +292,7 @@ static void vdagent_virtio_port_do_chunk(struct vdagent_virtio_port **vportp) avail = vport->chunk_header.size - pos; if (avail > read) { - fprintf(vport->errfile, "chunk larger then message, lost sync?\n"); + syslog(LOG_ERR, "chunk larger then message, lost sync?"); vdagent_virtio_port_destroy(vportp); return; } @@ -344,8 +342,7 @@ static void vdagent_virtio_port_do_read(struct vdagent_virtio_port **vportp) if (n < 0) { if (errno == EINTR) return; - fprintf(vport->errfile, "reading from vdagent virtio port: %s\n", - strerror(errno)); + syslog(LOG_ERR, "reading from vdagent virtio port: %m"); } if (n == 0 && vport->opening) { /* When we open the virtio serial port, the following happens: @@ -381,12 +378,12 @@ static void vdagent_virtio_port_do_read(struct vdagent_virtio_port **vportp) vport->chunk_header_read += n; if (vport->chunk_header_read == sizeof(vport->chunk_header)) { if (vport->chunk_header.size > VD_AGENT_MAX_DATA_SIZE) { - fprintf(vport->errfile, "chunk size too large\n"); + syslog(LOG_ERR, "chunk size too large"); vdagent_virtio_port_destroy(vportp); return; } if (vport->chunk_header.port > VDP_LAST_PORT) { - fprintf(vport->errfile, "chunk port out of range\n"); + syslog(LOG_ERR, "chunk port out of range"); vdagent_virtio_port_destroy(vportp); return; } @@ -409,13 +406,12 @@ static void vdagent_virtio_port_do_write(struct vdagent_virtio_port **vportp) struct vdagent_virtio_port_buf* wbuf = vport->write_buf; if (!wbuf) { - fprintf(vport->errfile, - "do_write called on a port without a write buf ?!\n"); + syslog(LOG_ERR, "do_write called on a port without a write buf ?!"); return; } if (wbuf->write_pos != wbuf->size) { - fprintf(vport->errfile, "do_write: buffer is incomplete!!\n"); + syslog(LOG_ERR, "do_write: buffer is incomplete!!"); return; } @@ -424,8 +420,7 @@ static void vdagent_virtio_port_do_write(struct vdagent_virtio_port **vportp) if (n < 0) { if (errno == EINTR) return; - fprintf(vport->errfile, "writing to vdagent virtio port: %s\n", - strerror(errno)); + syslog(LOG_ERR, "writing to vdagent virtio port: %m"); vdagent_virtio_port_destroy(vportp); return; } diff --git a/src/vdagent-virtio-port.h b/src/vdagent-virtio-port.h index b69f6a8..c47fbcc 100644 --- a/src/vdagent-virtio-port.h +++ b/src/vdagent-virtio-port.h @@ -54,8 +54,7 @@ typedef void (*vdagent_virtio_port_disconnect_callback)( /* Create a vdagent virtio port object for port portname */ struct vdagent_virtio_port *vdagent_virtio_port_create(const char *portname, vdagent_virtio_port_read_callback read_callback, - vdagent_virtio_port_disconnect_callback disconnect_callback, - FILE *errfile); + vdagent_virtio_port_disconnect_callback disconnect_callback); /* The contents of portp will be made NULL */ void vdagent_virtio_port_destroy(struct vdagent_virtio_port **vportp); diff --git a/src/vdagent-x11-priv.h b/src/vdagent-x11-priv.h index 25d387c..2f447f6 100644 --- a/src/vdagent-x11-priv.h +++ b/src/vdagent-x11-priv.h @@ -10,13 +10,13 @@ /* Macros to print a message to the logfile prefixed by the selection */ #define SELPRINTF(format, ...) \ - fprintf(x11->errfile, "%s: " format, \ + syslog(LOG_ERR, "%s: " format, \ vdagent_x11_sel_to_str(selection), ##__VA_ARGS__) #define VSELPRINTF(format, ...) \ do { \ - if (x11->verbose) { \ - fprintf(x11->errfile, "%s: " format, \ + if (x11->debug) { \ + syslog(LOG_DEBUG, "%s: " format, \ vdagent_x11_sel_to_str(selection), ##__VA_ARGS__); \ } \ } while (0) @@ -77,8 +77,7 @@ struct vdagent_x11 { Window root_window; Window selection_window; struct udscs_connection *vdagentd; - FILE *errfile; - int verbose; + int debug; int fd; int screen; int width; diff --git a/src/vdagent-x11-randr.c b/src/vdagent-x11-randr.c index 9c15861..55f99dc 100644 --- a/src/vdagent-x11-randr.c +++ b/src/vdagent-x11-randr.c @@ -1,4 +1,5 @@ #include <string.h> +#include <syslog.h> #include <stdlib.h> #include <X11/extensions/Xinerama.h> @@ -102,7 +103,7 @@ static int update_randr_res(struct vdagent_x11 *x11) &x11->randr.min_height, &x11->randr.max_width, &x11->randr.max_height) != 1) { - fprintf(x11->errfile, "RRGetScreenSizeRange failed\n"); + syslog(LOG_ERR, "RRGetScreenSizeRange failed"); } } @@ -113,7 +114,7 @@ void vdagent_x11_randr_init(struct vdagent_x11 *x11) if (XRRQueryExtension(x11->display, &i, &i)) { x11->has_xrandr = 1; if (!update_randr_res(x11)) { - fprintf(x11->errfile, "get screen info failed\n"); + syslog(LOG_ERR, "get screen info failed"); } XRRQueryVersion(x11->display, &x11->xrandr_major, &x11->xrandr_minor); } else { @@ -125,17 +126,17 @@ void vdagent_x11_randr_init(struct vdagent_x11 *x11) switch (x11->has_xrandr << 4 | x11->has_xinerama) { case 0x00: - fprintf(x11->errfile, "Neither Xrandr nor Xinerama found, assuming single monitor setup\n"); + syslog(LOG_ERR, "Neither Xrandr nor Xinerama found, assuming single monitor setup"); break; case 0x01: - if (x11->verbose) - fprintf(x11->errfile, "Found Xinerama extension without Xrandr, assuming a multi monitor setup\n"); + if (x11->debug) + syslog(LOG_DEBUG, "Found Xinerama extension without Xrandr, assuming Xinerama multi monitor setup"); break; case 0x10: - fprintf(x11->errfile, "Found Xrandr but no Xinerama, weird! Assuming a single monitor setup\n"); + syslog(LOG_ERR, "Found Xrandr but no Xinerama, weird!"); break; case 0x11: - /* Standard single monitor setup, nothing to see here */ + /* Standard xrandr setup, nothing to see here */ break; } } @@ -206,10 +207,8 @@ static void delete_mode(struct vdagent_x11 *x11, int output_index, const char *n output_info = x11->randr.outputs[output_index]; if (output_info->ncrtc != 1) { - fprintf(x11->errfile, - "output has %d crtcs, expected exactly 1, " - "failed to delete mode\n", - output_info->ncrtc); + syslog(LOG_ERR, "output has %d crtcs, expected exactly 1, " + "failed to delete mode", output_info->ncrtc); return; } crtc_info = crtc_from_id(x11, output_info->crtcs[0]); @@ -225,8 +224,8 @@ static void delete_mode(struct vdagent_x11 *x11, int output_index, const char *n } if (the_mode) { if (crtc && the_mode->id == current_mode) { - fprintf(x11->errfile, - "delete_mode of in use mode, setting crtc to NULL mode\n"); + syslog(LOG_ERR, + "delete_mode of in use mode, setting crtc to NULL mode"); XRRSetCrtcConfig(x11->display, x11->randr.res, crtc, CurrentTime, 0, 0, None, RR_Rotate_0, NULL, 0); } @@ -335,7 +334,7 @@ static XRRModeInfo *create_new_mode(struct vdagent_x11 *x11, int output_index, XRRCreateMode (x11->display, x11->root_window, &mode); /* silly to update everytime for more then one monitor */ if (!update_randr_res(x11)) { - fprintf(x11->errfile, "get screen info failed\n"); + syslog(LOG_ERR, "get screen info failed"); } return find_mode_by_name(x11, modename); } @@ -349,8 +348,8 @@ static int xrandr_add_and_set(struct vdagent_x11 *x11, int output, int x, int y, RROutput outputs[1]; if (!x11->randr.res || output >= x11->randr.res->noutput || output < 0) { - fprintf(x11->errfile, "%s: program error: missing RANDR or bad output\n", - __FUNCTION__); + syslog(LOG_ERR, "%s: program error: missing RANDR or bad output", + __FUNCTION__); return 0; } if (x11->set_crtc_config_not_functional) { @@ -364,7 +363,7 @@ static int xrandr_add_and_set(struct vdagent_x11 *x11, int output, int x, int y, mode = create_new_mode(x11, output, width, height); } if (!mode) { - fprintf(x11->errfile, "failed to add a new mode\n"); + syslog(LOG_ERR, "failed to add a new mode"); return 0; } XRRAddOutputMode(x11->display, xid, mode->id); // Call this anyway? @@ -373,7 +372,7 @@ static int xrandr_add_and_set(struct vdagent_x11 *x11, int output, int x, int y, CurrentTime, x, y, mode->id, RR_Rotate_0, outputs, 1); if (s != RRSetConfigSuccess) { - fprintf(x11->errfile, "failed to XRRSetCrtcConfig\n"); + syslog(LOG_ERR, "failed to XRRSetCrtcConfig"); // TODO - return crtc config to default return 0; } @@ -385,8 +384,8 @@ static int xrandr_disable_output(struct vdagent_x11 *x11, int output) Status s; if (!x11->randr.res || output >= x11->randr.res->noutput || output < 0) { - fprintf(x11->errfile, "%s: program error: missing RANDR or bad output\n", - __FUNCTION__); + syslog(LOG_ERR, "%s: program error: missing RANDR or bad output", + __FUNCTION__); return; } @@ -396,7 +395,7 @@ static int xrandr_disable_output(struct vdagent_x11 *x11, int output) NULL, 0); if (s != RRSetConfigSuccess) - fprintf(x11->errfile, "failed to disable monitor\n"); + syslog(LOG_ERR, "failed to disable monitor"); } static int set_screen_to_best_size(struct vdagent_x11 *x11, int width, int height, @@ -410,7 +409,7 @@ static int set_screen_to_best_size(struct vdagent_x11 *x11, int width, int heigh sizes = XRRSizes(x11->display, x11->screen, &num_sizes); if (!sizes || !num_sizes) { - fprintf(x11->errfile, "XRRSizes failed\n"); + syslog(LOG_ERR, "XRRSizes failed"); return 0; } @@ -430,13 +429,13 @@ static int set_screen_to_best_size(struct vdagent_x11 *x11, int width, int heigh } if (best == -1) { - fprintf(x11->errfile, "no suitable resolution found for monitor\n"); + syslog(LOG_ERR, "no suitable resolution found for monitor"); return 0; } config = XRRGetScreenInfo(x11->display, x11->root_window); if(!config) { - fprintf(x11->errfile, "get screen info failed\n"); + syslog(LOG_ERR, "get screen info failed"); return 0; } XRRConfigCurrentConfiguration(config, &rotation); @@ -498,13 +497,11 @@ void constrain_to_screen(struct vdagent_x11 *x11, int *w, int *h) ly = x11->randr.min_height; hy = x11->randr.max_height; if (constrain_to_range(lx, w, hx)) { - fprintf(x11->errfile, - "width not in driver range: ! %d < %d < %d\n", + syslog(LOG_ERR, "width not in driver range: ! %d < %d < %d", lx, orig_w, hx); } if (constrain_to_range(ly, h, hy)) { - fprintf(x11->errfile, - "height not in driver range: ! %d < %d < %d\n", + syslog(LOG_ERR, "height not in driver range: ! %d < %d < %d", ly, orig_h, hy); } } @@ -550,9 +547,8 @@ static void zero_base_monitors(struct vdagent_x11 *x11, max_y = max_int(mon_config->monitors[i].y + *mon_height, max_y); } if (min_x != 0 || min_y != 0) { - fprintf(x11->errfile, - "%s: agent config %d,%d rooted, adjusting to 0,0.\n", - __FUNCTION__, min_x, min_y); + syslog(LOG_ERR, "%s: agent config %d,%d rooted, adjusting to 0,0.", + __FUNCTION__, min_x, min_y); for (i = 0 ; i < mon_config->num_of_monitors; ++i) { mon_config->monitors[i].x -= min_x; mon_config->monitors[i].y -= min_y; @@ -573,14 +569,14 @@ int same_monitor_configs(struct vdagent_x11 *x11, VDAgentMonitorsConfig *mon) XRRScreenResources *res = x11->randr.res; if (res->noutput > res->ncrtc) { - fprintf(x11->errfile, "error: unexpected noutput > ncrtc in driver\n"); + syslog(LOG_ERR, "error: unexpected noutput > ncrtc in driver"); return 0; } if (mon->num_of_monitors > res->noutput) { - fprintf(x11->errfile, "error: unexpected client request: " - "#mon %d > driver output %d\n", - mon->num_of_monitors, res->noutput); + syslog(LOG_ERR, + "error: unexpected client request: #mon %d > driver output %d", + mon->num_of_monitors, res->noutput); return 0; } @@ -589,14 +585,13 @@ int same_monitor_configs(struct vdagent_x11 *x11, VDAgentMonitorsConfig *mon) return 0; } if (x11->randr.outputs[i]->ncrtc != 1) { - fprintf(x11->errfile, - "error: unexpected driver config, ncrtc %d != 1", - x11->randr.outputs[i]->ncrtc); + syslog(LOG_ERR, "error: unexpected driver config, ncrtc %d != 1", + x11->randr.outputs[i]->ncrtc); return 0; } crtc = crtc_from_id(x11, x11->randr.outputs[i]->crtcs[0]); if (!crtc) { - fprintf(x11->errfile, "error: inconsistent or stale data from X\n"); + syslog(LOG_ERR, "error: inconsistent or stale data from X"); return 0; } client_mode = &mon->monitors[i]; @@ -623,12 +618,12 @@ static void dump_monitors_config(struct vdagent_x11 *x11, int i; VDAgentMonConfig *m; - fprintf(x11->errfile, "%s: %d, %x\n", prefix, mon_config->num_of_monitors, - mon_config->flags); + syslog(LOG_DEBUG, "%s: %d, %x", prefix, mon_config->num_of_monitors, + mon_config->flags); for (i = 0 ; i < mon_config->num_of_monitors; ++i) { m = &mon_config->monitors[i]; - fprintf(x11->errfile, "received monitor %d config %dx%d+%d+%d\n", i, - m->width, m->height, m->x, m->y); + syslog(LOG_DEBUG, "received monitor %d config %dx%d+%d+%d", i, + m->width, m->height, m->x, m->y); } } @@ -655,8 +650,8 @@ void vdagent_x11_set_monitor_config(struct vdagent_x11 *x11, goto exit; if (mon_config->num_of_monitors < 1) { - fprintf(x11->errfile, "client sent invalid monitor config number %d\n", - mon_config->num_of_monitors); + syslog(LOG_ERR, "client sent invalid monitor config number %d", + mon_config->num_of_monitors); goto exit; } @@ -664,7 +659,7 @@ void vdagent_x11_set_monitor_config(struct vdagent_x11 *x11, goto exit; } - if (x11->verbose) { + if (x11->debug) { dump_monitors_config(x11, mon_config, "from guest"); } @@ -672,7 +667,7 @@ void vdagent_x11_set_monitor_config(struct vdagent_x11 *x11, constrain_to_screen(x11, &primary_w, &primary_h); - if (x11->verbose) { + if (x11->debug) { dump_monitors_config(x11, mon_config, "after zeroing"); } @@ -700,7 +695,7 @@ void vdagent_x11_set_monitor_config(struct vdagent_x11 *x11, } if (!update_randr_res(x11)) { - fprintf(x11->errfile, "get screen info failed\n"); + syslog(LOG_ERR, "get screen info failed"); } x11->width = primary_w; x11->height = primary_h; @@ -734,7 +729,7 @@ void vdagent_x11_send_daemon_guest_xorg_res(struct vdagent_x11 *x11) res = malloc(screen_count * sizeof(*res)); if (!res) { - fprintf(x11->errfile, "out of memory while trying to send resolutions, not sending resolutions.\n"); + syslog(LOG_ERR, "out of memory while trying to send resolutions, not sending resolutions."); if (screen_info) XFree(screen_info); return; @@ -743,8 +738,8 @@ void vdagent_x11_send_daemon_guest_xorg_res(struct vdagent_x11 *x11) if (screen_info) { for (i = 0; i < screen_count; i++) { if (screen_info[i].screen_number >= screen_count) { - fprintf(x11->errfile, "Invalid screen number in xinerama screen info (%d >= %d)\n", - screen_info[i].screen_number, screen_count); + syslog(LOG_ERR, "Invalid screen number in xinerama screen info (%d >= %d)", + screen_info[i].screen_number, screen_count); XFree(screen_info); free(res); return; diff --git a/src/vdagent-x11.c b/src/vdagent-x11.c index 3af8e75..20f0cc6 100644 --- a/src/vdagent-x11.c +++ b/src/vdagent-x11.c @@ -34,6 +34,7 @@ #include <stdlib.h> #include <limits.h> #include <string.h> +#include <syslog.h> #include <assert.h> #include <X11/Xatom.h> #include <X11/Xlib.h> @@ -73,7 +74,7 @@ static int debug_error_handler(Display *display, XErrorEvent *error) } struct vdagent_x11 *vdagent_x11_create(struct udscs_connection *vdagentd, - FILE *errfile, int verbose, int sync) + int debug, int sync) { struct vdagent_x11 *x11; XWindowAttributes attrib; @@ -81,17 +82,16 @@ struct vdagent_x11 *vdagent_x11_create(struct udscs_connection *vdagentd, x11 = calloc(1, sizeof(*x11)); if (!x11) { - fprintf(errfile, "out of memory allocating vdagent_x11 struct\n"); + syslog(LOG_ERR, "out of memory allocating vdagent_x11 struct"); return NULL; } x11->vdagentd = vdagentd; - x11->errfile = errfile; - x11->verbose = verbose; + x11->debug = debug; x11->display = XOpenDisplay(NULL); if (!x11->display) { - fprintf(x11->errfile, "could not connect to X-server\n"); + syslog(LOG_ERR, "could not connect to X-server"); free(x11); return NULL; } @@ -123,9 +123,8 @@ struct vdagent_x11 *vdagent_x11_create(struct udscs_connection *vdagentd, /* We should not store properties (for selections) on the root window */ x11->selection_window = XCreateSimpleWindow(x11->display, x11->root_window, 0, 0, 1, 1, 0, 0, 0); - if (x11->verbose) - fprintf(x11->errfile, "Selection window: %u\n", - (unsigned int)x11->selection_window); + if (x11->debug) + syslog(LOG_DEBUG, "Selection window: %u", (int)x11->selection_window); vdagent_x11_randr_init(x11); @@ -143,8 +142,7 @@ struct vdagent_x11 *vdagent_x11_create(struct udscs_connection *vdagentd, XFixesSelectionWindowDestroyNotifyMask| XFixesSelectionClientCloseNotifyMask); } else - fprintf(x11->errfile, - "no xfixes, no guest -> client copy paste support\n"); + syslog(LOG_ERR, "no xfixes, no guest -> client copy paste support"); x11->max_prop_size = XExtendedMaxRequestSize(x11->display); if (x11->max_prop_size) { @@ -224,7 +222,7 @@ static void vdagent_x11_set_clipboard_owner(struct vdagent_x11 *x11, if (curr_sel->selection == selection) { if (once) { SELPRINTF("selection requests pending on clipboard ownership " - "change, clearing\n"); + "change, clearing"); once = 0; } vdagent_x11_send_selection_notify(x11, None, curr_sel); @@ -253,7 +251,7 @@ static void vdagent_x11_set_clipboard_owner(struct vdagent_x11 *x11, if (curr_conv->selection == selection) { if (once) { SELPRINTF("client clipboard request pending on clipboard " - "ownership change, clearing\n"); + "ownership change, clearing"); once = 0; } udscs_write(x11->vdagentd, VDAGENTD_CLIPBOARD_DATA, selection, @@ -290,7 +288,7 @@ static int vdagent_x11_get_clipboard_atom(struct vdagent_x11 *x11, uint8_t selec } else if (selection == VD_AGENT_CLIPBOARD_SELECTION_PRIMARY) { *clipboard = x11->clipboard_primary_atom; } else { - fprintf(x11->errfile, "get_clipboard_atom: unknown selection\n"); + syslog(LOG_ERR, "get_clipboard_atom: unknown selection"); return -1; } @@ -310,7 +308,7 @@ static int vdagent_x11_get_clipboard_selection(struct vdagent_x11 *x11, } else if (event->type == SelectionRequest) { atom = event->xselectionrequest.selection; } else { - fprintf(x11->errfile, "get_clipboard_selection: unknown event type\n"); + syslog(LOG_ERR, "get_clipboard_selection: unknown event type"); return -1; } @@ -319,7 +317,7 @@ static int vdagent_x11_get_clipboard_selection(struct vdagent_x11 *x11, } else if (atom == x11->clipboard_primary_atom) { *selection = VD_AGENT_CLIPBOARD_SELECTION_PRIMARY; } else { - fprintf(x11->errfile, "get_clipboard_selection: unknown selection\n"); + syslog(LOG_ERR, "get_clipboard_selection: unknown selection"); return -1; } @@ -351,11 +349,11 @@ static void vdagent_x11_handle_event(struct vdagent_x11 *x11, XEvent event) ev.xfev.owner = None; break; default: - VSELPRINTF("unexpected xfix event subtype %d window %d\n", + VSELPRINTF("unexpected xfix event subtype %d window %d", (int)ev.xfev.subtype, (int)event.xany.window); return; } - VSELPRINTF("New selection owner: %u\n", (unsigned int)ev.xfev.owner); + VSELPRINTF("New selection owner: %u", (unsigned int)ev.xfev.owner); /* Ignore becoming the owner ourselves */ if (ev.xfev.owner == x11->selection_window) @@ -424,7 +422,7 @@ static void vdagent_x11_handle_event(struct vdagent_x11 *x11, XEvent event) new_req = malloc(sizeof(*new_req)); if (!new_req) { - SELPRINTF("out of memory on SelectionRequest, ignoring.\n"); + SELPRINTF("out of memory on SelectionRequest, ignoring."); break; } @@ -449,9 +447,9 @@ static void vdagent_x11_handle_event(struct vdagent_x11 *x11, XEvent event) break; } } - if (!handled && x11->verbose) - fprintf(x11->errfile, "unhandled x11 event, type %d, window %d\n", - (int)event.type, (int)event.xany.window); + if (!handled && x11->debug) + syslog(LOG_DEBUG, "unhandled x11 event, type %d, window %d", + (int)event.type, (int)event.xany.window); } void vdagent_x11_do_read(struct vdagent_x11 *x11) @@ -486,13 +484,13 @@ static int vdagent_x11_get_selection(struct vdagent_x11 *x11, XEvent *event, if (!incr) { if (event->xselection.property == None) { - VSELPRINTF("XConvertSelection refused by clipboard owner\n"); + VSELPRINTF("XConvertSelection refused by clipboard owner"); goto exit; } if (event->xselection.requestor != x11->selection_window || event->xselection.property != prop) { - SELPRINTF("SelectionNotify parameters mismatch\n"); + SELPRINTF("SelectionNotify parameters mismatch"); goto exit; } } @@ -500,7 +498,7 @@ static int vdagent_x11_get_selection(struct vdagent_x11 *x11, XEvent *event, if (XGetWindowProperty(x11->display, x11->selection_window, prop, 0, LONG_MAX, del, type, &type_ret, &format_ret, &len, &remain, &data) != Success) { - SELPRINTF("XGetWindowProperty failed\n"); + SELPRINTF("XGetWindowProperty failed"); goto exit; } @@ -510,7 +508,7 @@ static int vdagent_x11_get_selection(struct vdagent_x11 *x11, XEvent *event, if (x11->expect_property_notify) { SELPRINTF("received an incr SelectionNotify while " - "still reading another incr property\n"); + "still reading another incr property"); goto exit; } @@ -518,7 +516,7 @@ static int vdagent_x11_get_selection(struct vdagent_x11 *x11, XEvent *event, free(x11->clipboard_data); x11->clipboard_data = malloc(prop_min_size); if (!x11->clipboard_data) { - SELPRINTF("out of memory allocating clipboard buffer\n"); + SELPRINTF("out of memory allocating clipboard buffer"); x11->clipboard_data_space = 0; goto exit; } @@ -535,14 +533,14 @@ static int vdagent_x11_get_selection(struct vdagent_x11 *x11, XEvent *event, } if (type_ret != type) { - SELPRINTF("expected property type: %s, got: %s\n", + SELPRINTF("expected property type: %s, got: %s", vdagent_x11_get_atom_name(x11, type), vdagent_x11_get_atom_name(x11, type_ret)); goto exit; } if (format_ret != format) { - SELPRINTF("expected %d bit format, got %d bits\n", format, format_ret); + SELPRINTF("expected %d bit format, got %d bits", format, format_ret); goto exit; } @@ -567,7 +565,7 @@ static int vdagent_x11_get_selection(struct vdagent_x11 *x11, XEvent *event, x11->clipboard_data = realloc(x11->clipboard_data, x11->clipboard_data_space); if (!x11->clipboard_data) { - SELPRINTF("out of memory allocating clipboard buffer\n"); + SELPRINTF("out of memory allocating clipboard buffer"); x11->clipboard_data_space = 0; free(old_clipboard_data); goto exit; @@ -575,7 +573,7 @@ static int vdagent_x11_get_selection(struct vdagent_x11 *x11, XEvent *event, } memcpy(x11->clipboard_data + x11->clipboard_data_size, data, len); x11->clipboard_data_size += len; - VSELPRINTF("Appended %ld bytes to buffer\n", len); + VSELPRINTF("Appended %ld bytes to buffer", len); XFree(data); return 0; /* Wait for more data */ } @@ -587,7 +585,7 @@ static int vdagent_x11_get_selection(struct vdagent_x11 *x11, XEvent *event, if (len > 0) { ret_val = len; } else { - SELPRINTF("property contains no data (zero length)\n"); + SELPRINTF("property contains no data (zero length)"); *data_ret = NULL; } @@ -630,7 +628,7 @@ static uint32_t vdagent_x11_target_to_type(struct vdagent_x11 *x11, } } - SELPRINTF("unexpected selection type %s\n", + SELPRINTF("unexpected selection type %s", vdagent_x11_get_atom_name(x11, target)); return VD_AGENT_CLIPBOARD_NONE; } @@ -645,7 +643,7 @@ static Atom vdagent_x11_type_to_target(struct vdagent_x11 *x11, return x11->clipboard_x11_targets[selection][i]; } } - SELPRINTF("client requested unavailable type %u\n", type); + SELPRINTF("client requested unavailable type %u", type); return None; } @@ -672,7 +670,7 @@ static void vdagent_x11_handle_selection_notify(struct vdagent_x11 *x11, Atom clip; if (!x11->conversion_req) { - fprintf(x11->errfile, "SelectionNotify received without a target\n"); + syslog(LOG_ERR, "SelectionNotify received without a target"); return; } vdagent_x11_get_clipboard_atom(x11, x11->conversion_req->selection, &clip); @@ -686,13 +684,13 @@ static void vdagent_x11_handle_selection_notify(struct vdagent_x11 *x11, if (vdagent_x11_get_clipboard_selection(x11, event, &selection)) { len = -1; } else if (selection != x11->conversion_req->selection) { - SELPRINTF("Requested data for selection %d got %d\n", + SELPRINTF("Requested data for selection %d got %d", (int)x11->conversion_req->selection, (int)selection); len = -1; } if (event->xselection.target != x11->conversion_req->target && event->xselection.target != x11->incr_atom) { - SELPRINTF("Requested %s target got %s\n", + SELPRINTF("Requested %s target got %s", vdagent_x11_get_atom_name(x11, x11->conversion_req->target), vdagent_x11_get_atom_name(x11, event->xselection.target)); len = -1; @@ -739,9 +737,9 @@ static void vdagent_x11_print_targets(struct vdagent_x11 *x11, uint8_t selection, const char *action, Atom *atoms, int c) { int i; - VSELPRINTF("%s %d targets:\n", action, c); + VSELPRINTF("%s %d targets:", action, c); for (i = 0; i < c; i++) - VSELPRINTF("%s\n", vdagent_x11_get_atom_name(x11, atoms[i])); + VSELPRINTF("%s", vdagent_x11_get_atom_name(x11, atoms[i])); } static void vdagent_x11_handle_targets_notify(struct vdagent_x11 *x11, @@ -757,7 +755,7 @@ static void vdagent_x11_handle_targets_notify(struct vdagent_x11 *x11, } if (!x11->expected_targets_notifies[selection]) { - SELPRINTF("unexpected selection notify TARGETS\n"); + SELPRINTF("unexpected selection notify TARGETS"); return; } @@ -792,7 +790,7 @@ static void vdagent_x11_handle_targets_notify(struct vdagent_x11 *x11, (*type_count)++; if (*type_count == sizeof(x11->clipboard_agent_types[0])/sizeof(uint32_t)) { - SELPRINTF("handle_targets_notify: too many types\n"); + SELPRINTF("handle_targets_notify: too many types"); break; } } @@ -850,7 +848,7 @@ static void vdagent_x11_send_targets(struct vdagent_x11 *x11, targets[target_count] = x11->clipboard_formats[j].atoms[k]; target_count++; if (target_count == sizeof(targets)/sizeof(Atom)) { - SELPRINTF("send_targets: too many targets\n"); + SELPRINTF("send_targets: too many targets"); goto exit_loop; } } @@ -883,14 +881,14 @@ static void vdagent_x11_handle_selection_request(struct vdagent_x11 *x11) if (x11->clipboard_owner[selection] != owner_client) { SELPRINTF("received selection request event for target %s, " - "while not owning client clipboard\n", + "while not owning client clipboard", vdagent_x11_get_atom_name(x11, event->xselectionrequest.target)); vdagent_x11_send_selection_notify(x11, None, NULL); return; } if (event->xselectionrequest.target == x11->multiple_atom) { - SELPRINTF("multiple target not supported\n"); + SELPRINTF("multiple target not supported"); vdagent_x11_send_selection_notify(x11, None, NULL); return; } @@ -932,12 +930,12 @@ static void vdagent_x11_handle_property_delete_notify(struct vdagent_x11 *x11, } if (len) { - VSELPRINTF("Sending %d-%d/%d bytes of clipboard data\n", + VSELPRINTF("Sending %d-%d/%d bytes of clipboard data", x11->selection_req_data_pos, x11->selection_req_data_pos + len - 1, x11->selection_req_data_size); } else { - VSELPRINTF("Ending incr send of clipboard data\n"); + VSELPRINTF("Ending incr send of clipboard data"); } XChangeProperty(x11->display, sel_event->xselectionrequest.requestor, x11->selection_req_atom, @@ -973,7 +971,7 @@ void vdagent_x11_clipboard_request(struct vdagent_x11 *x11, } if (x11->clipboard_owner[selection] != owner_guest) { - SELPRINTF("received clipboard req while not owning guest clipboard\n"); + SELPRINTF("received clipboard req while not owning guest clipboard"); goto none; } @@ -984,7 +982,7 @@ void vdagent_x11_clipboard_request(struct vdagent_x11 *x11, new_req = malloc(sizeof(*new_req)); if (!new_req) { - SELPRINTF("out of memory on client clipboard request, ignoring.\n"); + SELPRINTF("out of memory on client clipboard request, ignoring."); return; } @@ -1023,7 +1021,7 @@ void vdagent_x11_clipboard_grab(struct vdagent_x11 *x11, uint8_t selection, } if (type_count > sizeof(x11->clipboard_agent_types[0])/sizeof(uint32_t)) { - SELPRINTF("x11_clipboard_grab: too many types\n"); + SELPRINTF("x11_clipboard_grab: too many types"); type_count = sizeof(x11->clipboard_agent_types[0])/sizeof(uint32_t); } @@ -1049,7 +1047,7 @@ void vdagent_x11_clipboard_data(struct vdagent_x11 *x11, uint8_t selection, if (x11->selection_req_data) { if (type || size) { SELPRINTF("received clipboard data while still sending" - " data from previous request, ignoring\n"); + " data from previous request, ignoring"); } free(data); return; @@ -1058,7 +1056,7 @@ void vdagent_x11_clipboard_data(struct vdagent_x11 *x11, uint8_t selection, if (!x11->selection_req) { if (type || size) { SELPRINTF("received clipboard data without an " - "outstanding selection request, ignoring\n"); + "outstanding selection request, ignoring"); } free(data); return; @@ -1071,11 +1069,11 @@ void vdagent_x11_clipboard_data(struct vdagent_x11 *x11, uint8_t selection, if (type_from_event != type || selection != x11->selection_req->selection) { if (selection != x11->selection_req->selection) { - SELPRINTF("expecting data for selection %d got %d\n", + SELPRINTF("expecting data for selection %d got %d", (int)x11->selection_req->selection, (int)selection); } if (type_from_event != type) { - SELPRINTF("expecting type %u clipboard data got %u\n", + SELPRINTF("expecting type %u clipboard data got %u", type_from_event, type); } vdagent_x11_send_selection_notify(x11, None, NULL); @@ -1092,7 +1090,7 @@ void vdagent_x11_clipboard_data(struct vdagent_x11 *x11, uint8_t selection, if (size > x11->max_prop_size) { unsigned long len = size; - VSELPRINTF("Starting incr send of clipboard data\n"); + VSELPRINTF("Starting incr send of clipboard data"); x11->selection_req_data = data; x11->selection_req_data_pos = 0; x11->selection_req_data_size = size; @@ -1125,7 +1123,7 @@ void vdagent_x11_clipboard_release(struct vdagent_x11 *x11, uint8_t selection) } if (x11->clipboard_owner[selection] != owner_client) { - SELPRINTF("received release while not owning client clipboard\n"); + SELPRINTF("received release while not owning client clipboard"); return; } diff --git a/src/vdagent-x11.h b/src/vdagent-x11.h index f595cac..e856470 100644 --- a/src/vdagent-x11.h +++ b/src/vdagent-x11.h @@ -29,7 +29,7 @@ struct vdagent_x11; struct vdagent_x11 *vdagent_x11_create(struct udscs_connection *vdagentd, - FILE *errfile, int verbose, int sync); + int debug, int sync); void vdagent_x11_destroy(struct vdagent_x11 *x11); int vdagent_x11_get_fd(struct vdagent_x11 *x11); diff --git a/src/vdagent.c b/src/vdagent.c index 82c5ff9..0af82b1 100644 --- a/src/vdagent.c +++ b/src/vdagent.c @@ -26,6 +26,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <syslog.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> @@ -40,10 +41,9 @@ #include "vdagent-x11.h" static const char *portdev = "/dev/virtio-ports/com.redhat.spice.0"; -static int verbose = 0; +static int debug = 0; static struct vdagent_x11 *x11 = NULL; static struct udscs_connection *client = NULL; -static FILE *logfile = NULL; static int quit = 0; static int version_mismatch = 0; @@ -76,17 +76,15 @@ void daemon_read_complete(struct udscs_connection **connp, break; case VDAGENTD_VERSION: if (strcmp((char *)data, VERSION) != 0) { - fprintf(logfile, - "Fatal vdagentd version mismatch: got %s expected %s\n", - data, VERSION); + syslog(LOG_INFO, "vdagentd version mismatch: got %s expected %s", + data, VERSION); udscs_destroy_connection(connp); version_mismatch = 1; } break; default: - if (verbose) - fprintf(logfile, "Unknown message from vdagentd type: %d\n", - header->type); + syslog(LOG_ERR, "Unknown message from vdagentd type: %d, ignoring", + header->type); free(data); } } @@ -96,7 +94,7 @@ int client_setup(int reconnect) while (!quit) { client = udscs_connect(VDAGENTD_SOCKET, daemon_read_complete, NULL, vdagentd_messages, VDAGENTD_NO_MESSAGES, - verbose ? logfile : NULL, logfile); + debug); if (client || !reconnect || quit) { break; } @@ -113,7 +111,7 @@ static void usage(FILE *fp) " -h print this text\n" " -d log debug messages\n" " -s <port> set virtio serial port [%s]\n" - " -x don't daemonize (and log to logfile)\n", + " -x don't daemonize\n", portdev); } @@ -134,11 +132,9 @@ void daemonize(void) x = open("/dev/null", O_RDWR); x = dup(x); x = dup(x); break; case -1: - fprintf(logfile, "fork: %s\n", strerror(errno)); + syslog(LOG_ERR, "fork: %s", strerror(errno)); retval = 1; default: - if (logfile != stderr) - fclose(logfile); exit(retval); } } @@ -153,10 +149,9 @@ static int file_test(const char *path) int main(int argc, char *argv[]) { fd_set readfds, writefds; - int c, n, nfds, x11_fd, retval = 0; + int c, n, nfds, x11_fd; int do_daemonize = 1; int x11_sync = 0; - char *home, filename[1024]; struct sigaction act; for (;;) { @@ -164,7 +159,7 @@ int main(int argc, char *argv[]) break; switch (c) { case 'd': - verbose++; + debug++; break; case 's': portdev = optarg; @@ -192,29 +187,13 @@ int main(int argc, char *argv[]) sigaction(SIGTERM, &act, NULL); sigaction(SIGQUIT, &act, NULL); - logfile = stderr; - home = getenv("HOME"); - if (home) { - snprintf(filename, sizeof(filename), "%s/.spice-vdagent", home); - n = mkdir(filename, 0755); - snprintf(filename, sizeof(filename), "%s/.spice-vdagent/log", home); - if (do_daemonize) { - logfile = fopen(filename, "w"); - if (!logfile) { - fprintf(stderr, "Error opening %s: %s\n", filename, - strerror(errno)); - logfile = stderr; - } - } - } else { - fprintf(stderr, "Could not get home directory, logging to stderr\n"); - } + openlog("spice-vdagent", do_daemonize ? LOG_PID : (LOG_PID | LOG_PERROR), + LOG_USER); if (file_test(portdev) != 0) { - fprintf(logfile, "Missing virtio device '%s': %s\n", + syslog(LOG_ERR, "Missing virtio device '%s': %s", portdev, strerror(errno)); - retval = 1; - goto finish; + return 1; } if (do_daemonize) @@ -222,23 +201,19 @@ int main(int argc, char *argv[]) reconnect: if (version_mismatch) { - fprintf(logfile, "Version mismatch, restarting\n"); - if (logfile != stderr) - fclose(logfile); + syslog(LOG_INFO, "Version mismatch, restarting"); sleep(1); execvp(argv[0], argv); } if (client_setup(do_daemonize)) { - retval = 1; - goto finish; + return 1; } - x11 = vdagent_x11_create(client, logfile, verbose, x11_sync); + x11 = vdagent_x11_create(client, debug, x11_sync); if (!x11) { udscs_destroy_connection(&client); - retval = 1; - goto finish; + return 1; } while (client && !quit) { @@ -255,15 +230,13 @@ reconnect: if (n == -1) { if (errno == EINTR) continue; - fprintf(logfile, "Fatal error select: %s\n", strerror(errno)); - retval = 1; + syslog(LOG_ERR, "Fatal error select: %s", strerror(errno)); break; } if (FD_ISSET(x11_fd, &readfds)) vdagent_x11_do_read(x11); udscs_client_handle_fds(&client, &readfds, &writefds); - fflush(logfile); } vdagent_x11_destroy(x11); @@ -271,9 +244,5 @@ reconnect: if (!quit) goto reconnect; -finish: - if (logfile != stderr) - fclose(logfile); - - return retval; + return 0; } diff --git a/src/vdagentd-uinput.c b/src/vdagentd-uinput.c index 8c19e1f..685e9c9 100644 --- a/src/vdagentd-uinput.c +++ b/src/vdagentd-uinput.c @@ -1,6 +1,6 @@ /* vdagentd-uinput.c vdagentd uinput handling code - Copyright 2010 Red Hat, Inc. + Copyright 2010-2012 Red Hat, Inc. Red Hat Authors: Hans de Goede <hdegoede@redhat.com> @@ -24,7 +24,7 @@ #endif #include <stdlib.h> -#include <string.h> +#include <syslog.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> @@ -36,19 +36,18 @@ struct vdagentd_uinput { const char *devname; int fd; - int verbose; + int debug; int width; int height; struct vdagentd_guest_xorg_resolution *screen_info; int screen_count; - FILE *errfile; VDAgentMouseState last; }; struct vdagentd_uinput *vdagentd_uinput_create(const char *devname, int width, int height, struct vdagentd_guest_xorg_resolution *screen_info, int screen_count, - FILE *errfile, int verbose) + int debug) { struct vdagentd_uinput *uinput; @@ -58,9 +57,8 @@ struct vdagentd_uinput *vdagentd_uinput_create(const char *devname, uinput->devname = devname; uinput->fd = -1; /* Gets opened by vdagentd_uinput_update_size() */ - uinput->verbose = verbose; - uinput->errfile = errfile; - + uinput->debug = debug; + vdagentd_uinput_update_size(&uinput, width, height, screen_info, screen_count); @@ -116,16 +114,14 @@ void vdagentd_uinput_update_size(struct vdagentd_uinput **uinputp, uinput->fd = open(uinput->devname, O_RDWR); if (uinput->fd == -1) { - fprintf(uinput->errfile, "open %s: %s\n", - uinput->devname, strerror(errno)); + syslog(LOG_ERR, "open %s: %m", uinput->devname); vdagentd_uinput_destroy(uinputp); return; } rc = write(uinput->fd, &device, sizeof(device)); if (rc != sizeof(device)) { - fprintf(uinput->errfile, "write %s: %s\n", - uinput->devname, strerror(errno)); + syslog(LOG_ERR, "write %s: %m", uinput->devname); vdagentd_uinput_destroy(uinputp); return; } @@ -147,8 +143,7 @@ void vdagentd_uinput_update_size(struct vdagentd_uinput **uinputp, rc = ioctl(uinput->fd, UI_DEV_CREATE); if (rc < 0) { - fprintf(uinput->errfile, "create %s: %s\n", - uinput->devname, strerror(errno)); + syslog(LOG_ERR, "create %s: %m", uinput->devname); vdagentd_uinput_destroy(uinputp); } } @@ -166,8 +161,7 @@ static void uinput_send_event(struct vdagentd_uinput **uinputp, rc = write(uinput->fd, &event, sizeof(event)); if (rc != sizeof(event)) { - fprintf(uinput->errfile, "write %s: %s\n", - uinput->devname, strerror(errno)); + syslog(LOG_ERR, "write %s: %m", uinput->devname); vdagentd_uinput_destroy(uinputp); } } @@ -194,8 +188,8 @@ void vdagentd_uinput_do_mouse(struct vdagentd_uinput **uinputp, if (*uinputp) { if (mouse->display_id >= uinput->screen_count) { - fprintf(uinput->errfile, "mouse event for unknown monitor (%d >= %d)\n", - mouse->display_id, uinput->screen_count); + syslog(LOG_WARNING, "mouse event for unknown monitor (%d >= %d)", + mouse->display_id, uinput->screen_count); return; } mouse->x += uinput->screen_info[mouse->display_id].x; @@ -207,13 +201,13 @@ void vdagentd_uinput_do_mouse(struct vdagentd_uinput **uinputp, } if (*uinputp && uinput->last.x != mouse->x) { - if (uinput->verbose) - fprintf(uinput->errfile, "mouse: abs-x %d\n", mouse->x); + if (uinput->debug) + syslog(LOG_DEBUG, "mouse: abs-x %d", mouse->x); uinput_send_event(uinputp, EV_ABS, ABS_X, mouse->x); } if (*uinputp && uinput->last.y != mouse->y) { - if (uinput->verbose) - fprintf(uinput->errfile, "mouse: abs-y %d\n", mouse->y); + if (uinput->debug) + syslog(LOG_DEBUG, "mouse: abs-y %d", mouse->y); uinput_send_event(uinputp, EV_ABS, ABS_Y, mouse->y); } for (i = 0; i < sizeof(btns)/sizeof(btns[0]) && *uinputp; i++) { @@ -221,8 +215,8 @@ void vdagentd_uinput_do_mouse(struct vdagentd_uinput **uinputp, (mouse->buttons & btns[i].mask)) continue; down = !!(mouse->buttons & btns[i].mask); - if (uinput->verbose) - fprintf(uinput->errfile, "mouse: btn-%s %s\n", + if (uinput->debug) + syslog(LOG_DEBUG, "mouse: btn-%s %s", btns[i].name, down ? "down" : "up"); uinput_send_event(uinputp, EV_KEY, btns[i].btn, down); } @@ -231,15 +225,15 @@ void vdagentd_uinput_do_mouse(struct vdagentd_uinput **uinputp, (mouse->buttons & wheel[i].mask)) continue; if (mouse->buttons & wheel[i].mask) { - if (uinput->verbose) - fprintf(uinput->errfile, "mouse: wheel-%s\n", wheel[i].name); + if (uinput->debug) + syslog(LOG_DEBUG, "mouse: wheel-%s", wheel[i].name); uinput_send_event(uinputp, EV_REL, REL_WHEEL, wheel[i].btn); } } if (*uinputp) { - if (uinput->verbose) - fprintf(uinput->errfile, "mouse: syn\n"); + if (uinput->debug) + syslog(LOG_DEBUG, "mouse: syn"); uinput_send_event(uinputp, EV_SYN, SYN_REPORT, 0); } diff --git a/src/vdagentd-uinput.h b/src/vdagentd-uinput.h index 27cd375..81f0376 100644 --- a/src/vdagentd-uinput.h +++ b/src/vdagentd-uinput.h @@ -1,6 +1,6 @@ /* vdagentd-uinput.c vdagentd uinput handling header - Copyright 2010 Red Hat, Inc. + Copyright 2010-2012 Red Hat, Inc. Red Hat Authors: Hans de Goede <hdegoede@redhat.com> @@ -30,7 +30,7 @@ struct vdagentd_uinput; struct vdagentd_uinput *vdagentd_uinput_create(const char *devname, int width, int height, struct vdagentd_guest_xorg_resolution *screen_info, int screen_count, - FILE *errfile, int verbose); + int debug); void vdagentd_uinput_destroy(struct vdagentd_uinput **uinputp); void vdagentd_uinput_do_mouse(struct vdagentd_uinput **uinputp, diff --git a/src/vdagentd-xorg-conf.c b/src/vdagentd-xorg-conf.c index 5610837..cfdd4ae 100644 --- a/src/vdagentd-xorg-conf.c +++ b/src/vdagentd-xorg-conf.c @@ -1,6 +1,6 @@ /* vdagentd.c vdagentd xorg.conf writing code - Copyright 2011 Red Hat, Inc. + Copyright 2011, 2012 Red Hat, Inc. Red Hat Authors: Hans de Goede <hdegoede@redhat.com> @@ -29,22 +29,21 @@ #include <string.h> #include <errno.h> #include <limits.h> +#include <syslog.h> #include "vdagentd-xorg-conf.h" #define FPRINTF(...) \ do { \ r = fprintf(f, __VA_ARGS__); \ if (r < 0) { \ - fprintf(logfile, "Error writing to %s: %s\n", \ - xorg_conf, strerror(errno)); \ + syslog(LOG_ERR, "Error writing to %s: %m", xorg_conf); \ fclose(f); \ pci_system_cleanup(); \ return; \ } \ } while(0) -void vdagentd_write_xorg_conf(VDAgentMonitorsConfig *monitor_conf, - FILE *logfile) +void vdagentd_write_xorg_conf(VDAgentMonitorsConfig *monitor_conf) { #ifdef HAVE_PCIACCESS int i, r, count, min_x = INT_MAX, min_y = INT_MAX; @@ -62,36 +61,35 @@ void vdagentd_write_xorg_conf(VDAgentMonitorsConfig *monitor_conf, r = rename(xorg_conf, xorg_conf_old); if (r && errno != ENOENT) { - fprintf(logfile, - "Error renaming %s to %s: %s, not generating xorg.conf\n", - xorg_conf, xorg_conf_old, strerror(errno)); + syslog(LOG_ERR, + "Error renaming %s to %s: %m, not generating xorg.conf", + xorg_conf, xorg_conf_old); return; } r = pci_system_init(); if (r) { - fprintf(logfile, "Error initializing libpciaccess: %d, not generating xorg.conf\n", r); + syslog(LOG_ERR, "Error initializing libpciaccess: %d, not generating xorg.conf", r); return; } it = pci_id_match_iterator_create(&qxl_id_match); if (!it) { - fprintf(logfile, "Error could not create pci id iterator for QXL devices, not generating xorg.conf\n"); + syslog(LOG_ERR, "Error could not create pci id iterator for QXL devices, not generating xorg.conf"); pci_system_cleanup(); return; } dev = pci_device_next(it); if (!dev) { - fprintf(logfile, "No QXL devices found, not generating xorg.conf\n"); + syslog(LOG_ERR, "No QXL devices found, not generating xorg.conf"); pci_system_cleanup(); return; } f = fopen(xorg_conf, "w"); if (!f) { - fprintf(logfile, "Error opening %s for writing: %s\n", - xorg_conf, strerror(errno)); + syslog(LOG_ERR, "Error opening %s for writing: %m", xorg_conf); pci_system_cleanup(); return; } @@ -122,8 +120,9 @@ void vdagentd_write_xorg_conf(VDAgentMonitorsConfig *monitor_conf, } while ((dev = pci_device_next(it))); if (i < monitor_conf->num_of_monitors) { - fprintf(logfile, "Client has %d monitors, but only %d qxl devices found\n", - monitor_conf->num_of_monitors, i); + syslog(LOG_WARNING, + "Client has %d monitors, but only %d qxl devices found", + monitor_conf->num_of_monitors, i); FPRINTF("# Client has %d monitors, but only %d qxl devices found\n", monitor_conf->num_of_monitors, i); FPRINTF("# Only generation %d \"Screen\" sections\n\n", i); diff --git a/src/vdagentd-xorg-conf.h b/src/vdagentd-xorg-conf.h index f45b018..c0a2b28 100644 --- a/src/vdagentd-xorg-conf.h +++ b/src/vdagentd-xorg-conf.h @@ -1,6 +1,6 @@ /* vdagentd.c vdagentd xorg.conf writing code - Copyright 2011 Red Hat, Inc. + Copyright 2011, 2012 Red Hat, Inc. Red Hat Authors: Hans de Goede <hdegoede@redhat.com> @@ -24,7 +24,6 @@ #include <stdio.h> #include <spice/vd_agent.h> -void vdagentd_write_xorg_conf(VDAgentMonitorsConfig *monitor_conf, - FILE *logfile); +void vdagentd_write_xorg_conf(VDAgentMonitorsConfig *monitor_conf); #endif diff --git a/src/vdagentd.c b/src/vdagentd.c index aaa3038..a4db935 100644 --- a/src/vdagentd.c +++ b/src/vdagentd.c @@ -30,6 +30,7 @@ #include <fcntl.h> #include <errno.h> #include <signal.h> +#include <syslog.h> #include <sys/select.h> #include <sys/stat.h> #include <spice/vd_agent.h> @@ -51,7 +52,6 @@ struct agent_data { }; /* variables */ -static const char *logfilename = "/var/log/spice-vdagentd/spice-vdagentd.log"; static const char *pidfilename = "/var/run/spice-vdagentd/spice-vdagentd.pid"; static const char *portdev = "/dev/virtio-ports/com.redhat.spice.0"; static const char *uinput_device = "/dev/uinput"; @@ -72,7 +72,6 @@ static unsigned int session_count = 0; #endif static struct udscs_connection *active_session_conn = NULL; static int agent_owns_clipboard[256] = { 0, }; -static FILE *logfile = NULL; static int quit = 0; static int retval = 0; @@ -87,8 +86,7 @@ static void send_capabilities(struct vdagent_virtio_port *vport, size = sizeof(*caps) + VD_AGENT_CAPS_BYTES; caps = calloc(1, size); if (!caps) { - fprintf(logfile, - "out of memory allocating capabilities array (write)\n"); + syslog(LOG_ERR, "out of memory allocating capabilities array (write)"); return; } @@ -115,18 +113,18 @@ static void do_client_monitors(struct vdagent_virtio_port *vport, int port_nr, size = sizeof(VDAgentMonitorsConfig) + new_monitors->num_of_monitors * sizeof(VDAgentMonConfig); if (message_header->size != size) { - fprintf(logfile, "invalid message size for VDAgentMonitorsConfig\n"); + syslog(LOG_ERR, "invalid message size for VDAgentMonitorsConfig"); return; } - vdagentd_write_xorg_conf(new_monitors, logfile); + vdagentd_write_xorg_conf(new_monitors); if (!mon_config || mon_config->num_of_monitors != new_monitors->num_of_monitors) { free(mon_config); mon_config = malloc(size); if (!mon_config) { - fprintf(logfile, "out of memory allocating monitors config\n"); + syslog(LOG_ERR, "out of memory allocating monitors config"); return; } } @@ -154,8 +152,7 @@ static void do_client_capabilities(struct vdagent_virtio_port *vport, free(capabilities); capabilities = malloc(capabilities_size * sizeof(uint32_t)); if (!capabilities) { - fprintf(logfile, - "out of memory allocating capabilities array (read)\n"); + syslog(LOG_ERR, "oom allocating capabilities array (read)"); capabilities_size = 0; return; } @@ -172,9 +169,9 @@ static void do_client_clipboard(struct vdagent_virtio_port *vport, uint8_t selection = VD_AGENT_CLIPBOARD_SELECTION_CLIPBOARD; if (!active_session_conn) { - fprintf(logfile, - "Could not find an agent connnection belonging to the " - "active session, ignoring client clipboard request\n"); + syslog(LOG_WARNING, + "Could not find an agent connnection belonging to the " + "active session, ignoring client clipboard request"); return; } @@ -226,7 +223,7 @@ int virtio_port_read_complete( uint32_t min_size = 0; if (message_header->protocol != VD_AGENT_PROTOCOL) { - fprintf(logfile, "message with wrong protocol version ignoring\n"); + syslog(LOG_ERR, "message with wrong protocol version ignoring"); return 0; } @@ -245,9 +242,9 @@ int virtio_port_read_complete( agent_data->height, agent_data->screen_info, agent_data->screen_count, - logfile, debug > 1); + debug > 1); if (!uinput) { - fprintf(logfile, "Fatal uinput error\n"); + syslog(LOG_CRIT, "Fatal uinput error"); retval = 1; quit = 1; } @@ -287,16 +284,15 @@ int virtio_port_read_complete( do_client_clipboard(vport, message_header, data); break; default: - if (debug) - fprintf(logfile, "unknown message type %d\n", message_header->type); - break; + syslog(LOG_WARNING, "unknown message type %d, ignoring", + message_header->type); } return 0; size_error: - fprintf(logfile, "read: invalid message size: %u for message type: %u\n", - message_header->size, message_header->type); + syslog(LOG_ERR, "read: invalid message size: %u for message type: %u", + message_header->size, message_header->type); return 0; } @@ -313,14 +309,14 @@ int do_agent_clipboard(struct udscs_connection *conn, /* Check that this agent is from the currently active session */ if (conn != active_session_conn) { - fprintf(logfile, "Clipboard request from agent " - "which is not in the active session?\n"); + if (debug) + syslog(LOG_DEBUG, "%p clipboard req from agent which is not in " + "the active session?", conn); goto error; } if (!virtio_port) { - fprintf(logfile, - "Clipboard request from agent but no client connection\n"); + syslog(LOG_ERR, "Clipboard req from agent but no client connection"); goto error; } @@ -352,8 +348,8 @@ int do_agent_clipboard(struct udscs_connection *conn, } if (size != header->size) { - fprintf(logfile, - "unexpected extra data in clipboard msg, disconnecting agent\n"); + syslog(LOG_ERR, + "unexpected extra data in clipboard msg, disconnecting agent"); return -1; } @@ -408,7 +404,7 @@ static void check_xorg_resolution(void) agent_data->height, agent_data->screen_info, agent_data->screen_count, - logfile, debug > 1); + debug > 1); else vdagentd_uinput_update_size(&uinput, agent_data->width, @@ -416,20 +412,19 @@ static void check_xorg_resolution(void) agent_data->screen_info, agent_data->screen_count); if (!uinput) { - fprintf(logfile, "Fatal uinput error\n"); + syslog(LOG_CRIT, "Fatal uinput error"); retval = 1; quit = 1; return; } if (!virtio_port) { - fprintf(logfile, "opening vdagent virtio channel\n"); + syslog(LOG_INFO, "opening vdagent virtio channel"); virtio_port = vdagent_virtio_port_create(portdev, virtio_port_read_complete, - NULL, logfile); + NULL); if (!virtio_port) { - fprintf(logfile, - "Fatal error opening vdagent virtio channel\n"); + syslog(LOG_CRIT, "Fatal error opening vdagent virtio channel"); retval = 1; quit = 1; return; @@ -443,7 +438,7 @@ static void check_xorg_resolution(void) if (virtio_port) { vdagent_virtio_port_flush(&virtio_port); vdagent_virtio_port_destroy(&virtio_port); - fprintf(logfile, "closed vdagent virtio channel\n"); + syslog(LOG_INFO, "closed vdagent virtio channel"); } } } @@ -497,6 +492,8 @@ void update_active_session_connection(void) return; active_session_conn = new_conn; + if (debug) + syslog(LOG_DEBUG, "%p is now the active session", new_conn); #endif release_clipboards(); @@ -511,7 +508,7 @@ void agent_connect(struct udscs_connection *conn) agent_data = calloc(1, sizeof(*agent_data)); if (!agent_data) { - fprintf(logfile, "Out of memory allocating agent data, disconnecting\n"); + syslog(LOG_ERR, "Out of memory allocating agent data, disconnecting"); udscs_destroy_connection(&conn); return; } @@ -527,8 +524,8 @@ void agent_connect(struct udscs_connection *conn) * connections to the vdagentd and no consolekit since we can't * know to which one we should send data */ - fprintf(logfile, "Trying to use multiple vdagent without ConsoleKit support, " - "disabling vdagent to avoid potential information leak\n"); + syslog(LOG_ERR, "Trying to use multiple vdagent without ConsoleKit, " + "disabling vdagent to avoid potential information leak"); active_session_conn = NULL; } #endif @@ -578,14 +575,15 @@ void agent_read_complete(struct udscs_connection **connp, that stops it from getting the VDAGENTD_VERSION message, and then it will never re-exec the new version... */ if (header->arg1 == 0 && header->arg2 == 0) { - fprintf(logfile, "got old session agent xorg resolution message, ignoring\n"); + syslog(LOG_INFO, "got old session agent xorg resolution message, " + "ignoring"); free(data); return; } if (header->size != n * sizeof(*res)) { - fprintf(logfile, - "guest xorg resolution message has wrong size, disconnecting agent\n"); + syslog(LOG_ERR, "guest xorg resolution message has wrong size, " + "disconnecting agent"); udscs_destroy_connection(connp); free(data); return; @@ -594,7 +592,7 @@ void agent_read_complete(struct udscs_connection **connp, free(agent_data->screen_info); res = malloc(n * sizeof(*res)); if (!res) { - fprintf(logfile, "out of memory allocating screen info\n"); + syslog(LOG_ERR, "out of memory allocating screen info"); n = 0; } memcpy(res, data, n * sizeof(*res)); @@ -617,8 +615,8 @@ void agent_read_complete(struct udscs_connection **connp, } break; default: - fprintf(logfile, "unknown message from vdagent: %u, ignoring\n", - header->type); + syslog(LOG_ERR, "unknown message from vdagent: %u, ignoring", + header->type); } free(data); } @@ -634,7 +632,7 @@ static void usage(FILE *fp) " -d log debug messages (use twice for extra info)\n" " -s <port> set virtio serial port [%s]\n" " -u <dev> set uinput device [%s]\n" - " -x don't daemonize (and log to logfile)\n", + " -x don't daemonize\n", portdev, uinput_device); } @@ -656,12 +654,10 @@ void daemonize(void) } break; case -1: - fprintf(logfile, "fork: %s\n", strerror(errno)); + syslog(LOG_ERR, "fork: %m"); retval = 1; default: udscs_destroy_server(server); - if (logfile != stderr) - fclose(logfile); exit(retval); } } @@ -691,7 +687,7 @@ void main_loop(void) if (n == -1) { if (errno == EINTR) continue; - fprintf(logfile, "Fatal error select: %s\n", strerror(errno)); + syslog(LOG_CRIT, "Fatal error select: %m"); retval = 1; break; } @@ -701,15 +697,14 @@ void main_loop(void) if (virtio_port) { vdagent_virtio_port_handle_fds(&virtio_port, &readfds, &writefds); if (!virtio_port) { - fprintf(logfile, - "AIIEEE lost spice client connection, reconnecting\n"); + syslog(LOG_CRIT, + "AIIEEE lost spice client connection, reconnecting"); virtio_port = vdagent_virtio_port_create(portdev, virtio_port_read_complete, - NULL, logfile); + NULL); } if (!virtio_port) { - fprintf(logfile, - "Fatal error opening vdagent virtio channel\n"); + syslog(LOG_CRIT, "Fatal error opening vdagent virtio channel"); retval = 1; break; } @@ -721,7 +716,6 @@ void main_loop(void) update_active_session_connection(); } #endif - fflush(logfile); } } @@ -769,34 +763,22 @@ int main(int argc, char *argv[]) sigaction(SIGTERM, &act, NULL); sigaction(SIGQUIT, &act, NULL); - if (do_daemonize) { - logfile = fopen(logfilename, "a"); - if (!logfile) { - fprintf(stderr, "Error opening %s: %s\n", logfilename, - strerror(errno)); - logfile = stderr; - } - } else - logfile = stderr; + openlog("spice-vdagentd", do_daemonize ? 0 : LOG_PERROR, LOG_USER); /* Setup communication with vdagent process(es) */ server = udscs_create_server(VDAGENTD_SOCKET, agent_connect, agent_read_complete, agent_disconnect, vdagentd_messages, VDAGENTD_NO_MESSAGES, - debug? logfile:NULL, logfile); + debug); if (!server) { - fprintf(logfile, "Fatal could not create server socket %s\n", - VDAGENTD_SOCKET); - if (logfile != stderr) - fclose(logfile); + syslog(LOG_CRIT, "Fatal could not create server socket %s", + VDAGENTD_SOCKET); return 1; } if (chmod(VDAGENTD_SOCKET, 0666)) { - fprintf(logfile, "Fatal could not change permissions on %s: %s\n", - VDAGENTD_SOCKET, strerror(errno)); + syslog(LOG_CRIT, "Fatal could not change permissions on %s: %m", + VDAGENTD_SOCKET); udscs_destroy_server(server); - if (logfile != stderr) - fclose(logfile); return 1; } @@ -805,23 +787,19 @@ int main(int argc, char *argv[]) #ifdef WITH_STATIC_UINPUT uinput = vdagentd_uinput_create(uinput_device, 1024, 768, NULL, 0, - logfile, debug > 1); + debug > 1); if (!uinput) { udscs_destroy_server(server); - if (logfile != stderr) - fclose(logfile); return 1; } #endif #ifdef HAVE_SESSION_INFO - session_info = session_info_create(logfile, debug); + session_info = session_info_create(debug); if (!session_info) { - fprintf(logfile, "Fatal could not get session information\n"); + syslog(LOG_CRIT, "Fatal could not get session information"); vdagentd_uinput_destroy(&uinput); udscs_destroy_server(server); - if (logfile != stderr) - fclose(logfile); return 1; } #endif @@ -838,10 +816,8 @@ int main(int argc, char *argv[]) #endif udscs_destroy_server(server); if (unlink(VDAGENTD_SOCKET) != 0) - fprintf(logfile, "unlink %s: %s\n", VDAGENTD_SOCKET, strerror(errno)); - fprintf(logfile, "vdagentd quiting, returning status %d\n", retval); - if (logfile != stderr) - fclose(logfile); + syslog(LOG_ERR, "unlink %s: %s", VDAGENTD_SOCKET, strerror(errno)); + syslog(LOG_INFO, "vdagentd quiting, returning status %d", retval); if (do_daemonize) unlink(pidfilename); |