diff --git a/bin/tncd.c b/bin/tncd.c index 61deb77..a2a20d7 100644 --- a/bin/tncd.c +++ b/bin/tncd.c @@ -32,6 +32,37 @@ static void usage(int argc, char **argv, const char *message, ...) { exit(EX_USAGE); } +static int pty_close(patty_client *client, int fd) { + enum patty_client_call call = PATTY_CLIENT_CLOSE; + + patty_client_close_request request = { + .fd = fd + }; + + patty_client_close_response response; + + if (patty_client_write(client, &call, sizeof(call)) < 0) { + goto error_io; + } + + if (patty_client_write(client, &request, sizeof(request)) < 0) { + goto error_io; + } + + if (patty_client_read(client, &response, sizeof(response)) < 0) { + goto error_io; + } + + if (response.ret < 0) { + errno = response.eno; + } + + return response.ret; + +error_io: + return -1; +} + static int pty_promisc(patty_client *client, const char *ifname, char *pty, @@ -112,13 +143,13 @@ static int pty_promisc(patty_client *client, strncpy(pty, socket_response.path, len); - return 0; + return socket_response.fd; error_client_setsockopt: error_client_read_setsockopt_response: error_client_write_ifreq: error_client_write_setsockopt_request: - /* TODO: Close socket on error */ + (void)pty_close(client, socket_response.fd); error_client_socket: error_client_read_socket_response: @@ -132,6 +163,8 @@ int main(int argc, char **argv) { }; patty_client *client; + + int fd; char pty[256]; if (getopt_long(argc, argv, "", opts, NULL) >= 0) { @@ -149,22 +182,38 @@ int main(int argc, char **argv) { goto error_client_new; } - if (pty_promisc(client, argv[2], pty, sizeof(pty)) < 0) { + if ((fd = pty_promisc(client, argv[2], pty, sizeof(pty))) < 0) { fprintf(stderr, "%s: %s: %s: %s\n", argv[0], "pty_promisc()", argv[2], strerror(errno)); goto error_pty_promisc; } - printf("%s\n", pty); + printf("pty %s\n", pty); while (1) { - sleep(86400); + int pong; + + if ((pong = patty_client_ping(client)) < 0) { + fprintf(stderr, "%s: %s: %s\n", + argv[0], "patty_client_ping()", strerror(errno)); + + goto error_client_ping; + } else if (pong == 0) { + break; + } + + sleep(2); } patty_client_destroy(client); +error_client_ping: + (void)pty_close(client, fd); + error_pty_promisc: + patty_client_destroy(client); + error_client_new: return 1; }