aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/nsterm.m8
-rw-r--r--src/process.c247
-rw-r--r--src/sysdep.c4
-rw-r--r--src/syspoll.h32
-rw-r--r--src/sysselect.h38
5 files changed, 249 insertions, 80 deletions
diff --git a/src/nsterm.m b/src/nsterm.m
index fef7f0dc6c8..d54d9b74bff 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -4469,7 +4469,7 @@ ns_select_1 (int nfds, fd_set *readfds, fd_set *writefds,
4469 return -1; 4469 return -1;
4470 } 4470 }
4471 4471
4472 eassert (nfds <= FD_SETSIZE); 4472 eassert (nfds <= EMACS_MAX_FD);
4473 for (k = 0; k < nfds; k++) 4473 for (k = 0; k < nfds; k++)
4474 { 4474 {
4475 if (readfds && FD_ISSET(k, readfds)) ++nr; 4475 if (readfds && FD_ISSET(k, readfds)) ++nr;
@@ -5704,15 +5704,15 @@ ns_term_shutdown (int sig)
5704 maximum number of open files for the process in their first call. 5704 maximum number of open files for the process in their first call.
5705 We make dummy calls to them and then reduce the resource limit 5705 We make dummy calls to them and then reduce the resource limit
5706 here, since pselect cannot handle file descriptors that are 5706 here, since pselect cannot handle file descriptors that are
5707 greater than or equal to FD_SETSIZE. */ 5707 greater than or equal to EMACS_MAX_FD. */
5708 CFSocketGetTypeID (); 5708 CFSocketGetTypeID ();
5709 CFFileDescriptorGetTypeID (); 5709 CFFileDescriptorGetTypeID ();
5710 [[NSFileHandle alloc] init]; 5710 [[NSFileHandle alloc] init];
5711 struct rlimit rlim; 5711 struct rlimit rlim;
5712 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 5712 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
5713 && rlim.rlim_cur > FD_SETSIZE) 5713 && rlim.rlim_cur > EMACS_MAX_FD)
5714 { 5714 {
5715 rlim.rlim_cur = FD_SETSIZE; 5715 rlim.rlim_cur = EMACS_MAX_FD;
5716 setrlimit (RLIMIT_NOFILE, &rlim); 5716 setrlimit (RLIMIT_NOFILE, &rlim);
5717 } 5717 }
5718 if ([NSApp activationPolicy] == NSApplicationActivationPolicyProhibited) { 5718 if ([NSApp activationPolicy] == NSApplicationActivationPolicyProhibited) {
diff --git a/src/process.c b/src/process.c
index 08a02ad9423..5804b3a3bef 100644
--- a/src/process.c
+++ b/src/process.c
@@ -48,7 +48,7 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
48#ifdef HAVE_SETRLIMIT 48#ifdef HAVE_SETRLIMIT
49# include <sys/resource.h> 49# include <sys/resource.h>
50 50
51/* If NOFILE_LIMIT.rlim_cur is greater than FD_SETSIZE, then 51/* If NOFILE_LIMIT.rlim_cur is greater than EMACS_MAX_FD, then
52 NOFILE_LIMIT is the initial limit on the number of open files, 52 NOFILE_LIMIT is the initial limit on the number of open files,
53 which should be restored in child processes. */ 53 which should be restored in child processes. */
54static struct rlimit nofile_limit; 54static struct rlimit nofile_limit;
@@ -299,7 +299,7 @@ static void child_signal_read (int, void *);
299static void child_signal_notify (void); 299static void child_signal_notify (void);
300 300
301/* Indexed by descriptor, gives the process (if any) for that descriptor. */ 301/* Indexed by descriptor, gives the process (if any) for that descriptor. */
302static Lisp_Object chan_process[FD_SETSIZE]; 302static Lisp_Object chan_process[EMACS_MAX_FD];
303static void wait_for_socket_fds (Lisp_Object, char const *); 303static void wait_for_socket_fds (Lisp_Object, char const *);
304 304
305/* Alist of elements (NAME . PROCESS). */ 305/* Alist of elements (NAME . PROCESS). */
@@ -311,18 +311,18 @@ static Lisp_Object Vprocess_alist;
311 output from the process is to read at least one char. 311 output from the process is to read at least one char.
312 Always -1 on systems that support FIONREAD. */ 312 Always -1 on systems that support FIONREAD. */
313 313
314static int proc_buffered_char[FD_SETSIZE]; 314static int proc_buffered_char[EMACS_MAX_FD];
315 315
316/* Table of `struct coding-system' for each process. */ 316/* Table of `struct coding-system' for each process. */
317static struct coding_system *proc_decode_coding_system[FD_SETSIZE]; 317static struct coding_system *proc_decode_coding_system[EMACS_MAX_FD];
318static struct coding_system *proc_encode_coding_system[FD_SETSIZE]; 318static struct coding_system *proc_encode_coding_system[EMACS_MAX_FD];
319 319
320#ifdef DATAGRAM_SOCKETS 320#ifdef DATAGRAM_SOCKETS
321/* Table of `partner address' for datagram sockets. */ 321/* Table of `partner address' for datagram sockets. */
322static struct sockaddr_and_len { 322static struct sockaddr_and_len {
323 struct sockaddr *sa; 323 struct sockaddr *sa;
324 ptrdiff_t len; 324 ptrdiff_t len;
325} datagram_address[FD_SETSIZE]; 325} datagram_address[EMACS_MAX_FD];
326#define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0) 326#define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
327#define DATAGRAM_CONN_P(proc) \ 327#define DATAGRAM_CONN_P(proc) \
328 (PROCESSP (proc) && \ 328 (PROCESSP (proc) && \
@@ -460,8 +460,113 @@ static struct fd_callback_data
460 /* If this fd is currently being selected on by a thread, this 460 /* If this fd is currently being selected on by a thread, this
461 points to the thread. Otherwise it is NULL. */ 461 points to the thread. Otherwise it is NULL. */
462 struct thread_state *waiting_thread; 462 struct thread_state *waiting_thread;
463} fd_callback_info[FD_SETSIZE]; 463} fd_callback_info[EMACS_MAX_FD];
464 464
465#ifdef USE_POLL
466struct pollfd pollfds[EMACS_MAX_FD];
467
468/* Convert a read set and a write set to the corresponding array of
469 struct pollfd. maxfds is the highest fd set in the sets. Returns
470 the number of file descriptors set in the array. rset and wset can
471 be NULL, in which case they will be treated as if they were empty
472 sets. */
473
474int
475fd_sets_to_pollfds (emacs_fd_set *rset, emacs_fd_set *wset, int maxfds)
476{
477 int poll_idx = 0;
478 emacs_fd_set dummy_rset;
479 emacs_fd_set dummy_wset;
480
481 if (!rset)
482 {
483 FD_ZERO (&dummy_rset);
484 rset = &dummy_rset;
485 }
486 if (!wset)
487 {
488 FD_ZERO (&dummy_wset);
489 wset = &dummy_wset;
490 }
491 for (int i = 0; i < maxfds; i++)
492 {
493 short flag = 0;
494 if (FD_ISSET (i, rset))
495 flag |= POLLIN;
496 if (FD_ISSET (i, wset))
497 flag |= POLLOUT;
498 if (flag != 0)
499 {
500 pollfds[poll_idx].fd = i;
501 pollfds[poll_idx].events = flag;
502 poll_idx++;
503 }
504 }
505 return poll_idx;
506}
507
508/* Convert an array of struct pollfd to the corresponding read and
509 write fd_sets. poll_count is the number of file descriptors set in
510 the array. rset and wset can be NULL, in which case they're
511 treated as if they were empty. */
512
513void
514pollfds_to_fd_sets (emacs_fd_set *rset, emacs_fd_set *wset, int poll_count)
515{
516 emacs_fd_set dummy_rset;
517 emacs_fd_set dummy_wset;
518
519 if (!rset)
520 rset = &dummy_rset;
521 FD_ZERO (rset);
522 if (!wset)
523 wset = &dummy_wset;
524 FD_ZERO (wset);
525 for (int i = 0; i < poll_count; i++)
526 {
527 if (pollfds[i].revents & (POLLIN|POLLHUP))
528 FD_SET (pollfds[i].fd, rset);
529 if (pollfds[i].revents & POLLOUT)
530 FD_SET (pollfds[i].fd, wset);
531 }
532}
533
534/* Convert a struct timespec to the corresponding timeout in
535 milliseconds. A NULL timespec is treated as infinity. */
536
537int
538timespec_to_timeout (const struct timespec *ts)
539{
540 if (!ts)
541 return -1;
542 return (ts->tv_sec * 1000 + ts->tv_nsec / 1000000);
543}
544
545/* Wrapper around `poll' with the calling convention of pselect.
546 Converts arguments as appropriate. The sigmask argument is not
547 handled, since Emacs doesn't actually use it. */
548int
549emacs_pselect (int nfds, emacs_fd_set *readfds, emacs_fd_set *writefds,
550 emacs_fd_set *errorfds, const struct timespec *timeout,
551 const sigset_t *sigmask)
552{
553 int ret;
554 int poll_count;
555
556 poll_count = fd_sets_to_pollfds (readfds, writefds, nfds);
557 ret = poll (pollfds, poll_count, timespec_to_timeout (timeout));
558 if (ret > 0)
559 pollfds_to_fd_sets(readfds, writefds, poll_count);
560 else
561 {
562 if (readfds)
563 FD_ZERO (readfds);
564 if (writefds)
565 FD_ZERO (writefds);
566 }
567 return ret;
568}
569#endif /* USE_POLL */
465 570
466/* Add a file descriptor FD to be monitored for when read is possible. 571/* Add a file descriptor FD to be monitored for when read is possible.
467 When read is possible, call FUNC with argument DATA. */ 572 When read is possible, call FUNC with argument DATA. */
@@ -471,7 +576,7 @@ add_read_fd (int fd, fd_callback func, void *data)
471{ 576{
472 add_keyboard_wait_descriptor (fd); 577 add_keyboard_wait_descriptor (fd);
473 578
474 eassert (0 <= fd && fd < FD_SETSIZE); 579 eassert (0 <= fd && fd < EMACS_MAX_FD);
475 fd_callback_info[fd].func = func; 580 fd_callback_info[fd].func = func;
476 fd_callback_info[fd].data = data; 581 fd_callback_info[fd].data = data;
477} 582}
@@ -486,14 +591,14 @@ add_non_keyboard_read_fd (int fd, fd_callback func, void *data)
486static void 591static void
487add_process_read_fd (int fd) 592add_process_read_fd (int fd)
488{ 593{
489 eassert (fd >= 0 && fd < FD_SETSIZE); 594 eassert (fd >= 0 && fd < EMACS_MAX_FD);
490 eassert (fd_callback_info[fd].func == NULL); 595 eassert (fd_callback_info[fd].func == NULL);
491 596
492 fd_callback_info[fd].flags &= ~KEYBOARD_FD; 597 fd_callback_info[fd].flags &= ~KEYBOARD_FD;
493 fd_callback_info[fd].flags |= FOR_READ; 598 fd_callback_info[fd].flags |= FOR_READ;
494 if (fd > max_desc) 599 if (fd > max_desc)
495 max_desc = fd; 600 max_desc = fd;
496 eassert (0 <= fd && fd < FD_SETSIZE); 601 eassert (0 <= fd && fd < EMACS_MAX_FD);
497 fd_callback_info[fd].flags |= PROCESS_FD; 602 fd_callback_info[fd].flags |= PROCESS_FD;
498} 603}
499 604
@@ -504,7 +609,7 @@ delete_read_fd (int fd)
504{ 609{
505 delete_keyboard_wait_descriptor (fd); 610 delete_keyboard_wait_descriptor (fd);
506 611
507 eassert (0 <= fd && fd < FD_SETSIZE); 612 eassert (0 <= fd && fd < EMACS_MAX_FD);
508 if (fd_callback_info[fd].flags == 0) 613 if (fd_callback_info[fd].flags == 0)
509 { 614 {
510 fd_callback_info[fd].func = 0; 615 fd_callback_info[fd].func = 0;
@@ -518,7 +623,7 @@ delete_read_fd (int fd)
518void 623void
519add_write_fd (int fd, fd_callback func, void *data) 624add_write_fd (int fd, fd_callback func, void *data)
520{ 625{
521 eassert (fd >= 0 && fd < FD_SETSIZE); 626 eassert (fd >= 0 && fd < EMACS_MAX_FD);
522 627
523 fd_callback_info[fd].func = func; 628 fd_callback_info[fd].func = func;
524 fd_callback_info[fd].data = data; 629 fd_callback_info[fd].data = data;
@@ -530,7 +635,7 @@ add_write_fd (int fd, fd_callback func, void *data)
530static void 635static void
531add_non_blocking_write_fd (int fd) 636add_non_blocking_write_fd (int fd)
532{ 637{
533 eassert (fd >= 0 && fd < FD_SETSIZE); 638 eassert (fd >= 0 && fd < EMACS_MAX_FD);
534 eassert (fd_callback_info[fd].func == NULL); 639 eassert (fd_callback_info[fd].func == NULL);
535 640
536 fd_callback_info[fd].flags |= FOR_WRITE | NON_BLOCKING_CONNECT_FD; 641 fd_callback_info[fd].flags |= FOR_WRITE | NON_BLOCKING_CONNECT_FD;
@@ -544,7 +649,7 @@ recompute_max_desc (void)
544{ 649{
545 int fd; 650 int fd;
546 651
547 eassert (max_desc < FD_SETSIZE); 652 eassert (max_desc < EMACS_MAX_FD);
548 for (fd = max_desc; fd >= 0; --fd) 653 for (fd = max_desc; fd >= 0; --fd)
549 { 654 {
550 if (fd_callback_info[fd].flags != 0) 655 if (fd_callback_info[fd].flags != 0)
@@ -553,7 +658,7 @@ recompute_max_desc (void)
553 break; 658 break;
554 } 659 }
555 } 660 }
556 eassert (max_desc < FD_SETSIZE); 661 eassert (max_desc < EMACS_MAX_FD);
557} 662}
558 663
559/* Stop monitoring file descriptor FD for when write is possible. */ 664/* Stop monitoring file descriptor FD for when write is possible. */
@@ -561,7 +666,7 @@ recompute_max_desc (void)
561void 666void
562delete_write_fd (int fd) 667delete_write_fd (int fd)
563{ 668{
564 eassert (0 <= fd && fd < FD_SETSIZE); 669 eassert (0 <= fd && fd < EMACS_MAX_FD);
565 if ((fd_callback_info[fd].flags & NON_BLOCKING_CONNECT_FD) != 0) 670 if ((fd_callback_info[fd].flags & NON_BLOCKING_CONNECT_FD) != 0)
566 { 671 {
567 if (--num_pending_connects < 0) 672 if (--num_pending_connects < 0)
@@ -584,7 +689,7 @@ compute_input_wait_mask (fd_set *mask)
584 int fd; 689 int fd;
585 690
586 FD_ZERO (mask); 691 FD_ZERO (mask);
587 eassert (max_desc < FD_SETSIZE); 692 eassert (max_desc < EMACS_MAX_FD);
588 for (fd = 0; fd <= max_desc; ++fd) 693 for (fd = 0; fd <= max_desc; ++fd)
589 { 694 {
590 if (fd_callback_info[fd].thread != NULL 695 if (fd_callback_info[fd].thread != NULL
@@ -607,7 +712,7 @@ compute_non_process_wait_mask (fd_set *mask)
607 int fd; 712 int fd;
608 713
609 FD_ZERO (mask); 714 FD_ZERO (mask);
610 eassert (max_desc < FD_SETSIZE); 715 eassert (max_desc < EMACS_MAX_FD);
611 for (fd = 0; fd <= max_desc; ++fd) 716 for (fd = 0; fd <= max_desc; ++fd)
612 { 717 {
613 if (fd_callback_info[fd].thread != NULL 718 if (fd_callback_info[fd].thread != NULL
@@ -631,7 +736,7 @@ compute_non_keyboard_wait_mask (fd_set *mask)
631 int fd; 736 int fd;
632 737
633 FD_ZERO (mask); 738 FD_ZERO (mask);
634 eassert (max_desc < FD_SETSIZE); 739 eassert (max_desc < EMACS_MAX_FD);
635 for (fd = 0; fd <= max_desc; ++fd) 740 for (fd = 0; fd <= max_desc; ++fd)
636 { 741 {
637 if (fd_callback_info[fd].thread != NULL 742 if (fd_callback_info[fd].thread != NULL
@@ -655,7 +760,7 @@ compute_write_mask (fd_set *mask)
655 int fd; 760 int fd;
656 761
657 FD_ZERO (mask); 762 FD_ZERO (mask);
658 eassert (max_desc < FD_SETSIZE); 763 eassert (max_desc < EMACS_MAX_FD);
659 for (fd = 0; fd <= max_desc; ++fd) 764 for (fd = 0; fd <= max_desc; ++fd)
660 { 765 {
661 if (fd_callback_info[fd].thread != NULL 766 if (fd_callback_info[fd].thread != NULL
@@ -677,7 +782,7 @@ clear_waiting_thread_info (void)
677{ 782{
678 int fd; 783 int fd;
679 784
680 eassert (max_desc < FD_SETSIZE); 785 eassert (max_desc < EMACS_MAX_FD);
681 for (fd = 0; fd <= max_desc; ++fd) 786 for (fd = 0; fd <= max_desc; ++fd)
682 { 787 {
683 if (fd_callback_info[fd].waiting_thread == current_thread) 788 if (fd_callback_info[fd].waiting_thread == current_thread)
@@ -969,10 +1074,10 @@ update_processes_for_thread_death (Lisp_Object dying_thread)
969 struct Lisp_Process *proc = XPROCESS (process); 1074 struct Lisp_Process *proc = XPROCESS (process);
970 1075
971 pset_thread (proc, Qnil); 1076 pset_thread (proc, Qnil);
972 eassert (proc->infd < FD_SETSIZE); 1077 eassert (proc->infd < EMACS_MAX_FD);
973 if (proc->infd >= 0) 1078 if (proc->infd >= 0)
974 fd_callback_info[proc->infd].thread = NULL; 1079 fd_callback_info[proc->infd].thread = NULL;
975 eassert (proc->outfd < FD_SETSIZE); 1080 eassert (proc->outfd < EMACS_MAX_FD);
976 if (proc->outfd >= 0) 1081 if (proc->outfd >= 0)
977 fd_callback_info[proc->outfd].thread = NULL; 1082 fd_callback_info[proc->outfd].thread = NULL;
978 } 1083 }
@@ -1413,10 +1518,10 @@ If THREAD is nil, the process is unlocked. */)
1413 1518
1414 proc = XPROCESS (process); 1519 proc = XPROCESS (process);
1415 pset_thread (proc, thread); 1520 pset_thread (proc, thread);
1416 eassert (proc->infd < FD_SETSIZE); 1521 eassert (proc->infd < EMACS_MAX_FD);
1417 if (proc->infd >= 0) 1522 if (proc->infd >= 0)
1418 fd_callback_info[proc->infd].thread = tstate; 1523 fd_callback_info[proc->infd].thread = tstate;
1419 eassert (proc->outfd < FD_SETSIZE); 1524 eassert (proc->outfd < EMACS_MAX_FD);
1420 if (proc->outfd >= 0) 1525 if (proc->outfd >= 0)
1421 fd_callback_info[proc->outfd].thread = tstate; 1526 fd_callback_info[proc->outfd].thread = tstate;
1422 1527
@@ -2144,7 +2249,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
2144 } 2249 }
2145 } 2250 }
2146 2251
2147 if (FD_SETSIZE <= inchannel || FD_SETSIZE <= outchannel) 2252 if (EMACS_MAX_FD <= inchannel || EMACS_MAX_FD <= outchannel)
2148 report_file_errno ("Creating pipe", Qnil, EMFILE); 2253 report_file_errno ("Creating pipe", Qnil, EMFILE);
2149 2254
2150#ifndef WINDOWSNT 2255#ifndef WINDOWSNT
@@ -2156,7 +2261,7 @@ create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
2156 fcntl (outchannel, F_SETFL, O_NONBLOCK); 2261 fcntl (outchannel, F_SETFL, O_NONBLOCK);
2157 2262
2158 /* Record this as an active process, with its channels. */ 2263 /* Record this as an active process, with its channels. */
2159 eassert (0 <= inchannel && inchannel < FD_SETSIZE); 2264 eassert (0 <= inchannel && inchannel < EMACS_MAX_FD);
2160 chan_process[inchannel] = process; 2265 chan_process[inchannel] = process;
2161 p->infd = inchannel; 2266 p->infd = inchannel;
2162 p->outfd = outchannel; 2267 p->outfd = outchannel;
@@ -2251,7 +2356,7 @@ create_pty (Lisp_Object process)
2251 if (pty_fd >= 0) 2356 if (pty_fd >= 0)
2252 { 2357 {
2253 p->open_fd[SUBPROCESS_STDIN] = pty_fd; 2358 p->open_fd[SUBPROCESS_STDIN] = pty_fd;
2254 if (FD_SETSIZE <= pty_fd) 2359 if (EMACS_MAX_FD <= pty_fd)
2255 report_file_errno ("Opening pty", Qnil, EMFILE); 2360 report_file_errno ("Opening pty", Qnil, EMFILE);
2256#if ! defined (USG) || defined (USG_SUBTTY_WORKS) 2361#if ! defined (USG) || defined (USG_SUBTTY_WORKS)
2257 /* On most USG systems it does not work to open the pty's tty here, 2362 /* On most USG systems it does not work to open the pty's tty here,
@@ -2274,7 +2379,7 @@ create_pty (Lisp_Object process)
2274 2379
2275 /* Record this as an active process, with its channels. 2380 /* Record this as an active process, with its channels.
2276 As a result, child_setup will close Emacs's side of the pipes. */ 2381 As a result, child_setup will close Emacs's side of the pipes. */
2277 eassert (0 <= pty_fd && pty_fd < FD_SETSIZE); 2382 eassert (0 <= pty_fd && pty_fd < EMACS_MAX_FD);
2278 chan_process[pty_fd] = process; 2383 chan_process[pty_fd] = process;
2279 p->infd = pty_fd; 2384 p->infd = pty_fd;
2280 p->outfd = pty_fd; 2385 p->outfd = pty_fd;
@@ -2360,7 +2465,7 @@ usage: (make-pipe-process &rest ARGS) */)
2360 outchannel = p->open_fd[WRITE_TO_SUBPROCESS]; 2465 outchannel = p->open_fd[WRITE_TO_SUBPROCESS];
2361 inchannel = p->open_fd[READ_FROM_SUBPROCESS]; 2466 inchannel = p->open_fd[READ_FROM_SUBPROCESS];
2362 2467
2363 if (FD_SETSIZE <= inchannel || FD_SETSIZE <= outchannel) 2468 if (EMACS_MAX_FD <= inchannel || EMACS_MAX_FD <= outchannel)
2364 report_file_errno ("Creating pipe", Qnil, EMFILE); 2469 report_file_errno ("Creating pipe", Qnil, EMFILE);
2365 2470
2366 fcntl (inchannel, F_SETFL, O_NONBLOCK); 2471 fcntl (inchannel, F_SETFL, O_NONBLOCK);
@@ -2371,7 +2476,7 @@ usage: (make-pipe-process &rest ARGS) */)
2371#endif 2476#endif
2372 2477
2373 /* Record this as an active process, with its channels. */ 2478 /* Record this as an active process, with its channels. */
2374 eassert (0 <= inchannel && inchannel < FD_SETSIZE); 2479 eassert (0 <= inchannel && inchannel < EMACS_MAX_FD);
2375 chan_process[inchannel] = proc; 2480 chan_process[inchannel] = proc;
2376 p->infd = inchannel; 2481 p->infd = inchannel;
2377 p->outfd = outchannel; 2482 p->outfd = outchannel;
@@ -2702,7 +2807,7 @@ set up yet, this function will block until socket setup has completed. */)
2702 return Qnil; 2807 return Qnil;
2703 2808
2704 channel = XPROCESS (process)->infd; 2809 channel = XPROCESS (process)->infd;
2705 eassert (0 <= channel && channel < FD_SETSIZE); 2810 eassert (0 <= channel && channel < EMACS_MAX_FD);
2706 return conv_sockaddr_to_lisp (datagram_address[channel].sa, 2811 return conv_sockaddr_to_lisp (datagram_address[channel].sa,
2707 datagram_address[channel].len); 2812 datagram_address[channel].len);
2708} 2813}
@@ -2731,7 +2836,7 @@ set up yet, this function will block until socket setup has completed. */)
2731 channel = XPROCESS (process)->infd; 2836 channel = XPROCESS (process)->infd;
2732 2837
2733 len = get_lisp_to_sockaddr_size (address, &family); 2838 len = get_lisp_to_sockaddr_size (address, &family);
2734 eassert (0 <= channel && channel < FD_SETSIZE); 2839 eassert (0 <= channel && channel < EMACS_MAX_FD);
2735 if (len == 0 || datagram_address[channel].len != len) 2840 if (len == 0 || datagram_address[channel].len != len)
2736 return Qnil; 2841 return Qnil;
2737 conv_lisp_to_sockaddr (family, address, datagram_address[channel].sa, len); 2842 conv_lisp_to_sockaddr (family, address, datagram_address[channel].sa, len);
@@ -3105,13 +3210,13 @@ usage: (make-serial-process &rest ARGS) */)
3105 3210
3106 fd = serial_open (port); 3211 fd = serial_open (port);
3107 p->open_fd[SUBPROCESS_STDIN] = fd; 3212 p->open_fd[SUBPROCESS_STDIN] = fd;
3108 if (FD_SETSIZE <= fd) 3213 if (EMACS_MAX_FD <= fd)
3109 report_file_errno ("Opening serial port", port, EMFILE); 3214 report_file_errno ("Opening serial port", port, EMFILE);
3110 p->infd = fd; 3215 p->infd = fd;
3111 p->outfd = fd; 3216 p->outfd = fd;
3112 if (fd > max_desc) 3217 if (fd > max_desc)
3113 max_desc = fd; 3218 max_desc = fd;
3114 eassert (0 <= fd && fd < FD_SETSIZE); 3219 eassert (0 <= fd && fd < EMACS_MAX_FD);
3115 chan_process[fd] = proc; 3220 chan_process[fd] = proc;
3116 3221
3117 buffer = Fplist_get (contact, QCbuffer); 3222 buffer = Fplist_get (contact, QCbuffer);
@@ -3283,7 +3388,7 @@ finish_after_tls_connection (Lisp_Object proc)
3283 Fplist_get (contact, QChost), 3388 Fplist_get (contact, QChost),
3284 Fplist_get (contact, QCservice)); 3389 Fplist_get (contact, QCservice));
3285 3390
3286 eassert (p->outfd < FD_SETSIZE); 3391 eassert (p->outfd < EMACS_MAX_FD);
3287 if (NILP (result)) 3392 if (NILP (result))
3288 { 3393 {
3289 pset_status (p, list2 (Qfailed, 3394 pset_status (p, list2 (Qfailed,
@@ -3329,7 +3434,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
3329 if (!NILP (use_external_socket_p)) 3434 if (!NILP (use_external_socket_p))
3330 { 3435 {
3331 socket_to_use = external_sock_fd; 3436 socket_to_use = external_sock_fd;
3332 eassert (socket_to_use < FD_SETSIZE); 3437 eassert (socket_to_use < EMACS_MAX_FD);
3333 3438
3334 /* Ensure we don't consume the external socket twice. */ 3439 /* Ensure we don't consume the external socket twice. */
3335 external_sock_fd = -1; 3440 external_sock_fd = -1;
@@ -3372,7 +3477,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
3372 continue; 3477 continue;
3373 } 3478 }
3374 /* Reject file descriptors that would be too large. */ 3479 /* Reject file descriptors that would be too large. */
3375 if (FD_SETSIZE <= s) 3480 if (EMACS_MAX_FD <= s)
3376 { 3481 {
3377 emacs_close (s); 3482 emacs_close (s);
3378 s = -1; 3483 s = -1;
@@ -3510,7 +3615,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
3510 if (errno == EINTR) 3615 if (errno == EINTR)
3511 goto retry_select; 3616 goto retry_select;
3512 else 3617 else
3513 report_file_error ("Failed select", Qnil); 3618 report_file_error ("Failed select/poll", Qnil);
3514 } 3619 }
3515 eassert (sc > 0); 3620 eassert (sc > 0);
3516 3621
@@ -3543,7 +3648,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
3543#ifdef DATAGRAM_SOCKETS 3648#ifdef DATAGRAM_SOCKETS
3544 if (p->socktype == SOCK_DGRAM) 3649 if (p->socktype == SOCK_DGRAM)
3545 { 3650 {
3546 eassert (0 <= s && s < FD_SETSIZE); 3651 eassert (0 <= s && s < EMACS_MAX_FD);
3547 if (datagram_address[s].sa) 3652 if (datagram_address[s].sa)
3548 emacs_abort (); 3653 emacs_abort ();
3549 3654
@@ -3608,7 +3713,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
3608 inch = s; 3713 inch = s;
3609 outch = s; 3714 outch = s;
3610 3715
3611 eassert (0 <= inch && inch < FD_SETSIZE); 3716 eassert (0 <= inch && inch < EMACS_MAX_FD);
3612 chan_process[inch] = proc; 3717 chan_process[inch] = proc;
3613 3718
3614 fcntl (inch, F_SETFL, O_NONBLOCK); 3719 fcntl (inch, F_SETFL, O_NONBLOCK);
@@ -3635,7 +3740,7 @@ connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
3635 if (! (connecting_status (p->status) 3740 if (! (connecting_status (p->status)
3636 && EQ (XCDR (p->status), addrinfos))) 3741 && EQ (XCDR (p->status), addrinfos)))
3637 pset_status (p, Fcons (Qconnect, addrinfos)); 3742 pset_status (p, Fcons (Qconnect, addrinfos));
3638 eassert (0 <= inch && inch < FD_SETSIZE); 3743 eassert (0 <= inch && inch < EMACS_MAX_FD);
3639 if ((fd_callback_info[inch].flags & NON_BLOCKING_CONNECT_FD) == 0) 3744 if ((fd_callback_info[inch].flags & NON_BLOCKING_CONNECT_FD) == 0)
3640 add_non_blocking_write_fd (inch); 3745 add_non_blocking_write_fd (inch);
3641 } 3746 }
@@ -4703,7 +4808,7 @@ deactivate_process (Lisp_Object proc)
4703 close_process_fd (&p->open_fd[i]); 4808 close_process_fd (&p->open_fd[i]);
4704 4809
4705 inchannel = p->infd; 4810 inchannel = p->infd;
4706 eassert (inchannel < FD_SETSIZE); 4811 eassert (inchannel < EMACS_MAX_FD);
4707 if (inchannel >= 0) 4812 if (inchannel >= 0)
4708 { 4813 {
4709 p->infd = -1; 4814 p->infd = -1;
@@ -4839,7 +4944,7 @@ server_accept_connection (Lisp_Object server, int channel)
4839 4944
4840 s = accept4 (channel, &saddr.sa, &len, SOCK_CLOEXEC); 4945 s = accept4 (channel, &saddr.sa, &len, SOCK_CLOEXEC);
4841 4946
4842 if (FD_SETSIZE <= s) 4947 if (EMACS_MAX_FD <= s)
4843 { 4948 {
4844 emacs_close (s); 4949 emacs_close (s);
4845 s = -1; 4950 s = -1;
@@ -4943,7 +5048,7 @@ server_accept_connection (Lisp_Object server, int channel)
4943 Lisp_Object name = Fformat (nargs, args); 5048 Lisp_Object name = Fformat (nargs, args);
4944 Lisp_Object proc = make_process (name); 5049 Lisp_Object proc = make_process (name);
4945 5050
4946 eassert (0 <= s && s < FD_SETSIZE); 5051 eassert (0 <= s && s < EMACS_MAX_FD);
4947 chan_process[s] = proc; 5052 chan_process[s] = proc;
4948 5053
4949 fcntl (s, F_SETFL, O_NONBLOCK); 5054 fcntl (s, F_SETFL, O_NONBLOCK);
@@ -5226,7 +5331,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
5226 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell))) 5331 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
5227 break; 5332 break;
5228 5333
5229 eassert (max_desc < FD_SETSIZE); 5334 eassert (max_desc < EMACS_MAX_FD);
5230 5335
5231#if defined HAVE_GETADDRINFO_A || defined HAVE_GNUTLS 5336#if defined HAVE_GETADDRINFO_A || defined HAVE_GNUTLS
5232 { 5337 {
@@ -5359,7 +5464,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
5359 because otherwise we wouldn't run into a timeout 5464 because otherwise we wouldn't run into a timeout
5360 below. */ 5465 below. */
5361 int fd = child_signal_read_fd; 5466 int fd = child_signal_read_fd;
5362 eassert (fd < FD_SETSIZE); 5467 eassert (fd < EMACS_MAX_FD);
5363 if (0 <= fd) 5468 if (0 <= fd)
5364 FD_CLR (fd, &Atemp); 5469 FD_CLR (fd, &Atemp);
5365 5470
@@ -5453,7 +5558,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
5453 an asynchronous process. Otherwise this might deadlock if we 5558 an asynchronous process. Otherwise this might deadlock if we
5454 receive a SIGCHLD during `pselect'. */ 5559 receive a SIGCHLD during `pselect'. */
5455 int child_fd = child_signal_read_fd; 5560 int child_fd = child_signal_read_fd;
5456 eassert (child_fd < FD_SETSIZE); 5561 eassert (child_fd < EMACS_MAX_FD);
5457 if (0 <= child_fd) 5562 if (0 <= child_fd)
5458 FD_SET (child_fd, &Available); 5563 FD_SET (child_fd, &Available);
5459 5564
@@ -5563,7 +5668,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
5563 And if so, we need to skip the select which could block. */ 5668 And if so, we need to skip the select which could block. */
5564 FD_ZERO (&tls_available); 5669 FD_ZERO (&tls_available);
5565 tls_nfds = 0; 5670 tls_nfds = 0;
5566 for (channel = 0; channel < FD_SETSIZE; ++channel) 5671 for (channel = 0; channel < EMACS_MAX_FD; ++channel)
5567 if (! NILP (chan_process[channel]) 5672 if (! NILP (chan_process[channel])
5568 && FD_ISSET (channel, &Available)) 5673 && FD_ISSET (channel, &Available))
5569 { 5674 {
@@ -5625,7 +5730,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
5625 else if (nfds > 0) 5730 else if (nfds > 0)
5626 /* Slow path, merge one by one. Note: nfds does not need 5731 /* Slow path, merge one by one. Note: nfds does not need
5627 to be accurate, just positive is enough. */ 5732 to be accurate, just positive is enough. */
5628 for (channel = 0; channel < FD_SETSIZE; ++channel) 5733 for (channel = 0; channel < EMACS_MAX_FD; ++channel)
5629 if (FD_ISSET(channel, &tls_available)) 5734 if (FD_ISSET(channel, &tls_available))
5630 FD_SET(channel, &Available); 5735 FD_SET(channel, &Available);
5631 } 5736 }
@@ -6019,7 +6124,7 @@ read_process_output (Lisp_Object proc, int channel)
6019{ 6124{
6020 ssize_t nbytes; 6125 ssize_t nbytes;
6021 struct Lisp_Process *p = XPROCESS (proc); 6126 struct Lisp_Process *p = XPROCESS (proc);
6022 eassert (0 <= channel && channel < FD_SETSIZE); 6127 eassert (0 <= channel && channel < EMACS_MAX_FD);
6023 struct coding_system *coding = proc_decode_coding_system[channel]; 6128 struct coding_system *coding = proc_decode_coding_system[channel];
6024 int carryover = p->decoding_carryover; 6129 int carryover = p->decoding_carryover;
6025 ptrdiff_t readmax = clip_to_bounds (1, read_process_output_max, PTRDIFF_MAX); 6130 ptrdiff_t readmax = clip_to_bounds (1, read_process_output_max, PTRDIFF_MAX);
@@ -6184,7 +6289,7 @@ read_and_dispose_of_process_output (struct Lisp_Process *p, char *chars,
6184 proc_encode_coding_system[p->outfd] surely points to a 6289 proc_encode_coding_system[p->outfd] surely points to a
6185 valid memory because p->outfd will be changed once EOF is 6290 valid memory because p->outfd will be changed once EOF is
6186 sent to the process. */ 6291 sent to the process. */
6187 eassert (p->outfd < FD_SETSIZE); 6292 eassert (p->outfd < EMACS_MAX_FD);
6188 if (NILP (p->encode_coding_system) && p->outfd >= 0 6293 if (NILP (p->encode_coding_system) && p->outfd >= 0
6189 && proc_encode_coding_system[p->outfd]) 6294 && proc_encode_coding_system[p->outfd])
6190 { 6295 {
@@ -6415,7 +6520,7 @@ send_process (Lisp_Object proc, const char *buf, ptrdiff_t len,
6415 if (p->outfd < 0) 6520 if (p->outfd < 0)
6416 error ("Output file descriptor of %s is closed", SDATA (p->name)); 6521 error ("Output file descriptor of %s is closed", SDATA (p->name));
6417 6522
6418 eassert (p->outfd < FD_SETSIZE); 6523 eassert (p->outfd < EMACS_MAX_FD);
6419 coding = proc_encode_coding_system[p->outfd]; 6524 coding = proc_encode_coding_system[p->outfd];
6420 Vlast_coding_system_used = CODING_ID_NAME (coding->id); 6525 Vlast_coding_system_used = CODING_ID_NAME (coding->id);
6421 6526
@@ -6528,7 +6633,7 @@ send_process (Lisp_Object proc, const char *buf, ptrdiff_t len,
6528 if (outfd < 0) 6633 if (outfd < 0)
6529 error ("Output file descriptor of %s is closed", 6634 error ("Output file descriptor of %s is closed",
6530 SDATA (p->name)); 6635 SDATA (p->name));
6531 eassert (0 <= outfd && outfd < FD_SETSIZE); 6636 eassert (0 <= outfd && outfd < EMACS_MAX_FD);
6532#ifdef DATAGRAM_SOCKETS 6637#ifdef DATAGRAM_SOCKETS
6533 if (DATAGRAM_CHAN_P (outfd)) 6638 if (DATAGRAM_CHAN_P (outfd))
6534 { 6639 {
@@ -6980,7 +7085,7 @@ traffic. */)
6980 struct Lisp_Process *p; 7085 struct Lisp_Process *p;
6981 7086
6982 p = XPROCESS (process); 7087 p = XPROCESS (process);
6983 eassert (p->infd < FD_SETSIZE); 7088 eassert (p->infd < EMACS_MAX_FD);
6984 if (EQ (p->command, Qt) 7089 if (EQ (p->command, Qt)
6985 && p->infd >= 0 7090 && p->infd >= 0
6986 && (!EQ (p->filter, Qt) || EQ (p->status, Qlisten))) 7091 && (!EQ (p->filter, Qt) || EQ (p->status, Qlisten)))
@@ -7124,7 +7229,7 @@ process has been transmitted to the serial port. */)
7124 7229
7125 7230
7126 outfd = XPROCESS (proc)->outfd; 7231 outfd = XPROCESS (proc)->outfd;
7127 eassert (outfd < FD_SETSIZE); 7232 eassert (outfd < EMACS_MAX_FD);
7128 if (outfd >= 0) 7233 if (outfd >= 0)
7129 coding = proc_encode_coding_system[outfd]; 7234 coding = proc_encode_coding_system[outfd];
7130 7235
@@ -7172,13 +7277,13 @@ process has been transmitted to the serial port. */)
7172 p->open_fd[WRITE_TO_SUBPROCESS] = new_outfd; 7277 p->open_fd[WRITE_TO_SUBPROCESS] = new_outfd;
7173 p->outfd = new_outfd; 7278 p->outfd = new_outfd;
7174 7279
7175 eassert (0 <= new_outfd && new_outfd < FD_SETSIZE); 7280 eassert (0 <= new_outfd && new_outfd < EMACS_MAX_FD);
7176 if (!proc_encode_coding_system[new_outfd]) 7281 if (!proc_encode_coding_system[new_outfd])
7177 proc_encode_coding_system[new_outfd] 7282 proc_encode_coding_system[new_outfd]
7178 = xmalloc (sizeof (struct coding_system)); 7283 = xmalloc (sizeof (struct coding_system));
7179 if (old_outfd >= 0) 7284 if (old_outfd >= 0)
7180 { 7285 {
7181 eassert (old_outfd < FD_SETSIZE); 7286 eassert (old_outfd < EMACS_MAX_FD);
7182 *proc_encode_coding_system[new_outfd] 7287 *proc_encode_coding_system[new_outfd]
7183 = *proc_encode_coding_system[old_outfd]; 7288 = *proc_encode_coding_system[old_outfd];
7184 memset (proc_encode_coding_system[old_outfd], 0, 7289 memset (proc_encode_coding_system[old_outfd], 0,
@@ -7251,7 +7356,7 @@ child_signal_init (void)
7251 int fds[2]; 7356 int fds[2];
7252 if (emacs_pipe (fds) < 0) 7357 if (emacs_pipe (fds) < 0)
7253 report_file_error ("Creating pipe for child signal", Qnil); 7358 report_file_error ("Creating pipe for child signal", Qnil);
7254 if (FD_SETSIZE <= fds[0]) 7359 if (EMACS_MAX_FD <= fds[0])
7255 { 7360 {
7256 /* Since we need to `pselect' on the read end, it has to fit 7361 /* Since we need to `pselect' on the read end, it has to fit
7257 into an `fd_set'. */ 7362 into an `fd_set'. */
@@ -7723,7 +7828,7 @@ DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p,
7723 struct Lisp_Process *p = XPROCESS (process); 7828 struct Lisp_Process *p = XPROCESS (process);
7724 if (p->infd < 0) 7829 if (p->infd < 0)
7725 return Qnil; 7830 return Qnil;
7726 eassert (p->infd < FD_SETSIZE); 7831 eassert (p->infd < EMACS_MAX_FD);
7727 struct coding_system *coding = proc_decode_coding_system[p->infd]; 7832 struct coding_system *coding = proc_decode_coding_system[p->infd];
7728 return (CODING_FOR_UNIBYTE (coding) ? Qnil : Qt); 7833 return (CODING_FOR_UNIBYTE (coding) ? Qnil : Qt);
7729} 7834}
@@ -7757,7 +7862,7 @@ keyboard_bit_set (fd_set *mask)
7757{ 7862{
7758 int fd; 7863 int fd;
7759 7864
7760 eassert (max_desc < FD_SETSIZE); 7865 eassert (max_desc < EMACS_MAX_FD);
7761 for (fd = 0; fd <= max_desc; fd++) 7866 for (fd = 0; fd <= max_desc; fd++)
7762 if (FD_ISSET (fd, mask) 7867 if (FD_ISSET (fd, mask)
7763 && ((fd_callback_info[fd].flags & (FOR_READ | KEYBOARD_FD)) 7868 && ((fd_callback_info[fd].flags & (FOR_READ | KEYBOARD_FD))
@@ -8005,7 +8110,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
8005void 8110void
8006add_timer_wait_descriptor (int fd) 8111add_timer_wait_descriptor (int fd)
8007{ 8112{
8008 eassert (0 <= fd && fd < FD_SETSIZE); 8113 eassert (0 <= fd && fd < EMACS_MAX_FD);
8009 add_read_fd (fd, timerfd_callback, NULL); 8114 add_read_fd (fd, timerfd_callback, NULL);
8010 fd_callback_info[fd].flags &= ~KEYBOARD_FD; 8115 fd_callback_info[fd].flags &= ~KEYBOARD_FD;
8011} 8116}
@@ -8031,7 +8136,7 @@ void
8031add_keyboard_wait_descriptor (int desc) 8136add_keyboard_wait_descriptor (int desc)
8032{ 8137{
8033#ifdef subprocesses /* Actually means "not MSDOS". */ 8138#ifdef subprocesses /* Actually means "not MSDOS". */
8034 eassert (desc >= 0 && desc < FD_SETSIZE); 8139 eassert (desc >= 0 && desc < EMACS_MAX_FD);
8035 fd_callback_info[desc].flags &= ~PROCESS_FD; 8140 fd_callback_info[desc].flags &= ~PROCESS_FD;
8036 fd_callback_info[desc].flags |= (FOR_READ | KEYBOARD_FD); 8141 fd_callback_info[desc].flags |= (FOR_READ | KEYBOARD_FD);
8037 if (desc > max_desc) 8142 if (desc > max_desc)
@@ -8045,7 +8150,7 @@ void
8045delete_keyboard_wait_descriptor (int desc) 8150delete_keyboard_wait_descriptor (int desc)
8046{ 8151{
8047#ifdef subprocesses 8152#ifdef subprocesses
8048 eassert (desc >= 0 && desc < FD_SETSIZE); 8153 eassert (desc >= 0 && desc < EMACS_MAX_FD);
8049 8154
8050 fd_callback_info[desc].flags &= ~(FOR_READ | KEYBOARD_FD | PROCESS_FD); 8155 fd_callback_info[desc].flags &= ~(FOR_READ | KEYBOARD_FD | PROCESS_FD);
8051 8156
@@ -8068,7 +8173,7 @@ setup_process_coding_systems (Lisp_Object process)
8068 if (inch < 0 || outch < 0) 8173 if (inch < 0 || outch < 0)
8069 return; 8174 return;
8070 8175
8071 eassert (0 <= inch && inch < FD_SETSIZE); 8176 eassert (0 <= inch && inch < EMACS_MAX_FD);
8072 if (!proc_decode_coding_system[inch]) 8177 if (!proc_decode_coding_system[inch])
8073 proc_decode_coding_system[inch] = xmalloc (sizeof (struct coding_system)); 8178 proc_decode_coding_system[inch] = xmalloc (sizeof (struct coding_system));
8074 coding_system = p->decode_coding_system; 8179 coding_system = p->decode_coding_system;
@@ -8080,7 +8185,7 @@ setup_process_coding_systems (Lisp_Object process)
8080 } 8185 }
8081 setup_coding_system (coding_system, proc_decode_coding_system[inch]); 8186 setup_coding_system (coding_system, proc_decode_coding_system[inch]);
8082 8187
8083 eassert (0 <= outch && outch < FD_SETSIZE); 8188 eassert (0 <= outch && outch < EMACS_MAX_FD);
8084 if (!proc_encode_coding_system[outch]) 8189 if (!proc_encode_coding_system[outch])
8085 proc_encode_coding_system[outch] = xmalloc (sizeof (struct coding_system)); 8190 proc_encode_coding_system[outch] = xmalloc (sizeof (struct coding_system));
8086 setup_coding_system (p->encode_coding_system, 8191 setup_coding_system (p->encode_coding_system,
@@ -8322,7 +8427,7 @@ void
8322restore_nofile_limit (void) 8427restore_nofile_limit (void)
8323{ 8428{
8324#ifdef HAVE_SETRLIMIT 8429#ifdef HAVE_SETRLIMIT
8325 if (FD_SETSIZE < nofile_limit.rlim_cur) 8430 if (EMACS_MAX_FD < nofile_limit.rlim_cur)
8326 setrlimit (RLIMIT_NOFILE, &nofile_limit); 8431 setrlimit (RLIMIT_NOFILE, &nofile_limit);
8327#endif 8432#endif
8328} 8433}
@@ -8383,13 +8488,13 @@ init_process_emacs (int sockfd)
8383 } 8488 }
8384 8489
8385#ifdef HAVE_SETRLIMIT 8490#ifdef HAVE_SETRLIMIT
8386 /* Don't allocate more than FD_SETSIZE file descriptors for Emacs itself. */ 8491 /* Don't allocate more than EMACS_MAX_FD file descriptors for Emacs itself. */
8387 if (getrlimit (RLIMIT_NOFILE, &nofile_limit) != 0) 8492 if (getrlimit (RLIMIT_NOFILE, &nofile_limit) != 0)
8388 nofile_limit.rlim_cur = 0; 8493 nofile_limit.rlim_cur = 0;
8389 else if (FD_SETSIZE < nofile_limit.rlim_cur) 8494 else if (EMACS_MAX_FD < nofile_limit.rlim_cur)
8390 { 8495 {
8391 struct rlimit rlim = nofile_limit; 8496 struct rlimit rlim = nofile_limit;
8392 rlim.rlim_cur = FD_SETSIZE; 8497 rlim.rlim_cur = EMACS_MAX_FD;
8393 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0) 8498 if (setrlimit (RLIMIT_NOFILE, &rlim) != 0)
8394 nofile_limit.rlim_cur = 0; 8499 nofile_limit.rlim_cur = 0;
8395 } 8500 }
@@ -8425,7 +8530,7 @@ init_process_emacs (int sockfd)
8425 8530
8426 Vprocess_alist = Qnil; 8531 Vprocess_alist = Qnil;
8427 deleted_pid_list = Qnil; 8532 deleted_pid_list = Qnil;
8428 for (i = 0; i < FD_SETSIZE; i++) 8533 for (i = 0; i < EMACS_MAX_FD; i++)
8429 { 8534 {
8430 chan_process[i] = Qnil; 8535 chan_process[i] = Qnil;
8431 proc_buffered_char[i] = -1; 8536 proc_buffered_char[i] = -1;
diff --git a/src/sysdep.c b/src/sysdep.c
index 95295e7e676..5988b35927a 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -777,7 +777,7 @@ restore_signal_handlers (struct save_signal *saved_handlers)
777} 777}
778 778
779#ifdef USABLE_SIGIO 779#ifdef USABLE_SIGIO
780static int old_fcntl_flags[FD_SETSIZE]; 780static int old_fcntl_flags[EMACS_MAX_FD];
781#endif 781#endif
782 782
783void 783void
@@ -1079,7 +1079,7 @@ emacs_set_tty (int fd, struct emacs_tty *settings, bool flushp)
1079 1079
1080 1080
1081#ifdef F_SETOWN 1081#ifdef F_SETOWN
1082static int old_fcntl_owner[FD_SETSIZE]; 1082static int old_fcntl_owner[EMACS_MAX_FD];
1083#endif /* F_SETOWN */ 1083#endif /* F_SETOWN */
1084 1084
1085/* Initialize the terminal mode on all tty devices that are currently 1085/* Initialize the terminal mode on all tty devices that are currently
diff --git a/src/syspoll.h b/src/syspoll.h
new file mode 100644
index 00000000000..d133794fb13
--- /dev/null
+++ b/src/syspoll.h
@@ -0,0 +1,32 @@
1/* syspoll.h - System-dependent definitions for the poll function.
2 Copyright (C) 2022 Free Software Foundation, Inc.
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or (at
9your option) any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
18
19#ifndef SYSPOLL_H
20#define SYSPOLL_H 1
21
22#if !defined (DOS_NT) && !defined (WINDOWSNT)
23#include <sys/poll.h>
24
25extern int fd_sets_to_pollfds (emacs_fd_set *, emacs_fd_set *, int);
26extern void pollfds_to_fd_sets (emacs_fd_set *, emacs_fd_set *, int);
27extern int timespec_to_timeout (const struct timespec *);
28extern int emacs_pselect (int, emacs_fd_set *, emacs_fd_set *,
29 emacs_fd_set *, const struct timespec *,
30 const sigset_t *);
31#endif
32#endif
diff --git a/src/sysselect.h b/src/sysselect.h
index 45cc22bc4cb..029a913f324 100644
--- a/src/sysselect.h
+++ b/src/sysselect.h
@@ -29,6 +29,7 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
29 where w32 needs it, but not where sysselect.h is included. The w32 29 where w32 needs it, but not where sysselect.h is included. The w32
30 definitions in w32.h are incompatible with the below. */ 30 definitions in w32.h are incompatible with the below. */
31#ifndef WINDOWSNT 31#ifndef WINDOWSNT
32#ifndef USE_POLL
32#ifdef FD_SET 33#ifdef FD_SET
33#ifndef FD_SETSIZE 34#ifndef FD_SETSIZE
34#define FD_SETSIZE 64 35#define FD_SETSIZE 64
@@ -43,6 +44,37 @@ typedef int fd_set;
43#define FD_ISSET(n, p) (*(p) & (1 << (n))) 44#define FD_ISSET(n, p) (*(p) & (1 << (n)))
44#define FD_ZERO(p) (*(p) = 0) 45#define FD_ZERO(p) (*(p) = 0)
45#endif /* no FD_SET */ 46#endif /* no FD_SET */
47#define EMACS_MAX_FD FD_SETSIZE
48#else /* no USE_POLL */
49#define EMACS_MAX_FD (10 * FD_SETSIZE)
50#define fd_set emacs_fd_set
51#define pselect emacs_pselect
52#undef FD_CLR
53#undef FD_ISSET
54#undef FD_SET
55#undef FD_ZERO
56
57typedef struct {
58 EMACS_UINT bits[EMACS_MAX_FD / EMACS_UINT_WIDTH];
59} emacs_fd_set;
60
61/* standard access macros */
62#define FD_SET(n, p) \
63 do { \
64 if ((n) < EMACS_MAX_FD) { \
65 (p)->bits[(n)/EMACS_UINT_WIDTH] |= (1 << (n)%EMACS_UINT_WIDTH); \
66 } \
67 } while (0)
68#define FD_CLR(n, p) \
69 do { \
70 if ((n) < EMACS_MAX_FD) { \
71 (p)->bits[(n)/EMACS_UINT_WIDTH] &= ~(1 << (n)%EMACS_UINT_WIDTH); \
72 } \
73 } while (0)
74#define FD_ISSET(n, p) ((n) < EMACS_MAX_FD ? ((p)->bits[(n)/EMACS_UINT_WIDTH] & (1 << (n)%EMACS_UINT_WIDTH)) : 0)
75#define FD_ZERO(p) memset((p), 0, sizeof(emacs_fd_set))
76#include "syspoll.h"
77#endif /* no USE_POLL */
46#endif /* not WINDOWSNT */ 78#endif /* not WINDOWSNT */
47 79
48#if !defined (HAVE_SELECT) 80#if !defined (HAVE_SELECT)
@@ -66,21 +98,21 @@ INLINE_HEADER_BEGIN
66INLINE void 98INLINE void
67fd_CLR (int fd, fd_set *set) 99fd_CLR (int fd, fd_set *set)
68{ 100{
69 eassume (0 <= fd && fd < FD_SETSIZE); 101 eassume (0 <= fd && fd < EMACS_MAX_FD);
70 FD_CLR (fd, set); 102 FD_CLR (fd, set);
71} 103}
72 104
73INLINE bool 105INLINE bool
74fd_ISSET (int fd, fd_set *set) 106fd_ISSET (int fd, fd_set *set)
75{ 107{
76 eassume (0 <= fd && fd < FD_SETSIZE); 108 eassume (0 <= fd && fd < EMACS_MAX_FD);
77 return FD_ISSET (fd, set) != 0; 109 return FD_ISSET (fd, set) != 0;
78} 110}
79 111
80INLINE void 112INLINE void
81fd_SET (int fd, fd_set *set) 113fd_SET (int fd, fd_set *set)
82{ 114{
83 eassume (0 <= fd && fd < FD_SETSIZE); 115 eassume (0 <= fd && fd < EMACS_MAX_FD);
84 FD_SET (fd, set); 116 FD_SET (fd, set);
85} 117}
86 118