diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-12-09 13:15:10 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-12-09 13:15:10 +0100 |
commit | 7a994c9631c590d6a73c842fa5d2d3567b4771dd (patch) | |
tree | 228ade01f99ba217151a0ed20f29c5f3a5028619 /src/mds-vt.c | |
parent | mds-kbdc: compile-layout: macro_call: fix bug: do not duplicate the arguments if there are none (diff) | |
parent | report an error, rather than causing failure the caller but not for the called function (diff) | |
download | mds-7a994c9631c590d6a73c842fa5d2d3567b4771dd.tar.gz mds-7a994c9631c590d6a73c842fa5d2d3567b4771dd.tar.bz2 mds-7a994c9631c590d6a73c842fa5d2d3567b4771dd.tar.xz |
merge track-errors and resolve conflict
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | src/mds-vt.c | 158 |
1 files changed, 81 insertions, 77 deletions
diff --git a/src/mds-vt.c b/src/mds-vt.c index c0a13ef..d761658 100644 --- a/src/mds-vt.c +++ b/src/mds-vt.c @@ -154,21 +154,20 @@ int __attribute__((const)) preinitialise_server(void) static int write_vt_file(void) { char buf[(sizeof(int) + sizeof(struct stat)) / sizeof(char)]; - int fd, r, old_errno; int* intbuf = (int*)buf; + int fd = -1, saved_errno; *intbuf = display_vt; *(struct stat*)(buf + sizeof(int) / sizeof(char)) = old_vt_stat; - fd = open(vtfile_path, O_WRONLY | O_CREAT); - if (fd < 0) - return -1; - - r = full_write(fd, buf, sizeof(buf)); - old_errno = errno; - close(fd); - errno = old_errno; - return r; + fail_if (open(vtfile_path, O_WRONLY | O_CREAT), fd < 0); + fail_if (full_write(fd, buf, sizeof(buf))); + return 0; + fail: + saved_errno = errno; + if (fd >= 0) + close(fd); + return errno = saved_errno, -1; } @@ -183,24 +182,20 @@ static int read_vt_file(void) size_t len; int fd; - fd = open(vtfile_path, O_RDONLY); - if (fd < 0) - return -1; - - buf = full_read(fd, &len); - if (buf == NULL) - return -1; + fail_if (fd = open(vtfile_path, O_RDONLY), fd < 0); + fail_if (buf = full_read(fd, &len), buf == NULL); if (len != sizeof(int) + sizeof(struct stat)) { eprint("VT file is of wrong size."); - errno = 0; - return -1; + return errno = 0, -1; } display_vt = *(int*)buf; old_vt_stat = *(struct stat*)(buf + sizeof(int) / sizeof(char)); return 0; + fail: + return -1; } @@ -215,6 +210,7 @@ int initialise_server(void) struct vt_mode mode; char* display_env; int primary_socket_fd; + int stage = 0; const char* const message = "Command: intercept\n" "Message ID: 0\n" @@ -231,8 +227,7 @@ int initialise_server(void) "Command: switching-vt\n"; primary_socket_fd = socket_fd; - if (connect_to_display()) - return 1; + fail_if (connect_to_display()); secondary_socket_fd = socket_fd; socket_fd = primary_socket_fd; @@ -243,12 +238,12 @@ int initialise_server(void) memset(vtfile_path, 0, sizeof(vtfile_path)); xsnprintf(vtfile_path, "%s/%s.vt", MDS_RUNTIME_ROOT_DIRECTORY, display_env + 1); + stage = 1; if (is_respawn == 0) { display_vt = select_vt(); - if (display_vt < 0) - goto pfail; + fail_if (display_vt < 0); display_tty_fd = vt_open(display_vt, &old_vt_stat); fail_if (write_vt_file() < 0); fail_if (vt_set_active(display_vt) < 0); @@ -260,12 +255,10 @@ int initialise_server(void) fail_if (vt_is_active < 0); } - if (full_send(secondary_socket_fd, secondary_message, strlen(secondary_message))) - return 1; - if (full_send(socket_fd, message, strlen(message))) - return 1; + fail_if (full_send(secondary_socket_fd, secondary_message, strlen(secondary_message))); + fail_if (full_send(socket_fd, message, strlen(message))); fail_if (server_initialised() < 0); - fail_if (mds_message_initialise(&received)); + fail_if (mds_message_initialise(&received)); stage = 2; fail_if (xsigaction(SIGRTMIN + 2, received_switch_vt) < 0); fail_if (xsigaction(SIGRTMIN + 3, received_switch_vt) < 0); @@ -278,12 +271,14 @@ int initialise_server(void) no_display: eprint("no display has been set, how did this happen."); return 1; - pfail: + fail: xperror(*argv); - unlink(vtfile_path); + if (stage >= 1) + unlink(vtfile_path); if (display_tty_fd >= 0) vt_close(display_tty_fd, &old_vt_stat); - mds_message_destroy(&received); + if (stage >= 2) + mds_message_destroy(&received); return 1; } @@ -302,14 +297,15 @@ int postinitialise_server(void) if (reconnect_to_display()) { mds_message_destroy(&received); - return 1; + fail_if (1); } connected = 1; - if ((errno = pthread_create(&secondary_thread, NULL, secondary_loop, NULL))) - return 1; + fail_if ((errno = pthread_create(&secondary_thread, NULL, secondary_loop, NULL))); return 0; + fail: + return 1; } @@ -430,19 +426,18 @@ int master_loop(void) if (r == -2) { eprint("corrupt message received, aborting."); - goto fail; + goto done; } else if (errno == EINTR) continue; - else if (errno != ECONNRESET) - goto pfail; + else + fail_if (errno != ECONNRESET); eprint("lost primary connection to server."); mds_message_destroy(&received); mds_message_initialise(&received); connected = 0; - if (reconnect_to_display()) - goto fail; + fail_if (reconnect_to_display()); connected = 1; } @@ -452,10 +447,10 @@ int master_loop(void) if (unlink(vtfile_path) < 0) xperror(*argv); vt_close(display_tty_fd, &old_vt_stat); - goto fail; - pfail: - xperror(*argv); + goto done; fail: + xperror(*argv); + done: rc |= secondary_thread_failed; if (rc || !reexecing) mds_message_destroy(&received); @@ -490,24 +485,23 @@ void* secondary_loop(void* data) if (r == -2) { eprint("corrupt message received, aborting."); - goto fail; + secondary_thread_failed = 1; + goto done; } else if (errno == EINTR) continue; - else if (errno != ECONNRESET) - goto pfail; + else + fail_if (errno != ECONNRESET); eprint("lost secondary connection to server."); mds_message_destroy(&secondary_received); mds_message_initialise(&secondary_received); - if (reconnect_fd_to_display(&secondary_socket_fd) < 0) - goto fail; + fail_if (reconnect_fd_to_display(&secondary_socket_fd) < 0); } goto done; - pfail: - xperror(*argv); fail: + xperror(*argv); secondary_thread_failed = 1; done: secondary_thread_started = 0; @@ -538,7 +532,10 @@ int switch_vt(int leave_foreground) message_id = message_id == UINT32_MAX ? 0 : (message_id + 1); - return -!!full_send(socket_fd, buf, strlen(buf)); + fail_if (full_send(socket_fd, buf, strlen(buf))); + return 0; + fail: + return -1; } @@ -641,7 +638,10 @@ int handle_get_vt(const char* client, const char* message) message_id = message_id == UINT32_MAX ? 0 : (message_id + 1); r = full_send(socket_fd, buf, strlen(buf)); - return ((active < 0) || r) ? -1 : 0; + fail_if ((active < 0) || r); + return 0; + fail: + return -1; } @@ -685,7 +685,10 @@ int handle_configure_vt(const char* client, const char* message, const char* gra message_id = message_id == UINT32_MAX ? 0 : (message_id + 1); - return -!!full_send(socket_fd, buf, strlen(buf)); + fail_if (full_send(socket_fd, buf, strlen(buf))); + return 0; + fail: + return -1; } @@ -738,15 +741,15 @@ int full_send(int socket, const char* message, size_t length) eprint("Sent more of a message than exists in the message, aborting."); return -1; } - else if ((sent < length) && (errno != EINTR)) - { - xperror(*argv); - return -1; - } + else + fail_if ((sent < length) && (errno != EINTR)); message += sent; length -= sent; } return 0; + fail: + xperror(*argv); + return -1; } @@ -771,7 +774,7 @@ int select_vt(void) if (r < 0) { eprint("the environment variable XDG_VTNR contains an invalid value."); - return errno = 0, -1; + fail_if (errno = 0, 1); } } else @@ -781,11 +784,13 @@ int select_vt(void) if (rc == 0) { eprint("out of available virtual terminals, I am stymied."); - return errno = 0, -1; + fail_if (errno = 0, 1); } } return rc; + fail: + return -1; } @@ -797,10 +802,10 @@ int select_vt(void) int vt_get_next_available(void) { int next_vt = -1; - int r = ioctl(STDIN_FILENO, VT_OPENQRY, &next_vt); - if (r < 0) - return r; + fail_if (ioctl(STDIN_FILENO, VT_OPENQRY, &next_vt) < 0); return ((next_vt < 0) || (MAX_NR_CONSOLES < next_vt)) ? 0 : next_vt; + fail: + return -1; } @@ -812,9 +817,10 @@ int vt_get_next_available(void) int vt_get_active(void) { struct vt_stat state; - if (ioctl(STDIN_FILENO, VT_GETSTATE, &state) < 0) - return -1; + fail_if (ioctl(STDIN_FILENO, VT_GETSTATE, &state) < 0); return state.v_active; + fail: + return -1; } @@ -826,13 +832,13 @@ int vt_get_active(void) */ int vt_set_active(int vt) { - if (ioctl(STDIN_FILENO, VT_ACTIVATE, vt) < 0) - return -1; - + fail_if (ioctl(STDIN_FILENO, VT_ACTIVATE, vt) < 0); if (ioctl(STDIN_FILENO, VT_WAITACTIVE, vt) < 0) xperror(*argv); return 0; + fail: + return -1; } @@ -848,18 +854,16 @@ int vt_open(int vt, struct stat* restrict old_stat) char vtpath[64]; /* Should be small enought and large enought for any lunatic alternative to /dev/ttyNNN, if not you will need to apply a patch (or fix your system.) */ - int fd; + int fd = -1, saved_errno; sprintf(vtpath, VT_PATH_PATTERN, vt); - fd = open(vtpath, O_RDWR); - if (fd < 0) - return -1; - if ((fstat(fd, old_stat) < 0) || - (fchown(fd, getuid(), getgid()) < 0)) - { - close(fd); - return -1; - } + fail_if (fd = open(vtpath, O_RDWR), fd < 0); + fail_if ((fstat(fd, old_stat) < 0) || (fchown(fd, getuid(), getgid()) < 0)); return fd; + fail: + saved_errno = errno; + if (fd >= 0) + close(fd); + return errno = saved_errno, -1; } |