commit eb229cf15844b7edcebfcd2430d3d340f2c60bf8
parent 9063f86505b1a895dbe9553941bab88146f036fc
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Mon, 13 Jan 2025 17:33:06 +0000
src/poke.c: add timeout
Diffstat:
M | src/poke.c | | | 56 | +++++++++++++++++++++++++++++++++++++++----------------- |
1 file changed, 39 insertions(+), 17 deletions(-)
diff --git a/src/poke.c b/src/poke.c
@@ -5,33 +5,55 @@
#include <u.h>
#include <libc.h>
+ulong timeout = 3000;
+int timedout;
+char *tstr, **addr;
+
+int timeoutf(void *, char *);
+
void
-usage(void)
-{
- fprint(2, "usage: %s addr1 [addr2 addr3 ...]\n", argv0);
+usage(void) {
+ fprint(2, "usage: %s [-t timeout] addr1 [addr2 addr3 ...]\n", argv0);
exits("usage");
};
void
-main(int argc, char **argv)
-{
+poke(char *addr) {
+ int d;
+ timedout = 0;
+ alarm(timeout);
+ d = dial(addr, nil, nil, nil);
+ if(d > 0){
+ close(d);
+ print("%s open\n", addr);
+ } else if (timedout == 0) print("%s closed %r\n", addr);
+ else print("%s timeout\n", addr);
+}
+
+void
+main(int argc, char **argv){
ARGBEGIN{
+ case 't':
+ tstr = EARGF(usage());
+ if(tstr != nil) timeout = strtoul(tstr, nil, 10);
+ break;
default:
usage();
}ARGEND
- if (argc == 0) usage();
-
- char **addr = malloc(sizeof(char *) * argc);
+ if(argc == 0) usage();
+ atnotify(timeoutf, 1);
+ addr = malloc(sizeof(char *) * argc);
int i;
- for (i = 0; i < argc; i++) {
+ for(i = 0; i < argc; i++){
addr[i] = netmkaddr(argv[i], "tcp", "echo");
- if (addr[i] == nil) sysfatal("failed at %s, %r", argv[i]);
- }
- for (i = 0; i < argc; i++) {
- int d = dial(addr[i], nil, nil, nil);
- if (d > 0) {
- close(d);
- print("%s open\n", addr[i]);
- } else print("%s closed\n", addr[i]);
+ if(addr[i] == nil) sysfatal("failed at %s, %r", argv[i]);
}
+ for(i = 0; i < argc; i++) poke(addr[i]);
+}
+
+int
+timeoutf(void *, char *note){
+ if(strcmp(note, "alarm") != 0) return 0;
+ timedout = 1;
+ return 1;
}