1 /* Copyright libuv project contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22
23 #include "os390-syscalls.h"
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <search.h>
27 #include <termios.h>
28 #include <sys/msg.h>
29
30 static QUEUE global_epoll_queue;
31 static uv_mutex_t global_epoll_lock;
32 static uv_once_t once = UV_ONCE_INIT;
33
scandir(const char * maindir,struct dirent *** namelist,int (* filter)(const struct dirent *),int (* compar)(const struct dirent **,const struct dirent **))34 int scandir(const char* maindir, struct dirent*** namelist,
35 int (*filter)(const struct dirent*),
36 int (*compar)(const struct dirent**,
37 const struct dirent **)) {
38 struct dirent** nl;
39 struct dirent** nl_copy;
40 struct dirent* dirent;
41 unsigned count;
42 size_t allocated;
43 DIR* mdir;
44
45 nl = NULL;
46 count = 0;
47 allocated = 0;
48 mdir = opendir(maindir);
49 if (!mdir)
50 return -1;
51
52 for (;;) {
53 dirent = readdir(mdir);
54 if (!dirent)
55 break;
56 if (!filter || filter(dirent)) {
57 struct dirent* copy;
58 copy = uv__malloc(sizeof(*copy));
59 if (!copy)
60 goto error;
61 memcpy(copy, dirent, sizeof(*copy));
62
63 nl_copy = uv__realloc(nl, sizeof(*copy) * (count + 1));
64 if (nl_copy == NULL) {
65 uv__free(copy);
66 goto error;
67 }
68
69 nl = nl_copy;
70 nl[count++] = copy;
71 }
72 }
73
74 qsort(nl, count, sizeof(struct dirent *),
75 (int (*)(const void *, const void *)) compar);
76
77 closedir(mdir);
78
79 *namelist = nl;
80 return count;
81
82 error:
83 while (count > 0) {
84 dirent = nl[--count];
85 uv__free(dirent);
86 }
87 uv__free(nl);
88 closedir(mdir);
89 errno = ENOMEM;
90 return -1;
91 }
92
93
next_power_of_two(unsigned int val)94 static unsigned int next_power_of_two(unsigned int val) {
95 val -= 1;
96 val |= val >> 1;
97 val |= val >> 2;
98 val |= val >> 4;
99 val |= val >> 8;
100 val |= val >> 16;
101 val += 1;
102 return val;
103 }
104
105
maybe_resize(uv__os390_epoll * lst,unsigned int len)106 static void maybe_resize(uv__os390_epoll* lst, unsigned int len) {
107 unsigned int newsize;
108 unsigned int i;
109 struct pollfd* newlst;
110 struct pollfd event;
111
112 if (len <= lst->size)
113 return;
114
115 if (lst->size == 0)
116 event.fd = -1;
117 else {
118 /* Extract the message queue at the end. */
119 event = lst->items[lst->size - 1];
120 lst->items[lst->size - 1].fd = -1;
121 }
122
123 newsize = next_power_of_two(len);
124 newlst = uv__reallocf(lst->items, newsize * sizeof(lst->items[0]));
125
126 if (newlst == NULL)
127 abort();
128 for (i = lst->size; i < newsize; ++i)
129 newlst[i].fd = -1;
130
131 /* Restore the message queue at the end */
132 newlst[newsize - 1] = event;
133
134 lst->items = newlst;
135 lst->size = newsize;
136 }
137
138
init_message_queue(uv__os390_epoll * lst)139 static void init_message_queue(uv__os390_epoll* lst) {
140 struct {
141 long int header;
142 char body;
143 } msg;
144
145 /* initialize message queue */
146 lst->msg_queue = msgget(IPC_PRIVATE, 0600 | IPC_CREAT);
147 if (lst->msg_queue == -1)
148 abort();
149
150 /*
151 On z/OS, the message queue will be affiliated with the process only
152 when a send is performed on it. Once this is done, the system
153 can be queried for all message queues belonging to our process id.
154 */
155 msg.header = 1;
156 if (msgsnd(lst->msg_queue, &msg, sizeof(msg.body), 0) != 0)
157 abort();
158
159 /* Clean up the dummy message sent above */
160 if (msgrcv(lst->msg_queue, &msg, sizeof(msg.body), 0, 0) != sizeof(msg.body))
161 abort();
162 }
163
164
before_fork(void)165 static void before_fork(void) {
166 uv_mutex_lock(&global_epoll_lock);
167 }
168
169
after_fork(void)170 static void after_fork(void) {
171 uv_mutex_unlock(&global_epoll_lock);
172 }
173
174
child_fork(void)175 static void child_fork(void) {
176 QUEUE* q;
177 uv_once_t child_once = UV_ONCE_INIT;
178
179 /* reset once */
180 memcpy(&once, &child_once, sizeof(child_once));
181
182 /* reset epoll list */
183 while (!QUEUE_EMPTY(&global_epoll_queue)) {
184 uv__os390_epoll* lst;
185 q = QUEUE_HEAD(&global_epoll_queue);
186 QUEUE_REMOVE(q);
187 lst = QUEUE_DATA(q, uv__os390_epoll, member);
188 uv__free(lst->items);
189 lst->items = NULL;
190 lst->size = 0;
191 }
192
193 uv_mutex_unlock(&global_epoll_lock);
194 uv_mutex_destroy(&global_epoll_lock);
195 }
196
197
epoll_init(void)198 static void epoll_init(void) {
199 QUEUE_INIT(&global_epoll_queue);
200 if (uv_mutex_init(&global_epoll_lock))
201 abort();
202
203 if (pthread_atfork(&before_fork, &after_fork, &child_fork))
204 abort();
205 }
206
207
epoll_create1(int flags)208 uv__os390_epoll* epoll_create1(int flags) {
209 uv__os390_epoll* lst;
210
211 lst = uv__malloc(sizeof(*lst));
212 if (lst != NULL) {
213 /* initialize list */
214 lst->size = 0;
215 lst->items = NULL;
216 init_message_queue(lst);
217 maybe_resize(lst, 1);
218 lst->items[lst->size - 1].fd = lst->msg_queue;
219 lst->items[lst->size - 1].events = POLLIN;
220 lst->items[lst->size - 1].revents = 0;
221 uv_once(&once, epoll_init);
222 uv_mutex_lock(&global_epoll_lock);
223 QUEUE_INSERT_TAIL(&global_epoll_queue, &lst->member);
224 uv_mutex_unlock(&global_epoll_lock);
225 }
226
227 return lst;
228 }
229
230
epoll_ctl(uv__os390_epoll * lst,int op,int fd,struct epoll_event * event)231 int epoll_ctl(uv__os390_epoll* lst,
232 int op,
233 int fd,
234 struct epoll_event *event) {
235 uv_mutex_lock(&global_epoll_lock);
236
237 if (op == EPOLL_CTL_DEL) {
238 if (fd >= lst->size || lst->items[fd].fd == -1) {
239 uv_mutex_unlock(&global_epoll_lock);
240 errno = ENOENT;
241 return -1;
242 }
243 lst->items[fd].fd = -1;
244 } else if (op == EPOLL_CTL_ADD) {
245
246 /* Resizing to 'fd + 1' would expand the list to contain at least
247 * 'fd'. But we need to guarantee that the last index on the list
248 * is reserved for the message queue. So specify 'fd + 2' instead.
249 */
250 maybe_resize(lst, fd + 2);
251 if (lst->items[fd].fd != -1) {
252 uv_mutex_unlock(&global_epoll_lock);
253 errno = EEXIST;
254 return -1;
255 }
256 lst->items[fd].fd = fd;
257 lst->items[fd].events = event->events;
258 lst->items[fd].revents = 0;
259 } else if (op == EPOLL_CTL_MOD) {
260 if (fd >= lst->size - 1 || lst->items[fd].fd == -1) {
261 uv_mutex_unlock(&global_epoll_lock);
262 errno = ENOENT;
263 return -1;
264 }
265 lst->items[fd].events = event->events;
266 lst->items[fd].revents = 0;
267 } else
268 abort();
269
270 uv_mutex_unlock(&global_epoll_lock);
271 return 0;
272 }
273
274 #define EP_MAX_PFDS (ULONG_MAX / sizeof(struct pollfd))
275 #define EP_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
276
epoll_wait(uv__os390_epoll * lst,struct epoll_event * events,int maxevents,int timeout)277 int epoll_wait(uv__os390_epoll* lst, struct epoll_event* events,
278 int maxevents, int timeout) {
279 nmsgsfds_t size;
280 struct pollfd* pfds;
281 int pollret;
282 int reventcount;
283 int nevents;
284 struct pollfd msg_fd;
285 int i;
286
287 if (!lst || !lst->items || !events) {
288 errno = EFAULT;
289 return -1;
290 }
291
292 if (lst->size > EP_MAX_PFDS) {
293 errno = EINVAL;
294 return -1;
295 }
296
297 if (maxevents <= 0 || maxevents > EP_MAX_EVENTS) {
298 errno = EINVAL;
299 return -1;
300 }
301
302 if (lst->size > 0)
303 _SET_FDS_MSGS(size, 1, lst->size - 1);
304 else
305 _SET_FDS_MSGS(size, 0, 0);
306 pfds = lst->items;
307 pollret = poll(pfds, size, timeout);
308 if (pollret <= 0)
309 return pollret;
310
311 assert(lst->size > 0);
312
313 pollret = _NFDS(pollret) + _NMSGS(pollret);
314
315 reventcount = 0;
316 nevents = 0;
317 msg_fd = pfds[lst->size - 1];
318 for (i = 0;
319 i < lst->size && i < maxevents && reventcount < pollret; ++i) {
320 struct epoll_event ev;
321 struct pollfd* pfd;
322
323 pfd = &pfds[i];
324 if (pfd->fd == -1 || pfd->revents == 0)
325 continue;
326
327 ev.fd = pfd->fd;
328 ev.events = pfd->revents;
329 ev.is_msg = 0;
330 if (pfd->revents & POLLIN && pfd->revents & POLLOUT)
331 reventcount += 2;
332 else if (pfd->revents & (POLLIN | POLLOUT))
333 ++reventcount;
334
335 pfd->revents = 0;
336 events[nevents++] = ev;
337 }
338
339 if (msg_fd.revents != 0 && msg_fd.fd != -1)
340 if (i == lst->size)
341 events[nevents - 1].is_msg = 1;
342
343 return nevents;
344 }
345
346
epoll_file_close(int fd)347 int epoll_file_close(int fd) {
348 QUEUE* q;
349
350 uv_once(&once, epoll_init);
351 uv_mutex_lock(&global_epoll_lock);
352 QUEUE_FOREACH(q, &global_epoll_queue) {
353 uv__os390_epoll* lst;
354
355 lst = QUEUE_DATA(q, uv__os390_epoll, member);
356 if (fd < lst->size && lst->items != NULL && lst->items[fd].fd != -1)
357 lst->items[fd].fd = -1;
358 }
359
360 uv_mutex_unlock(&global_epoll_lock);
361 return 0;
362 }
363
epoll_queue_close(uv__os390_epoll * lst)364 void epoll_queue_close(uv__os390_epoll* lst) {
365 /* Remove epoll instance from global queue */
366 uv_mutex_lock(&global_epoll_lock);
367 QUEUE_REMOVE(&lst->member);
368 uv_mutex_unlock(&global_epoll_lock);
369
370 /* Free resources */
371 msgctl(lst->msg_queue, IPC_RMID, NULL);
372 lst->msg_queue = -1;
373 uv__free(lst->items);
374 lst->items = NULL;
375 }
376
377
mkdtemp(char * path)378 char* mkdtemp(char* path) {
379 static const char* tempchars =
380 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
381 static const size_t num_chars = 62;
382 static const size_t num_x = 6;
383 char *ep, *cp;
384 unsigned int tries, i;
385 size_t len;
386 uint64_t v;
387 int fd;
388 int retval;
389 int saved_errno;
390
391 len = strlen(path);
392 ep = path + len;
393 if (len < num_x || strncmp(ep - num_x, "XXXXXX", num_x)) {
394 errno = EINVAL;
395 return NULL;
396 }
397
398 fd = open("/dev/urandom", O_RDONLY);
399 if (fd == -1)
400 return NULL;
401
402 tries = TMP_MAX;
403 retval = -1;
404 do {
405 if (read(fd, &v, sizeof(v)) != sizeof(v))
406 break;
407
408 cp = ep - num_x;
409 for (i = 0; i < num_x; i++) {
410 *cp++ = tempchars[v % num_chars];
411 v /= num_chars;
412 }
413
414 if (mkdir(path, S_IRWXU) == 0) {
415 retval = 0;
416 break;
417 }
418 else if (errno != EEXIST)
419 break;
420 } while (--tries);
421
422 saved_errno = errno;
423 uv__close(fd);
424 if (tries == 0) {
425 errno = EEXIST;
426 return NULL;
427 }
428
429 if (retval == -1) {
430 errno = saved_errno;
431 return NULL;
432 }
433
434 return path;
435 }
436
437
os390_readlink(const char * path,char * buf,size_t len)438 ssize_t os390_readlink(const char* path, char* buf, size_t len) {
439 ssize_t rlen;
440 ssize_t vlen;
441 ssize_t plen;
442 char* delimiter;
443 char old_delim;
444 char* tmpbuf;
445 char realpathstr[PATH_MAX + 1];
446
447 tmpbuf = uv__malloc(len + 1);
448 if (tmpbuf == NULL) {
449 errno = ENOMEM;
450 return -1;
451 }
452
453 rlen = readlink(path, tmpbuf, len);
454 if (rlen < 0) {
455 uv__free(tmpbuf);
456 return rlen;
457 }
458
459 if (rlen < 3 || strncmp("/$", tmpbuf, 2) != 0) {
460 /* Straightforward readlink. */
461 memcpy(buf, tmpbuf, rlen);
462 uv__free(tmpbuf);
463 return rlen;
464 }
465
466 /*
467 * There is a parmlib variable at the beginning
468 * which needs interpretation.
469 */
470 tmpbuf[rlen] = '\0';
471 delimiter = strchr(tmpbuf + 2, '/');
472 if (delimiter == NULL)
473 /* No slash at the end */
474 delimiter = strchr(tmpbuf + 2, '\0');
475
476 /* Read real path of the variable. */
477 old_delim = *delimiter;
478 *delimiter = '\0';
479 if (realpath(tmpbuf, realpathstr) == NULL) {
480 uv__free(tmpbuf);
481 return -1;
482 }
483
484 /* realpathstr is not guaranteed to end with null byte.*/
485 realpathstr[PATH_MAX] = '\0';
486
487 /* Reset the delimiter and fill up the buffer. */
488 *delimiter = old_delim;
489 plen = strlen(delimiter);
490 vlen = strlen(realpathstr);
491 rlen = plen + vlen;
492 if (rlen > len) {
493 uv__free(tmpbuf);
494 errno = ENAMETOOLONG;
495 return -1;
496 }
497 memcpy(buf, realpathstr, vlen);
498 memcpy(buf + vlen, delimiter, plen);
499
500 /* Done using temporary buffer. */
501 uv__free(tmpbuf);
502
503 return rlen;
504 }
505
506
sem_init(UV_PLATFORM_SEM_T * semid,int pshared,unsigned int value)507 int sem_init(UV_PLATFORM_SEM_T* semid, int pshared, unsigned int value) {
508 UNREACHABLE();
509 }
510
511
sem_destroy(UV_PLATFORM_SEM_T * semid)512 int sem_destroy(UV_PLATFORM_SEM_T* semid) {
513 UNREACHABLE();
514 }
515
516
sem_post(UV_PLATFORM_SEM_T * semid)517 int sem_post(UV_PLATFORM_SEM_T* semid) {
518 UNREACHABLE();
519 }
520
521
sem_trywait(UV_PLATFORM_SEM_T * semid)522 int sem_trywait(UV_PLATFORM_SEM_T* semid) {
523 UNREACHABLE();
524 }
525
526
sem_wait(UV_PLATFORM_SEM_T * semid)527 int sem_wait(UV_PLATFORM_SEM_T* semid) {
528 UNREACHABLE();
529 }
530