Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* fcntl module */ |
| 33 | |
| 34 | #include "allobjects.h" |
| 35 | #include "modsupport.h" |
| 36 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 37 | #ifdef HAVE_UNISTD_H |
| 38 | #include <unistd.h> |
| 39 | #endif |
| 40 | |
| 41 | #ifdef HAVE_SYS_FILE_H |
| 42 | #include <sys/file.h> |
| 43 | #endif |
| 44 | |
Guido van Rossum | 3d65fa3 | 1996-12-09 18:49:14 +0000 | [diff] [blame] | 45 | #include <sys/ioctl.h> |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 46 | #include <fcntl.h> |
| 47 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 48 | |
| 49 | /* fcntl(fd, opt, [arg]) */ |
| 50 | |
| 51 | static object * |
| 52 | fcntl_fcntl(self, args) |
| 53 | object *self; /* Not used */ |
| 54 | object *args; |
| 55 | { |
| 56 | int fd; |
| 57 | int code; |
| 58 | int arg; |
| 59 | int ret; |
| 60 | char *str; |
| 61 | int len; |
| 62 | char buf[1024]; |
| 63 | |
| 64 | if (getargs(args, "(iis#)", &fd, &code, &str, &len)) { |
| 65 | if (len > sizeof buf) { |
| 66 | err_setstr(ValueError, "fcntl string arg too long"); |
| 67 | return NULL; |
| 68 | } |
| 69 | memcpy(buf, str, len); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 70 | BGN_SAVE |
| 71 | ret = fcntl(fd, code, buf); |
| 72 | END_SAVE |
| 73 | if (ret < 0) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 74 | err_errno(IOError); |
| 75 | return NULL; |
| 76 | } |
| 77 | return newsizedstringobject(buf, len); |
| 78 | } |
| 79 | |
| 80 | err_clear(); |
| 81 | if (getargs(args, "(ii)", &fd, &code)) |
| 82 | arg = 0; |
| 83 | else { |
| 84 | err_clear(); |
| 85 | if (!getargs(args, "(iii)", &fd, &code, &arg)) |
| 86 | return NULL; |
| 87 | } |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 88 | BGN_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 89 | ret = fcntl(fd, code, arg); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 90 | END_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 91 | if (ret < 0) { |
| 92 | err_errno(IOError); |
| 93 | return NULL; |
| 94 | } |
| 95 | return newintobject((long)ret); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /* ioctl(fd, opt, [arg]) */ |
| 100 | |
| 101 | static object * |
| 102 | fcntl_ioctl(self, args) |
| 103 | object *self; /* Not used */ |
| 104 | object *args; |
| 105 | { |
| 106 | int fd; |
| 107 | int code; |
| 108 | int arg; |
| 109 | int ret; |
| 110 | char *str; |
| 111 | int len; |
| 112 | char buf[1024]; |
| 113 | |
| 114 | if (getargs(args, "(iis#)", &fd, &code, &str, &len)) { |
| 115 | if (len > sizeof buf) { |
| 116 | err_setstr(ValueError, "ioctl string arg too long"); |
| 117 | return NULL; |
| 118 | } |
| 119 | memcpy(buf, str, len); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 120 | BGN_SAVE |
| 121 | ret = ioctl(fd, code, buf); |
| 122 | END_SAVE |
| 123 | if (ret < 0) { |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 124 | err_errno(IOError); |
| 125 | return NULL; |
| 126 | } |
| 127 | return newsizedstringobject(buf, len); |
| 128 | } |
| 129 | |
| 130 | err_clear(); |
| 131 | if (getargs(args, "(ii)", &fd, &code)) |
| 132 | arg = 0; |
| 133 | else { |
| 134 | err_clear(); |
| 135 | if (!getargs(args, "(iii)", &fd, &code, &arg)) |
| 136 | return NULL; |
| 137 | } |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 138 | BGN_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 139 | ret = ioctl(fd, code, arg); |
Guido van Rossum | 903f487 | 1995-10-07 19:18:22 +0000 | [diff] [blame] | 140 | END_SAVE |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 141 | if (ret < 0) { |
| 142 | err_errno(IOError); |
| 143 | return NULL; |
| 144 | } |
| 145 | return newintobject((long)ret); |
| 146 | } |
| 147 | |
| 148 | |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 149 | /* flock(fd, operation) */ |
| 150 | |
| 151 | static object * |
| 152 | fcntl_flock(self, args) |
| 153 | object *self; /* Not used */ |
| 154 | object *args; |
| 155 | { |
| 156 | int fd; |
| 157 | int code; |
| 158 | int ret; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 159 | |
| 160 | if (!getargs(args, "(ii)", &fd, &code)) |
| 161 | return NULL; |
| 162 | |
| 163 | BGN_SAVE |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 164 | #ifdef HAVE_FLOCK |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 165 | ret = flock(fd, code); |
Guido van Rossum | 3c0b79c | 1996-06-11 15:11:34 +0000 | [diff] [blame] | 166 | #else |
| 167 | |
| 168 | #ifndef LOCK_SH |
| 169 | #define LOCK_SH 1 /* shared lock */ |
| 170 | #define LOCK_EX 2 /* exclusive lock */ |
| 171 | #define LOCK_NB 4 /* don't block when locking */ |
| 172 | #define LOCK_UN 8 /* unlock */ |
| 173 | #endif |
| 174 | { |
| 175 | struct flock l; |
| 176 | if (code == LOCK_UN) |
| 177 | l.l_type = F_UNLCK; |
| 178 | else if (code & LOCK_SH) |
| 179 | l.l_type = F_RDLCK; |
| 180 | else if (code & LOCK_EX) |
| 181 | l.l_type = F_WRLCK; |
| 182 | else { |
| 183 | err_setstr(ValueError, "unrecognized flock argument"); |
| 184 | return NULL; |
| 185 | } |
| 186 | l.l_whence = l.l_start = l.l_len = 0; |
| 187 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
| 188 | } |
| 189 | #endif /* HAVE_FLOCK */ |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 190 | END_SAVE |
| 191 | if (ret < 0) { |
| 192 | err_errno(IOError); |
| 193 | return NULL; |
| 194 | } |
| 195 | INCREF(None); |
| 196 | return None; |
| 197 | } |
| 198 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 199 | /* lockf(fd, operation) */ |
| 200 | static object * |
| 201 | fcntl_lockf(self, args) |
| 202 | object *self; /* Not used */ |
| 203 | object *args; |
| 204 | { |
| 205 | int fd, code, len = 0, start = 0, whence = 0, ret; |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 207 | if (!PyArg_ParseTuple(args, "ii|iii", &fd, &code, &len, |
| 208 | &start, &whence)) |
| 209 | return NULL; |
| 210 | |
| 211 | BGN_SAVE |
| 212 | #ifndef LOCK_SH |
| 213 | #define LOCK_SH 1 /* shared lock */ |
| 214 | #define LOCK_EX 2 /* exclusive lock */ |
| 215 | #define LOCK_NB 4 /* don't block when locking */ |
| 216 | #define LOCK_UN 8 /* unlock */ |
| 217 | #endif |
| 218 | { |
| 219 | struct flock l; |
| 220 | if (code == LOCK_UN) |
| 221 | l.l_type = F_UNLCK; |
| 222 | else if (code & LOCK_SH) |
| 223 | l.l_type = F_RDLCK; |
| 224 | else if (code & LOCK_EX) |
| 225 | l.l_type = F_WRLCK; |
| 226 | else { |
| 227 | err_setstr(ValueError, "unrecognized flock argument"); |
| 228 | return NULL; |
| 229 | } |
| 230 | l.l_len = len; |
| 231 | l.l_start = start; |
| 232 | l.l_whence = whence; |
| 233 | ret = fcntl(fd, (code & LOCK_NB) ? F_SETLK : F_SETLKW, &l); |
| 234 | } |
| 235 | END_SAVE |
| 236 | if (ret < 0) { |
| 237 | err_errno(IOError); |
| 238 | return NULL; |
| 239 | } |
| 240 | INCREF(None); |
| 241 | return None; |
| 242 | } |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 244 | /* List of functions */ |
| 245 | |
| 246 | static struct methodlist fcntl_methods[] = { |
| 247 | {"fcntl", fcntl_fcntl}, |
| 248 | {"ioctl", fcntl_ioctl}, |
Guido van Rossum | 3539b1e | 1996-05-23 22:56:38 +0000 | [diff] [blame] | 249 | {"flock", fcntl_flock}, |
Guido van Rossum | c864364 | 1996-09-11 23:17:20 +0000 | [diff] [blame] | 250 | {"lockf", fcntl_lockf, 1}, |
Guido van Rossum | 0297512 | 1992-08-17 08:55:12 +0000 | [diff] [blame] | 251 | {NULL, NULL} /* sentinel */ |
| 252 | }; |
| 253 | |
| 254 | |
| 255 | /* Module initialisation */ |
| 256 | |
| 257 | void |
| 258 | initfcntl() |
| 259 | { |
| 260 | object *m, *d; |
| 261 | |
| 262 | /* Create the module and add the functions */ |
| 263 | m = initmodule("fcntl", fcntl_methods); |
| 264 | |
| 265 | /* Add some symbolic constants to the module */ |
| 266 | d = getmoduledict(m); |
| 267 | |
| 268 | /* Check for errors */ |
| 269 | if (err_occurred()) |
| 270 | fatal("can't initialize module fcntl"); |
| 271 | } |