/src/tmux/compat/getdtablecount.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com> |
3 | | * |
4 | | * Permission to use, copy, modify, and distribute this software for any |
5 | | * purpose with or without fee is hereby granted, provided that the above |
6 | | * copyright notice and this permission notice appear in all copies. |
7 | | * |
8 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
10 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
11 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
12 | | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
13 | | * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
14 | | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
15 | | */ |
16 | | |
17 | | #include <sys/types.h> |
18 | | |
19 | | #include <glob.h> |
20 | | #include <unistd.h> |
21 | | #if defined(HAVE_LIBPROC_H) |
22 | | #include <libproc.h> |
23 | | #endif |
24 | | |
25 | | #include "compat.h" |
26 | | |
27 | | void fatal(const char *, ...); |
28 | | void fatalx(const char *, ...); |
29 | | |
30 | | #if defined(HAVE_LIBPROC_H) && defined(HAVE_PROC_PIDINFO) |
31 | | int |
32 | | getdtablecount(void) |
33 | | { |
34 | | int sz; |
35 | | pid_t pid = getpid(); |
36 | | |
37 | | sz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); |
38 | | if (sz == -1) |
39 | | return (0); |
40 | | return (sz / PROC_PIDLISTFD_SIZE); |
41 | | } |
42 | | #elif defined(HAVE_PROC_PID) |
43 | | int |
44 | | getdtablecount(void) |
45 | 0 | { |
46 | 0 | char path[PATH_MAX]; |
47 | 0 | glob_t g; |
48 | 0 | int n = 0; |
49 | |
|
50 | 0 | if (snprintf(path, sizeof path, "/proc/%ld/fd/*", (long)getpid()) < 0) |
51 | 0 | fatal("snprintf overflow"); |
52 | 0 | if (glob(path, 0, NULL, &g) == 0) |
53 | 0 | n = g.gl_pathc; |
54 | 0 | globfree(&g); |
55 | 0 | return (n); |
56 | 0 | } |
57 | | #else |
58 | | int |
59 | | getdtablecount(void) |
60 | | { |
61 | | return (0); |
62 | | } |
63 | | #endif |