|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Mariusz Kozłowski |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation, version 2 |
| 7 | + * of the license. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this library; if not, write to the Free Software Foundation, |
| 16 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <stdio.h> |
| 20 | +#include <stdlib.h> |
| 21 | +#include <sys/types.h> |
| 22 | +#include <sys/stat.h> |
| 23 | +#include <fcntl.h> |
| 24 | +#include <unistd.h> |
| 25 | +#include <string.h> |
| 26 | +#include <signal.h> |
| 27 | +#include <assert.h> |
| 28 | + |
| 29 | +#define PFX "[test-fd] " |
| 30 | + |
| 31 | +#define FD_NUM 1000 |
| 32 | + |
| 33 | +static volatile sig_atomic_t signalled; |
| 34 | + |
| 35 | +static void sighandler(int num) |
| 36 | +{ |
| 37 | + signalled = num; |
| 38 | +} |
| 39 | + |
| 40 | +static void notify_ready(const char *pipe) |
| 41 | +{ |
| 42 | + int ret; |
| 43 | + int fd; |
| 44 | + char msg[64]; |
| 45 | + |
| 46 | + fd = open(pipe, O_WRONLY); |
| 47 | + assert(fd >= 0); |
| 48 | + |
| 49 | + snprintf(msg, sizeof(msg), PFX "pid %d ready\n", getpid()); |
| 50 | + ret = write(fd, msg, strlen(msg)); |
| 51 | + assert(ret == strlen(msg)); |
| 52 | + |
| 53 | + close(fd); |
| 54 | +} |
| 55 | + |
| 56 | +int main(int argc, char *argv[]) |
| 57 | +{ |
| 58 | + int ret; |
| 59 | + int flags[FD_NUM]; |
| 60 | + |
| 61 | + if (argc < 2) |
| 62 | + return 1; |
| 63 | + |
| 64 | + signal(SIGUSR1, sighandler); |
| 65 | + |
| 66 | + printf(PFX "pid %d\n", getpid()); |
| 67 | + |
| 68 | + for (int i = 0; i < FD_NUM - 3; i++) { |
| 69 | + ret = open("/dev/null", O_RDWR); |
| 70 | + if (ret == -1) { |
| 71 | + perror(PFX "open"); |
| 72 | + return 1; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + for (int fd = 0; fd < FD_NUM; fd++) { |
| 77 | + ret = fcntl(fd, F_GETFD); |
| 78 | + if (ret == -1) { |
| 79 | + perror(PFX "fcntl"); |
| 80 | + return 1; |
| 81 | + } |
| 82 | + |
| 83 | + flags[fd] = ret; |
| 84 | + } |
| 85 | + |
| 86 | + printf(PFX "waiting for SIGUSR1\n"); |
| 87 | + |
| 88 | + notify_ready(argv[1]); |
| 89 | + |
| 90 | + while (!signalled) |
| 91 | + usleep(10 * 1000); |
| 92 | + |
| 93 | + printf(PFX "signalled (%s)\n", strsignal(signalled)); |
| 94 | + |
| 95 | + /* test that the fds are still open and have the same flags */ |
| 96 | + |
| 97 | + for (int fd = 0; fd < FD_NUM; fd++) { |
| 98 | + ret = fcntl(fd, F_GETFD); |
| 99 | + |
| 100 | + if (ret != flags[fd]) { |
| 101 | + printf(PFX "not ok, %d != %d for fd %d\n", ret, flags[fd], fd); |
| 102 | + return 1; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + for (int fd = 3; fd < FD_NUM; fd++) |
| 107 | + close(fd); |
| 108 | + |
| 109 | + printf(PFX "ok\n"); |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
0 commit comments