Improve bin/tncd.c
Changes: * Ensure TNC PTYs are closed on error * Instead of waiting indefinitely, use patty_client_ping() to poll the status of the patty daemon to exit in a timely manner
This commit is contained in:
parent
e053d6cdbc
commit
62f4f2ca7c
1 changed files with 54 additions and 5 deletions
59
bin/tncd.c
59
bin/tncd.c
|
@ -32,6 +32,37 @@ static void usage(int argc, char **argv, const char *message, ...) {
|
||||||
exit(EX_USAGE);
|
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,
|
static int pty_promisc(patty_client *client,
|
||||||
const char *ifname,
|
const char *ifname,
|
||||||
char *pty,
|
char *pty,
|
||||||
|
@ -112,13 +143,13 @@ static int pty_promisc(patty_client *client,
|
||||||
|
|
||||||
strncpy(pty, socket_response.path, len);
|
strncpy(pty, socket_response.path, len);
|
||||||
|
|
||||||
return 0;
|
return socket_response.fd;
|
||||||
|
|
||||||
error_client_setsockopt:
|
error_client_setsockopt:
|
||||||
error_client_read_setsockopt_response:
|
error_client_read_setsockopt_response:
|
||||||
error_client_write_ifreq:
|
error_client_write_ifreq:
|
||||||
error_client_write_setsockopt_request:
|
error_client_write_setsockopt_request:
|
||||||
/* TODO: Close socket on error */
|
(void)pty_close(client, socket_response.fd);
|
||||||
|
|
||||||
error_client_socket:
|
error_client_socket:
|
||||||
error_client_read_socket_response:
|
error_client_read_socket_response:
|
||||||
|
@ -132,6 +163,8 @@ int main(int argc, char **argv) {
|
||||||
};
|
};
|
||||||
|
|
||||||
patty_client *client;
|
patty_client *client;
|
||||||
|
|
||||||
|
int fd;
|
||||||
char pty[256];
|
char pty[256];
|
||||||
|
|
||||||
if (getopt_long(argc, argv, "", opts, NULL) >= 0) {
|
if (getopt_long(argc, argv, "", opts, NULL) >= 0) {
|
||||||
|
@ -149,22 +182,38 @@ int main(int argc, char **argv) {
|
||||||
goto error_client_new;
|
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",
|
fprintf(stderr, "%s: %s: %s: %s\n",
|
||||||
argv[0], "pty_promisc()", argv[2], strerror(errno));
|
argv[0], "pty_promisc()", argv[2], strerror(errno));
|
||||||
|
|
||||||
goto error_pty_promisc;
|
goto error_pty_promisc;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n", pty);
|
printf("pty %s\n", pty);
|
||||||
|
|
||||||
while (1) {
|
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);
|
patty_client_destroy(client);
|
||||||
|
|
||||||
|
error_client_ping:
|
||||||
|
(void)pty_close(client, fd);
|
||||||
|
|
||||||
error_pty_promisc:
|
error_pty_promisc:
|
||||||
|
patty_client_destroy(client);
|
||||||
|
|
||||||
error_client_new:
|
error_client_new:
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue