xref: /linux/tools/lib/api/fd/array.c (revision 473f6c8f437b049f8ec015d57cd59bb983b1d85c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2014, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4  */
5 #include "array.h"
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <poll.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 
13 void fdarray__init(struct fdarray *fda, int nr_autogrow)
14 {
15 	fda->entries	 = NULL;
16 	fda->priv	 = NULL;
17 	fda->nr		 = fda->nr_alloc = 0;
18 	fda->nr_autogrow = nr_autogrow;
19 }
20 
21 int fdarray__grow(struct fdarray *fda, int nr)
22 {
23 	struct priv *priv;
24 	int nr_alloc = fda->nr_alloc + nr;
25 	size_t psize = sizeof(fda->priv[0]) * nr_alloc;
26 	size_t size  = sizeof(struct pollfd) * nr_alloc;
27 	struct pollfd *entries = realloc(fda->entries, size);
28 
29 	if (entries == NULL)
30 		return -ENOMEM;
31 
32 	priv = realloc(fda->priv, psize);
33 	if (priv == NULL) {
34 		/* this will be freed by fdarray__exit() */
35 		fda->entries = entries;
36 		return -ENOMEM;
37 	}
38 
39 	memset(&entries[fda->nr_alloc], 0, sizeof(struct pollfd) * nr);
40 	memset(&priv[fda->nr_alloc], 0, sizeof(fda->priv[0]) * nr);
41 
42 	fda->nr_alloc = nr_alloc;
43 	fda->entries  = entries;
44 	fda->priv     = priv;
45 	return 0;
46 }
47 
48 struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow)
49 {
50 	struct fdarray *fda = calloc(1, sizeof(*fda));
51 
52 	if (fda != NULL) {
53 		if (fdarray__grow(fda, nr_alloc)) {
54 			fdarray__delete(fda);
55 			fda = NULL;
56 		} else {
57 			fda->nr_autogrow = nr_autogrow;
58 		}
59 	}
60 
61 	return fda;
62 }
63 
64 void fdarray__exit(struct fdarray *fda)
65 {
66 	free(fda->entries);
67 	free(fda->priv);
68 	fdarray__init(fda, 0);
69 }
70 
71 void fdarray__delete(struct fdarray *fda)
72 {
73 	fdarray__exit(fda);
74 	free(fda);
75 }
76 
77 int fdarray__add(struct fdarray *fda, int fd, short revents, enum fdarray_flags flags)
78 {
79 	int pos = fda->nr;
80 
81 	if (fda->nr == fda->nr_alloc &&
82 	    fdarray__grow(fda, fda->nr_autogrow) < 0)
83 		return -ENOMEM;
84 
85 	fda->entries[fda->nr].fd     = fd;
86 	fda->entries[fda->nr].events = revents;
87 	fda->priv[fda->nr].flags = flags;
88 	fda->nr++;
89 	return pos;
90 }
91 
92 int fdarray__dup_entry_from(struct fdarray *fda, int pos, struct fdarray *from)
93 {
94 	struct pollfd *entry;
95 	int npos;
96 
97 	if (pos >= from->nr)
98 		return -EINVAL;
99 
100 	entry = &from->entries[pos];
101 
102 	npos = fdarray__add(fda, entry->fd, entry->events, from->priv[pos].flags);
103 	if (npos >= 0)
104 		fda->priv[npos] = from->priv[pos];
105 
106 	return npos;
107 }
108 
109 int fdarray__filter(struct fdarray *fda, short revents,
110 		    void (*entry_destructor)(struct fdarray *fda, int fd, void *arg),
111 		    void *arg)
112 {
113 	int fd, nr = 0;
114 
115 	if (fda->nr == 0)
116 		return 0;
117 
118 	for (fd = 0; fd < fda->nr; ++fd) {
119 		if (fda->priv[fd].flags & fdarray_flag__nonfilterable)
120 			continue;
121 
122 		if (!fda->entries[fd].events)
123 			continue;
124 
125 		if (fda->entries[fd].revents & revents) {
126 			if (entry_destructor)
127 				entry_destructor(fda, fd, arg);
128 
129 			/*
130 			 * Set fd to -1 so poll() ignores this entry; otherwise
131 			 * POLLHUP/POLLERR are still reported for events=0 fds
132 			 * (POSIX: always checked), causing a poll storm.
133 			 */
134 			fda->entries[fd].fd = -1;
135 			fda->entries[fd].revents = fda->entries[fd].events = 0;
136 			continue;
137 		}
138 
139 		++nr;
140 	}
141 
142 	return nr;
143 }
144 
145 int fdarray__poll(struct fdarray *fda, int timeout)
146 {
147 	return poll(fda->entries, fda->nr, timeout);
148 }
149 
150 int fdarray__fprintf(struct fdarray *fda, FILE *fp)
151 {
152 	int fd, printed = fprintf(fp, "%d [ ", fda->nr);
153 
154 	for (fd = 0; fd < fda->nr; ++fd)
155 		printed += fprintf(fp, "%s%d", fd ? ", " : "", fda->entries[fd].fd);
156 
157 	return printed + fprintf(fp, " ]");
158 }
159