[740] | 1 | /*
|
---|
| 2 | Unix SMB/Netbios implementation.
|
---|
| 3 | Version 3.0
|
---|
| 4 | Samba select/poll implementation
|
---|
| 5 | Copyright (C) Andrew Tridgell 1992-1998
|
---|
| 6 |
|
---|
| 7 | This program is free software; you can redistribute it and/or modify
|
---|
| 8 | it under the terms of the GNU General Public License as published by
|
---|
| 9 | the Free Software Foundation; either version 3 of the License, or
|
---|
| 10 | (at your option) any later version.
|
---|
| 11 |
|
---|
| 12 | This program is distributed in the hope that it will be useful,
|
---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | GNU General Public License for more details.
|
---|
| 16 |
|
---|
| 17 | You should have received a copy of the GNU General Public License
|
---|
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 | #include "includes.h"
|
---|
| 22 | #include "system/filesys.h"
|
---|
| 23 | #include "system/select.h"
|
---|
| 24 | #include "lib/util/select.h"
|
---|
| 25 |
|
---|
| 26 | /* This is here because it allows us to avoid a nasty race in signal handling.
|
---|
| 27 | We need to guarantee that when we get a signal we get out of a select immediately
|
---|
| 28 | but doing that involves a race condition. We can avoid the race by getting the
|
---|
| 29 | signal handler to write to a pipe that is in the select/poll list
|
---|
| 30 |
|
---|
| 31 | This means all Samba signal handlers should call sys_select_signal().
|
---|
| 32 | */
|
---|
[745] | 33 | #ifdef __OS2__
|
---|
| 34 | #define pipe(A) os2_pipe(A)
|
---|
| 35 | #endif
|
---|
[740] | 36 | static pid_t initialised;
|
---|
| 37 | static int select_pipe[2];
|
---|
| 38 | static volatile unsigned pipe_written, pipe_read;
|
---|
| 39 |
|
---|
| 40 | /*******************************************************************
|
---|
| 41 | Call this from all Samba signal handlers if you want to avoid a
|
---|
| 42 | nasty signal race condition.
|
---|
| 43 | ********************************************************************/
|
---|
| 44 |
|
---|
| 45 | void sys_select_signal(char c)
|
---|
| 46 | {
|
---|
| 47 | int saved_errno = errno;
|
---|
| 48 |
|
---|
| 49 | if (!initialised) return;
|
---|
| 50 |
|
---|
| 51 | if (pipe_written > pipe_read+256) return;
|
---|
| 52 |
|
---|
| 53 | if (write(select_pipe[1], &c, 1) == 1) pipe_written++;
|
---|
| 54 |
|
---|
| 55 | errno = saved_errno;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /*
|
---|
| 59 | * sys_poll expects pollfd's to be a talloc'ed array.
|
---|
| 60 | *
|
---|
| 61 | * It expects the talloc_array_length(fds) >= num_fds+1 to give space
|
---|
| 62 | * to the signal pipe.
|
---|
| 63 | */
|
---|
| 64 |
|
---|
| 65 | int sys_poll(struct pollfd *fds, int num_fds, int timeout)
|
---|
| 66 | {
|
---|
| 67 | int ret;
|
---|
| 68 |
|
---|
| 69 | if (talloc_array_length(fds) < num_fds+1) {
|
---|
| 70 | errno = ENOSPC;
|
---|
| 71 | return -1;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | if (initialised != sys_getpid()) {
|
---|
| 75 | if (pipe(select_pipe) == -1)
|
---|
| 76 | {
|
---|
| 77 | int saved_errno = errno;
|
---|
| 78 | DEBUG(0, ("sys_poll: pipe failed (%s)\n",
|
---|
| 79 | strerror(errno)));
|
---|
| 80 | errno = saved_errno;
|
---|
| 81 | return -1;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /*
|
---|
| 85 | * These next two lines seem to fix a bug with the Linux
|
---|
| 86 | * 2.0.x kernel (and probably other UNIXes as well) where
|
---|
| 87 | * the one byte read below can block even though the
|
---|
| 88 | * select returned that there is data in the pipe and
|
---|
| 89 | * the pipe_written variable was incremented. Thanks to
|
---|
| 90 | * HP for finding this one. JRA.
|
---|
| 91 | */
|
---|
| 92 |
|
---|
| 93 | if(set_blocking(select_pipe[0],0)==-1)
|
---|
| 94 | smb_panic("select_pipe[0]: O_NONBLOCK failed");
|
---|
| 95 | if(set_blocking(select_pipe[1],0)==-1)
|
---|
| 96 | smb_panic("select_pipe[1]: O_NONBLOCK failed");
|
---|
| 97 |
|
---|
| 98 | initialised = sys_getpid();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | ZERO_STRUCT(fds[num_fds]);
|
---|
| 102 | fds[num_fds].fd = select_pipe[0];
|
---|
| 103 | fds[num_fds].events = POLLIN|POLLHUP;
|
---|
| 104 |
|
---|
| 105 | errno = 0;
|
---|
| 106 | ret = poll(fds, num_fds+1, timeout);
|
---|
| 107 |
|
---|
| 108 | if ((ret >= 0) && (fds[num_fds].revents & (POLLIN|POLLHUP|POLLERR))) {
|
---|
| 109 | char c;
|
---|
| 110 | int saved_errno = errno;
|
---|
| 111 |
|
---|
| 112 | if (read(select_pipe[0], &c, 1) == 1) {
|
---|
| 113 | pipe_read += 1;
|
---|
| 114 |
|
---|
| 115 | /* Mark Weaver <mark-clist@npsl.co.uk> pointed out a critical
|
---|
| 116 | fix to ensure we don't lose signals. We must always
|
---|
| 117 | return -1 when the select pipe is set, otherwise if another
|
---|
| 118 | fd is also ready (so ret == 2) then we used to eat the
|
---|
| 119 | byte in the pipe and lose the signal. JRA.
|
---|
| 120 | */
|
---|
| 121 | ret = -1;
|
---|
| 122 | #if 0
|
---|
| 123 | /* JRA - we can use this to debug the signal messaging... */
|
---|
| 124 | DEBUG(0,("select got %u signal\n", (unsigned int)c));
|
---|
| 125 | #endif
|
---|
| 126 | errno = EINTR;
|
---|
| 127 | } else {
|
---|
| 128 | ret -= 1;
|
---|
| 129 | errno = saved_errno;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | return ret;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | int sys_poll_intr(struct pollfd *fds, int num_fds, int timeout)
|
---|
| 137 | {
|
---|
| 138 | int orig_timeout = timeout;
|
---|
| 139 | struct timespec start;
|
---|
| 140 | int ret;
|
---|
| 141 |
|
---|
| 142 | clock_gettime_mono(&start);
|
---|
| 143 |
|
---|
| 144 | while (true) {
|
---|
| 145 | struct timespec now;
|
---|
| 146 | int64_t elapsed;
|
---|
| 147 |
|
---|
| 148 | ret = poll(fds, num_fds, timeout);
|
---|
| 149 | if (ret != -1) {
|
---|
| 150 | break;
|
---|
| 151 | }
|
---|
| 152 | if (errno != EINTR) {
|
---|
| 153 | break;
|
---|
| 154 | }
|
---|
| 155 | clock_gettime_mono(&now);
|
---|
| 156 | elapsed = nsec_time_diff(&now, &start);
|
---|
| 157 | timeout = (orig_timeout - elapsed) / 1000000;
|
---|
| 158 | };
|
---|
| 159 | return ret;
|
---|
| 160 | }
|
---|