Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win. In that case the |
| 5 | module actually calls itself 'nt', not 'posix', and a few |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8 | of the compiler used. Different compilers define their own feature |
Victor Stinner | f427a14 | 2014-10-22 12:33:23 +0200 | [diff] [blame] | 9 | test macro, e.g. '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11 | |
| 12 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13 | #ifdef __APPLE__ |
| 14 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 17 | * at the end of this file for more information. |
| 18 | */ |
| 19 | # pragma weak lchown |
| 20 | # pragma weak statvfs |
| 21 | # pragma weak fstatvfs |
| 22 | |
| 23 | #endif /* __APPLE__ */ |
| 24 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 25 | #define PY_SSIZE_T_CLEAN |
| 26 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | #include "Python.h" |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 28 | #ifdef MS_WINDOWS |
| 29 | /* include <windows.h> early to avoid conflict with pycore_condvar.h: |
| 30 | |
| 31 | #define WIN32_LEAN_AND_MEAN |
| 32 | #include <windows.h> |
| 33 | |
| 34 | FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */ |
| 35 | # include <windows.h> |
| 36 | #endif |
| 37 | |
| 38 | #include "pycore_ceval.h" /* _PyEval_ReInitThreads() */ |
Victor Stinner | 01b63ec | 2019-06-19 00:48:09 +0200 | [diff] [blame] | 39 | #include "pycore_import.h" /* _PyImport_ReInitLock() */ |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 40 | #include "pycore_pystate.h" /* _PyRuntime */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 41 | #include "pythread.h" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 42 | #include "structmember.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 43 | #ifndef MS_WINDOWS |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 44 | # include "posixmodule.h" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 45 | #else |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 46 | # include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 47 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 48 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 49 | /* On android API level 21, 'AT_EACCESS' is not declared although |
| 50 | * HAVE_FACCESSAT is defined. */ |
| 51 | #ifdef __ANDROID__ |
| 52 | #undef HAVE_FACCESSAT |
| 53 | #endif |
| 54 | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | fa76eee | 2016-05-28 21:03:48 +0000 | [diff] [blame] | 55 | #include <stdio.h> /* needed for ctermid() */ |
| 56 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 57 | #ifdef __cplusplus |
| 58 | extern "C" { |
| 59 | #endif |
| 60 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 61 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 62 | "This module provides access to operating system functionality that is\n\ |
| 63 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 64 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 65 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 67 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 68 | #ifdef HAVE_SYS_UIO_H |
| 69 | #include <sys/uio.h> |
| 70 | #endif |
| 71 | |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 72 | #ifdef HAVE_SYS_SYSMACROS_H |
| 73 | /* GNU C Library: major(), minor(), makedev() */ |
| 74 | #include <sys/sysmacros.h> |
| 75 | #endif |
| 76 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 79 | #endif /* HAVE_SYS_TYPES_H */ |
| 80 | |
| 81 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 83 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 85 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 86 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 87 | #endif |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 88 | #ifdef HAVE_LINUX_WAIT_H |
| 89 | #include <linux/wait.h> // For P_PIDFD |
| 90 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 91 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 92 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 93 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 94 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 96 | #ifdef HAVE_FCNTL_H |
| 97 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 98 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 100 | #ifdef HAVE_GRP_H |
| 101 | #include <grp.h> |
| 102 | #endif |
| 103 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 104 | #ifdef HAVE_SYSEXITS_H |
| 105 | #include <sysexits.h> |
| 106 | #endif /* HAVE_SYSEXITS_H */ |
| 107 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 108 | #ifdef HAVE_SYS_LOADAVG_H |
| 109 | #include <sys/loadavg.h> |
| 110 | #endif |
| 111 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 112 | #ifdef HAVE_SYS_SENDFILE_H |
| 113 | #include <sys/sendfile.h> |
| 114 | #endif |
| 115 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 116 | #if defined(__APPLE__) |
| 117 | #include <copyfile.h> |
| 118 | #endif |
| 119 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 120 | #ifdef HAVE_SCHED_H |
| 121 | #include <sched.h> |
| 122 | #endif |
| 123 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 124 | #ifdef HAVE_COPY_FILE_RANGE |
| 125 | #include <unistd.h> |
| 126 | #endif |
| 127 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 128 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 129 | #undef HAVE_SCHED_SETAFFINITY |
| 130 | #endif |
| 131 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 132 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__) |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 133 | #define USE_XATTRS |
| 134 | #endif |
| 135 | |
| 136 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 137 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 138 | #endif |
| 139 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 140 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 141 | #ifdef HAVE_SYS_SOCKET_H |
| 142 | #include <sys/socket.h> |
| 143 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 144 | #endif |
| 145 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 146 | #ifdef HAVE_DLFCN_H |
| 147 | #include <dlfcn.h> |
| 148 | #endif |
| 149 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 150 | #ifdef __hpux |
| 151 | #include <sys/mpctl.h> |
| 152 | #endif |
| 153 | |
| 154 | #if defined(__DragonFly__) || \ |
| 155 | defined(__OpenBSD__) || \ |
| 156 | defined(__FreeBSD__) || \ |
| 157 | defined(__NetBSD__) || \ |
| 158 | defined(__APPLE__) |
| 159 | #include <sys/sysctl.h> |
| 160 | #endif |
| 161 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 162 | #ifdef HAVE_LINUX_RANDOM_H |
| 163 | # include <linux/random.h> |
| 164 | #endif |
| 165 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 166 | # include <sys/syscall.h> |
| 167 | #endif |
| 168 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 169 | #if defined(MS_WINDOWS) |
| 170 | # define TERMSIZE_USE_CONIO |
| 171 | #elif defined(HAVE_SYS_IOCTL_H) |
| 172 | # include <sys/ioctl.h> |
| 173 | # if defined(HAVE_TERMIOS_H) |
| 174 | # include <termios.h> |
| 175 | # endif |
| 176 | # if defined(TIOCGWINSZ) |
| 177 | # define TERMSIZE_USE_IOCTL |
| 178 | # endif |
| 179 | #endif /* MS_WINDOWS */ |
| 180 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 181 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 182 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 183 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 184 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 185 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 186 | #include <process.h> |
| 187 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 188 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 189 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 190 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 191 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 192 | #define HAVE_EXECV 1 |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 193 | #define HAVE_WSPAWNV 1 |
| 194 | #define HAVE_WEXECV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 195 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 196 | #define HAVE_SYSTEM 1 |
| 197 | #define HAVE_CWAIT 1 |
| 198 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 199 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 200 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 201 | /* Unix functions that the configure script doesn't check for */ |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 202 | #ifndef __VXWORKS__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 203 | #define HAVE_EXECV 1 |
| 204 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 205 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 206 | #define HAVE_FORK1 1 |
| 207 | #endif |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 208 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 209 | #define HAVE_GETEGID 1 |
| 210 | #define HAVE_GETEUID 1 |
| 211 | #define HAVE_GETGID 1 |
| 212 | #define HAVE_GETPPID 1 |
| 213 | #define HAVE_GETUID 1 |
| 214 | #define HAVE_KILL 1 |
| 215 | #define HAVE_OPENDIR 1 |
| 216 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 217 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 218 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 219 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 220 | #endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 221 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 222 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 223 | _Py_IDENTIFIER(__fspath__); |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 224 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 225 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 226 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 227 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 228 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 229 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 230 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 231 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 232 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 233 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 234 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 235 | (default) */ |
| 236 | extern char *ctermid_r(char *); |
| 237 | #endif |
| 238 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 239 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 240 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 241 | #if defined(__VXWORKS__) |
| 242 | #include <vxCpuLib.h> |
| 243 | #include <rtpLib.h> |
| 244 | #include <wait.h> |
| 245 | #include <taskLib.h> |
| 246 | #ifndef _P_WAIT |
| 247 | #define _P_WAIT 0 |
| 248 | #define _P_NOWAIT 1 |
| 249 | #define _P_NOWAITO 1 |
| 250 | #endif |
| 251 | #endif /* __VXWORKS__ */ |
| 252 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 253 | #ifdef HAVE_POSIX_SPAWN |
| 254 | #include <spawn.h> |
| 255 | #endif |
| 256 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #ifdef HAVE_UTIME_H |
| 258 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 259 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 261 | #ifdef HAVE_SYS_UTIME_H |
| 262 | #include <sys/utime.h> |
| 263 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 264 | #endif /* HAVE_SYS_UTIME_H */ |
| 265 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | #ifdef HAVE_SYS_TIMES_H |
| 267 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 268 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 269 | |
| 270 | #ifdef HAVE_SYS_PARAM_H |
| 271 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 272 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | |
| 274 | #ifdef HAVE_SYS_UTSNAME_H |
| 275 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 276 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 277 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 278 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 280 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 281 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 282 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 283 | #include <direct.h> |
| 284 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 285 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 286 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 287 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 288 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 289 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 290 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 291 | #endif |
| 292 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 293 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 294 | #endif |
| 295 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 296 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 297 | #endif |
| 298 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 300 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 301 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 302 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 303 | #endif |
| 304 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 305 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 306 | #endif |
| 307 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 308 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 309 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 310 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 311 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 312 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 313 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 314 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 315 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 316 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 317 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 318 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 319 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 320 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 321 | #define HAVE_SYMLINK |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 322 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 323 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 324 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 325 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 326 | #define MAXPATHLEN PATH_MAX |
| 327 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 328 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 329 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 330 | #endif /* MAXPATHLEN */ |
| 331 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 332 | #ifdef UNION_WAIT |
| 333 | /* Emulate some macros on systems that have a union instead of macros */ |
| 334 | |
| 335 | #ifndef WIFEXITED |
| 336 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 337 | #endif |
| 338 | |
| 339 | #ifndef WEXITSTATUS |
| 340 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 341 | #endif |
| 342 | |
| 343 | #ifndef WTERMSIG |
| 344 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 345 | #endif |
| 346 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 347 | #define WAIT_TYPE union wait |
| 348 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 349 | |
| 350 | #else /* !UNION_WAIT */ |
| 351 | #define WAIT_TYPE int |
| 352 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 353 | #endif /* UNION_WAIT */ |
| 354 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 355 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 356 | prototype for it, at least on Solaris -- maybe others as well?). */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 357 | #if defined(HAVE_CTERMID_R) |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 358 | #define USE_CTERMID_R |
| 359 | #endif |
| 360 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 361 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 362 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 363 | #undef FSTAT |
| 364 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 365 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 366 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 367 | # define LSTAT win32_lstat |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 368 | # define FSTAT _Py_fstat_noraise |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 369 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 370 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 371 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 372 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 373 | # define FSTAT fstat |
| 374 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 375 | #endif |
| 376 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 377 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 378 | #include <sys/mkdev.h> |
| 379 | #else |
| 380 | #if defined(MAJOR_IN_SYSMACROS) |
| 381 | #include <sys/sysmacros.h> |
| 382 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 383 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 384 | #include <sys/mkdev.h> |
| 385 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 386 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 387 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 388 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 389 | #define INITFUNC PyInit_nt |
| 390 | #define MODNAME "nt" |
| 391 | #else |
| 392 | #define INITFUNC PyInit_posix |
| 393 | #define MODNAME "posix" |
| 394 | #endif |
| 395 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 396 | #if defined(__sun) |
| 397 | /* Something to implement in autoconf, not present in autoconf 2.69 */ |
| 398 | #define HAVE_STRUCT_STAT_ST_FSTYPE 1 |
| 399 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 400 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 401 | /* memfd_create is either defined in sys/mman.h or sys/memfd.h |
| 402 | * linux/memfd.h defines additional flags |
| 403 | */ |
| 404 | #ifdef HAVE_SYS_MMAN_H |
| 405 | #include <sys/mman.h> |
| 406 | #endif |
| 407 | #ifdef HAVE_SYS_MEMFD_H |
| 408 | #include <sys/memfd.h> |
| 409 | #endif |
| 410 | #ifdef HAVE_LINUX_MEMFD_H |
| 411 | #include <linux/memfd.h> |
| 412 | #endif |
| 413 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 414 | #ifdef _Py_MEMORY_SANITIZER |
| 415 | # include <sanitizer/msan_interface.h> |
| 416 | #endif |
| 417 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 418 | #ifdef HAVE_FORK |
| 419 | static void |
| 420 | run_at_forkers(PyObject *lst, int reverse) |
| 421 | { |
| 422 | Py_ssize_t i; |
| 423 | PyObject *cpy; |
| 424 | |
| 425 | if (lst != NULL) { |
| 426 | assert(PyList_CheckExact(lst)); |
| 427 | |
| 428 | /* Use a list copy in case register_at_fork() is called from |
| 429 | * one of the callbacks. |
| 430 | */ |
| 431 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); |
| 432 | if (cpy == NULL) |
| 433 | PyErr_WriteUnraisable(lst); |
| 434 | else { |
| 435 | if (reverse) |
| 436 | PyList_Reverse(cpy); |
| 437 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { |
| 438 | PyObject *func, *res; |
| 439 | func = PyList_GET_ITEM(cpy, i); |
Jeroen Demeyer | 7f41c8e | 2019-07-04 12:35:31 +0200 | [diff] [blame] | 440 | res = _PyObject_CallNoArg(func); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 441 | if (res == NULL) |
| 442 | PyErr_WriteUnraisable(func); |
| 443 | else |
| 444 | Py_DECREF(res); |
| 445 | } |
| 446 | Py_DECREF(cpy); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void |
| 452 | PyOS_BeforeFork(void) |
| 453 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 454 | run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 455 | |
| 456 | _PyImport_AcquireLock(); |
| 457 | } |
| 458 | |
| 459 | void |
| 460 | PyOS_AfterFork_Parent(void) |
| 461 | { |
| 462 | if (_PyImport_ReleaseLock() <= 0) |
| 463 | Py_FatalError("failed releasing import lock after fork"); |
| 464 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 465 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void |
| 469 | PyOS_AfterFork_Child(void) |
| 470 | { |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 471 | _PyRuntimeState *runtime = &_PyRuntime; |
| 472 | _PyGILState_Reinit(runtime); |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 473 | _PyEval_ReInitThreads(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 474 | _PyImport_ReInitLock(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 475 | _PySignal_AfterFork(); |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 476 | _PyRuntimeState_ReInitThreads(runtime); |
Victor Stinner | b49858b | 2019-05-24 15:20:23 +0200 | [diff] [blame] | 477 | _PyInterpreterState_DeleteExceptMain(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 478 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 479 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static int |
| 483 | register_at_forker(PyObject **lst, PyObject *func) |
| 484 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 485 | if (func == NULL) /* nothing to register? do nothing. */ |
| 486 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 487 | if (*lst == NULL) { |
| 488 | *lst = PyList_New(0); |
| 489 | if (*lst == NULL) |
| 490 | return -1; |
| 491 | } |
| 492 | return PyList_Append(*lst, func); |
| 493 | } |
| 494 | #endif |
| 495 | |
| 496 | /* Legacy wrapper */ |
| 497 | void |
| 498 | PyOS_AfterFork(void) |
| 499 | { |
| 500 | #ifdef HAVE_FORK |
| 501 | PyOS_AfterFork_Child(); |
| 502 | #endif |
| 503 | } |
| 504 | |
| 505 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 506 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 507 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 508 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 509 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 510 | ULONG, struct _Py_stat_struct *); |
| 511 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 512 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 513 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 514 | #ifndef MS_WINDOWS |
| 515 | PyObject * |
| 516 | _PyLong_FromUid(uid_t uid) |
| 517 | { |
| 518 | if (uid == (uid_t)-1) |
| 519 | return PyLong_FromLong(-1); |
| 520 | return PyLong_FromUnsignedLong(uid); |
| 521 | } |
| 522 | |
| 523 | PyObject * |
| 524 | _PyLong_FromGid(gid_t gid) |
| 525 | { |
| 526 | if (gid == (gid_t)-1) |
| 527 | return PyLong_FromLong(-1); |
| 528 | return PyLong_FromUnsignedLong(gid); |
| 529 | } |
| 530 | |
| 531 | int |
| 532 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 533 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 534 | uid_t uid; |
| 535 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 536 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 537 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 538 | unsigned long uresult; |
| 539 | |
| 540 | index = PyNumber_Index(obj); |
| 541 | if (index == NULL) { |
| 542 | PyErr_Format(PyExc_TypeError, |
| 543 | "uid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 544 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 545 | return 0; |
| 546 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 547 | |
| 548 | /* |
| 549 | * Handling uid_t is complicated for two reasons: |
| 550 | * * Although uid_t is (always?) unsigned, it still |
| 551 | * accepts -1. |
| 552 | * * We don't know its size in advance--it may be |
| 553 | * bigger than an int, or it may be smaller than |
| 554 | * a long. |
| 555 | * |
| 556 | * So a bit of defensive programming is in order. |
| 557 | * Start with interpreting the value passed |
| 558 | * in as a signed long and see if it works. |
| 559 | */ |
| 560 | |
| 561 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 562 | |
| 563 | if (!overflow) { |
| 564 | uid = (uid_t)result; |
| 565 | |
| 566 | if (result == -1) { |
| 567 | if (PyErr_Occurred()) |
| 568 | goto fail; |
| 569 | /* It's a legitimate -1, we're done. */ |
| 570 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 571 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 572 | |
| 573 | /* Any other negative number is disallowed. */ |
| 574 | if (result < 0) |
| 575 | goto underflow; |
| 576 | |
| 577 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 578 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 579 | (long)uid != result) |
| 580 | goto underflow; |
| 581 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 582 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 583 | |
| 584 | if (overflow < 0) |
| 585 | goto underflow; |
| 586 | |
| 587 | /* |
| 588 | * Okay, the value overflowed a signed long. If it |
| 589 | * fits in an *unsigned* long, it may still be okay, |
| 590 | * as uid_t may be unsigned long on this platform. |
| 591 | */ |
| 592 | uresult = PyLong_AsUnsignedLong(index); |
| 593 | if (PyErr_Occurred()) { |
| 594 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 595 | goto overflow; |
| 596 | goto fail; |
| 597 | } |
| 598 | |
| 599 | uid = (uid_t)uresult; |
| 600 | |
| 601 | /* |
| 602 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 603 | * but this value would get interpreted as (uid_t)-1 by chown |
| 604 | * and its siblings. That's not what the user meant! So we |
| 605 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 606 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 607 | */ |
| 608 | if (uid == (uid_t)-1) |
| 609 | goto overflow; |
| 610 | |
| 611 | /* Ensure the value wasn't truncated. */ |
| 612 | if (sizeof(uid_t) < sizeof(long) && |
| 613 | (unsigned long)uid != uresult) |
| 614 | goto overflow; |
| 615 | /* fallthrough */ |
| 616 | |
| 617 | success: |
| 618 | Py_DECREF(index); |
| 619 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 620 | return 1; |
| 621 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 622 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 623 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 624 | "uid is less than minimum"); |
| 625 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 626 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 627 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 628 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 629 | "uid is greater than maximum"); |
| 630 | /* fallthrough */ |
| 631 | |
| 632 | fail: |
| 633 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | int |
| 638 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 639 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 640 | gid_t gid; |
| 641 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 642 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 643 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 644 | unsigned long uresult; |
| 645 | |
| 646 | index = PyNumber_Index(obj); |
| 647 | if (index == NULL) { |
| 648 | PyErr_Format(PyExc_TypeError, |
| 649 | "gid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 650 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 651 | return 0; |
| 652 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 653 | |
| 654 | /* |
| 655 | * Handling gid_t is complicated for two reasons: |
| 656 | * * Although gid_t is (always?) unsigned, it still |
| 657 | * accepts -1. |
| 658 | * * We don't know its size in advance--it may be |
| 659 | * bigger than an int, or it may be smaller than |
| 660 | * a long. |
| 661 | * |
| 662 | * So a bit of defensive programming is in order. |
| 663 | * Start with interpreting the value passed |
| 664 | * in as a signed long and see if it works. |
| 665 | */ |
| 666 | |
| 667 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 668 | |
| 669 | if (!overflow) { |
| 670 | gid = (gid_t)result; |
| 671 | |
| 672 | if (result == -1) { |
| 673 | if (PyErr_Occurred()) |
| 674 | goto fail; |
| 675 | /* It's a legitimate -1, we're done. */ |
| 676 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 677 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 678 | |
| 679 | /* Any other negative number is disallowed. */ |
| 680 | if (result < 0) { |
| 681 | goto underflow; |
| 682 | } |
| 683 | |
| 684 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 685 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 686 | (long)gid != result) |
| 687 | goto underflow; |
| 688 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 689 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 690 | |
| 691 | if (overflow < 0) |
| 692 | goto underflow; |
| 693 | |
| 694 | /* |
| 695 | * Okay, the value overflowed a signed long. If it |
| 696 | * fits in an *unsigned* long, it may still be okay, |
| 697 | * as gid_t may be unsigned long on this platform. |
| 698 | */ |
| 699 | uresult = PyLong_AsUnsignedLong(index); |
| 700 | if (PyErr_Occurred()) { |
| 701 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 702 | goto overflow; |
| 703 | goto fail; |
| 704 | } |
| 705 | |
| 706 | gid = (gid_t)uresult; |
| 707 | |
| 708 | /* |
| 709 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 710 | * but this value would get interpreted as (gid_t)-1 by chown |
| 711 | * and its siblings. That's not what the user meant! So we |
| 712 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 713 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 714 | */ |
| 715 | if (gid == (gid_t)-1) |
| 716 | goto overflow; |
| 717 | |
| 718 | /* Ensure the value wasn't truncated. */ |
| 719 | if (sizeof(gid_t) < sizeof(long) && |
| 720 | (unsigned long)gid != uresult) |
| 721 | goto overflow; |
| 722 | /* fallthrough */ |
| 723 | |
| 724 | success: |
| 725 | Py_DECREF(index); |
| 726 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 727 | return 1; |
| 728 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 729 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 730 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 731 | "gid is less than minimum"); |
| 732 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 733 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 734 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 735 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 736 | "gid is greater than maximum"); |
| 737 | /* fallthrough */ |
| 738 | |
| 739 | fail: |
| 740 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 741 | return 0; |
| 742 | } |
| 743 | #endif /* MS_WINDOWS */ |
| 744 | |
| 745 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 746 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 747 | |
| 748 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 749 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 750 | static int |
| 751 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 752 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 753 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 754 | if (PyErr_Occurred()) |
| 755 | return 0; |
| 756 | return 1; |
| 757 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 758 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 759 | |
| 760 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 761 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 762 | /* |
| 763 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 764 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 765 | * which doesn't play nicely with all the initializer lines in this file that |
| 766 | * look like this: |
| 767 | * int dir_fd = DEFAULT_DIR_FD; |
| 768 | */ |
| 769 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 770 | #else |
| 771 | #define DEFAULT_DIR_FD (-100) |
| 772 | #endif |
| 773 | |
| 774 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 775 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 776 | { |
| 777 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 778 | long long_value; |
| 779 | |
| 780 | PyObject *index = PyNumber_Index(o); |
| 781 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 782 | return 0; |
| 783 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 784 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 785 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 786 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 787 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 788 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 789 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 790 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 791 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 792 | return 0; |
| 793 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 794 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 795 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 796 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 797 | return 0; |
| 798 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 799 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 800 | *p = (int)long_value; |
| 801 | return 1; |
| 802 | } |
| 803 | |
| 804 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 805 | dir_fd_converter(PyObject *o, void *p) |
| 806 | { |
| 807 | if (o == Py_None) { |
| 808 | *(int *)p = DEFAULT_DIR_FD; |
| 809 | return 1; |
| 810 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 811 | else if (PyIndex_Check(o)) { |
| 812 | return _fd_converter(o, (int *)p); |
| 813 | } |
| 814 | else { |
| 815 | PyErr_Format(PyExc_TypeError, |
| 816 | "argument should be integer or None, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 817 | _PyType_Name(Py_TYPE(o))); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 818 | return 0; |
| 819 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 822 | typedef struct { |
| 823 | PyObject *billion; |
| 824 | PyObject *posix_putenv_garbage; |
| 825 | PyObject *DirEntryType; |
| 826 | PyObject *ScandirIteratorType; |
| 827 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 828 | PyObject *SchedParamType; |
| 829 | #endif |
| 830 | PyObject *StatResultType; |
| 831 | PyObject *StatVFSResultType; |
| 832 | PyObject *TerminalSizeType; |
| 833 | PyObject *TimesResultType; |
| 834 | PyObject *UnameResultType; |
| 835 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 836 | PyObject *WaitidResultType; |
| 837 | #endif |
| 838 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 839 | PyObject *struct_rusage; |
| 840 | #endif |
| 841 | PyObject *st_mode; |
| 842 | } _posixstate; |
| 843 | |
| 844 | static struct PyModuleDef posixmodule; |
| 845 | |
| 846 | #define _posixstate(o) ((_posixstate *)PyModule_GetState(o)) |
| 847 | #define _posixstate_global ((_posixstate *)PyModule_GetState(PyState_FindModule(&posixmodule))) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 848 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 849 | /* |
| 850 | * A PyArg_ParseTuple "converter" function |
| 851 | * that handles filesystem paths in the manner |
| 852 | * preferred by the os module. |
| 853 | * |
| 854 | * path_converter accepts (Unicode) strings and their |
| 855 | * subclasses, and bytes and their subclasses. What |
| 856 | * it does with the argument depends on the platform: |
| 857 | * |
| 858 | * * On Windows, if we get a (Unicode) string we |
| 859 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 860 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 861 | * |
| 862 | * * On all other platforms, strings are encoded |
| 863 | * to bytes using PyUnicode_FSConverter, then we |
| 864 | * extract the char * from the bytes object and |
| 865 | * return that. |
| 866 | * |
| 867 | * path_converter also optionally accepts signed |
| 868 | * integers (representing open file descriptors) instead |
| 869 | * of path strings. |
| 870 | * |
| 871 | * Input fields: |
| 872 | * path.nullable |
| 873 | * If nonzero, the path is permitted to be None. |
| 874 | * path.allow_fd |
| 875 | * If nonzero, the path is permitted to be a file handle |
| 876 | * (a signed int) instead of a string. |
| 877 | * path.function_name |
| 878 | * If non-NULL, path_converter will use that as the name |
| 879 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 880 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 881 | * path.argument_name |
| 882 | * If non-NULL, path_converter will use that as the name |
| 883 | * of the parameter in error messages. |
| 884 | * (If path.argument_name is NULL it uses "path".) |
| 885 | * |
| 886 | * Output fields: |
| 887 | * path.wide |
| 888 | * Points to the path if it was expressed as Unicode |
| 889 | * and was not encoded. (Only used on Windows.) |
| 890 | * path.narrow |
| 891 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 892 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 893 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 894 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 895 | * path.fd |
| 896 | * Contains a file descriptor if path.accept_fd was true |
| 897 | * and the caller provided a signed integer instead of any |
| 898 | * sort of string. |
| 899 | * |
| 900 | * WARNING: if your "path" parameter is optional, and is |
| 901 | * unspecified, path_converter will never get called. |
| 902 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 903 | * yourself! |
| 904 | * path.length |
| 905 | * The length of the path in characters, if specified as |
| 906 | * a string. |
| 907 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 908 | * The original object passed in (if get a PathLike object, |
| 909 | * the result of PyOS_FSPath() is treated as the original object). |
| 910 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 911 | * path.cleanup |
| 912 | * For internal use only. May point to a temporary object. |
| 913 | * (Pay no attention to the man behind the curtain.) |
| 914 | * |
| 915 | * At most one of path.wide or path.narrow will be non-NULL. |
| 916 | * If path was None and path.nullable was set, |
| 917 | * or if path was an integer and path.allow_fd was set, |
| 918 | * both path.wide and path.narrow will be NULL |
| 919 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 920 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 921 | * path_converter takes care to not write to the path_t |
| 922 | * unless it's successful. However it must reset the |
| 923 | * "cleanup" field each time it's called. |
| 924 | * |
| 925 | * Use as follows: |
| 926 | * path_t path; |
| 927 | * memset(&path, 0, sizeof(path)); |
| 928 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 929 | * // ... use values from path ... |
| 930 | * path_cleanup(&path); |
| 931 | * |
| 932 | * (Note that if PyArg_Parse fails you don't need to call |
| 933 | * path_cleanup(). However it is safe to do so.) |
| 934 | */ |
| 935 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 936 | const char *function_name; |
| 937 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 938 | int nullable; |
| 939 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 940 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 941 | #ifdef MS_WINDOWS |
| 942 | BOOL narrow; |
| 943 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 944 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 945 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 946 | int fd; |
| 947 | Py_ssize_t length; |
| 948 | PyObject *object; |
| 949 | PyObject *cleanup; |
| 950 | } path_t; |
| 951 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 952 | #ifdef MS_WINDOWS |
| 953 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 954 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 955 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 956 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 957 | {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL} |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 958 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 959 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 960 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 961 | path_cleanup(path_t *path) |
| 962 | { |
| 963 | Py_CLEAR(path->object); |
| 964 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 968 | path_converter(PyObject *o, void *p) |
| 969 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 970 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 971 | PyObject *bytes = NULL; |
| 972 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 973 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 974 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 975 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 976 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 977 | const wchar_t *wide; |
| 978 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 979 | |
| 980 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 981 | PyErr_Format(exc, "%s%s" fmt, \ |
| 982 | path->function_name ? path->function_name : "", \ |
| 983 | path->function_name ? ": " : "", \ |
| 984 | path->argument_name ? path->argument_name : "path") |
| 985 | |
| 986 | /* Py_CLEANUP_SUPPORTED support */ |
| 987 | if (o == NULL) { |
| 988 | path_cleanup(path); |
| 989 | return 1; |
| 990 | } |
| 991 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 992 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 993 | path->object = path->cleanup = NULL; |
| 994 | /* path->object owns a reference to the original object */ |
| 995 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 996 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 997 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 998 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 999 | #ifdef MS_WINDOWS |
| 1000 | path->narrow = FALSE; |
| 1001 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1002 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1003 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1004 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1005 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1008 | /* Only call this here so that we don't treat the return value of |
| 1009 | os.fspath() as an fd or buffer. */ |
| 1010 | is_index = path->allow_fd && PyIndex_Check(o); |
| 1011 | is_buffer = PyObject_CheckBuffer(o); |
| 1012 | is_bytes = PyBytes_Check(o); |
| 1013 | is_unicode = PyUnicode_Check(o); |
| 1014 | |
| 1015 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 1016 | /* Inline PyOS_FSPath() for better error messages. */ |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1017 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1018 | |
| 1019 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 1020 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1021 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1022 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1023 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1024 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1025 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1026 | goto error_exit; |
| 1027 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1028 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1029 | is_unicode = 1; |
| 1030 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1031 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1032 | is_bytes = 1; |
| 1033 | } |
| 1034 | else { |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1035 | PyErr_Format(PyExc_TypeError, |
| 1036 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1037 | "not %.200s", _PyType_Name(Py_TYPE(o)), |
| 1038 | _PyType_Name(Py_TYPE(res))); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1039 | Py_DECREF(res); |
| 1040 | goto error_exit; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1041 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1042 | |
| 1043 | /* still owns a reference to the original object */ |
| 1044 | Py_DECREF(o); |
| 1045 | o = res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1049 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1050 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1051 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1052 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1053 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1054 | if (length > 32767) { |
| 1055 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1056 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1057 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1058 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1059 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1060 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1061 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1062 | |
| 1063 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1064 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1065 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1066 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1067 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1068 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1069 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1070 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1071 | #endif |
| 1072 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1073 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1074 | bytes = o; |
| 1075 | Py_INCREF(bytes); |
| 1076 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1077 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1078 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1079 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1080 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1081 | "%s%s%s should be %s, not %.200s", |
| 1082 | path->function_name ? path->function_name : "", |
| 1083 | path->function_name ? ": " : "", |
| 1084 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1085 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1086 | "integer or None" : |
| 1087 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1088 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1089 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1090 | _PyType_Name(Py_TYPE(o)))) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1091 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1092 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1093 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1094 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1095 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1096 | } |
| 1097 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1098 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1099 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1100 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1101 | } |
| 1102 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1103 | #ifdef MS_WINDOWS |
| 1104 | path->narrow = FALSE; |
| 1105 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1106 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1107 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1108 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1109 | } |
| 1110 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1111 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1112 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1113 | path->function_name ? path->function_name : "", |
| 1114 | path->function_name ? ": " : "", |
| 1115 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1116 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1117 | "integer or None" : |
| 1118 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1119 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1120 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1121 | _PyType_Name(Py_TYPE(o))); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1122 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1125 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1126 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1127 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1128 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1129 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1132 | #ifdef MS_WINDOWS |
| 1133 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1134 | narrow, |
| 1135 | length |
| 1136 | ); |
| 1137 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1138 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1141 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1142 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1143 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1144 | } |
| 1145 | if (length > 32767) { |
| 1146 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1147 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1148 | } |
| 1149 | if (wcslen(wide) != length) { |
| 1150 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1151 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1152 | } |
| 1153 | path->wide = wide; |
| 1154 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1155 | path->cleanup = wo; |
| 1156 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1157 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1158 | path->wide = NULL; |
| 1159 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1160 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1161 | /* Still a reference owned by path->object, don't have to |
| 1162 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1163 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1164 | } |
| 1165 | else { |
| 1166 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1167 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1168 | #endif |
| 1169 | path->fd = -1; |
| 1170 | |
| 1171 | success_exit: |
| 1172 | path->length = length; |
| 1173 | path->object = o; |
| 1174 | return Py_CLEANUP_SUPPORTED; |
| 1175 | |
| 1176 | error_exit: |
| 1177 | Py_XDECREF(o); |
| 1178 | Py_XDECREF(bytes); |
| 1179 | #ifdef MS_WINDOWS |
| 1180 | Py_XDECREF(wo); |
| 1181 | #endif |
| 1182 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1186 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1187 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1188 | PyErr_Format(PyExc_NotImplementedError, |
| 1189 | "%s%s%s unavailable on this platform", |
| 1190 | (function_name != NULL) ? function_name : "", |
| 1191 | (function_name != NULL) ? ": ": "", |
| 1192 | argument_name); |
| 1193 | } |
| 1194 | |
| 1195 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1196 | dir_fd_unavailable(PyObject *o, void *p) |
| 1197 | { |
| 1198 | int dir_fd; |
| 1199 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1200 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1201 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1202 | argument_unavailable_error(NULL, "dir_fd"); |
| 1203 | return 0; |
| 1204 | } |
| 1205 | *(int *)p = dir_fd; |
| 1206 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1210 | fd_specified(const char *function_name, int fd) |
| 1211 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1212 | if (fd == -1) |
| 1213 | return 0; |
| 1214 | |
| 1215 | argument_unavailable_error(function_name, "fd"); |
| 1216 | return 1; |
| 1217 | } |
| 1218 | |
| 1219 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1220 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1221 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1222 | if (follow_symlinks) |
| 1223 | return 0; |
| 1224 | |
| 1225 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1226 | return 1; |
| 1227 | } |
| 1228 | |
| 1229 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1230 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1231 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1232 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1233 | #ifndef MS_WINDOWS |
| 1234 | && !path->narrow |
| 1235 | #endif |
| 1236 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1237 | PyErr_Format(PyExc_ValueError, |
| 1238 | "%s: can't specify dir_fd without matching path", |
| 1239 | function_name); |
| 1240 | return 1; |
| 1241 | } |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1246 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1247 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1248 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1249 | PyErr_Format(PyExc_ValueError, |
| 1250 | "%s: can't specify both dir_fd and fd", |
| 1251 | function_name); |
| 1252 | return 1; |
| 1253 | } |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1258 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1259 | int follow_symlinks) |
| 1260 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1261 | if ((fd > 0) && (!follow_symlinks)) { |
| 1262 | PyErr_Format(PyExc_ValueError, |
| 1263 | "%s: cannot use fd and follow_symlinks together", |
| 1264 | function_name); |
| 1265 | return 1; |
| 1266 | } |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1271 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1272 | int follow_symlinks) |
| 1273 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1274 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1275 | PyErr_Format(PyExc_ValueError, |
| 1276 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1277 | function_name); |
| 1278 | return 1; |
| 1279 | } |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1283 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1284 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1285 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1286 | typedef off_t Py_off_t; |
| 1287 | #endif |
| 1288 | |
| 1289 | static int |
| 1290 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1291 | { |
| 1292 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1293 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1294 | #else |
| 1295 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1296 | #endif |
| 1297 | if (PyErr_Occurred()) |
| 1298 | return 0; |
| 1299 | return 1; |
| 1300 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1301 | |
| 1302 | static PyObject * |
| 1303 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1304 | { |
| 1305 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1306 | return PyLong_FromLongLong(offset); |
| 1307 | #else |
| 1308 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1309 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1310 | } |
| 1311 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1312 | #ifdef HAVE_SIGSET_T |
| 1313 | /* Convert an iterable of integers to a sigset. |
| 1314 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1315 | int |
| 1316 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1317 | { |
| 1318 | sigset_t *mask = (sigset_t *)addr; |
| 1319 | PyObject *iterator, *item; |
| 1320 | long signum; |
| 1321 | int overflow; |
| 1322 | |
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1323 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
| 1324 | if (sigemptyset(mask) < (0)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1325 | /* Probably only if mask == NULL. */ |
| 1326 | PyErr_SetFromErrno(PyExc_OSError); |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | iterator = PyObject_GetIter(obj); |
| 1331 | if (iterator == NULL) { |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
| 1335 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1336 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1337 | Py_DECREF(item); |
| 1338 | if (signum <= 0 || signum >= NSIG) { |
| 1339 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1340 | PyErr_Format(PyExc_ValueError, |
| 1341 | "signal number %ld out of range", signum); |
| 1342 | } |
| 1343 | goto error; |
| 1344 | } |
| 1345 | if (sigaddset(mask, (int)signum)) { |
| 1346 | if (errno != EINVAL) { |
| 1347 | /* Probably impossible */ |
| 1348 | PyErr_SetFromErrno(PyExc_OSError); |
| 1349 | goto error; |
| 1350 | } |
| 1351 | /* For backwards compatibility, allow idioms such as |
| 1352 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1353 | */ |
| 1354 | const char msg[] = |
| 1355 | "invalid signal number %ld, please use valid_signals()"; |
| 1356 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1357 | goto error; |
| 1358 | } |
| 1359 | } |
| 1360 | } |
| 1361 | if (!PyErr_Occurred()) { |
| 1362 | Py_DECREF(iterator); |
| 1363 | return 1; |
| 1364 | } |
| 1365 | |
| 1366 | error: |
| 1367 | Py_DECREF(iterator); |
| 1368 | return 0; |
| 1369 | } |
| 1370 | #endif /* HAVE_SIGSET_T */ |
| 1371 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1372 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1373 | |
| 1374 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1375 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1376 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1377 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1378 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1379 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1380 | |
| 1381 | if (0 == DeviceIoControl( |
| 1382 | reparse_point_handle, |
| 1383 | FSCTL_GET_REPARSE_POINT, |
| 1384 | NULL, 0, /* in buffer */ |
| 1385 | target_buffer, sizeof(target_buffer), |
| 1386 | &n_bytes_returned, |
| 1387 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1388 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1389 | |
| 1390 | if (reparse_tag) |
| 1391 | *reparse_tag = rdb->ReparseTag; |
| 1392 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1393 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1394 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1395 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1396 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1397 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1398 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1399 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1400 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1401 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1402 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1403 | */ |
| 1404 | #include <crt_externs.h> |
| 1405 | static char **environ; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1406 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1407 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1408 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1409 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1410 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1411 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1412 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1413 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1414 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1415 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1416 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1417 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1418 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1419 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1420 | d = PyDict_New(); |
| 1421 | if (d == NULL) |
| 1422 | return NULL; |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1423 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1424 | if (environ == NULL) |
| 1425 | environ = *_NSGetEnviron(); |
| 1426 | #endif |
| 1427 | #ifdef MS_WINDOWS |
| 1428 | /* _wenviron must be initialized in this way if the program is started |
| 1429 | through main() instead of wmain(). */ |
| 1430 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1431 | e = _wenviron; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1432 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1433 | e = environ; |
| 1434 | #endif |
| 1435 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1436 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1437 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1438 | PyObject *k; |
| 1439 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1440 | #ifdef MS_WINDOWS |
| 1441 | const wchar_t *p = wcschr(*e, L'='); |
| 1442 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1443 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1444 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1445 | if (p == NULL) |
| 1446 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1447 | #ifdef MS_WINDOWS |
| 1448 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1449 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1450 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1451 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1452 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1453 | Py_DECREF(d); |
| 1454 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1455 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1456 | #ifdef MS_WINDOWS |
| 1457 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1458 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1459 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1460 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1461 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1462 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1463 | Py_DECREF(d); |
| 1464 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1465 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1466 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1467 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1468 | Py_DECREF(v); |
| 1469 | Py_DECREF(k); |
| 1470 | Py_DECREF(d); |
| 1471 | return NULL; |
| 1472 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1473 | } |
| 1474 | Py_DECREF(k); |
| 1475 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1476 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1477 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1480 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1481 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1482 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1483 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1484 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1485 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1486 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1487 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1488 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1489 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1490 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1491 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1492 | /* XXX We should pass the function name along in the future. |
| 1493 | (winreg.c also wants to pass the function name.) |
| 1494 | This would however require an additional param to the |
| 1495 | Windows error object, which is non-trivial. |
| 1496 | */ |
| 1497 | errno = GetLastError(); |
| 1498 | if (filename) |
| 1499 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1500 | else |
| 1501 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1502 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1503 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1504 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1505 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1506 | { |
| 1507 | /* XXX - see win32_error for comments on 'function' */ |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1508 | if (filename) |
| 1509 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1510 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1511 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1512 | filename); |
| 1513 | else |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1514 | return PyErr_SetFromWindowsErr(err); |
| 1515 | } |
| 1516 | |
| 1517 | static PyObject * |
| 1518 | win32_error_object(const char* function, PyObject* filename) |
| 1519 | { |
| 1520 | errno = GetLastError(); |
| 1521 | return win32_error_object_err(function, filename, errno); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1522 | } |
| 1523 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1524 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1525 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1526 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1527 | posix_path_object_error(PyObject *path) |
| 1528 | { |
| 1529 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1530 | } |
| 1531 | |
| 1532 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1533 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1534 | { |
| 1535 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1536 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1537 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1538 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1539 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1540 | #endif |
| 1541 | } |
| 1542 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1543 | static PyObject * |
| 1544 | path_object_error2(PyObject *path, PyObject *path2) |
| 1545 | { |
| 1546 | #ifdef MS_WINDOWS |
| 1547 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1548 | PyExc_OSError, 0, path, path2); |
| 1549 | #else |
| 1550 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1551 | #endif |
| 1552 | } |
| 1553 | |
| 1554 | static PyObject * |
| 1555 | path_error(path_t *path) |
| 1556 | { |
| 1557 | return path_object_error(path->object); |
| 1558 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1559 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1560 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1561 | posix_path_error(path_t *path) |
| 1562 | { |
| 1563 | return posix_path_object_error(path->object); |
| 1564 | } |
| 1565 | |
| 1566 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1567 | path_error2(path_t *path, path_t *path2) |
| 1568 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1569 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1573 | /* POSIX generic methods */ |
| 1574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1575 | static int |
| 1576 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1577 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1578 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1579 | int *pointer = (int *)p; |
| 1580 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1581 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1582 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1583 | *pointer = fd; |
| 1584 | return 1; |
| 1585 | } |
| 1586 | |
| 1587 | static PyObject * |
| 1588 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1589 | { |
| 1590 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1591 | int async_err = 0; |
| 1592 | |
| 1593 | do { |
| 1594 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1595 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1596 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1597 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1598 | Py_END_ALLOW_THREADS |
| 1599 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1600 | if (res != 0) |
| 1601 | return (!async_err) ? posix_error() : NULL; |
| 1602 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1603 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1604 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1605 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1606 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1607 | /* This is a reimplementation of the C library's chdir function, |
| 1608 | but one that produces Win32 errors instead of DOS error codes. |
| 1609 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1610 | it also needs to set "magic" environment variables indicating |
| 1611 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1612 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1613 | win32_wchdir(LPCWSTR path) |
| 1614 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1615 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1616 | int result; |
| 1617 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1618 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1619 | if(!SetCurrentDirectoryW(path)) |
| 1620 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1621 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1622 | if (!result) |
| 1623 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1624 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1625 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1626 | if (!new_path) { |
| 1627 | SetLastError(ERROR_OUTOFMEMORY); |
| 1628 | return FALSE; |
| 1629 | } |
| 1630 | result = GetCurrentDirectoryW(result, new_path); |
| 1631 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1632 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1633 | return FALSE; |
| 1634 | } |
| 1635 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1636 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1637 | wcsncmp(new_path, L"//", 2) == 0); |
| 1638 | if (!is_unc_like_path) { |
| 1639 | env[1] = new_path[0]; |
| 1640 | result = SetEnvironmentVariableW(env, new_path); |
| 1641 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1642 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1643 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1644 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1645 | } |
| 1646 | #endif |
| 1647 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1648 | #ifdef MS_WINDOWS |
| 1649 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1650 | - time stamps are restricted to second resolution |
| 1651 | - file modification times suffer from forth-and-back conversions between |
| 1652 | UTC and local time |
| 1653 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1654 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1655 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1656 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1657 | #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1658 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1659 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1660 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1661 | BY_HANDLE_FILE_INFORMATION *info, |
| 1662 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1663 | { |
| 1664 | memset(info, 0, sizeof(*info)); |
| 1665 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1666 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1667 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1668 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1669 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1670 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1671 | /* info->nNumberOfLinks = 1; */ |
| 1672 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1673 | *reparse_tag = pFileData->dwReserved0; |
| 1674 | else |
| 1675 | *reparse_tag = 0; |
| 1676 | } |
| 1677 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1678 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1679 | attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1680 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1681 | HANDLE hFindFile; |
| 1682 | WIN32_FIND_DATAW FileData; |
| 1683 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1684 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1685 | return FALSE; |
| 1686 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1687 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1688 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1691 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1692 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1693 | BOOL traverse) |
| 1694 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1695 | HANDLE hFile; |
| 1696 | BY_HANDLE_FILE_INFORMATION fileInfo; |
| 1697 | FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 }; |
| 1698 | DWORD fileType, error; |
| 1699 | BOOL isUnhandledTag = FALSE; |
| 1700 | int retval = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1701 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1702 | DWORD access = FILE_READ_ATTRIBUTES; |
| 1703 | DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */ |
| 1704 | if (!traverse) { |
| 1705 | flags |= FILE_FLAG_OPEN_REPARSE_POINT; |
| 1706 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1707 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1708 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1709 | if (hFile == INVALID_HANDLE_VALUE) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1710 | /* Either the path doesn't exist, or the caller lacks access. */ |
| 1711 | error = GetLastError(); |
| 1712 | switch (error) { |
| 1713 | case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */ |
| 1714 | case ERROR_SHARING_VIOLATION: /* It's a paging file. */ |
| 1715 | /* Try reading the parent directory. */ |
| 1716 | if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) { |
| 1717 | /* Cannot read the parent directory. */ |
| 1718 | SetLastError(error); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1719 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1720 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1721 | if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1722 | if (traverse || |
| 1723 | !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1724 | /* The stat call has to traverse but cannot, so fail. */ |
| 1725 | SetLastError(error); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1726 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1727 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1728 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1729 | break; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1730 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1731 | case ERROR_INVALID_PARAMETER: |
| 1732 | /* \\.\con requires read or write access. */ |
| 1733 | hFile = CreateFileW(path, access | GENERIC_READ, |
| 1734 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1735 | OPEN_EXISTING, flags, NULL); |
| 1736 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1737 | SetLastError(error); |
| 1738 | return -1; |
| 1739 | } |
| 1740 | break; |
| 1741 | |
| 1742 | case ERROR_CANT_ACCESS_FILE: |
| 1743 | /* bpo37834: open unhandled reparse points if traverse fails. */ |
| 1744 | if (traverse) { |
| 1745 | traverse = FALSE; |
| 1746 | isUnhandledTag = TRUE; |
| 1747 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, |
| 1748 | flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 1749 | } |
| 1750 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1751 | SetLastError(error); |
| 1752 | return -1; |
| 1753 | } |
| 1754 | break; |
| 1755 | |
| 1756 | default: |
| 1757 | return -1; |
| 1758 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1759 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1760 | |
| 1761 | if (hFile != INVALID_HANDLE_VALUE) { |
| 1762 | /* Handle types other than files on disk. */ |
| 1763 | fileType = GetFileType(hFile); |
| 1764 | if (fileType != FILE_TYPE_DISK) { |
| 1765 | if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) { |
| 1766 | retval = -1; |
| 1767 | goto cleanup; |
| 1768 | } |
| 1769 | DWORD fileAttributes = GetFileAttributesW(path); |
| 1770 | memset(result, 0, sizeof(*result)); |
| 1771 | if (fileAttributes != INVALID_FILE_ATTRIBUTES && |
| 1772 | fileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 1773 | /* \\.\pipe\ or \\.\mailslot\ */ |
| 1774 | result->st_mode = _S_IFDIR; |
| 1775 | } else if (fileType == FILE_TYPE_CHAR) { |
| 1776 | /* \\.\nul */ |
| 1777 | result->st_mode = _S_IFCHR; |
| 1778 | } else if (fileType == FILE_TYPE_PIPE) { |
| 1779 | /* \\.\pipe\spam */ |
| 1780 | result->st_mode = _S_IFIFO; |
| 1781 | } |
| 1782 | /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */ |
| 1783 | goto cleanup; |
| 1784 | } |
| 1785 | |
| 1786 | /* Query the reparse tag, and traverse a non-link. */ |
| 1787 | if (!traverse) { |
| 1788 | if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, |
| 1789 | &tagInfo, sizeof(tagInfo))) { |
| 1790 | /* Allow devices that do not support FileAttributeTagInfo. */ |
| 1791 | switch (GetLastError()) { |
| 1792 | case ERROR_INVALID_PARAMETER: |
| 1793 | case ERROR_INVALID_FUNCTION: |
| 1794 | case ERROR_NOT_SUPPORTED: |
| 1795 | tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL; |
| 1796 | tagInfo.ReparseTag = 0; |
| 1797 | break; |
| 1798 | default: |
| 1799 | retval = -1; |
| 1800 | goto cleanup; |
| 1801 | } |
| 1802 | } else if (tagInfo.FileAttributes & |
| 1803 | FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1804 | if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1805 | if (isUnhandledTag) { |
| 1806 | /* Traversing previously failed for either this link |
| 1807 | or its target. */ |
| 1808 | SetLastError(ERROR_CANT_ACCESS_FILE); |
| 1809 | retval = -1; |
| 1810 | goto cleanup; |
| 1811 | } |
| 1812 | /* Traverse a non-link, but not if traversing already failed |
| 1813 | for an unhandled tag. */ |
| 1814 | } else if (!isUnhandledTag) { |
| 1815 | CloseHandle(hFile); |
| 1816 | return win32_xstat_impl(path, result, TRUE); |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | if (!GetFileInformationByHandle(hFile, &fileInfo)) { |
| 1822 | switch (GetLastError()) { |
| 1823 | case ERROR_INVALID_PARAMETER: |
| 1824 | case ERROR_INVALID_FUNCTION: |
| 1825 | case ERROR_NOT_SUPPORTED: |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1826 | /* Volumes and physical disks are block devices, e.g. |
| 1827 | \\.\C: and \\.\PhysicalDrive0. */ |
| 1828 | memset(result, 0, sizeof(*result)); |
| 1829 | result->st_mode = 0x6000; /* S_IFBLK */ |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1830 | goto cleanup; |
| 1831 | } |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1832 | retval = -1; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1833 | goto cleanup; |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result); |
| 1838 | |
| 1839 | if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1840 | /* Fix the file execute permissions. This hack sets S_IEXEC if |
| 1841 | the filename has an extension that is commonly used by files |
| 1842 | that CreateProcessW can execute. A real implementation calls |
| 1843 | GetSecurityInfo, OpenThreadToken/OpenProcessToken, and |
| 1844 | AccessCheck to check for generic read, write, and execute |
| 1845 | access. */ |
| 1846 | const wchar_t *fileExtension = wcsrchr(path, '.'); |
| 1847 | if (fileExtension) { |
| 1848 | if (_wcsicmp(fileExtension, L".exe") == 0 || |
| 1849 | _wcsicmp(fileExtension, L".bat") == 0 || |
| 1850 | _wcsicmp(fileExtension, L".cmd") == 0 || |
| 1851 | _wcsicmp(fileExtension, L".com") == 0) { |
| 1852 | result->st_mode |= 0111; |
| 1853 | } |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | cleanup: |
| 1858 | if (hFile != INVALID_HANDLE_VALUE) { |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1859 | /* Preserve last error if we are failing */ |
| 1860 | error = retval ? GetLastError() : 0; |
| 1861 | if (!CloseHandle(hFile)) { |
| 1862 | retval = -1; |
| 1863 | } else if (retval) { |
| 1864 | /* Restore last error */ |
| 1865 | SetLastError(error); |
| 1866 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | return retval; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1873 | win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1874 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1875 | /* Protocol violation: we explicitly clear errno, instead of |
| 1876 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1877 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1878 | errno = 0; |
| 1879 | return code; |
| 1880 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1881 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1882 | |
| 1883 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1884 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1885 | default does not traverse symlinks and instead returns attributes for |
| 1886 | the symlink. |
| 1887 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1888 | Instead, we will open the file (which *does* traverse symlinks by default) |
| 1889 | and GetFileInformationByHandle(). */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1890 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1891 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1892 | win32_lstat(const wchar_t* path, struct _Py_stat_struct *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1893 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1894 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1897 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1898 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1899 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1900 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1903 | #endif /* MS_WINDOWS */ |
| 1904 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1905 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1906 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1907 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1908 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1909 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1910 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1911 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1912 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1913 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1914 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1915 | |
| 1916 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1917 | {"st_mode", "protection bits"}, |
| 1918 | {"st_ino", "inode"}, |
| 1919 | {"st_dev", "device"}, |
| 1920 | {"st_nlink", "number of hard links"}, |
| 1921 | {"st_uid", "user ID of owner"}, |
| 1922 | {"st_gid", "group ID of owner"}, |
| 1923 | {"st_size", "total size, in bytes"}, |
| 1924 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1925 | {NULL, "integer time of last access"}, |
| 1926 | {NULL, "integer time of last modification"}, |
| 1927 | {NULL, "integer time of last change"}, |
| 1928 | {"st_atime", "time of last access"}, |
| 1929 | {"st_mtime", "time of last modification"}, |
| 1930 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1931 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1932 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1933 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1934 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1935 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1936 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1937 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1938 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1939 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1940 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1941 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1942 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1943 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1944 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1945 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1946 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1947 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1948 | #endif |
| 1949 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1950 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1951 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1952 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1953 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1954 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1955 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1956 | {"st_fstype", "Type of filesystem"}, |
| 1957 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1958 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 1959 | {"st_reparse_tag", "Windows reparse tag"}, |
| 1960 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1961 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1962 | }; |
| 1963 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1964 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1965 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1966 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1967 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1968 | #endif |
| 1969 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1970 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1971 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1972 | #else |
| 1973 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1974 | #endif |
| 1975 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1976 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1977 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1978 | #else |
| 1979 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1980 | #endif |
| 1981 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1982 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1983 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1984 | #else |
| 1985 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1986 | #endif |
| 1987 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1988 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1989 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1990 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1991 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1992 | #endif |
| 1993 | |
| 1994 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1995 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1996 | #else |
| 1997 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1998 | #endif |
| 1999 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2000 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2001 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 2002 | #else |
| 2003 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 2004 | #endif |
| 2005 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2006 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2007 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 2008 | #else |
| 2009 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 2010 | #endif |
| 2011 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2012 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2013 | #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1) |
| 2014 | #else |
| 2015 | #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX |
| 2016 | #endif |
| 2017 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2018 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2019 | "stat_result", /* name */ |
| 2020 | stat_result__doc__, /* doc */ |
| 2021 | stat_result_fields, |
| 2022 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2023 | }; |
| 2024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2025 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2026 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2027 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2028 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2029 | or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2030 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2031 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2032 | |
| 2033 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2034 | {"f_bsize", }, |
| 2035 | {"f_frsize", }, |
| 2036 | {"f_blocks", }, |
| 2037 | {"f_bfree", }, |
| 2038 | {"f_bavail", }, |
| 2039 | {"f_files", }, |
| 2040 | {"f_ffree", }, |
| 2041 | {"f_favail", }, |
| 2042 | {"f_flag", }, |
| 2043 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 2044 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2045 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2046 | }; |
| 2047 | |
| 2048 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2049 | "statvfs_result", /* name */ |
| 2050 | statvfs_result__doc__, /* doc */ |
| 2051 | statvfs_result_fields, |
| 2052 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2053 | }; |
| 2054 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2055 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2056 | PyDoc_STRVAR(waitid_result__doc__, |
| 2057 | "waitid_result: Result from waitid.\n\n\ |
| 2058 | This object may be accessed either as a tuple of\n\ |
| 2059 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2060 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2061 | \n\ |
| 2062 | See os.waitid for more information."); |
| 2063 | |
| 2064 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2065 | {"si_pid", }, |
| 2066 | {"si_uid", }, |
| 2067 | {"si_signo", }, |
| 2068 | {"si_status", }, |
| 2069 | {"si_code", }, |
| 2070 | {0} |
| 2071 | }; |
| 2072 | |
| 2073 | static PyStructSequence_Desc waitid_result_desc = { |
| 2074 | "waitid_result", /* name */ |
| 2075 | waitid_result__doc__, /* doc */ |
| 2076 | waitid_result_fields, |
| 2077 | 5 |
| 2078 | }; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2079 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2080 | static newfunc structseq_new; |
| 2081 | |
| 2082 | static PyObject * |
| 2083 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2084 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2085 | PyStructSequence *result; |
| 2086 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2087 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2088 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2089 | if (!result) |
| 2090 | return NULL; |
| 2091 | /* If we have been initialized from a tuple, |
| 2092 | st_?time might be set to None. Initialize it |
| 2093 | from the int slots. */ |
| 2094 | for (i = 7; i <= 9; i++) { |
| 2095 | if (result->ob_item[i+3] == Py_None) { |
| 2096 | Py_DECREF(Py_None); |
| 2097 | Py_INCREF(result->ob_item[i]); |
| 2098 | result->ob_item[i+3] = result->ob_item[i]; |
| 2099 | } |
| 2100 | } |
| 2101 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2104 | static int |
| 2105 | _posix_clear(PyObject *module) |
| 2106 | { |
| 2107 | Py_CLEAR(_posixstate(module)->billion); |
| 2108 | Py_CLEAR(_posixstate(module)->posix_putenv_garbage); |
| 2109 | Py_CLEAR(_posixstate(module)->DirEntryType); |
| 2110 | Py_CLEAR(_posixstate(module)->ScandirIteratorType); |
| 2111 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 2112 | Py_CLEAR(_posixstate(module)->SchedParamType); |
| 2113 | #endif |
| 2114 | Py_CLEAR(_posixstate(module)->StatResultType); |
| 2115 | Py_CLEAR(_posixstate(module)->StatVFSResultType); |
| 2116 | Py_CLEAR(_posixstate(module)->TerminalSizeType); |
| 2117 | Py_CLEAR(_posixstate(module)->TimesResultType); |
| 2118 | Py_CLEAR(_posixstate(module)->UnameResultType); |
| 2119 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2120 | Py_CLEAR(_posixstate(module)->WaitidResultType); |
| 2121 | #endif |
| 2122 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 2123 | Py_CLEAR(_posixstate(module)->struct_rusage); |
| 2124 | #endif |
| 2125 | Py_CLEAR(_posixstate(module)->st_mode); |
| 2126 | return 0; |
| 2127 | } |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2128 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2129 | static int |
| 2130 | _posix_traverse(PyObject *module, visitproc visit, void *arg) |
| 2131 | { |
| 2132 | Py_VISIT(_posixstate(module)->billion); |
| 2133 | Py_VISIT(_posixstate(module)->posix_putenv_garbage); |
| 2134 | Py_VISIT(_posixstate(module)->DirEntryType); |
| 2135 | Py_VISIT(_posixstate(module)->ScandirIteratorType); |
| 2136 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 2137 | Py_VISIT(_posixstate(module)->SchedParamType); |
| 2138 | #endif |
| 2139 | Py_VISIT(_posixstate(module)->StatResultType); |
| 2140 | Py_VISIT(_posixstate(module)->StatVFSResultType); |
| 2141 | Py_VISIT(_posixstate(module)->TerminalSizeType); |
| 2142 | Py_VISIT(_posixstate(module)->TimesResultType); |
| 2143 | Py_VISIT(_posixstate(module)->UnameResultType); |
| 2144 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2145 | Py_VISIT(_posixstate(module)->WaitidResultType); |
| 2146 | #endif |
| 2147 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 2148 | Py_VISIT(_posixstate(module)->struct_rusage); |
| 2149 | #endif |
| 2150 | Py_VISIT(_posixstate(module)->st_mode); |
| 2151 | return 0; |
| 2152 | } |
| 2153 | |
| 2154 | static void |
| 2155 | _posix_free(void *module) |
| 2156 | { |
| 2157 | _posix_clear((PyObject *)module); |
| 2158 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2159 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2160 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2161 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2162 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2163 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2164 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2165 | PyObject *s_in_ns = NULL; |
| 2166 | PyObject *ns_total = NULL; |
| 2167 | PyObject *float_s = NULL; |
| 2168 | |
| 2169 | if (!(s && ns_fractional)) |
| 2170 | goto exit; |
| 2171 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2172 | s_in_ns = PyNumber_Multiply(s, _posixstate_global->billion); |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2173 | if (!s_in_ns) |
| 2174 | goto exit; |
| 2175 | |
| 2176 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2177 | if (!ns_total) |
| 2178 | goto exit; |
| 2179 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2180 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2181 | if (!float_s) { |
| 2182 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | PyStructSequence_SET_ITEM(v, index, s); |
| 2186 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2187 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2188 | s = NULL; |
| 2189 | float_s = NULL; |
| 2190 | ns_total = NULL; |
| 2191 | exit: |
| 2192 | Py_XDECREF(s); |
| 2193 | Py_XDECREF(ns_fractional); |
| 2194 | Py_XDECREF(s_in_ns); |
| 2195 | Py_XDECREF(ns_total); |
| 2196 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2199 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2200 | (used by posix_stat() and posix_fstat()) */ |
| 2201 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2202 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2203 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2204 | unsigned long ansec, mnsec, cnsec; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2205 | PyObject *StatResultType = _posixstate_global->StatResultType; |
| 2206 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2207 | if (v == NULL) |
| 2208 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2209 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2210 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2211 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2212 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2213 | #ifdef MS_WINDOWS |
| 2214 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2215 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2216 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2217 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2218 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2219 | #if defined(MS_WINDOWS) |
| 2220 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2221 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2222 | #else |
| 2223 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2224 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2225 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2226 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2227 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2228 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2229 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2230 | ansec = st->st_atim.tv_nsec; |
| 2231 | mnsec = st->st_mtim.tv_nsec; |
| 2232 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2233 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2234 | ansec = st->st_atimespec.tv_nsec; |
| 2235 | mnsec = st->st_mtimespec.tv_nsec; |
| 2236 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2237 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2238 | ansec = st->st_atime_nsec; |
| 2239 | mnsec = st->st_mtime_nsec; |
| 2240 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2241 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2242 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2243 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2244 | fill_time(v, 7, st->st_atime, ansec); |
| 2245 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2246 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2247 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2248 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2249 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2250 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2251 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2252 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2253 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2254 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2255 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2256 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2257 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2258 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2259 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2260 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2261 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2262 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2263 | #endif |
| 2264 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2265 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2266 | PyObject *val; |
| 2267 | unsigned long bsec,bnsec; |
| 2268 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2269 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2270 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2271 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2272 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2273 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2274 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2275 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2276 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2277 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2278 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2279 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2280 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2281 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2282 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2283 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2284 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2285 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2286 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2287 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2288 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2289 | PyUnicode_FromString(st->st_fstype)); |
| 2290 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2291 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2292 | PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX, |
| 2293 | PyLong_FromUnsignedLong(st->st_reparse_tag)); |
| 2294 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2295 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2296 | if (PyErr_Occurred()) { |
| 2297 | Py_DECREF(v); |
| 2298 | return NULL; |
| 2299 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2300 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2301 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2304 | /* POSIX methods */ |
| 2305 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2306 | |
| 2307 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2308 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2309 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2310 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2311 | STRUCT_STAT st; |
| 2312 | int result; |
| 2313 | |
| 2314 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2315 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2316 | return NULL; |
| 2317 | #endif |
| 2318 | |
| 2319 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2320 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2321 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2322 | return NULL; |
| 2323 | |
| 2324 | Py_BEGIN_ALLOW_THREADS |
| 2325 | if (path->fd != -1) |
| 2326 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2327 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2328 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2329 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2330 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2331 | result = win32_lstat(path->wide, &st); |
| 2332 | #else |
| 2333 | else |
| 2334 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2335 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2336 | result = LSTAT(path->narrow, &st); |
| 2337 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2338 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2339 | #ifdef HAVE_FSTATAT |
| 2340 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2341 | result = fstatat(dir_fd, path->narrow, &st, |
| 2342 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2343 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2344 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2345 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2346 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2347 | Py_END_ALLOW_THREADS |
| 2348 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2349 | if (result != 0) { |
| 2350 | return path_error(path); |
| 2351 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2352 | |
| 2353 | return _pystat_fromstructstat(&st); |
| 2354 | } |
| 2355 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2356 | /*[python input] |
| 2357 | |
| 2358 | for s in """ |
| 2359 | |
| 2360 | FACCESSAT |
| 2361 | FCHMODAT |
| 2362 | FCHOWNAT |
| 2363 | FSTATAT |
| 2364 | LINKAT |
| 2365 | MKDIRAT |
| 2366 | MKFIFOAT |
| 2367 | MKNODAT |
| 2368 | OPENAT |
| 2369 | READLINKAT |
| 2370 | SYMLINKAT |
| 2371 | UNLINKAT |
| 2372 | |
| 2373 | """.strip().split(): |
| 2374 | s = s.strip() |
| 2375 | print(""" |
| 2376 | #ifdef HAVE_{s} |
| 2377 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2378 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2379 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2380 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2381 | """.rstrip().format(s=s)) |
| 2382 | |
| 2383 | for s in """ |
| 2384 | |
| 2385 | FCHDIR |
| 2386 | FCHMOD |
| 2387 | FCHOWN |
| 2388 | FDOPENDIR |
| 2389 | FEXECVE |
| 2390 | FPATHCONF |
| 2391 | FSTATVFS |
| 2392 | FTRUNCATE |
| 2393 | |
| 2394 | """.strip().split(): |
| 2395 | s = s.strip() |
| 2396 | print(""" |
| 2397 | #ifdef HAVE_{s} |
| 2398 | #define PATH_HAVE_{s} 1 |
| 2399 | #else |
| 2400 | #define PATH_HAVE_{s} 0 |
| 2401 | #endif |
| 2402 | |
| 2403 | """.rstrip().format(s=s)) |
| 2404 | [python start generated code]*/ |
| 2405 | |
| 2406 | #ifdef HAVE_FACCESSAT |
| 2407 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2408 | #else |
| 2409 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2410 | #endif |
| 2411 | |
| 2412 | #ifdef HAVE_FCHMODAT |
| 2413 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2414 | #else |
| 2415 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2416 | #endif |
| 2417 | |
| 2418 | #ifdef HAVE_FCHOWNAT |
| 2419 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2420 | #else |
| 2421 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2422 | #endif |
| 2423 | |
| 2424 | #ifdef HAVE_FSTATAT |
| 2425 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2426 | #else |
| 2427 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2428 | #endif |
| 2429 | |
| 2430 | #ifdef HAVE_LINKAT |
| 2431 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2432 | #else |
| 2433 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2434 | #endif |
| 2435 | |
| 2436 | #ifdef HAVE_MKDIRAT |
| 2437 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2438 | #else |
| 2439 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2440 | #endif |
| 2441 | |
| 2442 | #ifdef HAVE_MKFIFOAT |
| 2443 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2444 | #else |
| 2445 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2446 | #endif |
| 2447 | |
| 2448 | #ifdef HAVE_MKNODAT |
| 2449 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2450 | #else |
| 2451 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2452 | #endif |
| 2453 | |
| 2454 | #ifdef HAVE_OPENAT |
| 2455 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2456 | #else |
| 2457 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2458 | #endif |
| 2459 | |
| 2460 | #ifdef HAVE_READLINKAT |
| 2461 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2462 | #else |
| 2463 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2464 | #endif |
| 2465 | |
| 2466 | #ifdef HAVE_SYMLINKAT |
| 2467 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2468 | #else |
| 2469 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2470 | #endif |
| 2471 | |
| 2472 | #ifdef HAVE_UNLINKAT |
| 2473 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2474 | #else |
| 2475 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2476 | #endif |
| 2477 | |
| 2478 | #ifdef HAVE_FCHDIR |
| 2479 | #define PATH_HAVE_FCHDIR 1 |
| 2480 | #else |
| 2481 | #define PATH_HAVE_FCHDIR 0 |
| 2482 | #endif |
| 2483 | |
| 2484 | #ifdef HAVE_FCHMOD |
| 2485 | #define PATH_HAVE_FCHMOD 1 |
| 2486 | #else |
| 2487 | #define PATH_HAVE_FCHMOD 0 |
| 2488 | #endif |
| 2489 | |
| 2490 | #ifdef HAVE_FCHOWN |
| 2491 | #define PATH_HAVE_FCHOWN 1 |
| 2492 | #else |
| 2493 | #define PATH_HAVE_FCHOWN 0 |
| 2494 | #endif |
| 2495 | |
| 2496 | #ifdef HAVE_FDOPENDIR |
| 2497 | #define PATH_HAVE_FDOPENDIR 1 |
| 2498 | #else |
| 2499 | #define PATH_HAVE_FDOPENDIR 0 |
| 2500 | #endif |
| 2501 | |
| 2502 | #ifdef HAVE_FEXECVE |
| 2503 | #define PATH_HAVE_FEXECVE 1 |
| 2504 | #else |
| 2505 | #define PATH_HAVE_FEXECVE 0 |
| 2506 | #endif |
| 2507 | |
| 2508 | #ifdef HAVE_FPATHCONF |
| 2509 | #define PATH_HAVE_FPATHCONF 1 |
| 2510 | #else |
| 2511 | #define PATH_HAVE_FPATHCONF 0 |
| 2512 | #endif |
| 2513 | |
| 2514 | #ifdef HAVE_FSTATVFS |
| 2515 | #define PATH_HAVE_FSTATVFS 1 |
| 2516 | #else |
| 2517 | #define PATH_HAVE_FSTATVFS 0 |
| 2518 | #endif |
| 2519 | |
| 2520 | #ifdef HAVE_FTRUNCATE |
| 2521 | #define PATH_HAVE_FTRUNCATE 1 |
| 2522 | #else |
| 2523 | #define PATH_HAVE_FTRUNCATE 0 |
| 2524 | #endif |
| 2525 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2526 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2527 | #ifdef MS_WINDOWS |
| 2528 | #undef PATH_HAVE_FTRUNCATE |
| 2529 | #define PATH_HAVE_FTRUNCATE 1 |
| 2530 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2531 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2532 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2533 | |
| 2534 | class path_t_converter(CConverter): |
| 2535 | |
| 2536 | type = "path_t" |
| 2537 | impl_by_reference = True |
| 2538 | parse_by_reference = True |
| 2539 | |
| 2540 | converter = 'path_converter' |
| 2541 | |
| 2542 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2543 | # right now path_t doesn't support default values. |
| 2544 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2545 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2546 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2547 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2548 | if self.c_default not in (None, 'Py_None'): |
| 2549 | raise RuntimeError("Can't specify a c_default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2550 | |
| 2551 | self.nullable = nullable |
| 2552 | self.allow_fd = allow_fd |
| 2553 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2554 | def pre_render(self): |
| 2555 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2556 | if isinstance(value, str): |
| 2557 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2558 | return str(int(bool(value))) |
| 2559 | |
| 2560 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2561 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2562 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2563 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2564 | strify(self.nullable), |
| 2565 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2566 | ) |
| 2567 | |
| 2568 | def cleanup(self): |
| 2569 | return "path_cleanup(&" + self.name + ");\n" |
| 2570 | |
| 2571 | |
| 2572 | class dir_fd_converter(CConverter): |
| 2573 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2575 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2576 | if self.default in (unspecified, None): |
| 2577 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2578 | if isinstance(requires, str): |
| 2579 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2580 | else: |
| 2581 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2582 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2583 | class fildes_converter(CConverter): |
| 2584 | type = 'int' |
| 2585 | converter = 'fildes_converter' |
| 2586 | |
| 2587 | class uid_t_converter(CConverter): |
| 2588 | type = "uid_t" |
| 2589 | converter = '_Py_Uid_Converter' |
| 2590 | |
| 2591 | class gid_t_converter(CConverter): |
| 2592 | type = "gid_t" |
| 2593 | converter = '_Py_Gid_Converter' |
| 2594 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2595 | class dev_t_converter(CConverter): |
| 2596 | type = 'dev_t' |
| 2597 | converter = '_Py_Dev_Converter' |
| 2598 | |
| 2599 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2600 | type = 'dev_t' |
| 2601 | conversion_fn = '_PyLong_FromDev' |
| 2602 | unsigned_cast = '(dev_t)' |
| 2603 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2604 | class FSConverter_converter(CConverter): |
| 2605 | type = 'PyObject *' |
| 2606 | converter = 'PyUnicode_FSConverter' |
| 2607 | def converter_init(self): |
| 2608 | if self.default is not unspecified: |
| 2609 | fail("FSConverter_converter does not support default values") |
| 2610 | self.c_default = 'NULL' |
| 2611 | |
| 2612 | def cleanup(self): |
| 2613 | return "Py_XDECREF(" + self.name + ");\n" |
| 2614 | |
| 2615 | class pid_t_converter(CConverter): |
| 2616 | type = 'pid_t' |
| 2617 | format_unit = '" _Py_PARSE_PID "' |
| 2618 | |
| 2619 | class idtype_t_converter(int_converter): |
| 2620 | type = 'idtype_t' |
| 2621 | |
| 2622 | class id_t_converter(CConverter): |
| 2623 | type = 'id_t' |
| 2624 | format_unit = '" _Py_PARSE_PID "' |
| 2625 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2626 | class intptr_t_converter(CConverter): |
| 2627 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2628 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2629 | |
| 2630 | class Py_off_t_converter(CConverter): |
| 2631 | type = 'Py_off_t' |
| 2632 | converter = 'Py_off_t_converter' |
| 2633 | |
| 2634 | class Py_off_t_return_converter(long_return_converter): |
| 2635 | type = 'Py_off_t' |
| 2636 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2637 | |
| 2638 | class path_confname_converter(CConverter): |
| 2639 | type="int" |
| 2640 | converter="conv_path_confname" |
| 2641 | |
| 2642 | class confstr_confname_converter(path_confname_converter): |
| 2643 | converter='conv_confstr_confname' |
| 2644 | |
| 2645 | class sysconf_confname_converter(path_confname_converter): |
| 2646 | converter="conv_sysconf_confname" |
| 2647 | |
| 2648 | class sched_param_converter(CConverter): |
| 2649 | type = 'struct sched_param' |
| 2650 | converter = 'convert_sched_param' |
| 2651 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2652 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2653 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2654 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2655 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2656 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2657 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2658 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2659 | |
| 2660 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2661 | Path to be examined; can be string, bytes, a path-like object or |
Xiang Zhang | 4459e00 | 2017-01-22 13:04:17 +0800 | [diff] [blame] | 2662 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2663 | |
| 2664 | * |
| 2665 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2666 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2667 | If not None, it should be a file descriptor open to a directory, |
| 2668 | and path should be a relative string; path will then be relative to |
| 2669 | that directory. |
| 2670 | |
| 2671 | follow_symlinks: bool = True |
| 2672 | If False, and the last element of the path is a symbolic link, |
| 2673 | stat will examine the symbolic link itself instead of the file |
| 2674 | the link points to. |
| 2675 | |
| 2676 | Perform a stat system call on the given path. |
| 2677 | |
| 2678 | dir_fd and follow_symlinks may not be implemented |
| 2679 | on your platform. If they are unavailable, using them will raise a |
| 2680 | NotImplementedError. |
| 2681 | |
| 2682 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2683 | an open file descriptor. |
| 2684 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2685 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2686 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2687 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2688 | os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2689 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2690 | { |
| 2691 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2692 | } |
| 2693 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2694 | |
| 2695 | /*[clinic input] |
| 2696 | os.lstat |
| 2697 | |
| 2698 | path : path_t |
| 2699 | |
| 2700 | * |
| 2701 | |
| 2702 | dir_fd : dir_fd(requires='fstatat') = None |
| 2703 | |
| 2704 | Perform a stat system call on the given path, without following symbolic links. |
| 2705 | |
| 2706 | Like stat(), but do not follow symbolic links. |
| 2707 | Equivalent to stat(path, follow_symlinks=False). |
| 2708 | [clinic start generated code]*/ |
| 2709 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2710 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2711 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2712 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2713 | { |
| 2714 | int follow_symlinks = 0; |
| 2715 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2716 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2717 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2718 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2719 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2720 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2721 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2722 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2723 | Path to be tested; can be string, bytes, or a path-like object. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2724 | |
| 2725 | mode: int |
| 2726 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2727 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2728 | |
| 2729 | * |
| 2730 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2731 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2732 | If not None, it should be a file descriptor open to a directory, |
| 2733 | and path should be relative; path will then be relative to that |
| 2734 | directory. |
| 2735 | |
| 2736 | effective_ids: bool = False |
| 2737 | If True, access will use the effective uid/gid instead of |
| 2738 | the real uid/gid. |
| 2739 | |
| 2740 | follow_symlinks: bool = True |
| 2741 | If False, and the last element of the path is a symbolic link, |
| 2742 | access will examine the symbolic link itself instead of the file |
| 2743 | the link points to. |
| 2744 | |
| 2745 | Use the real uid/gid to test for access to a path. |
| 2746 | |
| 2747 | {parameters} |
| 2748 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2749 | on your platform. If they are unavailable, using them will raise a |
| 2750 | NotImplementedError. |
| 2751 | |
| 2752 | Note that most operations will use the effective uid/gid, therefore this |
| 2753 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2754 | has the specified access to the path. |
| 2755 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2756 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2757 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2758 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2759 | os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2760 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2761 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2762 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2763 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2764 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2765 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2766 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2767 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2768 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2769 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2770 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2771 | #ifndef HAVE_FACCESSAT |
| 2772 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2773 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2774 | |
| 2775 | if (effective_ids) { |
| 2776 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2777 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2778 | } |
| 2779 | #endif |
| 2780 | |
| 2781 | #ifdef MS_WINDOWS |
| 2782 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2783 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2784 | Py_END_ALLOW_THREADS |
| 2785 | |
| 2786 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2787 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2788 | * * we didn't get a -1, and |
| 2789 | * * write access wasn't requested, |
| 2790 | * * or the file isn't read-only, |
| 2791 | * * or it's a directory. |
| 2792 | * (Directories cannot be read-only on Windows.) |
| 2793 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2794 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2795 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2796 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2797 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2798 | #else |
| 2799 | |
| 2800 | Py_BEGIN_ALLOW_THREADS |
| 2801 | #ifdef HAVE_FACCESSAT |
| 2802 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2803 | effective_ids || |
| 2804 | !follow_symlinks) { |
| 2805 | int flags = 0; |
| 2806 | if (!follow_symlinks) |
| 2807 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2808 | if (effective_ids) |
| 2809 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2810 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2811 | } |
| 2812 | else |
| 2813 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2814 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2815 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2816 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2817 | #endif |
| 2818 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2819 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2822 | #ifndef F_OK |
| 2823 | #define F_OK 0 |
| 2824 | #endif |
| 2825 | #ifndef R_OK |
| 2826 | #define R_OK 4 |
| 2827 | #endif |
| 2828 | #ifndef W_OK |
| 2829 | #define W_OK 2 |
| 2830 | #endif |
| 2831 | #ifndef X_OK |
| 2832 | #define X_OK 1 |
| 2833 | #endif |
| 2834 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2835 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2836 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2837 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2838 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2839 | |
| 2840 | fd: int |
| 2841 | Integer file descriptor handle. |
| 2842 | |
| 2843 | / |
| 2844 | |
| 2845 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2846 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2847 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2848 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2849 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2850 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2851 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2852 | |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2853 | long size = sysconf(_SC_TTY_NAME_MAX); |
| 2854 | if (size == -1) { |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2855 | return posix_error(); |
| 2856 | } |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2857 | char *buffer = (char *)PyMem_RawMalloc(size); |
| 2858 | if (buffer == NULL) { |
| 2859 | return PyErr_NoMemory(); |
| 2860 | } |
| 2861 | int ret = ttyname_r(fd, buffer, size); |
| 2862 | if (ret != 0) { |
| 2863 | PyMem_RawFree(buffer); |
| 2864 | errno = ret; |
| 2865 | return posix_error(); |
| 2866 | } |
| 2867 | PyObject *res = PyUnicode_DecodeFSDefault(buffer); |
| 2868 | PyMem_RawFree(buffer); |
| 2869 | return res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2870 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2871 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2872 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2873 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2874 | /*[clinic input] |
| 2875 | os.ctermid |
| 2876 | |
| 2877 | Return the name of the controlling terminal for this process. |
| 2878 | [clinic start generated code]*/ |
| 2879 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2880 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2881 | os_ctermid_impl(PyObject *module) |
| 2882 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2883 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2884 | char *ret; |
| 2885 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2886 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2887 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2888 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2889 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2890 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2891 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2892 | if (ret == NULL) |
| 2893 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2894 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2895 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2896 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2897 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2898 | |
| 2899 | /*[clinic input] |
| 2900 | os.chdir |
| 2901 | |
| 2902 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2903 | |
| 2904 | Change the current working directory to the specified path. |
| 2905 | |
| 2906 | path may always be specified as a string. |
| 2907 | On some platforms, path may also be specified as an open file descriptor. |
| 2908 | If this functionality is unavailable, using it raises an exception. |
| 2909 | [clinic start generated code]*/ |
| 2910 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2911 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2912 | os_chdir_impl(PyObject *module, path_t *path) |
| 2913 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2914 | { |
| 2915 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2916 | |
| 2917 | Py_BEGIN_ALLOW_THREADS |
| 2918 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2919 | /* on unix, success = 0, on windows, success = !0 */ |
| 2920 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2921 | #else |
| 2922 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2923 | if (path->fd != -1) |
| 2924 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2925 | else |
| 2926 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2927 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2928 | #endif |
| 2929 | Py_END_ALLOW_THREADS |
| 2930 | |
| 2931 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2932 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2935 | Py_RETURN_NONE; |
| 2936 | } |
| 2937 | |
| 2938 | |
| 2939 | #ifdef HAVE_FCHDIR |
| 2940 | /*[clinic input] |
| 2941 | os.fchdir |
| 2942 | |
| 2943 | fd: fildes |
| 2944 | |
| 2945 | Change to the directory of the given file descriptor. |
| 2946 | |
| 2947 | fd must be opened on a directory, not a file. |
| 2948 | Equivalent to os.chdir(fd). |
| 2949 | |
| 2950 | [clinic start generated code]*/ |
| 2951 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2952 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2953 | os_fchdir_impl(PyObject *module, int fd) |
| 2954 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2955 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2956 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2957 | } |
| 2958 | #endif /* HAVE_FCHDIR */ |
| 2959 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2960 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2961 | /*[clinic input] |
| 2962 | os.chmod |
| 2963 | |
| 2964 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2965 | Path to be modified. May always be specified as a str, bytes, or a path-like object. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2966 | On some platforms, path may also be specified as an open file descriptor. |
| 2967 | If this functionality is unavailable, using it raises an exception. |
| 2968 | |
| 2969 | mode: int |
| 2970 | Operating-system mode bitfield. |
| 2971 | |
| 2972 | * |
| 2973 | |
| 2974 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2975 | If not None, it should be a file descriptor open to a directory, |
| 2976 | and path should be relative; path will then be relative to that |
| 2977 | directory. |
| 2978 | |
| 2979 | follow_symlinks: bool = True |
| 2980 | If False, and the last element of the path is a symbolic link, |
| 2981 | chmod will modify the symbolic link itself instead of the file |
| 2982 | the link points to. |
| 2983 | |
| 2984 | Change the access permissions of a file. |
| 2985 | |
| 2986 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2987 | an open file descriptor. |
| 2988 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2989 | If they are unavailable, using them will raise a NotImplementedError. |
| 2990 | |
| 2991 | [clinic start generated code]*/ |
| 2992 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2993 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2994 | os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2995 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2996 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2997 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2998 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2999 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3000 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3001 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3002 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3003 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3004 | #ifdef HAVE_FCHMODAT |
| 3005 | int fchmodat_nofollow_unsupported = 0; |
| 3006 | #endif |
| 3007 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3008 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 3009 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3010 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3011 | #endif |
| 3012 | |
| 3013 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3014 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3015 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 3016 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3017 | result = 0; |
| 3018 | else { |
| 3019 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3020 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 3021 | else |
| 3022 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3023 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3024 | } |
| 3025 | Py_END_ALLOW_THREADS |
| 3026 | |
| 3027 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3028 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3029 | } |
| 3030 | #else /* MS_WINDOWS */ |
| 3031 | Py_BEGIN_ALLOW_THREADS |
| 3032 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3033 | if (path->fd != -1) |
| 3034 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3035 | else |
| 3036 | #endif |
| 3037 | #ifdef HAVE_LCHMOD |
| 3038 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3039 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3040 | else |
| 3041 | #endif |
| 3042 | #ifdef HAVE_FCHMODAT |
| 3043 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 3044 | /* |
| 3045 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 3046 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3047 | * and then says it isn't implemented yet. |
| 3048 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3049 | * |
| 3050 | * Once it is supported, os.chmod will automatically |
| 3051 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 3052 | * Until then, we need to be careful what exception we raise. |
| 3053 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3054 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3055 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3056 | /* |
| 3057 | * But wait! We can't throw the exception without allowing threads, |
| 3058 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 3059 | */ |
| 3060 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3061 | result && |
| 3062 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 3063 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3064 | } |
| 3065 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3066 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3067 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3068 | Py_END_ALLOW_THREADS |
| 3069 | |
| 3070 | if (result) { |
| 3071 | #ifdef HAVE_FCHMODAT |
| 3072 | if (fchmodat_nofollow_unsupported) { |
| 3073 | if (dir_fd != DEFAULT_DIR_FD) |
| 3074 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3075 | dir_fd, follow_symlinks); |
| 3076 | else |
| 3077 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 3078 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3079 | } |
| 3080 | else |
| 3081 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3082 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3083 | } |
| 3084 | #endif |
| 3085 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3086 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3089 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3090 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3091 | /*[clinic input] |
| 3092 | os.fchmod |
| 3093 | |
| 3094 | fd: int |
| 3095 | mode: int |
| 3096 | |
| 3097 | Change the access permissions of the file given by file descriptor fd. |
| 3098 | |
| 3099 | Equivalent to os.chmod(fd, mode). |
| 3100 | [clinic start generated code]*/ |
| 3101 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3102 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3103 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 3104 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3105 | { |
| 3106 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3107 | int async_err = 0; |
| 3108 | |
| 3109 | do { |
| 3110 | Py_BEGIN_ALLOW_THREADS |
| 3111 | res = fchmod(fd, mode); |
| 3112 | Py_END_ALLOW_THREADS |
| 3113 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3114 | if (res != 0) |
| 3115 | return (!async_err) ? posix_error() : NULL; |
| 3116 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3117 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3118 | } |
| 3119 | #endif /* HAVE_FCHMOD */ |
| 3120 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3121 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3122 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3123 | /*[clinic input] |
| 3124 | os.lchmod |
| 3125 | |
| 3126 | path: path_t |
| 3127 | mode: int |
| 3128 | |
| 3129 | Change the access permissions of a file, without following symbolic links. |
| 3130 | |
| 3131 | If path is a symlink, this affects the link itself rather than the target. |
| 3132 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 3133 | [clinic start generated code]*/ |
| 3134 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3135 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3136 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 3137 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3138 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3139 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3140 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3141 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3142 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3143 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3144 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3145 | return NULL; |
| 3146 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3147 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3148 | } |
| 3149 | #endif /* HAVE_LCHMOD */ |
| 3150 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3151 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3152 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3153 | /*[clinic input] |
| 3154 | os.chflags |
| 3155 | |
| 3156 | path: path_t |
| 3157 | flags: unsigned_long(bitwise=True) |
| 3158 | follow_symlinks: bool=True |
| 3159 | |
| 3160 | Set file flags. |
| 3161 | |
| 3162 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3163 | link, chflags will change flags on the symbolic link itself instead of the |
| 3164 | file the link points to. |
| 3165 | follow_symlinks may not be implemented on your platform. If it is |
| 3166 | unavailable, using it will raise a NotImplementedError. |
| 3167 | |
| 3168 | [clinic start generated code]*/ |
| 3169 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3170 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3171 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3172 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3173 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3174 | { |
| 3175 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3176 | |
| 3177 | #ifndef HAVE_LCHFLAGS |
| 3178 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3179 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3180 | #endif |
| 3181 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3182 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3183 | #ifdef HAVE_LCHFLAGS |
| 3184 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3185 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3186 | else |
| 3187 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3188 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3189 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3190 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3191 | if (result) |
| 3192 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3193 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3194 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3195 | } |
| 3196 | #endif /* HAVE_CHFLAGS */ |
| 3197 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3198 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3199 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3200 | /*[clinic input] |
| 3201 | os.lchflags |
| 3202 | |
| 3203 | path: path_t |
| 3204 | flags: unsigned_long(bitwise=True) |
| 3205 | |
| 3206 | Set file flags. |
| 3207 | |
| 3208 | This function will not follow symbolic links. |
| 3209 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3210 | [clinic start generated code]*/ |
| 3211 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3212 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3213 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3214 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3215 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3216 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3217 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3218 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3219 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3220 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3221 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3222 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3223 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3224 | } |
| 3225 | #endif /* HAVE_LCHFLAGS */ |
| 3226 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3227 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3228 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3229 | /*[clinic input] |
| 3230 | os.chroot |
| 3231 | path: path_t |
| 3232 | |
| 3233 | Change root directory to path. |
| 3234 | |
| 3235 | [clinic start generated code]*/ |
| 3236 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3237 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3238 | os_chroot_impl(PyObject *module, path_t *path) |
| 3239 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3240 | { |
| 3241 | int res; |
| 3242 | Py_BEGIN_ALLOW_THREADS |
| 3243 | res = chroot(path->narrow); |
| 3244 | Py_END_ALLOW_THREADS |
| 3245 | if (res < 0) |
| 3246 | return path_error(path); |
| 3247 | Py_RETURN_NONE; |
| 3248 | } |
| 3249 | #endif /* HAVE_CHROOT */ |
| 3250 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3251 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3252 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3253 | /*[clinic input] |
| 3254 | os.fsync |
| 3255 | |
| 3256 | fd: fildes |
| 3257 | |
| 3258 | Force write of fd to disk. |
| 3259 | [clinic start generated code]*/ |
| 3260 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3261 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3262 | os_fsync_impl(PyObject *module, int fd) |
| 3263 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3264 | { |
| 3265 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3266 | } |
| 3267 | #endif /* HAVE_FSYNC */ |
| 3268 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3269 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3270 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3271 | /*[clinic input] |
| 3272 | os.sync |
| 3273 | |
| 3274 | Force write of everything to disk. |
| 3275 | [clinic start generated code]*/ |
| 3276 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3277 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3278 | os_sync_impl(PyObject *module) |
| 3279 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3280 | { |
| 3281 | Py_BEGIN_ALLOW_THREADS |
| 3282 | sync(); |
| 3283 | Py_END_ALLOW_THREADS |
| 3284 | Py_RETURN_NONE; |
| 3285 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3286 | #endif /* HAVE_SYNC */ |
| 3287 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3288 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3289 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3290 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3291 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3292 | #endif |
| 3293 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3294 | /*[clinic input] |
| 3295 | os.fdatasync |
| 3296 | |
| 3297 | fd: fildes |
| 3298 | |
| 3299 | Force write of fd to disk without forcing update of metadata. |
| 3300 | [clinic start generated code]*/ |
| 3301 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3302 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3303 | os_fdatasync_impl(PyObject *module, int fd) |
| 3304 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3305 | { |
| 3306 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3307 | } |
| 3308 | #endif /* HAVE_FDATASYNC */ |
| 3309 | |
| 3310 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3311 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3312 | /*[clinic input] |
| 3313 | os.chown |
| 3314 | |
| 3315 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3316 | Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3317 | |
| 3318 | uid: uid_t |
| 3319 | |
| 3320 | gid: gid_t |
| 3321 | |
| 3322 | * |
| 3323 | |
| 3324 | dir_fd : dir_fd(requires='fchownat') = None |
| 3325 | If not None, it should be a file descriptor open to a directory, |
| 3326 | and path should be relative; path will then be relative to that |
| 3327 | directory. |
| 3328 | |
| 3329 | follow_symlinks: bool = True |
| 3330 | If False, and the last element of the path is a symbolic link, |
| 3331 | stat will examine the symbolic link itself instead of the file |
| 3332 | the link points to. |
| 3333 | |
| 3334 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3335 | |
| 3336 | path may always be specified as a string. |
| 3337 | On some platforms, path may also be specified as an open file descriptor. |
| 3338 | If this functionality is unavailable, using it raises an exception. |
| 3339 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3340 | and path should be relative; path will then be relative to that directory. |
| 3341 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3342 | link, chown will modify the symbolic link itself instead of the file the |
| 3343 | link points to. |
| 3344 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3345 | an open file descriptor. |
| 3346 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3347 | If they are unavailable, using them will raise a NotImplementedError. |
| 3348 | |
| 3349 | [clinic start generated code]*/ |
| 3350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3351 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3352 | os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3353 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3354 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3355 | { |
| 3356 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3357 | |
| 3358 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3359 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3360 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3361 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3362 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3363 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3364 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3365 | |
| 3366 | #ifdef __APPLE__ |
| 3367 | /* |
| 3368 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3369 | * (But we still have an lchown symbol because of weak-linking.) |
| 3370 | * It doesn't have fchownat either. So there's no possibility |
| 3371 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3372 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3373 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3374 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3375 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3376 | } |
| 3377 | #endif |
| 3378 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3379 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3380 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3381 | if (path->fd != -1) |
| 3382 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3383 | else |
| 3384 | #endif |
| 3385 | #ifdef HAVE_LCHOWN |
| 3386 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3387 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3388 | else |
| 3389 | #endif |
| 3390 | #ifdef HAVE_FCHOWNAT |
| 3391 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3392 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3393 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3394 | else |
| 3395 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3396 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3397 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3399 | if (result) |
| 3400 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3401 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3402 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3403 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3404 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3405 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3406 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3407 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3408 | /*[clinic input] |
| 3409 | os.fchown |
| 3410 | |
| 3411 | fd: int |
| 3412 | uid: uid_t |
| 3413 | gid: gid_t |
| 3414 | |
| 3415 | Change the owner and group id of the file specified by file descriptor. |
| 3416 | |
| 3417 | Equivalent to os.chown(fd, uid, gid). |
| 3418 | |
| 3419 | [clinic start generated code]*/ |
| 3420 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3421 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3422 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3423 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3424 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3425 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3426 | int async_err = 0; |
| 3427 | |
| 3428 | do { |
| 3429 | Py_BEGIN_ALLOW_THREADS |
| 3430 | res = fchown(fd, uid, gid); |
| 3431 | Py_END_ALLOW_THREADS |
| 3432 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3433 | if (res != 0) |
| 3434 | return (!async_err) ? posix_error() : NULL; |
| 3435 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3436 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3437 | } |
| 3438 | #endif /* HAVE_FCHOWN */ |
| 3439 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3440 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3441 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3442 | /*[clinic input] |
| 3443 | os.lchown |
| 3444 | |
| 3445 | path : path_t |
| 3446 | uid: uid_t |
| 3447 | gid: gid_t |
| 3448 | |
| 3449 | Change the owner and group id of path to the numeric uid and gid. |
| 3450 | |
| 3451 | This function will not follow symbolic links. |
| 3452 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3453 | [clinic start generated code]*/ |
| 3454 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3455 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3456 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3457 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3458 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3459 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3460 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3461 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3462 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3463 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3464 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3465 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3466 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3467 | } |
| 3468 | #endif /* HAVE_LCHOWN */ |
| 3469 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3470 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3471 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3472 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3473 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3474 | #ifdef MS_WINDOWS |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3475 | wchar_t wbuf[MAXPATHLEN]; |
| 3476 | wchar_t *wbuf2 = wbuf; |
| 3477 | DWORD len; |
| 3478 | |
| 3479 | Py_BEGIN_ALLOW_THREADS |
| 3480 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
| 3481 | /* If the buffer is large enough, len does not include the |
| 3482 | terminating \0. If the buffer is too small, len includes |
| 3483 | the space needed for the terminator. */ |
| 3484 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | ec3e20a | 2019-06-28 18:01:59 +0200 | [diff] [blame] | 3485 | if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3486 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3487 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3488 | else { |
| 3489 | wbuf2 = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3490 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3491 | if (wbuf2) { |
| 3492 | len = GetCurrentDirectoryW(len, wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3493 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3494 | } |
| 3495 | Py_END_ALLOW_THREADS |
| 3496 | |
| 3497 | if (!wbuf2) { |
| 3498 | PyErr_NoMemory(); |
| 3499 | return NULL; |
| 3500 | } |
| 3501 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3502 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3503 | PyMem_RawFree(wbuf2); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3504 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3505 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3506 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3507 | PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3508 | if (wbuf2 != wbuf) { |
| 3509 | PyMem_RawFree(wbuf2); |
| 3510 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3511 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3512 | if (use_bytes) { |
| 3513 | if (resobj == NULL) { |
| 3514 | return NULL; |
| 3515 | } |
| 3516 | Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj)); |
| 3517 | } |
| 3518 | |
| 3519 | return resobj; |
| 3520 | #else |
| 3521 | const size_t chunk = 1024; |
| 3522 | |
| 3523 | char *buf = NULL; |
| 3524 | char *cwd = NULL; |
| 3525 | size_t buflen = 0; |
| 3526 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3527 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3528 | do { |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3529 | char *newbuf; |
| 3530 | if (buflen <= PY_SSIZE_T_MAX - chunk) { |
| 3531 | buflen += chunk; |
| 3532 | newbuf = PyMem_RawRealloc(buf, buflen); |
| 3533 | } |
| 3534 | else { |
| 3535 | newbuf = NULL; |
| 3536 | } |
| 3537 | if (newbuf == NULL) { |
| 3538 | PyMem_RawFree(buf); |
| 3539 | buf = NULL; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3540 | break; |
| 3541 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3542 | buf = newbuf; |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3543 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3544 | cwd = getcwd(buf, buflen); |
| 3545 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3546 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3547 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3548 | if (buf == NULL) { |
| 3549 | return PyErr_NoMemory(); |
| 3550 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3551 | if (cwd == NULL) { |
| 3552 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3553 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3554 | } |
| 3555 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3556 | PyObject *obj; |
| 3557 | if (use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3558 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3559 | } |
| 3560 | else { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3561 | obj = PyUnicode_DecodeFSDefault(buf); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3562 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3563 | PyMem_RawFree(buf); |
| 3564 | |
| 3565 | return obj; |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3566 | #endif /* !MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3567 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3568 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3569 | |
| 3570 | /*[clinic input] |
| 3571 | os.getcwd |
| 3572 | |
| 3573 | Return a unicode string representing the current working directory. |
| 3574 | [clinic start generated code]*/ |
| 3575 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3576 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3577 | os_getcwd_impl(PyObject *module) |
| 3578 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3579 | { |
| 3580 | return posix_getcwd(0); |
| 3581 | } |
| 3582 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3583 | |
| 3584 | /*[clinic input] |
| 3585 | os.getcwdb |
| 3586 | |
| 3587 | Return a bytes string representing the current working directory. |
| 3588 | [clinic start generated code]*/ |
| 3589 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3590 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3591 | os_getcwdb_impl(PyObject *module) |
| 3592 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3593 | { |
| 3594 | return posix_getcwd(1); |
| 3595 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3596 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3597 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3598 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3599 | #define HAVE_LINK 1 |
| 3600 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3601 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3602 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3603 | /*[clinic input] |
| 3604 | |
| 3605 | os.link |
| 3606 | |
| 3607 | src : path_t |
| 3608 | dst : path_t |
| 3609 | * |
| 3610 | src_dir_fd : dir_fd = None |
| 3611 | dst_dir_fd : dir_fd = None |
| 3612 | follow_symlinks: bool = True |
| 3613 | |
| 3614 | Create a hard link to a file. |
| 3615 | |
| 3616 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3617 | descriptor open to a directory, and the respective path string (src or dst) |
| 3618 | should be relative; the path will then be relative to that directory. |
| 3619 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3620 | link, link will create a link to the symbolic link itself instead of the |
| 3621 | file the link points to. |
| 3622 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3623 | platform. If they are unavailable, using them will raise a |
| 3624 | NotImplementedError. |
| 3625 | [clinic start generated code]*/ |
| 3626 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3627 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3628 | os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3629 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3630 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3631 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3632 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3633 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3634 | #else |
| 3635 | int result; |
| 3636 | #endif |
| 3637 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3638 | #ifndef HAVE_LINKAT |
| 3639 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3640 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3641 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3642 | } |
| 3643 | #endif |
| 3644 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3645 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3646 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3647 | PyErr_SetString(PyExc_NotImplementedError, |
| 3648 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3649 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3650 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3651 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3652 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3653 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3654 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3655 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3656 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3657 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3658 | if (!result) |
| 3659 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3660 | #else |
| 3661 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3662 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3663 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3664 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3665 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3666 | result = linkat(src_dir_fd, src->narrow, |
| 3667 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3668 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3669 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3670 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3671 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3672 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3673 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3674 | if (result) |
| 3675 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3676 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3677 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3678 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3679 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3680 | #endif |
| 3681 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3682 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3683 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3684 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3685 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3686 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3687 | PyObject *v; |
| 3688 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3689 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3690 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3691 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3692 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3693 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3694 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3695 | WIN32_FIND_DATAW wFileData; |
| 3696 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3697 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3698 | if (!path->wide) { /* Default arg: "." */ |
| 3699 | po_wchars = L"."; |
| 3700 | len = 1; |
| 3701 | } else { |
| 3702 | po_wchars = path->wide; |
| 3703 | len = wcslen(path->wide); |
| 3704 | } |
| 3705 | /* The +5 is so we can append "\\*.*\0" */ |
| 3706 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3707 | if (!wnamebuf) { |
| 3708 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3709 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3710 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3711 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3712 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3713 | wchar_t wch = wnamebuf[len-1]; |
| 3714 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3715 | wnamebuf[len++] = SEP; |
| 3716 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3717 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3718 | if ((list = PyList_New(0)) == NULL) { |
| 3719 | goto exit; |
| 3720 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3721 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3722 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3723 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3724 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3725 | int error = GetLastError(); |
| 3726 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3727 | goto exit; |
| 3728 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3729 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3730 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3731 | } |
| 3732 | do { |
| 3733 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3734 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3735 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3736 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3737 | wcslen(wFileData.cFileName)); |
| 3738 | if (path->narrow && v) { |
| 3739 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3740 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3741 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3742 | Py_DECREF(list); |
| 3743 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3744 | break; |
| 3745 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3746 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3747 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3748 | Py_DECREF(list); |
| 3749 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3750 | break; |
| 3751 | } |
| 3752 | Py_DECREF(v); |
| 3753 | } |
| 3754 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3755 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3756 | Py_END_ALLOW_THREADS |
| 3757 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3758 | it got to the end of the directory. */ |
| 3759 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3760 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3761 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3762 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3763 | } |
| 3764 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3765 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3766 | exit: |
| 3767 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3768 | if (FindClose(hFindFile) == FALSE) { |
| 3769 | if (list != NULL) { |
| 3770 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3771 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3772 | } |
| 3773 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3774 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3775 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3776 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3777 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3778 | } /* end of _listdir_windows_no_opendir */ |
| 3779 | |
| 3780 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3781 | |
| 3782 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3783 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3784 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3785 | PyObject *v; |
| 3786 | DIR *dirp = NULL; |
| 3787 | struct dirent *ep; |
| 3788 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3789 | #ifdef HAVE_FDOPENDIR |
| 3790 | int fd = -1; |
| 3791 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3792 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3793 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3794 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3795 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3796 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3797 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3798 | if (fd == -1) |
| 3799 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3800 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3801 | return_str = 1; |
| 3802 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3803 | Py_BEGIN_ALLOW_THREADS |
| 3804 | dirp = fdopendir(fd); |
| 3805 | Py_END_ALLOW_THREADS |
| 3806 | } |
| 3807 | else |
| 3808 | #endif |
| 3809 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3810 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3811 | if (path->narrow) { |
| 3812 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3813 | /* only return bytes if they specified a bytes-like object */ |
| 3814 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3815 | } |
| 3816 | else { |
| 3817 | name = "."; |
| 3818 | return_str = 1; |
| 3819 | } |
| 3820 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3821 | Py_BEGIN_ALLOW_THREADS |
| 3822 | dirp = opendir(name); |
| 3823 | Py_END_ALLOW_THREADS |
| 3824 | } |
| 3825 | |
| 3826 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3827 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3828 | #ifdef HAVE_FDOPENDIR |
| 3829 | if (fd != -1) { |
| 3830 | Py_BEGIN_ALLOW_THREADS |
| 3831 | close(fd); |
| 3832 | Py_END_ALLOW_THREADS |
| 3833 | } |
| 3834 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3835 | goto exit; |
| 3836 | } |
| 3837 | if ((list = PyList_New(0)) == NULL) { |
| 3838 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3839 | } |
| 3840 | for (;;) { |
| 3841 | errno = 0; |
| 3842 | Py_BEGIN_ALLOW_THREADS |
| 3843 | ep = readdir(dirp); |
| 3844 | Py_END_ALLOW_THREADS |
| 3845 | if (ep == NULL) { |
| 3846 | if (errno == 0) { |
| 3847 | break; |
| 3848 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3849 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3850 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3851 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3852 | } |
| 3853 | } |
| 3854 | if (ep->d_name[0] == '.' && |
| 3855 | (NAMLEN(ep) == 1 || |
| 3856 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3857 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3858 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3859 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3860 | else |
| 3861 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3862 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3863 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3864 | break; |
| 3865 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3866 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3867 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3868 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3869 | break; |
| 3870 | } |
| 3871 | Py_DECREF(v); |
| 3872 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3873 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3874 | exit: |
| 3875 | if (dirp != NULL) { |
| 3876 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3877 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3878 | if (fd > -1) |
| 3879 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3880 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3881 | closedir(dirp); |
| 3882 | Py_END_ALLOW_THREADS |
| 3883 | } |
| 3884 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3885 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3886 | } /* end of _posix_listdir */ |
| 3887 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3888 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3889 | |
| 3890 | /*[clinic input] |
| 3891 | os.listdir |
| 3892 | |
| 3893 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3894 | |
| 3895 | Return a list containing the names of the files in the directory. |
| 3896 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3897 | path can be specified as either str, bytes, or a path-like object. If path is bytes, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3898 | the filenames returned will also be bytes; in all other circumstances |
| 3899 | the filenames returned will be str. |
| 3900 | If path is None, uses the path='.'. |
| 3901 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3902 | the file descriptor must refer to a directory. |
| 3903 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3904 | |
| 3905 | The list is in arbitrary order. It does not include the special |
| 3906 | entries '.' and '..' even if they are present in the directory. |
| 3907 | |
| 3908 | |
| 3909 | [clinic start generated code]*/ |
| 3910 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3911 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3912 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3913 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3914 | { |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 3915 | if (PySys_Audit("os.listdir", "O", |
| 3916 | path->object ? path->object : Py_None) < 0) { |
| 3917 | return NULL; |
| 3918 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3919 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3920 | return _listdir_windows_no_opendir(path, NULL); |
| 3921 | #else |
| 3922 | return _posix_listdir(path, NULL); |
| 3923 | #endif |
| 3924 | } |
| 3925 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3926 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3927 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3928 | /*[clinic input] |
| 3929 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3930 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3931 | path: path_t |
| 3932 | / |
| 3933 | |
| 3934 | [clinic start generated code]*/ |
| 3935 | |
| 3936 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3937 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3938 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3939 | { |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3940 | wchar_t *abspath; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3941 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3942 | /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */ |
| 3943 | if (_Py_abspath(path->wide, &abspath) < 0) { |
| 3944 | return win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3945 | } |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3946 | if (abspath == NULL) { |
| 3947 | return PyErr_NoMemory(); |
| 3948 | } |
| 3949 | |
| 3950 | PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath)); |
| 3951 | PyMem_RawFree(abspath); |
| 3952 | if (str == NULL) { |
| 3953 | return NULL; |
| 3954 | } |
| 3955 | if (path->narrow) { |
| 3956 | Py_SETREF(str, PyUnicode_EncodeFSDefault(str)); |
| 3957 | } |
| 3958 | return str; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3959 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3960 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3961 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3962 | /*[clinic input] |
| 3963 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3964 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3965 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3966 | / |
| 3967 | |
| 3968 | A helper function for samepath on windows. |
| 3969 | [clinic start generated code]*/ |
| 3970 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3971 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3972 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 3973 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3974 | { |
| 3975 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3976 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 3977 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3978 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3979 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3980 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3981 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3982 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3983 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3984 | 0, /* desired access */ |
| 3985 | 0, /* share mode */ |
| 3986 | NULL, /* security attributes */ |
| 3987 | OPEN_EXISTING, |
| 3988 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 3989 | FILE_FLAG_BACKUP_SEMANTICS, |
| 3990 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3991 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3992 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3993 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3994 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3995 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3996 | |
| 3997 | /* We have a good handle to the target, use it to determine the |
| 3998 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3999 | while (1) { |
| 4000 | Py_BEGIN_ALLOW_THREADS |
| 4001 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 4002 | buf_size, VOLUME_NAME_DOS); |
| 4003 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4004 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4005 | if (!result_length) { |
| 4006 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 4007 | path->object); |
| 4008 | goto cleanup; |
| 4009 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4010 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4011 | if (result_length < buf_size) { |
| 4012 | break; |
| 4013 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4014 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4015 | wchar_t *tmp; |
| 4016 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 4017 | result_length * sizeof(*tmp)); |
| 4018 | if (!tmp) { |
| 4019 | result = PyErr_NoMemory(); |
| 4020 | goto cleanup; |
| 4021 | } |
| 4022 | |
| 4023 | buf_size = result_length; |
| 4024 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4025 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4026 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4027 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4028 | if (result && path->narrow) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4029 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4030 | } |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4031 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4032 | cleanup: |
| 4033 | if (target_path != buf) { |
| 4034 | PyMem_Free(target_path); |
| 4035 | } |
| 4036 | CloseHandle(hFile); |
| 4037 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4038 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 4039 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4040 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4041 | /*[clinic input] |
| 4042 | os._getvolumepathname |
| 4043 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4044 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4045 | |
| 4046 | A helper function for ismount on Win32. |
| 4047 | [clinic start generated code]*/ |
| 4048 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4049 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4050 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 4051 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4052 | { |
| 4053 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4054 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4055 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4056 | BOOL ret; |
| 4057 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4058 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4059 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4060 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 4061 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4062 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4063 | return NULL; |
| 4064 | } |
| 4065 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4066 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4067 | if (mountpath == NULL) |
| 4068 | return PyErr_NoMemory(); |
| 4069 | |
| 4070 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4071 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4072 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4073 | Py_END_ALLOW_THREADS |
| 4074 | |
| 4075 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4076 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4077 | goto exit; |
| 4078 | } |
| 4079 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4080 | if (path->narrow) |
| 4081 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4082 | |
| 4083 | exit: |
| 4084 | PyMem_Free(mountpath); |
| 4085 | return result; |
| 4086 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4087 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4088 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4089 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4090 | |
| 4091 | /*[clinic input] |
| 4092 | os.mkdir |
| 4093 | |
| 4094 | path : path_t |
| 4095 | |
| 4096 | mode: int = 0o777 |
| 4097 | |
| 4098 | * |
| 4099 | |
| 4100 | dir_fd : dir_fd(requires='mkdirat') = None |
| 4101 | |
| 4102 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4103 | |
| 4104 | Create a directory. |
| 4105 | |
| 4106 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4107 | and path should be relative; path will then be relative to that directory. |
| 4108 | dir_fd may not be implemented on your platform. |
| 4109 | If it is unavailable, using it will raise a NotImplementedError. |
| 4110 | |
| 4111 | The mode argument is ignored on Windows. |
| 4112 | [clinic start generated code]*/ |
| 4113 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4114 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4115 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 4116 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4117 | { |
| 4118 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4119 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4120 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4121 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4122 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4123 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4124 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4125 | if (!result) |
| 4126 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4127 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4128 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4129 | #if HAVE_MKDIRAT |
| 4130 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4131 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4132 | else |
| 4133 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4134 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4135 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4136 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4137 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4138 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4139 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4140 | if (result < 0) |
| 4141 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4142 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4143 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4144 | } |
| 4145 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4146 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4147 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4148 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4149 | #include <sys/resource.h> |
| 4150 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4151 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4152 | |
| 4153 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4154 | /*[clinic input] |
| 4155 | os.nice |
| 4156 | |
| 4157 | increment: int |
| 4158 | / |
| 4159 | |
| 4160 | Add increment to the priority of process and return the new priority. |
| 4161 | [clinic start generated code]*/ |
| 4162 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4163 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4164 | os_nice_impl(PyObject *module, int increment) |
| 4165 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4166 | { |
| 4167 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4168 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4169 | /* There are two flavours of 'nice': one that returns the new |
| 4170 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4171 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4172 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4173 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4174 | If we are of the nice family that returns the new priority, we |
| 4175 | need to clear errno before the call, and check if errno is filled |
| 4176 | before calling posix_error() on a returnvalue of -1, because the |
| 4177 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4178 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4179 | errno = 0; |
| 4180 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4181 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4182 | if (value == 0) |
| 4183 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4184 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4185 | if (value == -1 && errno != 0) |
| 4186 | /* either nice() or getpriority() returned an error */ |
| 4187 | return posix_error(); |
| 4188 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4189 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4190 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4191 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4192 | |
| 4193 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4194 | /*[clinic input] |
| 4195 | os.getpriority |
| 4196 | |
| 4197 | which: int |
| 4198 | who: int |
| 4199 | |
| 4200 | Return program scheduling priority. |
| 4201 | [clinic start generated code]*/ |
| 4202 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4203 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4204 | os_getpriority_impl(PyObject *module, int which, int who) |
| 4205 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4206 | { |
| 4207 | int retval; |
| 4208 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4209 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4210 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4211 | if (errno != 0) |
| 4212 | return posix_error(); |
| 4213 | return PyLong_FromLong((long)retval); |
| 4214 | } |
| 4215 | #endif /* HAVE_GETPRIORITY */ |
| 4216 | |
| 4217 | |
| 4218 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4219 | /*[clinic input] |
| 4220 | os.setpriority |
| 4221 | |
| 4222 | which: int |
| 4223 | who: int |
| 4224 | priority: int |
| 4225 | |
| 4226 | Set program scheduling priority. |
| 4227 | [clinic start generated code]*/ |
| 4228 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4229 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4230 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4231 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4232 | { |
| 4233 | int retval; |
| 4234 | |
| 4235 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4236 | if (retval == -1) |
| 4237 | return posix_error(); |
| 4238 | Py_RETURN_NONE; |
| 4239 | } |
| 4240 | #endif /* HAVE_SETPRIORITY */ |
| 4241 | |
| 4242 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4243 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4244 | internal_rename(path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int is_replace) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4245 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4246 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4247 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4248 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4249 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4250 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4251 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4252 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4253 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4254 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4255 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4256 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4257 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4258 | #ifndef HAVE_RENAMEAT |
| 4259 | if (dir_fd_specified) { |
| 4260 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4261 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4262 | } |
| 4263 | #endif |
| 4264 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4265 | #ifdef MS_WINDOWS |
| 4266 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4267 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4268 | Py_END_ALLOW_THREADS |
| 4269 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4270 | if (!result) |
| 4271 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4272 | |
| 4273 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4274 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4275 | PyErr_Format(PyExc_ValueError, |
| 4276 | "%s: src and dst must be the same type", function_name); |
| 4277 | return NULL; |
| 4278 | } |
| 4279 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4280 | Py_BEGIN_ALLOW_THREADS |
| 4281 | #ifdef HAVE_RENAMEAT |
| 4282 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4283 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4284 | else |
| 4285 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4286 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4287 | Py_END_ALLOW_THREADS |
| 4288 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4289 | if (result) |
| 4290 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4291 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4292 | Py_RETURN_NONE; |
| 4293 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4294 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4295 | |
| 4296 | /*[clinic input] |
| 4297 | os.rename |
| 4298 | |
| 4299 | src : path_t |
| 4300 | dst : path_t |
| 4301 | * |
| 4302 | src_dir_fd : dir_fd = None |
| 4303 | dst_dir_fd : dir_fd = None |
| 4304 | |
| 4305 | Rename a file or directory. |
| 4306 | |
| 4307 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4308 | descriptor open to a directory, and the respective path string (src or dst) |
| 4309 | should be relative; the path will then be relative to that directory. |
| 4310 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4311 | If they are unavailable, using them will raise a NotImplementedError. |
| 4312 | [clinic start generated code]*/ |
| 4313 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4314 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4315 | os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4316 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4317 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4318 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4319 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4320 | } |
| 4321 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4322 | |
| 4323 | /*[clinic input] |
| 4324 | os.replace = os.rename |
| 4325 | |
| 4326 | Rename a file or directory, overwriting the destination. |
| 4327 | |
| 4328 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4329 | descriptor open to a directory, and the respective path string (src or dst) |
| 4330 | should be relative; the path will then be relative to that directory. |
| 4331 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4332 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4333 | [clinic start generated code]*/ |
| 4334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4335 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4336 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4337 | int dst_dir_fd) |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4338 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4339 | { |
| 4340 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4341 | } |
| 4342 | |
| 4343 | |
| 4344 | /*[clinic input] |
| 4345 | os.rmdir |
| 4346 | |
| 4347 | path: path_t |
| 4348 | * |
| 4349 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4350 | |
| 4351 | Remove a directory. |
| 4352 | |
| 4353 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4354 | and path should be relative; path will then be relative to that directory. |
| 4355 | dir_fd may not be implemented on your platform. |
| 4356 | If it is unavailable, using it will raise a NotImplementedError. |
| 4357 | [clinic start generated code]*/ |
| 4358 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4359 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4360 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4361 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4362 | { |
| 4363 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4364 | |
| 4365 | Py_BEGIN_ALLOW_THREADS |
| 4366 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4367 | /* Windows, success=1, UNIX, success=0 */ |
| 4368 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4369 | #else |
| 4370 | #ifdef HAVE_UNLINKAT |
| 4371 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4372 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4373 | else |
| 4374 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4375 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4376 | #endif |
| 4377 | Py_END_ALLOW_THREADS |
| 4378 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4379 | if (result) |
| 4380 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4381 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4382 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4385 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4386 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4387 | #ifdef MS_WINDOWS |
| 4388 | /*[clinic input] |
| 4389 | os.system -> long |
| 4390 | |
| 4391 | command: Py_UNICODE |
| 4392 | |
| 4393 | Execute the command in a subshell. |
| 4394 | [clinic start generated code]*/ |
| 4395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4396 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4397 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4398 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4399 | { |
| 4400 | long result; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4401 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4402 | if (PySys_Audit("os.system", "(u)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4403 | return -1; |
| 4404 | } |
| 4405 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4406 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4407 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4408 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4409 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4410 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4411 | return result; |
| 4412 | } |
| 4413 | #else /* MS_WINDOWS */ |
| 4414 | /*[clinic input] |
| 4415 | os.system -> long |
| 4416 | |
| 4417 | command: FSConverter |
| 4418 | |
| 4419 | Execute the command in a subshell. |
| 4420 | [clinic start generated code]*/ |
| 4421 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4422 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4423 | os_system_impl(PyObject *module, PyObject *command) |
| 4424 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4425 | { |
| 4426 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4427 | const char *bytes = PyBytes_AsString(command); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4428 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4429 | if (PySys_Audit("os.system", "(O)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4430 | return -1; |
| 4431 | } |
| 4432 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4433 | Py_BEGIN_ALLOW_THREADS |
| 4434 | result = system(bytes); |
| 4435 | Py_END_ALLOW_THREADS |
| 4436 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4437 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4438 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4439 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4440 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4441 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4442 | /*[clinic input] |
| 4443 | os.umask |
| 4444 | |
| 4445 | mask: int |
| 4446 | / |
| 4447 | |
| 4448 | Set the current numeric umask and return the previous umask. |
| 4449 | [clinic start generated code]*/ |
| 4450 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4451 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4452 | os_umask_impl(PyObject *module, int mask) |
| 4453 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4454 | { |
| 4455 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4456 | if (i < 0) |
| 4457 | return posix_error(); |
| 4458 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4461 | #ifdef MS_WINDOWS |
| 4462 | |
| 4463 | /* override the default DeleteFileW behavior so that directory |
| 4464 | symlinks can be removed with this function, the same as with |
| 4465 | Unix symlinks */ |
| 4466 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4467 | { |
| 4468 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4469 | WIN32_FIND_DATAW find_data; |
| 4470 | HANDLE find_data_handle; |
| 4471 | int is_directory = 0; |
| 4472 | int is_link = 0; |
| 4473 | |
| 4474 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4475 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4476 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4477 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4478 | it is a symlink */ |
| 4479 | if(is_directory && |
| 4480 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4481 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4482 | |
| 4483 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4484 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4485 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4486 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4487 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4488 | FindClose(find_data_handle); |
| 4489 | } |
| 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | if (is_directory && is_link) |
| 4494 | return RemoveDirectoryW(lpFileName); |
| 4495 | |
| 4496 | return DeleteFileW(lpFileName); |
| 4497 | } |
| 4498 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4499 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4500 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4501 | /*[clinic input] |
| 4502 | os.unlink |
| 4503 | |
| 4504 | path: path_t |
| 4505 | * |
| 4506 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4507 | |
| 4508 | Remove a file (same as remove()). |
| 4509 | |
| 4510 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4511 | and path should be relative; path will then be relative to that directory. |
| 4512 | dir_fd may not be implemented on your platform. |
| 4513 | If it is unavailable, using it will raise a NotImplementedError. |
| 4514 | |
| 4515 | [clinic start generated code]*/ |
| 4516 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4517 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4518 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4519 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4520 | { |
| 4521 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4522 | |
| 4523 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4524 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4525 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4526 | /* Windows, success=1, UNIX, success=0 */ |
| 4527 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4528 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4529 | #ifdef HAVE_UNLINKAT |
| 4530 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4531 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4532 | else |
| 4533 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4534 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4535 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4536 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4537 | Py_END_ALLOW_THREADS |
| 4538 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4539 | if (result) |
| 4540 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4541 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4542 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4545 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4546 | /*[clinic input] |
| 4547 | os.remove = os.unlink |
| 4548 | |
| 4549 | Remove a file (same as unlink()). |
| 4550 | |
| 4551 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4552 | and path should be relative; path will then be relative to that directory. |
| 4553 | dir_fd may not be implemented on your platform. |
| 4554 | If it is unavailable, using it will raise a NotImplementedError. |
| 4555 | [clinic start generated code]*/ |
| 4556 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4557 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4558 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4559 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4560 | { |
| 4561 | return os_unlink_impl(module, path, dir_fd); |
| 4562 | } |
| 4563 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4564 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4565 | static PyStructSequence_Field uname_result_fields[] = { |
| 4566 | {"sysname", "operating system name"}, |
| 4567 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4568 | {"release", "operating system release"}, |
| 4569 | {"version", "operating system version"}, |
| 4570 | {"machine", "hardware identifier"}, |
| 4571 | {NULL} |
| 4572 | }; |
| 4573 | |
| 4574 | PyDoc_STRVAR(uname_result__doc__, |
| 4575 | "uname_result: Result from os.uname().\n\n\ |
| 4576 | This object may be accessed either as a tuple of\n\ |
| 4577 | (sysname, nodename, release, version, machine),\n\ |
| 4578 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4579 | \n\ |
| 4580 | See os.uname for more information."); |
| 4581 | |
| 4582 | static PyStructSequence_Desc uname_result_desc = { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4583 | MODNAME ".uname_result", /* name */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4584 | uname_result__doc__, /* doc */ |
| 4585 | uname_result_fields, |
| 4586 | 5 |
| 4587 | }; |
| 4588 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4589 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4590 | /*[clinic input] |
| 4591 | os.uname |
| 4592 | |
| 4593 | Return an object identifying the current operating system. |
| 4594 | |
| 4595 | The object behaves like a named tuple with the following fields: |
| 4596 | (sysname, nodename, release, version, machine) |
| 4597 | |
| 4598 | [clinic start generated code]*/ |
| 4599 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4600 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4601 | os_uname_impl(PyObject *module) |
| 4602 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4603 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4604 | struct utsname u; |
| 4605 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4606 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4607 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4608 | Py_BEGIN_ALLOW_THREADS |
| 4609 | res = uname(&u); |
| 4610 | Py_END_ALLOW_THREADS |
| 4611 | if (res < 0) |
| 4612 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4613 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4614 | PyObject *UnameResultType = _posixstate(module)->UnameResultType; |
| 4615 | value = PyStructSequence_New((PyTypeObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4616 | if (value == NULL) |
| 4617 | return NULL; |
| 4618 | |
| 4619 | #define SET(i, field) \ |
| 4620 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4621 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4622 | if (!o) { \ |
| 4623 | Py_DECREF(value); \ |
| 4624 | return NULL; \ |
| 4625 | } \ |
| 4626 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4627 | } \ |
| 4628 | |
| 4629 | SET(0, u.sysname); |
| 4630 | SET(1, u.nodename); |
| 4631 | SET(2, u.release); |
| 4632 | SET(3, u.version); |
| 4633 | SET(4, u.machine); |
| 4634 | |
| 4635 | #undef SET |
| 4636 | |
| 4637 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4638 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4639 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4640 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4641 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4642 | |
| 4643 | typedef struct { |
| 4644 | int now; |
| 4645 | time_t atime_s; |
| 4646 | long atime_ns; |
| 4647 | time_t mtime_s; |
| 4648 | long mtime_ns; |
| 4649 | } utime_t; |
| 4650 | |
| 4651 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4652 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4653 | * they also intentionally leak the declaration of a pointer named "time" |
| 4654 | */ |
| 4655 | #define UTIME_TO_TIMESPEC \ |
| 4656 | struct timespec ts[2]; \ |
| 4657 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4658 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4659 | time = NULL; \ |
| 4660 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4661 | ts[0].tv_sec = ut->atime_s; \ |
| 4662 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4663 | ts[1].tv_sec = ut->mtime_s; \ |
| 4664 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4665 | time = ts; \ |
| 4666 | } \ |
| 4667 | |
| 4668 | #define UTIME_TO_TIMEVAL \ |
| 4669 | struct timeval tv[2]; \ |
| 4670 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4671 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4672 | time = NULL; \ |
| 4673 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4674 | tv[0].tv_sec = ut->atime_s; \ |
| 4675 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4676 | tv[1].tv_sec = ut->mtime_s; \ |
| 4677 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4678 | time = tv; \ |
| 4679 | } \ |
| 4680 | |
| 4681 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4682 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4683 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4684 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4685 | time = NULL; \ |
| 4686 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4687 | u.actime = ut->atime_s; \ |
| 4688 | u.modtime = ut->mtime_s; \ |
| 4689 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4690 | } |
| 4691 | |
| 4692 | #define UTIME_TO_TIME_T \ |
| 4693 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4694 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4695 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4696 | time = NULL; \ |
| 4697 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4698 | timet[0] = ut->atime_s; \ |
| 4699 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4700 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4701 | } \ |
| 4702 | |
| 4703 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4704 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4705 | |
| 4706 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4707 | utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4708 | { |
| 4709 | #ifdef HAVE_UTIMENSAT |
| 4710 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4711 | UTIME_TO_TIMESPEC; |
| 4712 | return utimensat(dir_fd, path, time, flags); |
| 4713 | #elif defined(HAVE_FUTIMESAT) |
| 4714 | UTIME_TO_TIMEVAL; |
| 4715 | /* |
| 4716 | * follow_symlinks will never be false here; |
| 4717 | * we only allow !follow_symlinks and dir_fd together |
| 4718 | * if we have utimensat() |
| 4719 | */ |
| 4720 | assert(follow_symlinks); |
| 4721 | return futimesat(dir_fd, path, time); |
| 4722 | #endif |
| 4723 | } |
| 4724 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4725 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4726 | #else |
| 4727 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4728 | #endif |
| 4729 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4730 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4731 | |
| 4732 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4733 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4734 | { |
| 4735 | #ifdef HAVE_FUTIMENS |
| 4736 | UTIME_TO_TIMESPEC; |
| 4737 | return futimens(fd, time); |
| 4738 | #else |
| 4739 | UTIME_TO_TIMEVAL; |
| 4740 | return futimes(fd, time); |
| 4741 | #endif |
| 4742 | } |
| 4743 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4744 | #define PATH_UTIME_HAVE_FD 1 |
| 4745 | #else |
| 4746 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4747 | #endif |
| 4748 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4749 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4750 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4751 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4752 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4753 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4754 | |
| 4755 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4756 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4757 | { |
| 4758 | #ifdef HAVE_UTIMENSAT |
| 4759 | UTIME_TO_TIMESPEC; |
| 4760 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4761 | #else |
| 4762 | UTIME_TO_TIMEVAL; |
| 4763 | return lutimes(path, time); |
| 4764 | #endif |
| 4765 | } |
| 4766 | |
| 4767 | #endif |
| 4768 | |
| 4769 | #ifndef MS_WINDOWS |
| 4770 | |
| 4771 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4772 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4773 | { |
| 4774 | #ifdef HAVE_UTIMENSAT |
| 4775 | UTIME_TO_TIMESPEC; |
| 4776 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4777 | #elif defined(HAVE_UTIMES) |
| 4778 | UTIME_TO_TIMEVAL; |
| 4779 | return utimes(path, time); |
| 4780 | #elif defined(HAVE_UTIME_H) |
| 4781 | UTIME_TO_UTIMBUF; |
| 4782 | return utime(path, time); |
| 4783 | #else |
| 4784 | UTIME_TO_TIME_T; |
| 4785 | return utime(path, time); |
| 4786 | #endif |
| 4787 | } |
| 4788 | |
| 4789 | #endif |
| 4790 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4791 | static int |
| 4792 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4793 | { |
| 4794 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4795 | PyObject *divmod; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4796 | divmod = PyNumber_Divmod(py_long, _posixstate_global->billion); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4797 | if (!divmod) |
| 4798 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4799 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4800 | PyErr_Format(PyExc_TypeError, |
| 4801 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4802 | _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod))); |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4803 | goto exit; |
| 4804 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4805 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4806 | if ((*s == -1) && PyErr_Occurred()) |
| 4807 | goto exit; |
| 4808 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4809 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4810 | goto exit; |
| 4811 | |
| 4812 | result = 1; |
| 4813 | exit: |
| 4814 | Py_XDECREF(divmod); |
| 4815 | return result; |
| 4816 | } |
| 4817 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4818 | |
| 4819 | /*[clinic input] |
| 4820 | os.utime |
| 4821 | |
| 4822 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4823 | times: object = None |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4824 | * |
| 4825 | ns: object = NULL |
| 4826 | dir_fd: dir_fd(requires='futimensat') = None |
| 4827 | follow_symlinks: bool=True |
| 4828 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4829 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4830 | |
| 4831 | Set the access and modified time of path. |
| 4832 | |
| 4833 | path may always be specified as a string. |
| 4834 | On some platforms, path may also be specified as an open file descriptor. |
| 4835 | If this functionality is unavailable, using it raises an exception. |
| 4836 | |
| 4837 | If times is not None, it must be a tuple (atime, mtime); |
| 4838 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4839 | If ns is specified, it must be a tuple (atime_ns, mtime_ns); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4840 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4841 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4842 | If times is None and ns is unspecified, utime uses the current time. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4843 | Specifying tuples for both times and ns is an error. |
| 4844 | |
| 4845 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4846 | and path should be relative; path will then be relative to that directory. |
| 4847 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4848 | link, utime will modify the symbolic link itself instead of the file the |
| 4849 | link points to. |
| 4850 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4851 | as an open file descriptor. |
| 4852 | dir_fd and follow_symlinks may not be available on your platform. |
| 4853 | If they are unavailable, using them will raise a NotImplementedError. |
| 4854 | |
| 4855 | [clinic start generated code]*/ |
| 4856 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4857 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4858 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4859 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4860 | /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4861 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4862 | #ifdef MS_WINDOWS |
| 4863 | HANDLE hFile; |
| 4864 | FILETIME atime, mtime; |
| 4865 | #else |
| 4866 | int result; |
| 4867 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4868 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4869 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4870 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4871 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4872 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4873 | if (times != Py_None && ns) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4874 | PyErr_SetString(PyExc_ValueError, |
| 4875 | "utime: you may specify either 'times'" |
| 4876 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4877 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4878 | } |
| 4879 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4880 | if (times != Py_None) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4881 | time_t a_sec, m_sec; |
| 4882 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4883 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4884 | PyErr_SetString(PyExc_TypeError, |
| 4885 | "utime: 'times' must be either" |
| 4886 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4887 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4888 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4889 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4890 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4891 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4892 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4893 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4894 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4895 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4896 | utime.atime_s = a_sec; |
| 4897 | utime.atime_ns = a_nsec; |
| 4898 | utime.mtime_s = m_sec; |
| 4899 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4900 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4901 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4902 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4903 | PyErr_SetString(PyExc_TypeError, |
| 4904 | "utime: 'ns' must be a tuple of two ints"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4905 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4906 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4907 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4908 | if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4909 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4910 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4911 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4912 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4913 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4914 | } |
| 4915 | else { |
| 4916 | /* times and ns are both None/unspecified. use "now". */ |
| 4917 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4918 | } |
| 4919 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4920 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4921 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4922 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4923 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4924 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4925 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4926 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4927 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4928 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4929 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4930 | #if !defined(HAVE_UTIMENSAT) |
| 4931 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4932 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4933 | "utime: cannot use dir_fd and follow_symlinks " |
| 4934 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4935 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4936 | } |
| 4937 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4938 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4939 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4940 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4941 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 4942 | NULL, OPEN_EXISTING, |
| 4943 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4944 | Py_END_ALLOW_THREADS |
| 4945 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4946 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4947 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4948 | } |
| 4949 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4950 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4951 | GetSystemTimeAsFileTime(&mtime); |
| 4952 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4953 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4954 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4955 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4956 | _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4957 | } |
| 4958 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4959 | /* Avoid putting the file name into the error here, |
| 4960 | as that may confuse the user into believing that |
| 4961 | something is wrong with the file, when it also |
| 4962 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4963 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4964 | CloseHandle(hFile); |
| 4965 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4966 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4967 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4968 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4969 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4970 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4971 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4972 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4973 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4974 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4975 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4976 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4977 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4978 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4979 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4980 | else |
| 4981 | #endif |
| 4982 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4983 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4984 | if (path->fd != -1) |
| 4985 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4986 | else |
| 4987 | #endif |
| 4988 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4989 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4990 | |
| 4991 | Py_END_ALLOW_THREADS |
| 4992 | |
| 4993 | if (result < 0) { |
| 4994 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4995 | posix_error(); |
| 4996 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4997 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4998 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4999 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5000 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5001 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5002 | } |
| 5003 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5004 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5005 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5006 | |
| 5007 | /*[clinic input] |
| 5008 | os._exit |
| 5009 | |
| 5010 | status: int |
| 5011 | |
| 5012 | Exit to the system with specified status, without normal exit processing. |
| 5013 | [clinic start generated code]*/ |
| 5014 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5015 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5016 | os__exit_impl(PyObject *module, int status) |
| 5017 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5018 | { |
| 5019 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5020 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5023 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5024 | #define EXECV_CHAR wchar_t |
| 5025 | #else |
| 5026 | #define EXECV_CHAR char |
| 5027 | #endif |
| 5028 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5029 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5030 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5031 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5032 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5033 | Py_ssize_t i; |
| 5034 | for (i = 0; i < count; i++) |
| 5035 | PyMem_Free(array[i]); |
| 5036 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5037 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5038 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5039 | static int |
| 5040 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5041 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5042 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5043 | PyObject *ub; |
| 5044 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5045 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5046 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5047 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5048 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 5049 | if (*out) |
| 5050 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5051 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5052 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5053 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5054 | size = PyBytes_GET_SIZE(ub); |
| 5055 | *out = PyMem_Malloc(size + 1); |
| 5056 | if (*out) { |
| 5057 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 5058 | result = 1; |
| 5059 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5060 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5061 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5062 | Py_DECREF(ub); |
| 5063 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5064 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5065 | #endif |
| 5066 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5067 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5068 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5069 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5070 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5071 | Py_ssize_t i, pos, envc; |
| 5072 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5073 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5074 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5075 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5076 | i = PyMapping_Size(env); |
| 5077 | if (i < 0) |
| 5078 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5079 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5080 | if (envlist == NULL) { |
| 5081 | PyErr_NoMemory(); |
| 5082 | return NULL; |
| 5083 | } |
| 5084 | envc = 0; |
| 5085 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5086 | if (!keys) |
| 5087 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5088 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5089 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5090 | goto error; |
| 5091 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5092 | PyErr_Format(PyExc_TypeError, |
| 5093 | "env.keys() or env.values() is not a list"); |
| 5094 | goto error; |
| 5095 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5096 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5097 | for (pos = 0; pos < i; pos++) { |
| 5098 | key = PyList_GetItem(keys, pos); |
| 5099 | val = PyList_GetItem(vals, pos); |
| 5100 | if (!key || !val) |
| 5101 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5102 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5103 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5104 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 5105 | goto error; |
| 5106 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 5107 | Py_DECREF(key2); |
| 5108 | goto error; |
| 5109 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5110 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 5111 | defining hidden environment variables. */ |
| 5112 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 5113 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 5114 | { |
| 5115 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5116 | Py_DECREF(key2); |
| 5117 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5118 | goto error; |
| 5119 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5120 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 5121 | #else |
| 5122 | if (!PyUnicode_FSConverter(key, &key2)) |
| 5123 | goto error; |
| 5124 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 5125 | Py_DECREF(key2); |
| 5126 | goto error; |
| 5127 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5128 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 5129 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 5130 | { |
| 5131 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5132 | Py_DECREF(key2); |
| 5133 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5134 | goto error; |
| 5135 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5136 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 5137 | PyBytes_AS_STRING(val2)); |
| 5138 | #endif |
| 5139 | Py_DECREF(key2); |
| 5140 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5141 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5142 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5143 | |
| 5144 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 5145 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5146 | goto error; |
| 5147 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5148 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5149 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5150 | } |
| 5151 | Py_DECREF(vals); |
| 5152 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5153 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5154 | envlist[envc] = 0; |
| 5155 | *envc_ptr = envc; |
| 5156 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5157 | |
| 5158 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5159 | Py_XDECREF(keys); |
| 5160 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5161 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5162 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5163 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5164 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5165 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5166 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5167 | { |
| 5168 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5169 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5170 | if (argvlist == NULL) { |
| 5171 | PyErr_NoMemory(); |
| 5172 | return NULL; |
| 5173 | } |
| 5174 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5175 | PyObject* item = PySequence_ITEM(argv, i); |
| 5176 | if (item == NULL) |
| 5177 | goto fail; |
| 5178 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5179 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5180 | goto fail; |
| 5181 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5182 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5183 | } |
| 5184 | argvlist[*argc] = NULL; |
| 5185 | return argvlist; |
| 5186 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5187 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5188 | free_string_array(argvlist, *argc); |
| 5189 | return NULL; |
| 5190 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5191 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5192 | #endif |
| 5193 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5194 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5195 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5196 | /*[clinic input] |
| 5197 | os.execv |
| 5198 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5199 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5200 | Path of executable file. |
| 5201 | argv: object |
| 5202 | Tuple or list of strings. |
| 5203 | / |
| 5204 | |
| 5205 | Execute an executable path with arguments, replacing current process. |
| 5206 | [clinic start generated code]*/ |
| 5207 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5208 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5209 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5210 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5211 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5212 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5213 | Py_ssize_t argc; |
| 5214 | |
| 5215 | /* execv has two arguments: (path, argv), where |
| 5216 | argv is a list or tuple of strings. */ |
| 5217 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5218 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5219 | PyErr_SetString(PyExc_TypeError, |
| 5220 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5221 | return NULL; |
| 5222 | } |
| 5223 | argc = PySequence_Size(argv); |
| 5224 | if (argc < 1) { |
| 5225 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5226 | return NULL; |
| 5227 | } |
| 5228 | |
| 5229 | argvlist = parse_arglist(argv, &argc); |
| 5230 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5231 | return NULL; |
| 5232 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5233 | if (!argvlist[0][0]) { |
| 5234 | PyErr_SetString(PyExc_ValueError, |
| 5235 | "execv() arg 2 first element cannot be empty"); |
| 5236 | free_string_array(argvlist, argc); |
| 5237 | return NULL; |
| 5238 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5239 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5240 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5241 | #ifdef HAVE_WEXECV |
| 5242 | _wexecv(path->wide, argvlist); |
| 5243 | #else |
| 5244 | execv(path->narrow, argvlist); |
| 5245 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5246 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5247 | |
| 5248 | /* If we get here it's definitely an error */ |
| 5249 | |
| 5250 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5251 | return posix_error(); |
| 5252 | } |
| 5253 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5254 | |
| 5255 | /*[clinic input] |
| 5256 | os.execve |
| 5257 | |
| 5258 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5259 | Path of executable file. |
| 5260 | argv: object |
| 5261 | Tuple or list of strings. |
| 5262 | env: object |
| 5263 | Dictionary of strings mapping to strings. |
| 5264 | |
| 5265 | Execute an executable path with arguments, replacing current process. |
| 5266 | [clinic start generated code]*/ |
| 5267 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5268 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5269 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5270 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5271 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5272 | EXECV_CHAR **argvlist = NULL; |
| 5273 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5274 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5275 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5276 | /* execve has three arguments: (path, argv, env), where |
| 5277 | argv is a list or tuple of strings and env is a dictionary |
| 5278 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5279 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5280 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5281 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5282 | "execve: argv must be a tuple or list"); |
| 5283 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5284 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5285 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5286 | if (argc < 1) { |
| 5287 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5288 | return NULL; |
| 5289 | } |
| 5290 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5291 | if (!PyMapping_Check(env)) { |
| 5292 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5293 | "execve: environment must be a mapping object"); |
| 5294 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5295 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5296 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5297 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5298 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5299 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5300 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5301 | if (!argvlist[0][0]) { |
| 5302 | PyErr_SetString(PyExc_ValueError, |
| 5303 | "execve: argv first element cannot be empty"); |
| 5304 | goto fail; |
| 5305 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5306 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5307 | envlist = parse_envlist(env, &envc); |
| 5308 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5309 | goto fail; |
| 5310 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5311 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5312 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5313 | if (path->fd > -1) |
| 5314 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5315 | else |
| 5316 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5317 | #ifdef HAVE_WEXECV |
| 5318 | _wexecve(path->wide, argvlist, envlist); |
| 5319 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5320 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5321 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5322 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5323 | |
| 5324 | /* If we get here it's definitely an error */ |
| 5325 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5326 | posix_path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5327 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5328 | free_string_array(envlist, envc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5329 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5330 | if (argvlist) |
| 5331 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5332 | return NULL; |
| 5333 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5334 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5335 | #endif /* HAVE_EXECV */ |
| 5336 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5337 | #ifdef HAVE_POSIX_SPAWN |
| 5338 | |
| 5339 | enum posix_spawn_file_actions_identifier { |
| 5340 | POSIX_SPAWN_OPEN, |
| 5341 | POSIX_SPAWN_CLOSE, |
| 5342 | POSIX_SPAWN_DUP2 |
| 5343 | }; |
| 5344 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5345 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5346 | static int |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5347 | convert_sched_param(PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5348 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5349 | |
| 5350 | static int |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5351 | parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup, |
| 5352 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5353 | PyObject *setsigdef, PyObject *scheduler, |
| 5354 | posix_spawnattr_t *attrp) |
| 5355 | { |
| 5356 | long all_flags = 0; |
| 5357 | |
| 5358 | errno = posix_spawnattr_init(attrp); |
| 5359 | if (errno) { |
| 5360 | posix_error(); |
| 5361 | return -1; |
| 5362 | } |
| 5363 | |
| 5364 | if (setpgroup) { |
| 5365 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5366 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5367 | goto fail; |
| 5368 | } |
| 5369 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5370 | if (errno) { |
| 5371 | posix_error(); |
| 5372 | goto fail; |
| 5373 | } |
| 5374 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5375 | } |
| 5376 | |
| 5377 | if (resetids) { |
| 5378 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5379 | } |
| 5380 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5381 | if (setsid) { |
| 5382 | #ifdef POSIX_SPAWN_SETSID |
| 5383 | all_flags |= POSIX_SPAWN_SETSID; |
| 5384 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5385 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5386 | #else |
| 5387 | argument_unavailable_error(func_name, "setsid"); |
| 5388 | return -1; |
| 5389 | #endif |
| 5390 | } |
| 5391 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5392 | if (setsigmask) { |
| 5393 | sigset_t set; |
| 5394 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5395 | goto fail; |
| 5396 | } |
| 5397 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5398 | if (errno) { |
| 5399 | posix_error(); |
| 5400 | goto fail; |
| 5401 | } |
| 5402 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5403 | } |
| 5404 | |
| 5405 | if (setsigdef) { |
| 5406 | sigset_t set; |
| 5407 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5408 | goto fail; |
| 5409 | } |
| 5410 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5411 | if (errno) { |
| 5412 | posix_error(); |
| 5413 | goto fail; |
| 5414 | } |
| 5415 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5416 | } |
| 5417 | |
| 5418 | if (scheduler) { |
| 5419 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5420 | PyObject *py_schedpolicy; |
| 5421 | struct sched_param schedparam; |
| 5422 | |
| 5423 | if (!PyArg_ParseTuple(scheduler, "OO&" |
| 5424 | ";A scheduler tuple must have two elements", |
| 5425 | &py_schedpolicy, convert_sched_param, &schedparam)) { |
| 5426 | goto fail; |
| 5427 | } |
| 5428 | if (py_schedpolicy != Py_None) { |
| 5429 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5430 | |
| 5431 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5432 | goto fail; |
| 5433 | } |
| 5434 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5435 | if (errno) { |
| 5436 | posix_error(); |
| 5437 | goto fail; |
| 5438 | } |
| 5439 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5440 | } |
| 5441 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5442 | if (errno) { |
| 5443 | posix_error(); |
| 5444 | goto fail; |
| 5445 | } |
| 5446 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5447 | #else |
| 5448 | PyErr_SetString(PyExc_NotImplementedError, |
| 5449 | "The scheduler option is not supported in this system."); |
| 5450 | goto fail; |
| 5451 | #endif |
| 5452 | } |
| 5453 | |
| 5454 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5455 | if (errno) { |
| 5456 | posix_error(); |
| 5457 | goto fail; |
| 5458 | } |
| 5459 | |
| 5460 | return 0; |
| 5461 | |
| 5462 | fail: |
| 5463 | (void)posix_spawnattr_destroy(attrp); |
| 5464 | return -1; |
| 5465 | } |
| 5466 | |
| 5467 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5468 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5469 | posix_spawn_file_actions_t *file_actionsp, |
| 5470 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5471 | { |
| 5472 | PyObject *seq; |
| 5473 | PyObject *file_action = NULL; |
| 5474 | PyObject *tag_obj; |
| 5475 | |
| 5476 | seq = PySequence_Fast(file_actions, |
| 5477 | "file_actions must be a sequence or None"); |
| 5478 | if (seq == NULL) { |
| 5479 | return -1; |
| 5480 | } |
| 5481 | |
| 5482 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5483 | if (errno) { |
| 5484 | posix_error(); |
| 5485 | Py_DECREF(seq); |
| 5486 | return -1; |
| 5487 | } |
| 5488 | |
Zackery Spytz | d52a83a | 2019-06-26 14:54:20 -0600 | [diff] [blame] | 5489 | for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) { |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5490 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5491 | Py_INCREF(file_action); |
| 5492 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5493 | PyErr_SetString(PyExc_TypeError, |
| 5494 | "Each file_actions element must be a non-empty tuple"); |
| 5495 | goto fail; |
| 5496 | } |
| 5497 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5498 | if (tag == -1 && PyErr_Occurred()) { |
| 5499 | goto fail; |
| 5500 | } |
| 5501 | |
| 5502 | /* Populate the file_actions object */ |
| 5503 | switch (tag) { |
| 5504 | case POSIX_SPAWN_OPEN: { |
| 5505 | int fd, oflag; |
| 5506 | PyObject *path; |
| 5507 | unsigned long mode; |
| 5508 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5509 | ";A open file_action tuple must have 5 elements", |
| 5510 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5511 | &oflag, &mode)) |
| 5512 | { |
| 5513 | goto fail; |
| 5514 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5515 | if (PyList_Append(temp_buffer, path)) { |
| 5516 | Py_DECREF(path); |
| 5517 | goto fail; |
| 5518 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5519 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5520 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5521 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5522 | if (errno) { |
| 5523 | posix_error(); |
| 5524 | goto fail; |
| 5525 | } |
| 5526 | break; |
| 5527 | } |
| 5528 | case POSIX_SPAWN_CLOSE: { |
| 5529 | int fd; |
| 5530 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5531 | ";A close file_action tuple must have 2 elements", |
| 5532 | &tag_obj, &fd)) |
| 5533 | { |
| 5534 | goto fail; |
| 5535 | } |
| 5536 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5537 | if (errno) { |
| 5538 | posix_error(); |
| 5539 | goto fail; |
| 5540 | } |
| 5541 | break; |
| 5542 | } |
| 5543 | case POSIX_SPAWN_DUP2: { |
| 5544 | int fd1, fd2; |
| 5545 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5546 | ";A dup2 file_action tuple must have 3 elements", |
| 5547 | &tag_obj, &fd1, &fd2)) |
| 5548 | { |
| 5549 | goto fail; |
| 5550 | } |
| 5551 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5552 | fd1, fd2); |
| 5553 | if (errno) { |
| 5554 | posix_error(); |
| 5555 | goto fail; |
| 5556 | } |
| 5557 | break; |
| 5558 | } |
| 5559 | default: { |
| 5560 | PyErr_SetString(PyExc_TypeError, |
| 5561 | "Unknown file_actions identifier"); |
| 5562 | goto fail; |
| 5563 | } |
| 5564 | } |
| 5565 | Py_DECREF(file_action); |
| 5566 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5567 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5568 | Py_DECREF(seq); |
| 5569 | return 0; |
| 5570 | |
| 5571 | fail: |
| 5572 | Py_DECREF(seq); |
| 5573 | Py_DECREF(file_action); |
| 5574 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5575 | return -1; |
| 5576 | } |
| 5577 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5578 | |
| 5579 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5580 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 5581 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5582 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5583 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5584 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5585 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5586 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5587 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5588 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5589 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5590 | posix_spawnattr_t attr; |
| 5591 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5592 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5593 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5594 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5595 | pid_t pid; |
| 5596 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5597 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5598 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5599 | argv is a list or tuple of strings and env is a dictionary |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5600 | like posix.environ. */ |
| 5601 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5602 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5603 | PyErr_Format(PyExc_TypeError, |
| 5604 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5605 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5606 | } |
| 5607 | argc = PySequence_Size(argv); |
| 5608 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5609 | PyErr_Format(PyExc_ValueError, |
| 5610 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5611 | return NULL; |
| 5612 | } |
| 5613 | |
| 5614 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5615 | PyErr_Format(PyExc_TypeError, |
| 5616 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5617 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5618 | } |
| 5619 | |
| 5620 | argvlist = parse_arglist(argv, &argc); |
| 5621 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5622 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5623 | } |
| 5624 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5625 | PyErr_Format(PyExc_ValueError, |
| 5626 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5627 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5628 | } |
| 5629 | |
| 5630 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5631 | if (envlist == NULL) { |
| 5632 | goto exit; |
| 5633 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5634 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 5635 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5636 | /* There is a bug in old versions of glibc that makes some of the |
| 5637 | * helper functions for manipulating file actions not copy the provided |
| 5638 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5639 | * copy the value of path for some old versions of glibc (<2.20). |
| 5640 | * The use of temp_buffer here is a workaround that keeps the |
| 5641 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5642 | * Check https://bugs.python.org/issue33630 and |
| 5643 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5644 | temp_buffer = PyList_New(0); |
| 5645 | if (!temp_buffer) { |
| 5646 | goto exit; |
| 5647 | } |
| 5648 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5649 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5650 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5651 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5652 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5653 | |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5654 | if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid, |
| 5655 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5656 | goto exit; |
| 5657 | } |
| 5658 | attrp = &attr; |
| 5659 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5660 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5661 | #ifdef HAVE_POSIX_SPAWNP |
| 5662 | if (use_posix_spawnp) { |
| 5663 | err_code = posix_spawnp(&pid, path->narrow, |
| 5664 | file_actionsp, attrp, argvlist, envlist); |
| 5665 | } |
| 5666 | else |
| 5667 | #endif /* HAVE_POSIX_SPAWNP */ |
| 5668 | { |
| 5669 | err_code = posix_spawn(&pid, path->narrow, |
| 5670 | file_actionsp, attrp, argvlist, envlist); |
| 5671 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5672 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5673 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5674 | if (err_code) { |
| 5675 | errno = err_code; |
| 5676 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5677 | goto exit; |
| 5678 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5679 | #ifdef _Py_MEMORY_SANITIZER |
| 5680 | __msan_unpoison(&pid, sizeof(pid)); |
| 5681 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5682 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5683 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5684 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5685 | if (file_actionsp) { |
| 5686 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5687 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5688 | if (attrp) { |
| 5689 | (void)posix_spawnattr_destroy(attrp); |
| 5690 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5691 | if (envlist) { |
| 5692 | free_string_array(envlist, envc); |
| 5693 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5694 | if (argvlist) { |
| 5695 | free_string_array(argvlist, argc); |
| 5696 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5697 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5698 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5699 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5700 | |
| 5701 | |
| 5702 | /*[clinic input] |
| 5703 | |
| 5704 | os.posix_spawn |
| 5705 | path: path_t |
| 5706 | Path of executable file. |
| 5707 | argv: object |
| 5708 | Tuple or list of strings. |
| 5709 | env: object |
| 5710 | Dictionary of strings mapping to strings. |
| 5711 | / |
| 5712 | * |
| 5713 | file_actions: object(c_default='NULL') = () |
| 5714 | A sequence of file action tuples. |
| 5715 | setpgroup: object = NULL |
| 5716 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5717 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5718 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 5719 | setsid: bool(accept={int}) = False |
| 5720 | If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated. |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5721 | setsigmask: object(c_default='NULL') = () |
| 5722 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5723 | setsigdef: object(c_default='NULL') = () |
| 5724 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5725 | scheduler: object = NULL |
| 5726 | A tuple with the scheduler policy (optional) and parameters. |
| 5727 | |
| 5728 | Execute the program specified by path in a new process. |
| 5729 | [clinic start generated code]*/ |
| 5730 | |
| 5731 | static PyObject * |
| 5732 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5733 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5734 | PyObject *setpgroup, int resetids, int setsid, |
| 5735 | PyObject *setsigmask, PyObject *setsigdef, |
| 5736 | PyObject *scheduler) |
| 5737 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5738 | { |
| 5739 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5740 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5741 | scheduler); |
| 5742 | } |
| 5743 | #endif /* HAVE_POSIX_SPAWN */ |
| 5744 | |
| 5745 | |
| 5746 | |
| 5747 | #ifdef HAVE_POSIX_SPAWNP |
| 5748 | /*[clinic input] |
| 5749 | |
| 5750 | os.posix_spawnp |
| 5751 | path: path_t |
| 5752 | Path of executable file. |
| 5753 | argv: object |
| 5754 | Tuple or list of strings. |
| 5755 | env: object |
| 5756 | Dictionary of strings mapping to strings. |
| 5757 | / |
| 5758 | * |
| 5759 | file_actions: object(c_default='NULL') = () |
| 5760 | A sequence of file action tuples. |
| 5761 | setpgroup: object = NULL |
| 5762 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5763 | resetids: bool(accept={int}) = False |
| 5764 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5765 | setsid: bool(accept={int}) = False |
| 5766 | If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated. |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5767 | setsigmask: object(c_default='NULL') = () |
| 5768 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5769 | setsigdef: object(c_default='NULL') = () |
| 5770 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5771 | scheduler: object = NULL |
| 5772 | A tuple with the scheduler policy (optional) and parameters. |
| 5773 | |
| 5774 | Execute the program specified by path in a new process. |
| 5775 | [clinic start generated code]*/ |
| 5776 | |
| 5777 | static PyObject * |
| 5778 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5779 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5780 | PyObject *setpgroup, int resetids, int setsid, |
| 5781 | PyObject *setsigmask, PyObject *setsigdef, |
| 5782 | PyObject *scheduler) |
| 5783 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5784 | { |
| 5785 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5786 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5787 | scheduler); |
| 5788 | } |
| 5789 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5790 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5791 | #ifdef HAVE_RTPSPAWN |
| 5792 | static intptr_t |
| 5793 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], |
| 5794 | const char *envp[]) |
| 5795 | { |
| 5796 | RTP_ID rtpid; |
| 5797 | int status; |
| 5798 | pid_t res; |
| 5799 | int async_err = 0; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5800 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5801 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
| 5802 | uStackSize=0 cannot be used, the default stack size is too small for |
| 5803 | Python. */ |
| 5804 | if (envp) { |
| 5805 | rtpid = rtpSpawn(rtpFileName, argv, envp, |
| 5806 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5807 | } |
| 5808 | else { |
| 5809 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, |
| 5810 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5811 | } |
| 5812 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { |
| 5813 | do { |
| 5814 | res = waitpid((pid_t)rtpid, &status, 0); |
| 5815 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 5816 | |
| 5817 | if (res < 0) |
| 5818 | return RTP_ID_ERROR; |
| 5819 | return ((intptr_t)status); |
| 5820 | } |
| 5821 | return ((intptr_t)rtpid); |
| 5822 | } |
| 5823 | #endif |
| 5824 | |
| 5825 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5826 | /*[clinic input] |
| 5827 | os.spawnv |
| 5828 | |
| 5829 | mode: int |
| 5830 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5831 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5832 | Path of executable file. |
| 5833 | argv: object |
| 5834 | Tuple or list of strings. |
| 5835 | / |
| 5836 | |
| 5837 | Execute the program specified by path in a new process. |
| 5838 | [clinic start generated code]*/ |
| 5839 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5840 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5841 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5842 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5843 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5844 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5845 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5846 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5847 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5848 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5849 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5850 | /* spawnv has three arguments: (mode, path, argv), where |
| 5851 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5852 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5853 | if (PyList_Check(argv)) { |
| 5854 | argc = PyList_Size(argv); |
| 5855 | getitem = PyList_GetItem; |
| 5856 | } |
| 5857 | else if (PyTuple_Check(argv)) { |
| 5858 | argc = PyTuple_Size(argv); |
| 5859 | getitem = PyTuple_GetItem; |
| 5860 | } |
| 5861 | else { |
| 5862 | PyErr_SetString(PyExc_TypeError, |
| 5863 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5864 | return NULL; |
| 5865 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5866 | if (argc == 0) { |
| 5867 | PyErr_SetString(PyExc_ValueError, |
| 5868 | "spawnv() arg 2 cannot be empty"); |
| 5869 | return NULL; |
| 5870 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5871 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5872 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5873 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5874 | return PyErr_NoMemory(); |
| 5875 | } |
| 5876 | for (i = 0; i < argc; i++) { |
| 5877 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5878 | &argvlist[i])) { |
| 5879 | free_string_array(argvlist, i); |
| 5880 | PyErr_SetString( |
| 5881 | PyExc_TypeError, |
| 5882 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5883 | return NULL; |
| 5884 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5885 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5886 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5887 | PyErr_SetString( |
| 5888 | PyExc_ValueError, |
| 5889 | "spawnv() arg 2 first element cannot be empty"); |
| 5890 | return NULL; |
| 5891 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5892 | } |
| 5893 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5894 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5895 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5896 | if (mode == _OLD_P_OVERLAY) |
| 5897 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5898 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5899 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5900 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5901 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5902 | #ifdef HAVE_WSPAWNV |
| 5903 | spawnval = _wspawnv(mode, path->wide, argvlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5904 | #elif defined(HAVE_RTPSPAWN) |
| 5905 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5906 | #else |
| 5907 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5908 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5909 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5910 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5911 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5912 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5913 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5914 | if (spawnval == -1) |
| 5915 | return posix_error(); |
| 5916 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5917 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5918 | } |
| 5919 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5920 | /*[clinic input] |
| 5921 | os.spawnve |
| 5922 | |
| 5923 | mode: int |
| 5924 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5925 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5926 | Path of executable file. |
| 5927 | argv: object |
| 5928 | Tuple or list of strings. |
| 5929 | env: object |
| 5930 | Dictionary of strings mapping to strings. |
| 5931 | / |
| 5932 | |
| 5933 | Execute the program specified by path in a new process. |
| 5934 | [clinic start generated code]*/ |
| 5935 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5936 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5937 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5938 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5939 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5940 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5941 | EXECV_CHAR **argvlist; |
| 5942 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5943 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5944 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5945 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5946 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5947 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5948 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5949 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5950 | argv is a list or tuple of strings and env is a dictionary |
| 5951 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5952 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5953 | if (PyList_Check(argv)) { |
| 5954 | argc = PyList_Size(argv); |
| 5955 | getitem = PyList_GetItem; |
| 5956 | } |
| 5957 | else if (PyTuple_Check(argv)) { |
| 5958 | argc = PyTuple_Size(argv); |
| 5959 | getitem = PyTuple_GetItem; |
| 5960 | } |
| 5961 | else { |
| 5962 | PyErr_SetString(PyExc_TypeError, |
| 5963 | "spawnve() arg 2 must be a tuple or list"); |
| 5964 | goto fail_0; |
| 5965 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5966 | if (argc == 0) { |
| 5967 | PyErr_SetString(PyExc_ValueError, |
| 5968 | "spawnve() arg 2 cannot be empty"); |
| 5969 | goto fail_0; |
| 5970 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5971 | if (!PyMapping_Check(env)) { |
| 5972 | PyErr_SetString(PyExc_TypeError, |
| 5973 | "spawnve() arg 3 must be a mapping object"); |
| 5974 | goto fail_0; |
| 5975 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5976 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5977 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5978 | if (argvlist == NULL) { |
| 5979 | PyErr_NoMemory(); |
| 5980 | goto fail_0; |
| 5981 | } |
| 5982 | for (i = 0; i < argc; i++) { |
| 5983 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5984 | &argvlist[i])) |
| 5985 | { |
| 5986 | lastarg = i; |
| 5987 | goto fail_1; |
| 5988 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5989 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5990 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5991 | PyErr_SetString( |
| 5992 | PyExc_ValueError, |
| 5993 | "spawnv() arg 2 first element cannot be empty"); |
| 5994 | goto fail_1; |
| 5995 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5996 | } |
| 5997 | lastarg = argc; |
| 5998 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5999 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6000 | envlist = parse_envlist(env, &envc); |
| 6001 | if (envlist == NULL) |
| 6002 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6003 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6004 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6005 | if (mode == _OLD_P_OVERLAY) |
| 6006 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6007 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6008 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6009 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6010 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6011 | #ifdef HAVE_WSPAWNV |
| 6012 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6013 | #elif defined(HAVE_RTPSPAWN) |
| 6014 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, |
| 6015 | (const char **)envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6016 | #else |
| 6017 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 6018 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6019 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6020 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6021 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6022 | if (spawnval == -1) |
| 6023 | (void) posix_error(); |
| 6024 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6025 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6026 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6027 | while (--envc >= 0) |
| 6028 | PyMem_DEL(envlist[envc]); |
| 6029 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 6030 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6031 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6032 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6033 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6034 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6035 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6036 | #endif /* HAVE_SPAWNV */ |
| 6037 | |
| 6038 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6039 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6040 | |
| 6041 | /* Helper function to validate arguments. |
| 6042 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 6043 | If obj is non-NULL it must be callable. */ |
| 6044 | static int |
| 6045 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 6046 | { |
| 6047 | if (obj && !PyCallable_Check(obj)) { |
| 6048 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6049 | obj_name, _PyType_Name(Py_TYPE(obj))); |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6050 | return -1; |
| 6051 | } |
| 6052 | return 0; |
| 6053 | } |
| 6054 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6055 | /*[clinic input] |
| 6056 | os.register_at_fork |
| 6057 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6058 | * |
| 6059 | before: object=NULL |
| 6060 | A callable to be called in the parent before the fork() syscall. |
| 6061 | after_in_child: object=NULL |
| 6062 | A callable to be called in the child after fork(). |
| 6063 | after_in_parent: object=NULL |
| 6064 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6065 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6066 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6067 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6068 | 'before' callbacks are called in reverse order. |
| 6069 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6070 | |
| 6071 | [clinic start generated code]*/ |
| 6072 | |
| 6073 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6074 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 6075 | PyObject *after_in_child, PyObject *after_in_parent) |
| 6076 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6077 | { |
| 6078 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6079 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6080 | if (!before && !after_in_child && !after_in_parent) { |
| 6081 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 6082 | return NULL; |
| 6083 | } |
| 6084 | if (check_null_or_callable(before, "before") || |
| 6085 | check_null_or_callable(after_in_child, "after_in_child") || |
| 6086 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6087 | return NULL; |
| 6088 | } |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 6089 | interp = _PyInterpreterState_Get(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6090 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6091 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6092 | return NULL; |
| 6093 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6094 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6095 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6096 | } |
| 6097 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 6098 | return NULL; |
| 6099 | } |
| 6100 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6101 | } |
| 6102 | #endif /* HAVE_FORK */ |
| 6103 | |
| 6104 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6105 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6106 | /*[clinic input] |
| 6107 | os.fork1 |
| 6108 | |
| 6109 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 6110 | |
| 6111 | Return 0 to child process and PID of child to parent process. |
| 6112 | [clinic start generated code]*/ |
| 6113 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6114 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6115 | os_fork1_impl(PyObject *module) |
| 6116 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6117 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6118 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6119 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6120 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6121 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6122 | return NULL; |
| 6123 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6124 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6125 | pid = fork1(); |
| 6126 | if (pid == 0) { |
| 6127 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6128 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6129 | } else { |
| 6130 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6131 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6132 | } |
| 6133 | if (pid == -1) |
| 6134 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6135 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6136 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6137 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6138 | |
| 6139 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6140 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6141 | /*[clinic input] |
| 6142 | os.fork |
| 6143 | |
| 6144 | Fork a child process. |
| 6145 | |
| 6146 | Return 0 to child process and PID of child to parent process. |
| 6147 | [clinic start generated code]*/ |
| 6148 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6149 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6150 | os_fork_impl(PyObject *module) |
| 6151 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6152 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6153 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6154 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6155 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6156 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6157 | return NULL; |
| 6158 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6159 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6160 | pid = fork(); |
| 6161 | if (pid == 0) { |
| 6162 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6163 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6164 | } else { |
| 6165 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6166 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6167 | } |
| 6168 | if (pid == -1) |
| 6169 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6170 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6171 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6172 | #endif /* HAVE_FORK */ |
| 6173 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6174 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6175 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6176 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6177 | /*[clinic input] |
| 6178 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6179 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6180 | policy: int |
| 6181 | |
| 6182 | Get the maximum scheduling priority for policy. |
| 6183 | [clinic start generated code]*/ |
| 6184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6185 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6186 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 6187 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6188 | { |
| 6189 | int max; |
| 6190 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6191 | max = sched_get_priority_max(policy); |
| 6192 | if (max < 0) |
| 6193 | return posix_error(); |
| 6194 | return PyLong_FromLong(max); |
| 6195 | } |
| 6196 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6197 | |
| 6198 | /*[clinic input] |
| 6199 | os.sched_get_priority_min |
| 6200 | |
| 6201 | policy: int |
| 6202 | |
| 6203 | Get the minimum scheduling priority for policy. |
| 6204 | [clinic start generated code]*/ |
| 6205 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6206 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6207 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 6208 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6209 | { |
| 6210 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6211 | if (min < 0) |
| 6212 | return posix_error(); |
| 6213 | return PyLong_FromLong(min); |
| 6214 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6215 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 6216 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6217 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6218 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 6219 | /*[clinic input] |
| 6220 | os.sched_getscheduler |
| 6221 | pid: pid_t |
| 6222 | / |
| 6223 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6224 | Get the scheduling policy for the process identified by pid. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6225 | |
| 6226 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6227 | [clinic start generated code]*/ |
| 6228 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6229 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6230 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6231 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6232 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6233 | int policy; |
| 6234 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6235 | policy = sched_getscheduler(pid); |
| 6236 | if (policy < 0) |
| 6237 | return posix_error(); |
| 6238 | return PyLong_FromLong(policy); |
| 6239 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6240 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6241 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6242 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6243 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6244 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6245 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6246 | |
| 6247 | @classmethod |
| 6248 | os.sched_param.__new__ |
| 6249 | |
| 6250 | sched_priority: object |
| 6251 | A scheduling parameter. |
| 6252 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6253 | Currently has only one field: sched_priority |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6254 | [clinic start generated code]*/ |
| 6255 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6256 | static PyObject * |
| 6257 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6258 | /*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6259 | { |
| 6260 | PyObject *res; |
| 6261 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6262 | res = PyStructSequence_New(type); |
| 6263 | if (!res) |
| 6264 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6265 | Py_INCREF(sched_priority); |
| 6266 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6267 | return res; |
| 6268 | } |
| 6269 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6270 | PyDoc_VAR(os_sched_param__doc__); |
| 6271 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6272 | static PyStructSequence_Field sched_param_fields[] = { |
| 6273 | {"sched_priority", "the scheduling priority"}, |
| 6274 | {0} |
| 6275 | }; |
| 6276 | |
| 6277 | static PyStructSequence_Desc sched_param_desc = { |
| 6278 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6279 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6280 | sched_param_fields, |
| 6281 | 1 |
| 6282 | }; |
| 6283 | |
| 6284 | static int |
| 6285 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 6286 | { |
| 6287 | long priority; |
| 6288 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6289 | PyObject *SchedParamType = _posixstate_global->SchedParamType; |
| 6290 | if (Py_TYPE(param) != (PyTypeObject *)SchedParamType) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6291 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6292 | return 0; |
| 6293 | } |
| 6294 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6295 | if (priority == -1 && PyErr_Occurred()) |
| 6296 | return 0; |
| 6297 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6298 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6299 | return 0; |
| 6300 | } |
| 6301 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6302 | return 1; |
| 6303 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6304 | #endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6305 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6306 | |
| 6307 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6308 | /*[clinic input] |
| 6309 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6310 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6311 | pid: pid_t |
| 6312 | policy: int |
| 6313 | param: sched_param |
| 6314 | / |
| 6315 | |
| 6316 | Set the scheduling policy for the process identified by pid. |
| 6317 | |
| 6318 | If pid is 0, the calling process is changed. |
| 6319 | param is an instance of sched_param. |
| 6320 | [clinic start generated code]*/ |
| 6321 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6322 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6323 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6324 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6325 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6326 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6327 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6328 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6329 | ** scheduling policy under Solaris/Illumos, and others. |
| 6330 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6331 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6332 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6333 | return posix_error(); |
| 6334 | Py_RETURN_NONE; |
| 6335 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6336 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6337 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6338 | |
| 6339 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6340 | /*[clinic input] |
| 6341 | os.sched_getparam |
| 6342 | pid: pid_t |
| 6343 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6344 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6345 | Returns scheduling parameters for the process identified by pid. |
| 6346 | |
| 6347 | If pid is 0, returns parameters for the calling process. |
| 6348 | Return value is an instance of sched_param. |
| 6349 | [clinic start generated code]*/ |
| 6350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6351 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6352 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6353 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6354 | { |
| 6355 | struct sched_param param; |
| 6356 | PyObject *result; |
| 6357 | PyObject *priority; |
| 6358 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6359 | if (sched_getparam(pid, ¶m)) |
| 6360 | return posix_error(); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6361 | PyObject *SchedParamType = _posixstate_global->SchedParamType; |
| 6362 | result = PyStructSequence_New((PyTypeObject *)SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6363 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6364 | return NULL; |
| 6365 | priority = PyLong_FromLong(param.sched_priority); |
| 6366 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6367 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6368 | return NULL; |
| 6369 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6370 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6371 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6372 | } |
| 6373 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6374 | |
| 6375 | /*[clinic input] |
| 6376 | os.sched_setparam |
| 6377 | pid: pid_t |
| 6378 | param: sched_param |
| 6379 | / |
| 6380 | |
| 6381 | Set scheduling parameters for the process identified by pid. |
| 6382 | |
| 6383 | If pid is 0, sets parameters for the calling process. |
| 6384 | param should be an instance of sched_param. |
| 6385 | [clinic start generated code]*/ |
| 6386 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6387 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6388 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6389 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6390 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6391 | { |
| 6392 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6393 | return posix_error(); |
| 6394 | Py_RETURN_NONE; |
| 6395 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6396 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6397 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6398 | |
| 6399 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6400 | /*[clinic input] |
| 6401 | os.sched_rr_get_interval -> double |
| 6402 | pid: pid_t |
| 6403 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6404 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6405 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6406 | |
| 6407 | Value returned is a float. |
| 6408 | [clinic start generated code]*/ |
| 6409 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6410 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6411 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6412 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6413 | { |
| 6414 | struct timespec interval; |
| 6415 | if (sched_rr_get_interval(pid, &interval)) { |
| 6416 | posix_error(); |
| 6417 | return -1.0; |
| 6418 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6419 | #ifdef _Py_MEMORY_SANITIZER |
| 6420 | __msan_unpoison(&interval, sizeof(interval)); |
| 6421 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6422 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6423 | } |
| 6424 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6425 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6426 | |
| 6427 | /*[clinic input] |
| 6428 | os.sched_yield |
| 6429 | |
| 6430 | Voluntarily relinquish the CPU. |
| 6431 | [clinic start generated code]*/ |
| 6432 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6433 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6434 | os_sched_yield_impl(PyObject *module) |
| 6435 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6436 | { |
| 6437 | if (sched_yield()) |
| 6438 | return posix_error(); |
| 6439 | Py_RETURN_NONE; |
| 6440 | } |
| 6441 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6442 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6443 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6444 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6445 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6446 | /*[clinic input] |
| 6447 | os.sched_setaffinity |
| 6448 | pid: pid_t |
| 6449 | mask : object |
| 6450 | / |
| 6451 | |
| 6452 | Set the CPU affinity of the process identified by pid to mask. |
| 6453 | |
| 6454 | mask should be an iterable of integers identifying CPUs. |
| 6455 | [clinic start generated code]*/ |
| 6456 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6457 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6458 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6459 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6460 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6461 | int ncpus; |
| 6462 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6463 | cpu_set_t *cpu_set = NULL; |
| 6464 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6465 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6466 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6467 | if (iterator == NULL) |
| 6468 | return NULL; |
| 6469 | |
| 6470 | ncpus = NCPUS_START; |
| 6471 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6472 | cpu_set = CPU_ALLOC(ncpus); |
| 6473 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6474 | PyErr_NoMemory(); |
| 6475 | goto error; |
| 6476 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6477 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6478 | |
| 6479 | while ((item = PyIter_Next(iterator))) { |
| 6480 | long cpu; |
| 6481 | if (!PyLong_Check(item)) { |
| 6482 | PyErr_Format(PyExc_TypeError, |
| 6483 | "expected an iterator of ints, " |
| 6484 | "but iterator yielded %R", |
| 6485 | Py_TYPE(item)); |
| 6486 | Py_DECREF(item); |
| 6487 | goto error; |
| 6488 | } |
| 6489 | cpu = PyLong_AsLong(item); |
| 6490 | Py_DECREF(item); |
| 6491 | if (cpu < 0) { |
| 6492 | if (!PyErr_Occurred()) |
| 6493 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6494 | goto error; |
| 6495 | } |
| 6496 | if (cpu > INT_MAX - 1) { |
| 6497 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6498 | goto error; |
| 6499 | } |
| 6500 | if (cpu >= ncpus) { |
| 6501 | /* Grow CPU mask to fit the CPU number */ |
| 6502 | int newncpus = ncpus; |
| 6503 | cpu_set_t *newmask; |
| 6504 | size_t newsetsize; |
| 6505 | while (newncpus <= cpu) { |
| 6506 | if (newncpus > INT_MAX / 2) |
| 6507 | newncpus = cpu + 1; |
| 6508 | else |
| 6509 | newncpus = newncpus * 2; |
| 6510 | } |
| 6511 | newmask = CPU_ALLOC(newncpus); |
| 6512 | if (newmask == NULL) { |
| 6513 | PyErr_NoMemory(); |
| 6514 | goto error; |
| 6515 | } |
| 6516 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6517 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6518 | memcpy(newmask, cpu_set, setsize); |
| 6519 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6520 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6521 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6522 | ncpus = newncpus; |
| 6523 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6524 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6525 | } |
Brandt Bucher | 45a30af | 2019-06-27 09:10:57 -0700 | [diff] [blame] | 6526 | if (PyErr_Occurred()) { |
| 6527 | goto error; |
| 6528 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6529 | Py_CLEAR(iterator); |
| 6530 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6531 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6532 | posix_error(); |
| 6533 | goto error; |
| 6534 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6535 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6536 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6537 | |
| 6538 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6539 | if (cpu_set) |
| 6540 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6541 | Py_XDECREF(iterator); |
| 6542 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6543 | } |
| 6544 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6545 | |
| 6546 | /*[clinic input] |
| 6547 | os.sched_getaffinity |
| 6548 | pid: pid_t |
| 6549 | / |
| 6550 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6551 | Return the affinity of the process identified by pid (or the current process if zero). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6552 | |
| 6553 | The affinity is returned as a set of CPU identifiers. |
| 6554 | [clinic start generated code]*/ |
| 6555 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6556 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6557 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6558 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6559 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6560 | int cpu, ncpus, count; |
| 6561 | size_t setsize; |
| 6562 | cpu_set_t *mask = NULL; |
| 6563 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6564 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6565 | ncpus = NCPUS_START; |
| 6566 | while (1) { |
| 6567 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6568 | mask = CPU_ALLOC(ncpus); |
| 6569 | if (mask == NULL) |
| 6570 | return PyErr_NoMemory(); |
| 6571 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6572 | break; |
| 6573 | CPU_FREE(mask); |
| 6574 | if (errno != EINVAL) |
| 6575 | return posix_error(); |
| 6576 | if (ncpus > INT_MAX / 2) { |
| 6577 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6578 | "a large enough CPU set"); |
| 6579 | return NULL; |
| 6580 | } |
| 6581 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6582 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6583 | |
| 6584 | res = PySet_New(NULL); |
| 6585 | if (res == NULL) |
| 6586 | goto error; |
| 6587 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6588 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6589 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6590 | --count; |
| 6591 | if (cpu_num == NULL) |
| 6592 | goto error; |
| 6593 | if (PySet_Add(res, cpu_num)) { |
| 6594 | Py_DECREF(cpu_num); |
| 6595 | goto error; |
| 6596 | } |
| 6597 | Py_DECREF(cpu_num); |
| 6598 | } |
| 6599 | } |
| 6600 | CPU_FREE(mask); |
| 6601 | return res; |
| 6602 | |
| 6603 | error: |
| 6604 | if (mask) |
| 6605 | CPU_FREE(mask); |
| 6606 | Py_XDECREF(res); |
| 6607 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6608 | } |
| 6609 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6610 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6611 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6612 | #endif /* HAVE_SCHED_H */ |
| 6613 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6614 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6615 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6616 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6617 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6618 | #define DEV_PTY_FILE "/dev/ptc" |
| 6619 | #define HAVE_DEV_PTMX |
| 6620 | #else |
| 6621 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6622 | #endif |
| 6623 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6624 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6625 | #ifdef HAVE_PTY_H |
| 6626 | #include <pty.h> |
| 6627 | #else |
| 6628 | #ifdef HAVE_LIBUTIL_H |
| 6629 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6630 | #else |
| 6631 | #ifdef HAVE_UTIL_H |
| 6632 | #include <util.h> |
| 6633 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6634 | #endif /* HAVE_LIBUTIL_H */ |
| 6635 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6636 | #ifdef HAVE_STROPTS_H |
| 6637 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6638 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6639 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6640 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6641 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6642 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6643 | /*[clinic input] |
| 6644 | os.openpty |
| 6645 | |
| 6646 | Open a pseudo-terminal. |
| 6647 | |
| 6648 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6649 | for both the master and slave ends. |
| 6650 | [clinic start generated code]*/ |
| 6651 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6652 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6653 | os_openpty_impl(PyObject *module) |
| 6654 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6655 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6656 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6657 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6658 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6659 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6660 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6661 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6662 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6663 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6664 | #endif |
| 6665 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6666 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6667 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6668 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6669 | goto posix_error; |
| 6670 | |
| 6671 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6672 | goto error; |
| 6673 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6674 | goto error; |
| 6675 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6676 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6677 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6678 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6679 | goto posix_error; |
| 6680 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6681 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6682 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6683 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6684 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6685 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6686 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6687 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6688 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6689 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6690 | goto posix_error; |
| 6691 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6692 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6693 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6694 | /* change permission of slave */ |
| 6695 | if (grantpt(master_fd) < 0) { |
| 6696 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6697 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6698 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6699 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6700 | /* unlock slave */ |
| 6701 | if (unlockpt(master_fd) < 0) { |
| 6702 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6703 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6704 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6705 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6706 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6707 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6708 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6709 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6710 | goto posix_error; |
| 6711 | |
| 6712 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6713 | if (slave_fd == -1) |
| 6714 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6715 | |
| 6716 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6717 | goto posix_error; |
| 6718 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6719 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6720 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6721 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6722 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6723 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6724 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6725 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6726 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6727 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6728 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6729 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6730 | posix_error: |
| 6731 | posix_error(); |
| 6732 | error: |
| 6733 | if (master_fd != -1) |
| 6734 | close(master_fd); |
| 6735 | if (slave_fd != -1) |
| 6736 | close(slave_fd); |
| 6737 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6738 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6739 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6740 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6741 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6742 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6743 | /*[clinic input] |
| 6744 | os.forkpty |
| 6745 | |
| 6746 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6747 | |
| 6748 | Returns a tuple of (pid, master_fd). |
| 6749 | Like fork(), return pid of 0 to the child process, |
| 6750 | and pid of child to the parent process. |
| 6751 | To both, return fd of newly opened pseudo-terminal. |
| 6752 | [clinic start generated code]*/ |
| 6753 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6754 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6755 | os_forkpty_impl(PyObject *module) |
| 6756 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6757 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6758 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6759 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6760 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6761 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6762 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6763 | return NULL; |
| 6764 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6765 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6766 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6767 | if (pid == 0) { |
| 6768 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6769 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6770 | } else { |
| 6771 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6772 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6773 | } |
| 6774 | if (pid == -1) |
| 6775 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6776 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6777 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6778 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6779 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6780 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6781 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6782 | /*[clinic input] |
| 6783 | os.getegid |
| 6784 | |
| 6785 | Return the current process's effective group id. |
| 6786 | [clinic start generated code]*/ |
| 6787 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6788 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6789 | os_getegid_impl(PyObject *module) |
| 6790 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6791 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6792 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6793 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6794 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6795 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6796 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6797 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6798 | /*[clinic input] |
| 6799 | os.geteuid |
| 6800 | |
| 6801 | Return the current process's effective user id. |
| 6802 | [clinic start generated code]*/ |
| 6803 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6804 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6805 | os_geteuid_impl(PyObject *module) |
| 6806 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6807 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6808 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6809 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6810 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6811 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6812 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6813 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6814 | /*[clinic input] |
| 6815 | os.getgid |
| 6816 | |
| 6817 | Return the current process's group id. |
| 6818 | [clinic start generated code]*/ |
| 6819 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6820 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6821 | os_getgid_impl(PyObject *module) |
| 6822 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6823 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6824 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6825 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6826 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6827 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6828 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6829 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6830 | /*[clinic input] |
| 6831 | os.getpid |
| 6832 | |
| 6833 | Return the current process id. |
| 6834 | [clinic start generated code]*/ |
| 6835 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6836 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6837 | os_getpid_impl(PyObject *module) |
| 6838 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6839 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6840 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6841 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6842 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6843 | |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6844 | #ifdef NGROUPS_MAX |
| 6845 | #define MAX_GROUPS NGROUPS_MAX |
| 6846 | #else |
| 6847 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6848 | #define MAX_GROUPS 64 |
| 6849 | #endif |
| 6850 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6851 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6852 | |
| 6853 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6854 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6855 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6856 | Returns a list of groups to which a user belongs.\n\n\ |
| 6857 | user: username to lookup\n\ |
| 6858 | group: base group id of the user"); |
| 6859 | |
| 6860 | static PyObject * |
| 6861 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6862 | { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6863 | const char *user; |
| 6864 | int i, ngroups; |
| 6865 | PyObject *list; |
| 6866 | #ifdef __APPLE__ |
| 6867 | int *groups, basegid; |
| 6868 | #else |
| 6869 | gid_t *groups, basegid; |
| 6870 | #endif |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6871 | |
| 6872 | /* |
| 6873 | * NGROUPS_MAX is defined by POSIX.1 as the maximum |
| 6874 | * number of supplimental groups a users can belong to. |
| 6875 | * We have to increment it by one because |
| 6876 | * getgrouplist() returns both the supplemental groups |
| 6877 | * and the primary group, i.e. all of the groups the |
| 6878 | * user belongs to. |
| 6879 | */ |
| 6880 | ngroups = 1 + MAX_GROUPS; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6881 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6882 | #ifdef __APPLE__ |
| 6883 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6884 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6885 | #else |
| 6886 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6887 | _Py_Gid_Converter, &basegid)) |
| 6888 | return NULL; |
| 6889 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6890 | |
| 6891 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6892 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6893 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6894 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6895 | #endif |
| 6896 | if (groups == NULL) |
| 6897 | return PyErr_NoMemory(); |
| 6898 | |
| 6899 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6900 | PyMem_Del(groups); |
| 6901 | return posix_error(); |
| 6902 | } |
| 6903 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6904 | #ifdef _Py_MEMORY_SANITIZER |
| 6905 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 6906 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 6907 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 6908 | #endif |
| 6909 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6910 | list = PyList_New(ngroups); |
| 6911 | if (list == NULL) { |
| 6912 | PyMem_Del(groups); |
| 6913 | return NULL; |
| 6914 | } |
| 6915 | |
| 6916 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6917 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6918 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6919 | #else |
| 6920 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6921 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6922 | if (o == NULL) { |
| 6923 | Py_DECREF(list); |
| 6924 | PyMem_Del(groups); |
| 6925 | return NULL; |
| 6926 | } |
| 6927 | PyList_SET_ITEM(list, i, o); |
| 6928 | } |
| 6929 | |
| 6930 | PyMem_Del(groups); |
| 6931 | |
| 6932 | return list; |
| 6933 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6934 | #endif /* HAVE_GETGROUPLIST */ |
| 6935 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6936 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6937 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6938 | /*[clinic input] |
| 6939 | os.getgroups |
| 6940 | |
| 6941 | Return list of supplemental group IDs for the process. |
| 6942 | [clinic start generated code]*/ |
| 6943 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6944 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6945 | os_getgroups_impl(PyObject *module) |
| 6946 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6947 | { |
| 6948 | PyObject *result = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6949 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6950 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6951 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6952 | * This is a helper variable to store the intermediate result when |
| 6953 | * that happens. |
| 6954 | * |
| 6955 | * To keep the code readable the OSX behaviour is unconditional, |
| 6956 | * according to the POSIX spec this should be safe on all unix-y |
| 6957 | * systems. |
| 6958 | */ |
| 6959 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6960 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6961 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6962 | #ifdef __APPLE__ |
| 6963 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6964 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6965 | * always first call getgroups with length 0 to get the actual number |
| 6966 | * of groups. |
| 6967 | */ |
| 6968 | n = getgroups(0, NULL); |
| 6969 | if (n < 0) { |
| 6970 | return posix_error(); |
| 6971 | } else if (n <= MAX_GROUPS) { |
| 6972 | /* groups will fit in existing array */ |
| 6973 | alt_grouplist = grouplist; |
| 6974 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6975 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6976 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6977 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6978 | } |
| 6979 | } |
| 6980 | |
| 6981 | n = getgroups(n, alt_grouplist); |
| 6982 | if (n == -1) { |
| 6983 | if (alt_grouplist != grouplist) { |
| 6984 | PyMem_Free(alt_grouplist); |
| 6985 | } |
| 6986 | return posix_error(); |
| 6987 | } |
| 6988 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6989 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6990 | if (n < 0) { |
| 6991 | if (errno == EINVAL) { |
| 6992 | n = getgroups(0, NULL); |
| 6993 | if (n == -1) { |
| 6994 | return posix_error(); |
| 6995 | } |
| 6996 | if (n == 0) { |
| 6997 | /* Avoid malloc(0) */ |
| 6998 | alt_grouplist = grouplist; |
| 6999 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7000 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7001 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7002 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7003 | } |
| 7004 | n = getgroups(n, alt_grouplist); |
| 7005 | if (n == -1) { |
| 7006 | PyMem_Free(alt_grouplist); |
| 7007 | return posix_error(); |
| 7008 | } |
| 7009 | } |
| 7010 | } else { |
| 7011 | return posix_error(); |
| 7012 | } |
| 7013 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7014 | #endif |
| 7015 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7016 | result = PyList_New(n); |
| 7017 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7018 | int i; |
| 7019 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7020 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7021 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7022 | Py_DECREF(result); |
| 7023 | result = NULL; |
| 7024 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7025 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7026 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7027 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7028 | } |
| 7029 | |
| 7030 | if (alt_grouplist != grouplist) { |
| 7031 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7032 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7033 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7034 | return result; |
| 7035 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7036 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7037 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7038 | #ifdef HAVE_INITGROUPS |
| 7039 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 7040 | "initgroups(username, gid) -> None\n\n\ |
| 7041 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 7042 | the groups of which the specified username is a member, plus the specified\n\ |
| 7043 | group id."); |
| 7044 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7045 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7046 | static PyObject * |
| 7047 | posix_initgroups(PyObject *self, PyObject *args) |
| 7048 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7049 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7050 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7051 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7052 | #ifdef __APPLE__ |
| 7053 | int gid; |
| 7054 | #else |
| 7055 | gid_t gid; |
| 7056 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7057 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7058 | #ifdef __APPLE__ |
| 7059 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 7060 | PyUnicode_FSConverter, &oname, |
| 7061 | &gid)) |
| 7062 | #else |
| 7063 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 7064 | PyUnicode_FSConverter, &oname, |
| 7065 | _Py_Gid_Converter, &gid)) |
| 7066 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7067 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7068 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7069 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7070 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7071 | Py_DECREF(oname); |
| 7072 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7073 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7074 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7075 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7076 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7077 | #endif /* HAVE_INITGROUPS */ |
| 7078 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7079 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7080 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7081 | /*[clinic input] |
| 7082 | os.getpgid |
| 7083 | |
| 7084 | pid: pid_t |
| 7085 | |
| 7086 | Call the system call getpgid(), and return the result. |
| 7087 | [clinic start generated code]*/ |
| 7088 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7089 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7090 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 7091 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7092 | { |
| 7093 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7094 | if (pgid < 0) |
| 7095 | return posix_error(); |
| 7096 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7097 | } |
| 7098 | #endif /* HAVE_GETPGID */ |
| 7099 | |
| 7100 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7101 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7102 | /*[clinic input] |
| 7103 | os.getpgrp |
| 7104 | |
| 7105 | Return the current process group id. |
| 7106 | [clinic start generated code]*/ |
| 7107 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7108 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7109 | os_getpgrp_impl(PyObject *module) |
| 7110 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7111 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7112 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7113 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7114 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7115 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7116 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7117 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7118 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7119 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7120 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7121 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7122 | /*[clinic input] |
| 7123 | os.setpgrp |
| 7124 | |
| 7125 | Make the current process the leader of its process group. |
| 7126 | [clinic start generated code]*/ |
| 7127 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7128 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7129 | os_setpgrp_impl(PyObject *module) |
| 7130 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7131 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7132 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7133 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7134 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7135 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7136 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7137 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7138 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7139 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7140 | #endif /* HAVE_SETPGRP */ |
| 7141 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7142 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7143 | |
| 7144 | #ifdef MS_WINDOWS |
| 7145 | #include <tlhelp32.h> |
| 7146 | |
| 7147 | static PyObject* |
| 7148 | win32_getppid() |
| 7149 | { |
| 7150 | HANDLE snapshot; |
| 7151 | pid_t mypid; |
| 7152 | PyObject* result = NULL; |
| 7153 | BOOL have_record; |
| 7154 | PROCESSENTRY32 pe; |
| 7155 | |
| 7156 | mypid = getpid(); /* This function never fails */ |
| 7157 | |
| 7158 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 7159 | if (snapshot == INVALID_HANDLE_VALUE) |
| 7160 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 7161 | |
| 7162 | pe.dwSize = sizeof(pe); |
| 7163 | have_record = Process32First(snapshot, &pe); |
| 7164 | while (have_record) { |
| 7165 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 7166 | /* We could cache the ulong value in a static variable. */ |
| 7167 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 7168 | break; |
| 7169 | } |
| 7170 | |
| 7171 | have_record = Process32Next(snapshot, &pe); |
| 7172 | } |
| 7173 | |
| 7174 | /* If our loop exits and our pid was not found (result will be NULL) |
| 7175 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 7176 | * error anyway, so let's raise it. */ |
| 7177 | if (!result) |
| 7178 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7179 | |
| 7180 | CloseHandle(snapshot); |
| 7181 | |
| 7182 | return result; |
| 7183 | } |
| 7184 | #endif /*MS_WINDOWS*/ |
| 7185 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7186 | |
| 7187 | /*[clinic input] |
| 7188 | os.getppid |
| 7189 | |
| 7190 | Return the parent's process id. |
| 7191 | |
| 7192 | If the parent process has already exited, Windows machines will still |
| 7193 | return its id; others systems will return the id of the 'init' process (1). |
| 7194 | [clinic start generated code]*/ |
| 7195 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7196 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7197 | os_getppid_impl(PyObject *module) |
| 7198 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7199 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7200 | #ifdef MS_WINDOWS |
| 7201 | return win32_getppid(); |
| 7202 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7203 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7204 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7205 | } |
| 7206 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7207 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7208 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7209 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7210 | /*[clinic input] |
| 7211 | os.getlogin |
| 7212 | |
| 7213 | Return the actual login name. |
| 7214 | [clinic start generated code]*/ |
| 7215 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7216 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7217 | os_getlogin_impl(PyObject *module) |
| 7218 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7219 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7220 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7221 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7222 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7223 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7224 | |
| 7225 | if (GetUserNameW(user_name, &num_chars)) { |
| 7226 | /* num_chars is the number of unicode chars plus null terminator */ |
| 7227 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7228 | } |
| 7229 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7230 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7231 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7232 | char *name; |
| 7233 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7234 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7235 | errno = 0; |
| 7236 | name = getlogin(); |
| 7237 | if (name == NULL) { |
| 7238 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7239 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7240 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7241 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7242 | } |
| 7243 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7244 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7245 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7246 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7247 | return result; |
| 7248 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7249 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7250 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7251 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7252 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7253 | /*[clinic input] |
| 7254 | os.getuid |
| 7255 | |
| 7256 | Return the current process's user id. |
| 7257 | [clinic start generated code]*/ |
| 7258 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7259 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7260 | os_getuid_impl(PyObject *module) |
| 7261 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7262 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7263 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7264 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7265 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7266 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7267 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7268 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7269 | #define HAVE_KILL |
| 7270 | #endif /* MS_WINDOWS */ |
| 7271 | |
| 7272 | #ifdef HAVE_KILL |
| 7273 | /*[clinic input] |
| 7274 | os.kill |
| 7275 | |
| 7276 | pid: pid_t |
| 7277 | signal: Py_ssize_t |
| 7278 | / |
| 7279 | |
| 7280 | Kill a process with a signal. |
| 7281 | [clinic start generated code]*/ |
| 7282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7283 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7284 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7285 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7286 | #ifndef MS_WINDOWS |
| 7287 | { |
| 7288 | if (kill(pid, (int)signal) == -1) |
| 7289 | return posix_error(); |
| 7290 | Py_RETURN_NONE; |
| 7291 | } |
| 7292 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7293 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7294 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7295 | DWORD sig = (DWORD)signal; |
| 7296 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7297 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7298 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7299 | /* Console processes which share a common console can be sent CTRL+C or |
| 7300 | CTRL+BREAK events, provided they handle said events. */ |
| 7301 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7302 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7303 | err = GetLastError(); |
| 7304 | PyErr_SetFromWindowsErr(err); |
| 7305 | } |
| 7306 | else |
| 7307 | Py_RETURN_NONE; |
| 7308 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7309 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7310 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7311 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7312 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7313 | if (handle == NULL) { |
| 7314 | err = GetLastError(); |
| 7315 | return PyErr_SetFromWindowsErr(err); |
| 7316 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7317 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7318 | if (TerminateProcess(handle, sig) == 0) { |
| 7319 | err = GetLastError(); |
| 7320 | result = PyErr_SetFromWindowsErr(err); |
| 7321 | } else { |
| 7322 | Py_INCREF(Py_None); |
| 7323 | result = Py_None; |
| 7324 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7325 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7326 | CloseHandle(handle); |
| 7327 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7328 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7329 | #endif /* !MS_WINDOWS */ |
| 7330 | #endif /* HAVE_KILL */ |
| 7331 | |
| 7332 | |
| 7333 | #ifdef HAVE_KILLPG |
| 7334 | /*[clinic input] |
| 7335 | os.killpg |
| 7336 | |
| 7337 | pgid: pid_t |
| 7338 | signal: int |
| 7339 | / |
| 7340 | |
| 7341 | Kill a process group with a signal. |
| 7342 | [clinic start generated code]*/ |
| 7343 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7344 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7345 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7346 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7347 | { |
| 7348 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7349 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7350 | take the same type. Moreover, pid_t is always at least as wide as |
| 7351 | int (else compilation of this module fails), which is safe. */ |
| 7352 | if (killpg(pgid, signal) == -1) |
| 7353 | return posix_error(); |
| 7354 | Py_RETURN_NONE; |
| 7355 | } |
| 7356 | #endif /* HAVE_KILLPG */ |
| 7357 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7358 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7359 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7360 | #ifdef HAVE_SYS_LOCK_H |
| 7361 | #include <sys/lock.h> |
| 7362 | #endif |
| 7363 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7364 | /*[clinic input] |
| 7365 | os.plock |
| 7366 | op: int |
| 7367 | / |
| 7368 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7369 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7370 | [clinic start generated code]*/ |
| 7371 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7372 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7373 | os_plock_impl(PyObject *module, int op) |
| 7374 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7375 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7376 | if (plock(op) == -1) |
| 7377 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7378 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7379 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7380 | #endif /* HAVE_PLOCK */ |
| 7381 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7382 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7383 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7384 | /*[clinic input] |
| 7385 | os.setuid |
| 7386 | |
| 7387 | uid: uid_t |
| 7388 | / |
| 7389 | |
| 7390 | Set the current process's user id. |
| 7391 | [clinic start generated code]*/ |
| 7392 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7393 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7394 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7395 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7396 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7397 | if (setuid(uid) < 0) |
| 7398 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7399 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7400 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7401 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7402 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7403 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7404 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7405 | /*[clinic input] |
| 7406 | os.seteuid |
| 7407 | |
| 7408 | euid: uid_t |
| 7409 | / |
| 7410 | |
| 7411 | Set the current process's effective user id. |
| 7412 | [clinic start generated code]*/ |
| 7413 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7414 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7415 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7416 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7417 | { |
| 7418 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7419 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7420 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7421 | } |
| 7422 | #endif /* HAVE_SETEUID */ |
| 7423 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7424 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7425 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7426 | /*[clinic input] |
| 7427 | os.setegid |
| 7428 | |
| 7429 | egid: gid_t |
| 7430 | / |
| 7431 | |
| 7432 | Set the current process's effective group id. |
| 7433 | [clinic start generated code]*/ |
| 7434 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7435 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7436 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7437 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7438 | { |
| 7439 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7440 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7441 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7442 | } |
| 7443 | #endif /* HAVE_SETEGID */ |
| 7444 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7445 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7446 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7447 | /*[clinic input] |
| 7448 | os.setreuid |
| 7449 | |
| 7450 | ruid: uid_t |
| 7451 | euid: uid_t |
| 7452 | / |
| 7453 | |
| 7454 | Set the current process's real and effective user ids. |
| 7455 | [clinic start generated code]*/ |
| 7456 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7457 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7458 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7459 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7460 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7461 | if (setreuid(ruid, euid) < 0) { |
| 7462 | return posix_error(); |
| 7463 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7464 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7465 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7466 | } |
| 7467 | #endif /* HAVE_SETREUID */ |
| 7468 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7469 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7470 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7471 | /*[clinic input] |
| 7472 | os.setregid |
| 7473 | |
| 7474 | rgid: gid_t |
| 7475 | egid: gid_t |
| 7476 | / |
| 7477 | |
| 7478 | Set the current process's real and effective group ids. |
| 7479 | [clinic start generated code]*/ |
| 7480 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7481 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7482 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7483 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7484 | { |
| 7485 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7486 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7487 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7488 | } |
| 7489 | #endif /* HAVE_SETREGID */ |
| 7490 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7491 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7492 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7493 | /*[clinic input] |
| 7494 | os.setgid |
| 7495 | gid: gid_t |
| 7496 | / |
| 7497 | |
| 7498 | Set the current process's group id. |
| 7499 | [clinic start generated code]*/ |
| 7500 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7501 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7502 | os_setgid_impl(PyObject *module, gid_t gid) |
| 7503 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7504 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7505 | if (setgid(gid) < 0) |
| 7506 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7507 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7508 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7509 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7510 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7511 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7512 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7513 | /*[clinic input] |
| 7514 | os.setgroups |
| 7515 | |
| 7516 | groups: object |
| 7517 | / |
| 7518 | |
| 7519 | Set the groups of the current process to list. |
| 7520 | [clinic start generated code]*/ |
| 7521 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7522 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7523 | os_setgroups(PyObject *module, PyObject *groups) |
| 7524 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7525 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7526 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7527 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7528 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7529 | if (!PySequence_Check(groups)) { |
| 7530 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7531 | return NULL; |
| 7532 | } |
| 7533 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7534 | if (len < 0) { |
| 7535 | return NULL; |
| 7536 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7537 | if (len > MAX_GROUPS) { |
| 7538 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7539 | return NULL; |
| 7540 | } |
| 7541 | for(i = 0; i < len; i++) { |
| 7542 | PyObject *elem; |
| 7543 | elem = PySequence_GetItem(groups, i); |
| 7544 | if (!elem) |
| 7545 | return NULL; |
| 7546 | if (!PyLong_Check(elem)) { |
| 7547 | PyErr_SetString(PyExc_TypeError, |
| 7548 | "groups must be integers"); |
| 7549 | Py_DECREF(elem); |
| 7550 | return NULL; |
| 7551 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7552 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7553 | Py_DECREF(elem); |
| 7554 | return NULL; |
| 7555 | } |
| 7556 | } |
| 7557 | Py_DECREF(elem); |
| 7558 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7559 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7560 | if (setgroups(len, grouplist) < 0) |
| 7561 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7562 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7563 | } |
| 7564 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7565 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7566 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7567 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7568 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7569 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7570 | PyObject *result; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7571 | PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7572 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7573 | if (pid == -1) |
| 7574 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7575 | |
Zackery Spytz | 682107c | 2019-09-09 09:48:32 -0600 | [diff] [blame] | 7576 | // If wait succeeded but no child was ready to report status, ru will not |
| 7577 | // have been populated. |
| 7578 | if (pid == 0) { |
| 7579 | memset(ru, 0, sizeof(*ru)); |
| 7580 | } |
| 7581 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7582 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7583 | if (m == NULL) |
| 7584 | return NULL; |
| 7585 | struct_rusage = PyObject_GetAttr(m, _posixstate_global->struct_rusage); |
| 7586 | Py_DECREF(m); |
| 7587 | if (struct_rusage == NULL) |
| 7588 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7589 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7590 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7591 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
Eddie Elizondo | e4db1f0 | 2019-11-25 19:07:37 -0800 | [diff] [blame^] | 7592 | Py_DECREF(struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7593 | if (!result) |
| 7594 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7595 | |
| 7596 | #ifndef doubletime |
| 7597 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7598 | #endif |
| 7599 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7600 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7601 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7602 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7603 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7604 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7605 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7606 | SET_INT(result, 2, ru->ru_maxrss); |
| 7607 | SET_INT(result, 3, ru->ru_ixrss); |
| 7608 | SET_INT(result, 4, ru->ru_idrss); |
| 7609 | SET_INT(result, 5, ru->ru_isrss); |
| 7610 | SET_INT(result, 6, ru->ru_minflt); |
| 7611 | SET_INT(result, 7, ru->ru_majflt); |
| 7612 | SET_INT(result, 8, ru->ru_nswap); |
| 7613 | SET_INT(result, 9, ru->ru_inblock); |
| 7614 | SET_INT(result, 10, ru->ru_oublock); |
| 7615 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7616 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7617 | SET_INT(result, 13, ru->ru_nsignals); |
| 7618 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7619 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7620 | #undef SET_INT |
| 7621 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7622 | if (PyErr_Occurred()) { |
| 7623 | Py_DECREF(result); |
| 7624 | return NULL; |
| 7625 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7626 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7627 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7628 | } |
| 7629 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7630 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7631 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7632 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7633 | /*[clinic input] |
| 7634 | os.wait3 |
| 7635 | |
| 7636 | options: int |
| 7637 | Wait for completion of a child process. |
| 7638 | |
| 7639 | Returns a tuple of information about the child process: |
| 7640 | (pid, status, rusage) |
| 7641 | [clinic start generated code]*/ |
| 7642 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7643 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7644 | os_wait3_impl(PyObject *module, int options) |
| 7645 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7646 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7647 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7648 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7649 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7650 | WAIT_TYPE status; |
| 7651 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7652 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7653 | do { |
| 7654 | Py_BEGIN_ALLOW_THREADS |
| 7655 | pid = wait3(&status, options, &ru); |
| 7656 | Py_END_ALLOW_THREADS |
| 7657 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7658 | if (pid < 0) |
| 7659 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7660 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7661 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7662 | } |
| 7663 | #endif /* HAVE_WAIT3 */ |
| 7664 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7665 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7666 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7667 | /*[clinic input] |
| 7668 | |
| 7669 | os.wait4 |
| 7670 | |
| 7671 | pid: pid_t |
| 7672 | options: int |
| 7673 | |
| 7674 | Wait for completion of a specific child process. |
| 7675 | |
| 7676 | Returns a tuple of information about the child process: |
| 7677 | (pid, status, rusage) |
| 7678 | [clinic start generated code]*/ |
| 7679 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7680 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7681 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7682 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7683 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7684 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7685 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7686 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7687 | WAIT_TYPE status; |
| 7688 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7689 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7690 | do { |
| 7691 | Py_BEGIN_ALLOW_THREADS |
| 7692 | res = wait4(pid, &status, options, &ru); |
| 7693 | Py_END_ALLOW_THREADS |
| 7694 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7695 | if (res < 0) |
| 7696 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7697 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7698 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7699 | } |
| 7700 | #endif /* HAVE_WAIT4 */ |
| 7701 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7702 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7703 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7704 | /*[clinic input] |
| 7705 | os.waitid |
| 7706 | |
| 7707 | idtype: idtype_t |
| 7708 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7709 | id: id_t |
| 7710 | The id to wait on. |
| 7711 | options: int |
| 7712 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7713 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7714 | / |
| 7715 | |
| 7716 | Returns the result of waiting for a process or processes. |
| 7717 | |
| 7718 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7719 | no children in a waitable state. |
| 7720 | [clinic start generated code]*/ |
| 7721 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7722 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7723 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7724 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7725 | { |
| 7726 | PyObject *result; |
| 7727 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7728 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7729 | siginfo_t si; |
| 7730 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7731 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7732 | do { |
| 7733 | Py_BEGIN_ALLOW_THREADS |
| 7734 | res = waitid(idtype, id, &si, options); |
| 7735 | Py_END_ALLOW_THREADS |
| 7736 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7737 | if (res < 0) |
| 7738 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7739 | |
| 7740 | if (si.si_pid == 0) |
| 7741 | Py_RETURN_NONE; |
| 7742 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7743 | PyObject *WaitidResultType = _posixstate(module)->WaitidResultType; |
| 7744 | result = PyStructSequence_New((PyTypeObject *)WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7745 | if (!result) |
| 7746 | return NULL; |
| 7747 | |
| 7748 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7749 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7750 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7751 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7752 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7753 | if (PyErr_Occurred()) { |
| 7754 | Py_DECREF(result); |
| 7755 | return NULL; |
| 7756 | } |
| 7757 | |
| 7758 | return result; |
| 7759 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7760 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7761 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7762 | |
| 7763 | #if defined(HAVE_WAITPID) |
| 7764 | /*[clinic input] |
| 7765 | os.waitpid |
| 7766 | pid: pid_t |
| 7767 | options: int |
| 7768 | / |
| 7769 | |
| 7770 | Wait for completion of a given child process. |
| 7771 | |
| 7772 | Returns a tuple of information regarding the child process: |
| 7773 | (pid, status) |
| 7774 | |
| 7775 | The options argument is ignored on Windows. |
| 7776 | [clinic start generated code]*/ |
| 7777 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7778 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7779 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7780 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7781 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7782 | pid_t res; |
| 7783 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7784 | WAIT_TYPE status; |
| 7785 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7786 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7787 | do { |
| 7788 | Py_BEGIN_ALLOW_THREADS |
| 7789 | res = waitpid(pid, &status, options); |
| 7790 | Py_END_ALLOW_THREADS |
| 7791 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7792 | if (res < 0) |
| 7793 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7794 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7795 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7796 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7797 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7798 | /* MS C has a variant of waitpid() that's usable for most purposes. */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7799 | /*[clinic input] |
| 7800 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7801 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7802 | options: int |
| 7803 | / |
| 7804 | |
| 7805 | Wait for completion of a given process. |
| 7806 | |
| 7807 | Returns a tuple of information regarding the process: |
| 7808 | (pid, status << 8) |
| 7809 | |
| 7810 | The options argument is ignored on Windows. |
| 7811 | [clinic start generated code]*/ |
| 7812 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7813 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7814 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7815 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7816 | { |
| 7817 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7818 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7819 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7820 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7821 | do { |
| 7822 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7823 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7824 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7825 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7826 | Py_END_ALLOW_THREADS |
| 7827 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7828 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7829 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7830 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7831 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7832 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7833 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7834 | #endif |
| 7835 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7836 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7837 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7838 | /*[clinic input] |
| 7839 | os.wait |
| 7840 | |
| 7841 | Wait for completion of a child process. |
| 7842 | |
| 7843 | Returns a tuple of information about the child process: |
| 7844 | (pid, status) |
| 7845 | [clinic start generated code]*/ |
| 7846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7847 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7848 | os_wait_impl(PyObject *module) |
| 7849 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7850 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7851 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7852 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7853 | WAIT_TYPE status; |
| 7854 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7855 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7856 | do { |
| 7857 | Py_BEGIN_ALLOW_THREADS |
| 7858 | pid = wait(&status); |
| 7859 | Py_END_ALLOW_THREADS |
| 7860 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7861 | if (pid < 0) |
| 7862 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7863 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7864 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7865 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7866 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7867 | |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 7868 | #if defined(__linux__) && defined(__NR_pidfd_open) |
| 7869 | /*[clinic input] |
| 7870 | os.pidfd_open |
| 7871 | pid: pid_t |
| 7872 | flags: unsigned_int = 0 |
| 7873 | |
| 7874 | Return a file descriptor referring to the process *pid*. |
| 7875 | |
| 7876 | The descriptor can be used to perform process management without races and |
| 7877 | signals. |
| 7878 | [clinic start generated code]*/ |
| 7879 | |
| 7880 | static PyObject * |
| 7881 | os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags) |
| 7882 | /*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/ |
| 7883 | { |
| 7884 | int fd = syscall(__NR_pidfd_open, pid, flags); |
| 7885 | if (fd < 0) { |
| 7886 | return posix_error(); |
| 7887 | } |
| 7888 | return PyLong_FromLong(fd); |
| 7889 | } |
| 7890 | #endif |
| 7891 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7892 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7893 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7894 | /*[clinic input] |
| 7895 | os.readlink |
| 7896 | |
| 7897 | path: path_t |
| 7898 | * |
| 7899 | dir_fd: dir_fd(requires='readlinkat') = None |
| 7900 | |
| 7901 | Return a string representing the path to which the symbolic link points. |
| 7902 | |
| 7903 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7904 | and path should be relative; path will then be relative to that directory. |
| 7905 | |
| 7906 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 7907 | using it will raise a NotImplementedError. |
| 7908 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7909 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7910 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7911 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 7912 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7913 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7914 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7915 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7916 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7917 | |
| 7918 | Py_BEGIN_ALLOW_THREADS |
| 7919 | #ifdef HAVE_READLINKAT |
| 7920 | if (dir_fd != DEFAULT_DIR_FD) |
| 7921 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 7922 | else |
| 7923 | #endif |
| 7924 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 7925 | Py_END_ALLOW_THREADS |
| 7926 | |
| 7927 | if (length < 0) { |
| 7928 | return path_error(path); |
| 7929 | } |
| 7930 | buffer[length] = '\0'; |
| 7931 | |
| 7932 | if (PyUnicode_Check(path->object)) |
| 7933 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7934 | else |
| 7935 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7936 | #elif defined(MS_WINDOWS) |
| 7937 | DWORD n_bytes_returned; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7938 | DWORD io_result = 0; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7939 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7940 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7941 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 7942 | PyObject *result = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7943 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7944 | /* First get a handle to the reparse point */ |
| 7945 | Py_BEGIN_ALLOW_THREADS |
| 7946 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7947 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7948 | 0, |
| 7949 | 0, |
| 7950 | 0, |
| 7951 | OPEN_EXISTING, |
| 7952 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7953 | 0); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7954 | if (reparse_point_handle != INVALID_HANDLE_VALUE) { |
| 7955 | /* New call DeviceIoControl to read the reparse point */ |
| 7956 | io_result = DeviceIoControl( |
| 7957 | reparse_point_handle, |
| 7958 | FSCTL_GET_REPARSE_POINT, |
| 7959 | 0, 0, /* in buffer */ |
| 7960 | target_buffer, sizeof(target_buffer), |
| 7961 | &n_bytes_returned, |
| 7962 | 0 /* we're not using OVERLAPPED_IO */ |
| 7963 | ); |
| 7964 | CloseHandle(reparse_point_handle); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7965 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7966 | Py_END_ALLOW_THREADS |
| 7967 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7968 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7969 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7970 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7971 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7972 | wchar_t *name = NULL; |
| 7973 | Py_ssize_t nameLen = 0; |
| 7974 | if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7975 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7976 | name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7977 | rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 7978 | nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7979 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7980 | else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) |
| 7981 | { |
| 7982 | name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer + |
| 7983 | rdb->MountPointReparseBuffer.SubstituteNameOffset); |
| 7984 | nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
| 7985 | } |
| 7986 | else |
| 7987 | { |
| 7988 | PyErr_SetString(PyExc_ValueError, "not a symbolic link"); |
| 7989 | } |
| 7990 | if (name) { |
| 7991 | if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) { |
| 7992 | /* Our buffer is mutable, so this is okay */ |
| 7993 | name[1] = L'\\'; |
| 7994 | } |
| 7995 | result = PyUnicode_FromWideChar(name, nameLen); |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 7996 | if (result && path->narrow) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7997 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
| 7998 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7999 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8000 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8001 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8002 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8003 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8004 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8005 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8006 | |
| 8007 | #if defined(MS_WINDOWS) |
| 8008 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8009 | /* Remove the last portion of the path - return 0 on success */ |
| 8010 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8011 | _dirnameW(WCHAR *path) |
| 8012 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8013 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8014 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 8015 | if (length == MAX_PATH) { |
| 8016 | return -1; |
| 8017 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8018 | |
| 8019 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8020 | for(ptr = path + length; ptr != path; ptr--) { |
| 8021 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8022 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8023 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8024 | } |
| 8025 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8026 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8027 | } |
| 8028 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8029 | /* Is this path absolute? */ |
| 8030 | static int |
| 8031 | _is_absW(const WCHAR *path) |
| 8032 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8033 | return path[0] == L'\\' || path[0] == L'/' || |
| 8034 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8035 | } |
| 8036 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8037 | /* join root and rest with a backslash - return 0 on success */ |
| 8038 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8039 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 8040 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8041 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8042 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8043 | } |
| 8044 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8045 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 8046 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8047 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8048 | |
| 8049 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 8050 | return -1; |
| 8051 | } |
| 8052 | |
| 8053 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8054 | } |
| 8055 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8056 | /* Return True if the path at src relative to dest is a directory */ |
| 8057 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8058 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8059 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8060 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 8061 | WCHAR dest_parent[MAX_PATH]; |
| 8062 | WCHAR src_resolved[MAX_PATH] = L""; |
| 8063 | |
| 8064 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8065 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 8066 | _dirnameW(dest_parent)) { |
| 8067 | return 0; |
| 8068 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8069 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8070 | if (_joinW(src_resolved, dest_parent, src)) { |
| 8071 | return 0; |
| 8072 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8073 | return ( |
| 8074 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 8075 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 8076 | ); |
| 8077 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8078 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8079 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8080 | |
| 8081 | /*[clinic input] |
| 8082 | os.symlink |
| 8083 | src: path_t |
| 8084 | dst: path_t |
| 8085 | target_is_directory: bool = False |
| 8086 | * |
| 8087 | dir_fd: dir_fd(requires='symlinkat')=None |
| 8088 | |
| 8089 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 8090 | |
| 8091 | Create a symbolic link pointing to src named dst. |
| 8092 | |
| 8093 | target_is_directory is required on Windows if the target is to be |
| 8094 | interpreted as a directory. (On Windows, symlink requires |
| 8095 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 8096 | target_is_directory is ignored on non-Windows platforms. |
| 8097 | |
| 8098 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8099 | and path should be relative; path will then be relative to that directory. |
| 8100 | dir_fd may not be implemented on your platform. |
| 8101 | If it is unavailable, using it will raise a NotImplementedError. |
| 8102 | |
| 8103 | [clinic start generated code]*/ |
| 8104 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8105 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8106 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8107 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8108 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8109 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8110 | #ifdef MS_WINDOWS |
| 8111 | DWORD result; |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8112 | DWORD flags = 0; |
| 8113 | |
| 8114 | /* Assumed true, set to false if detected to not be available. */ |
| 8115 | static int windows_has_symlink_unprivileged_flag = TRUE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8116 | #else |
| 8117 | int result; |
| 8118 | #endif |
| 8119 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8120 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8121 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8122 | if (windows_has_symlink_unprivileged_flag) { |
| 8123 | /* Allow non-admin symlinks if system allows it. */ |
| 8124 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 8125 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8126 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8127 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8128 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8129 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
| 8130 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { |
| 8131 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 8132 | } |
| 8133 | |
| 8134 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8135 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8136 | Py_END_ALLOW_THREADS |
| 8137 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8138 | if (windows_has_symlink_unprivileged_flag && !result && |
| 8139 | ERROR_INVALID_PARAMETER == GetLastError()) { |
| 8140 | |
| 8141 | Py_BEGIN_ALLOW_THREADS |
| 8142 | _Py_BEGIN_SUPPRESS_IPH |
| 8143 | /* This error might be caused by |
| 8144 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. |
| 8145 | Try again, and update windows_has_symlink_unprivileged_flag if we |
| 8146 | are successful this time. |
| 8147 | |
| 8148 | NOTE: There is a risk of a race condition here if there are other |
| 8149 | conditions than the flag causing ERROR_INVALID_PARAMETER, and |
| 8150 | another process (or thread) changes that condition in between our |
| 8151 | calls to CreateSymbolicLink. |
| 8152 | */ |
| 8153 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 8154 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
| 8155 | _Py_END_SUPPRESS_IPH |
| 8156 | Py_END_ALLOW_THREADS |
| 8157 | |
| 8158 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { |
| 8159 | windows_has_symlink_unprivileged_flag = FALSE; |
| 8160 | } |
| 8161 | } |
| 8162 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8163 | if (!result) |
| 8164 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8165 | |
| 8166 | #else |
| 8167 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8168 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 8169 | PyErr_SetString(PyExc_ValueError, |
| 8170 | "symlink: src and dst must be the same type"); |
| 8171 | return NULL; |
| 8172 | } |
| 8173 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8174 | Py_BEGIN_ALLOW_THREADS |
| 8175 | #if HAVE_SYMLINKAT |
| 8176 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8177 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8178 | else |
| 8179 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8180 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8181 | Py_END_ALLOW_THREADS |
| 8182 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8183 | if (result) |
| 8184 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8185 | #endif |
| 8186 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8187 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8188 | } |
| 8189 | #endif /* HAVE_SYMLINK */ |
| 8190 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8191 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8192 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8193 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8194 | static PyStructSequence_Field times_result_fields[] = { |
| 8195 | {"user", "user time"}, |
| 8196 | {"system", "system time"}, |
| 8197 | {"children_user", "user time of children"}, |
| 8198 | {"children_system", "system time of children"}, |
| 8199 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 8200 | {NULL} |
| 8201 | }; |
| 8202 | |
| 8203 | PyDoc_STRVAR(times_result__doc__, |
| 8204 | "times_result: Result from os.times().\n\n\ |
| 8205 | This object may be accessed either as a tuple of\n\ |
| 8206 | (user, system, children_user, children_system, elapsed),\n\ |
| 8207 | or via the attributes user, system, children_user, children_system,\n\ |
| 8208 | and elapsed.\n\ |
| 8209 | \n\ |
| 8210 | See os.times for more information."); |
| 8211 | |
| 8212 | static PyStructSequence_Desc times_result_desc = { |
| 8213 | "times_result", /* name */ |
| 8214 | times_result__doc__, /* doc */ |
| 8215 | times_result_fields, |
| 8216 | 5 |
| 8217 | }; |
| 8218 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8219 | #ifdef MS_WINDOWS |
| 8220 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 8221 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8222 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8223 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8224 | |
| 8225 | static PyObject * |
| 8226 | build_times_result(double user, double system, |
| 8227 | double children_user, double children_system, |
| 8228 | double elapsed) |
| 8229 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8230 | PyObject *TimesResultType = _posixstate_global->TimesResultType; |
| 8231 | PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8232 | if (value == NULL) |
| 8233 | return NULL; |
| 8234 | |
| 8235 | #define SET(i, field) \ |
| 8236 | { \ |
| 8237 | PyObject *o = PyFloat_FromDouble(field); \ |
| 8238 | if (!o) { \ |
| 8239 | Py_DECREF(value); \ |
| 8240 | return NULL; \ |
| 8241 | } \ |
| 8242 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 8243 | } \ |
| 8244 | |
| 8245 | SET(0, user); |
| 8246 | SET(1, system); |
| 8247 | SET(2, children_user); |
| 8248 | SET(3, children_system); |
| 8249 | SET(4, elapsed); |
| 8250 | |
| 8251 | #undef SET |
| 8252 | |
| 8253 | return value; |
| 8254 | } |
| 8255 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8256 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8257 | #ifndef MS_WINDOWS |
| 8258 | #define NEED_TICKS_PER_SECOND |
| 8259 | static long ticks_per_second = -1; |
| 8260 | #endif /* MS_WINDOWS */ |
| 8261 | |
| 8262 | /*[clinic input] |
| 8263 | os.times |
| 8264 | |
| 8265 | Return a collection containing process timing information. |
| 8266 | |
| 8267 | The object returned behaves like a named tuple with these fields: |
| 8268 | (utime, stime, cutime, cstime, elapsed_time) |
| 8269 | All fields are floating point numbers. |
| 8270 | [clinic start generated code]*/ |
| 8271 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8272 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8273 | os_times_impl(PyObject *module) |
| 8274 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8275 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8276 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8277 | FILETIME create, exit, kernel, user; |
| 8278 | HANDLE hProc; |
| 8279 | hProc = GetCurrentProcess(); |
| 8280 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8281 | /* The fields of a FILETIME structure are the hi and lo part |
| 8282 | of a 64-bit value expressed in 100 nanosecond units. |
| 8283 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8284 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8285 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8286 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8287 | (double)(user.dwHighDateTime*429.4967296 + |
| 8288 | user.dwLowDateTime*1e-7), |
| 8289 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8290 | kernel.dwLowDateTime*1e-7), |
| 8291 | (double)0, |
| 8292 | (double)0, |
| 8293 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8294 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8295 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8296 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8297 | |
| 8298 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8299 | struct tms t; |
| 8300 | clock_t c; |
| 8301 | errno = 0; |
| 8302 | c = times(&t); |
| 8303 | if (c == (clock_t) -1) |
| 8304 | return posix_error(); |
| 8305 | return build_times_result( |
| 8306 | (double)t.tms_utime / ticks_per_second, |
| 8307 | (double)t.tms_stime / ticks_per_second, |
| 8308 | (double)t.tms_cutime / ticks_per_second, |
| 8309 | (double)t.tms_cstime / ticks_per_second, |
| 8310 | (double)c / ticks_per_second); |
| 8311 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8312 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8313 | #endif /* HAVE_TIMES */ |
| 8314 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8315 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8316 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8317 | /*[clinic input] |
| 8318 | os.getsid |
| 8319 | |
| 8320 | pid: pid_t |
| 8321 | / |
| 8322 | |
| 8323 | Call the system call getsid(pid) and return the result. |
| 8324 | [clinic start generated code]*/ |
| 8325 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8326 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8327 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8328 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8329 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8330 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8331 | sid = getsid(pid); |
| 8332 | if (sid < 0) |
| 8333 | return posix_error(); |
| 8334 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8335 | } |
| 8336 | #endif /* HAVE_GETSID */ |
| 8337 | |
| 8338 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8339 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8340 | /*[clinic input] |
| 8341 | os.setsid |
| 8342 | |
| 8343 | Call the system call setsid(). |
| 8344 | [clinic start generated code]*/ |
| 8345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8346 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8347 | os_setsid_impl(PyObject *module) |
| 8348 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8349 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8350 | if (setsid() < 0) |
| 8351 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8352 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8353 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8354 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8355 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8356 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8357 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8358 | /*[clinic input] |
| 8359 | os.setpgid |
| 8360 | |
| 8361 | pid: pid_t |
| 8362 | pgrp: pid_t |
| 8363 | / |
| 8364 | |
| 8365 | Call the system call setpgid(pid, pgrp). |
| 8366 | [clinic start generated code]*/ |
| 8367 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8368 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8369 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8370 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8371 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8372 | if (setpgid(pid, pgrp) < 0) |
| 8373 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8374 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8375 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8376 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8377 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8378 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8379 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8380 | /*[clinic input] |
| 8381 | os.tcgetpgrp |
| 8382 | |
| 8383 | fd: int |
| 8384 | / |
| 8385 | |
| 8386 | Return the process group associated with the terminal specified by fd. |
| 8387 | [clinic start generated code]*/ |
| 8388 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8389 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8390 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8391 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8392 | { |
| 8393 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8394 | if (pgid < 0) |
| 8395 | return posix_error(); |
| 8396 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8397 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8398 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8399 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8400 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8401 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8402 | /*[clinic input] |
| 8403 | os.tcsetpgrp |
| 8404 | |
| 8405 | fd: int |
| 8406 | pgid: pid_t |
| 8407 | / |
| 8408 | |
| 8409 | Set the process group associated with the terminal specified by fd. |
| 8410 | [clinic start generated code]*/ |
| 8411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8412 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8413 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8414 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8415 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8416 | if (tcsetpgrp(fd, pgid) < 0) |
| 8417 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8418 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8419 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8420 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8421 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8422 | /* Functions acting on file descriptors */ |
| 8423 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8424 | #ifdef O_CLOEXEC |
| 8425 | extern int _Py_open_cloexec_works; |
| 8426 | #endif |
| 8427 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8428 | |
| 8429 | /*[clinic input] |
| 8430 | os.open -> int |
| 8431 | path: path_t |
| 8432 | flags: int |
| 8433 | mode: int = 0o777 |
| 8434 | * |
| 8435 | dir_fd: dir_fd(requires='openat') = None |
| 8436 | |
| 8437 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8438 | |
| 8439 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8440 | |
| 8441 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8442 | and path should be relative; path will then be relative to that directory. |
| 8443 | dir_fd may not be implemented on your platform. |
| 8444 | If it is unavailable, using it will raise a NotImplementedError. |
| 8445 | [clinic start generated code]*/ |
| 8446 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8447 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8448 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8449 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8450 | { |
| 8451 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8452 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8453 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8454 | #ifdef O_CLOEXEC |
| 8455 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8456 | #elif !defined(MS_WINDOWS) |
| 8457 | int *atomic_flag_works = NULL; |
| 8458 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8459 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8460 | #ifdef MS_WINDOWS |
| 8461 | flags |= O_NOINHERIT; |
| 8462 | #elif defined(O_CLOEXEC) |
| 8463 | flags |= O_CLOEXEC; |
| 8464 | #endif |
| 8465 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 8466 | if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) { |
| 8467 | return -1; |
| 8468 | } |
| 8469 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8470 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8471 | do { |
| 8472 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8473 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8474 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8475 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8476 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8477 | if (dir_fd != DEFAULT_DIR_FD) |
| 8478 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8479 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8480 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8481 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8482 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8483 | Py_END_ALLOW_THREADS |
| 8484 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8485 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8486 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8487 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8488 | if (!async_err) |
| 8489 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8490 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8491 | } |
| 8492 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8493 | #ifndef MS_WINDOWS |
| 8494 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8495 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8496 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8497 | } |
| 8498 | #endif |
| 8499 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8500 | return fd; |
| 8501 | } |
| 8502 | |
| 8503 | |
| 8504 | /*[clinic input] |
| 8505 | os.close |
| 8506 | |
| 8507 | fd: int |
| 8508 | |
| 8509 | Close a file descriptor. |
| 8510 | [clinic start generated code]*/ |
| 8511 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8512 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8513 | os_close_impl(PyObject *module, int fd) |
| 8514 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8515 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8516 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8517 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8518 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8519 | * for more details. |
| 8520 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8521 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8522 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8523 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8524 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8525 | Py_END_ALLOW_THREADS |
| 8526 | if (res < 0) |
| 8527 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8528 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8529 | } |
| 8530 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8531 | |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8532 | #ifdef HAVE_FDWALK |
| 8533 | static int |
| 8534 | _fdwalk_close_func(void *lohi, int fd) |
| 8535 | { |
| 8536 | int lo = ((int *)lohi)[0]; |
| 8537 | int hi = ((int *)lohi)[1]; |
| 8538 | |
| 8539 | if (fd >= hi) |
| 8540 | return 1; |
| 8541 | else if (fd >= lo) |
| 8542 | close(fd); |
| 8543 | return 0; |
| 8544 | } |
| 8545 | #endif /* HAVE_FDWALK */ |
| 8546 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8547 | /*[clinic input] |
| 8548 | os.closerange |
| 8549 | |
| 8550 | fd_low: int |
| 8551 | fd_high: int |
| 8552 | / |
| 8553 | |
| 8554 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8555 | [clinic start generated code]*/ |
| 8556 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8557 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8558 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8559 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8560 | { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8561 | #ifdef HAVE_FDWALK |
| 8562 | int lohi[2]; |
| 8563 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8564 | int i; |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8565 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8566 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8567 | _Py_BEGIN_SUPPRESS_IPH |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8568 | #ifdef HAVE_FDWALK |
| 8569 | lohi[0] = Py_MAX(fd_low, 0); |
| 8570 | lohi[1] = fd_high; |
| 8571 | fdwalk(_fdwalk_close_func, lohi); |
| 8572 | #else |
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 8573 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 8574 | close(i); |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8575 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8576 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8577 | Py_END_ALLOW_THREADS |
| 8578 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8579 | } |
| 8580 | |
| 8581 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8582 | /*[clinic input] |
| 8583 | os.dup -> int |
| 8584 | |
| 8585 | fd: int |
| 8586 | / |
| 8587 | |
| 8588 | Return a duplicate of a file descriptor. |
| 8589 | [clinic start generated code]*/ |
| 8590 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8591 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8592 | os_dup_impl(PyObject *module, int fd) |
| 8593 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8594 | { |
| 8595 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8596 | } |
| 8597 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8599 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8600 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8601 | fd: int |
| 8602 | fd2: int |
| 8603 | inheritable: bool=True |
| 8604 | |
| 8605 | Duplicate file descriptor. |
| 8606 | [clinic start generated code]*/ |
| 8607 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8608 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8609 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8610 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8611 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8612 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8613 | #if defined(HAVE_DUP3) && \ |
| 8614 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8615 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8616 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8617 | #endif |
| 8618 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8619 | if (fd < 0 || fd2 < 0) { |
| 8620 | posix_error(); |
| 8621 | return -1; |
| 8622 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8623 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8624 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8625 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8626 | * upon close(), and therefore below. |
| 8627 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8628 | #ifdef MS_WINDOWS |
| 8629 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8630 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8631 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8632 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8633 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8634 | if (res < 0) { |
| 8635 | posix_error(); |
| 8636 | return -1; |
| 8637 | } |
| 8638 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8639 | |
| 8640 | /* Character files like console cannot be make non-inheritable */ |
| 8641 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8642 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8643 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8644 | } |
| 8645 | |
| 8646 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8647 | Py_BEGIN_ALLOW_THREADS |
| 8648 | if (!inheritable) |
| 8649 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8650 | else |
| 8651 | res = dup2(fd, fd2); |
| 8652 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8653 | if (res < 0) { |
| 8654 | posix_error(); |
| 8655 | return -1; |
| 8656 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8657 | |
| 8658 | #else |
| 8659 | |
| 8660 | #ifdef HAVE_DUP3 |
| 8661 | if (!inheritable && dup3_works != 0) { |
| 8662 | Py_BEGIN_ALLOW_THREADS |
| 8663 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8664 | Py_END_ALLOW_THREADS |
| 8665 | if (res < 0) { |
| 8666 | if (dup3_works == -1) |
| 8667 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8668 | if (dup3_works) { |
| 8669 | posix_error(); |
| 8670 | return -1; |
| 8671 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8672 | } |
| 8673 | } |
| 8674 | |
| 8675 | if (inheritable || dup3_works == 0) |
| 8676 | { |
| 8677 | #endif |
| 8678 | Py_BEGIN_ALLOW_THREADS |
| 8679 | res = dup2(fd, fd2); |
| 8680 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8681 | if (res < 0) { |
| 8682 | posix_error(); |
| 8683 | return -1; |
| 8684 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8685 | |
| 8686 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8687 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8688 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8689 | } |
| 8690 | #ifdef HAVE_DUP3 |
| 8691 | } |
| 8692 | #endif |
| 8693 | |
| 8694 | #endif |
| 8695 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8696 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8697 | } |
| 8698 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8699 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8700 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8701 | /*[clinic input] |
| 8702 | os.lockf |
| 8703 | |
| 8704 | fd: int |
| 8705 | An open file descriptor. |
| 8706 | command: int |
| 8707 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8708 | length: Py_off_t |
| 8709 | The number of bytes to lock, starting at the current position. |
| 8710 | / |
| 8711 | |
| 8712 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8713 | |
| 8714 | [clinic start generated code]*/ |
| 8715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8716 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8717 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8718 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8719 | { |
| 8720 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8721 | |
| 8722 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8723 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8724 | Py_END_ALLOW_THREADS |
| 8725 | |
| 8726 | if (res < 0) |
| 8727 | return posix_error(); |
| 8728 | |
| 8729 | Py_RETURN_NONE; |
| 8730 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8731 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8732 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8733 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8734 | /*[clinic input] |
| 8735 | os.lseek -> Py_off_t |
| 8736 | |
| 8737 | fd: int |
| 8738 | position: Py_off_t |
| 8739 | how: int |
| 8740 | / |
| 8741 | |
| 8742 | Set the position of a file descriptor. Return the new position. |
| 8743 | |
| 8744 | Return the new cursor position in number of bytes |
| 8745 | relative to the beginning of the file. |
| 8746 | [clinic start generated code]*/ |
| 8747 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8748 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8749 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8750 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8751 | { |
| 8752 | Py_off_t result; |
| 8753 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8754 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8755 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8756 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8757 | case 0: how = SEEK_SET; break; |
| 8758 | case 1: how = SEEK_CUR; break; |
| 8759 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8760 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8761 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8762 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8763 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8764 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8765 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8766 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8767 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8768 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8769 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8770 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8771 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8772 | if (result < 0) |
| 8773 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8774 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8775 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8776 | } |
| 8777 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8778 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8779 | /*[clinic input] |
| 8780 | os.read |
| 8781 | fd: int |
| 8782 | length: Py_ssize_t |
| 8783 | / |
| 8784 | |
| 8785 | Read from a file descriptor. Returns a bytes object. |
| 8786 | [clinic start generated code]*/ |
| 8787 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8788 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8789 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 8790 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8791 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8792 | Py_ssize_t n; |
| 8793 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8794 | |
| 8795 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8796 | errno = EINVAL; |
| 8797 | return posix_error(); |
| 8798 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8799 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 8800 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8801 | |
| 8802 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8803 | if (buffer == NULL) |
| 8804 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8805 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8806 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8807 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8808 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8809 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8810 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8811 | |
| 8812 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8813 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8814 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8815 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8816 | } |
| 8817 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8818 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8819 | || defined(__APPLE__))) \ |
| 8820 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 8821 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 8822 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8823 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8824 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8825 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8826 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8827 | *iov = PyMem_New(struct iovec, cnt); |
| 8828 | if (*iov == NULL) { |
| 8829 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8830 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8831 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8832 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8833 | *buf = PyMem_New(Py_buffer, cnt); |
| 8834 | if (*buf == NULL) { |
| 8835 | PyMem_Del(*iov); |
| 8836 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8837 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8838 | } |
| 8839 | |
| 8840 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8841 | PyObject *item = PySequence_GetItem(seq, i); |
| 8842 | if (item == NULL) |
| 8843 | goto fail; |
| 8844 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8845 | Py_DECREF(item); |
| 8846 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8847 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8848 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8849 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8850 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8851 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8852 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8853 | |
| 8854 | fail: |
| 8855 | PyMem_Del(*iov); |
| 8856 | for (j = 0; j < i; j++) { |
| 8857 | PyBuffer_Release(&(*buf)[j]); |
| 8858 | } |
| 8859 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8860 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8861 | } |
| 8862 | |
| 8863 | static void |
| 8864 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8865 | { |
| 8866 | int i; |
| 8867 | PyMem_Del(iov); |
| 8868 | for (i = 0; i < cnt; i++) { |
| 8869 | PyBuffer_Release(&buf[i]); |
| 8870 | } |
| 8871 | PyMem_Del(buf); |
| 8872 | } |
| 8873 | #endif |
| 8874 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8875 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8876 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8877 | /*[clinic input] |
| 8878 | os.readv -> Py_ssize_t |
| 8879 | |
| 8880 | fd: int |
| 8881 | buffers: object |
| 8882 | / |
| 8883 | |
| 8884 | Read from a file descriptor fd into an iterable of buffers. |
| 8885 | |
| 8886 | The buffers should be mutable buffers accepting bytes. |
| 8887 | readv will transfer data into each buffer until it is full |
| 8888 | and then move on to the next buffer in the sequence to hold |
| 8889 | the rest of the data. |
| 8890 | |
| 8891 | readv returns the total number of bytes read, |
| 8892 | which may be less than the total capacity of all the buffers. |
| 8893 | [clinic start generated code]*/ |
| 8894 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8895 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8896 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 8897 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8898 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8899 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8900 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8901 | struct iovec *iov; |
| 8902 | Py_buffer *buf; |
| 8903 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8904 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8905 | PyErr_SetString(PyExc_TypeError, |
| 8906 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8907 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8908 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8909 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8910 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8911 | if (cnt < 0) |
| 8912 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8913 | |
| 8914 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8915 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8916 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8917 | do { |
| 8918 | Py_BEGIN_ALLOW_THREADS |
| 8919 | n = readv(fd, iov, cnt); |
| 8920 | Py_END_ALLOW_THREADS |
| 8921 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8922 | |
| 8923 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8924 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8925 | if (!async_err) |
| 8926 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8927 | return -1; |
| 8928 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8929 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8930 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8931 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8932 | #endif /* HAVE_READV */ |
| 8933 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8934 | |
| 8935 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8936 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8937 | os.pread |
| 8938 | |
| 8939 | fd: int |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 8940 | length: Py_ssize_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8941 | offset: Py_off_t |
| 8942 | / |
| 8943 | |
| 8944 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8945 | |
| 8946 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8947 | the beginning of the file. The file offset remains unchanged. |
| 8948 | [clinic start generated code]*/ |
| 8949 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8950 | static PyObject * |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 8951 | os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset) |
| 8952 | /*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8953 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8954 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8955 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8956 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8957 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8958 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8959 | errno = EINVAL; |
| 8960 | return posix_error(); |
| 8961 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8962 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8963 | if (buffer == NULL) |
| 8964 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8965 | |
| 8966 | do { |
| 8967 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8968 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8969 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8970 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8971 | Py_END_ALLOW_THREADS |
| 8972 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8973 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8974 | if (n < 0) { |
| 8975 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8976 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8977 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8978 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8979 | _PyBytes_Resize(&buffer, n); |
| 8980 | return buffer; |
| 8981 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8982 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8983 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 8984 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 8985 | /*[clinic input] |
| 8986 | os.preadv -> Py_ssize_t |
| 8987 | |
| 8988 | fd: int |
| 8989 | buffers: object |
| 8990 | offset: Py_off_t |
| 8991 | flags: int = 0 |
| 8992 | / |
| 8993 | |
| 8994 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 8995 | |
| 8996 | Combines the functionality of readv() and pread(). As readv(), it will |
| 8997 | transfer data into each buffer until it is full and then move on to the next |
| 8998 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 8999 | specifies the file offset at which the input operation is to be performed. It |
| 9000 | will return the total number of bytes read (which can be less than the total |
| 9001 | capacity of all the objects). |
| 9002 | |
| 9003 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9004 | |
| 9005 | - RWF_HIPRI |
| 9006 | - RWF_NOWAIT |
| 9007 | |
| 9008 | Using non-zero flags requires Linux 4.6 or newer. |
| 9009 | [clinic start generated code]*/ |
| 9010 | |
| 9011 | static Py_ssize_t |
| 9012 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9013 | int flags) |
| 9014 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 9015 | { |
| 9016 | Py_ssize_t cnt, n; |
| 9017 | int async_err = 0; |
| 9018 | struct iovec *iov; |
| 9019 | Py_buffer *buf; |
| 9020 | |
| 9021 | if (!PySequence_Check(buffers)) { |
| 9022 | PyErr_SetString(PyExc_TypeError, |
| 9023 | "preadv2() arg 2 must be a sequence"); |
| 9024 | return -1; |
| 9025 | } |
| 9026 | |
| 9027 | cnt = PySequence_Size(buffers); |
| 9028 | if (cnt < 0) { |
| 9029 | return -1; |
| 9030 | } |
| 9031 | |
| 9032 | #ifndef HAVE_PREADV2 |
| 9033 | if(flags != 0) { |
| 9034 | argument_unavailable_error("preadv2", "flags"); |
| 9035 | return -1; |
| 9036 | } |
| 9037 | #endif |
| 9038 | |
| 9039 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 9040 | return -1; |
| 9041 | } |
| 9042 | #ifdef HAVE_PREADV2 |
| 9043 | do { |
| 9044 | Py_BEGIN_ALLOW_THREADS |
| 9045 | _Py_BEGIN_SUPPRESS_IPH |
| 9046 | n = preadv2(fd, iov, cnt, offset, flags); |
| 9047 | _Py_END_SUPPRESS_IPH |
| 9048 | Py_END_ALLOW_THREADS |
| 9049 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9050 | #else |
| 9051 | do { |
| 9052 | Py_BEGIN_ALLOW_THREADS |
| 9053 | _Py_BEGIN_SUPPRESS_IPH |
| 9054 | n = preadv(fd, iov, cnt, offset); |
| 9055 | _Py_END_SUPPRESS_IPH |
| 9056 | Py_END_ALLOW_THREADS |
| 9057 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9058 | #endif |
| 9059 | |
| 9060 | iov_cleanup(iov, buf, cnt); |
| 9061 | if (n < 0) { |
| 9062 | if (!async_err) { |
| 9063 | posix_error(); |
| 9064 | } |
| 9065 | return -1; |
| 9066 | } |
| 9067 | |
| 9068 | return n; |
| 9069 | } |
| 9070 | #endif /* HAVE_PREADV */ |
| 9071 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9072 | |
| 9073 | /*[clinic input] |
| 9074 | os.write -> Py_ssize_t |
| 9075 | |
| 9076 | fd: int |
| 9077 | data: Py_buffer |
| 9078 | / |
| 9079 | |
| 9080 | Write a bytes object to a file descriptor. |
| 9081 | [clinic start generated code]*/ |
| 9082 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9083 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9084 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 9085 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9086 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9087 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9088 | } |
| 9089 | |
| 9090 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9091 | PyDoc_STRVAR(posix_sendfile__doc__, |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9092 | "sendfile(out_fd, in_fd, offset, count) -> byteswritten\n\ |
| 9093 | sendfile(out_fd, in_fd, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9094 | -> byteswritten\n\ |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9095 | Copy count bytes from file descriptor in_fd to file descriptor out_fd."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9096 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9097 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9098 | static PyObject * |
| 9099 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 9100 | { |
| 9101 | int in, out; |
| 9102 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9103 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9104 | off_t offset; |
| 9105 | |
| 9106 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 9107 | #ifndef __APPLE__ |
| 9108 | Py_ssize_t len; |
| 9109 | #endif |
| 9110 | PyObject *headers = NULL, *trailers = NULL; |
| 9111 | Py_buffer *hbuf, *tbuf; |
| 9112 | off_t sbytes; |
| 9113 | struct sf_hdtr sf; |
| 9114 | int flags = 0; |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9115 | static char *keywords[] = {"out_fd", "in_fd", |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 9116 | "offset", "count", |
| 9117 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9118 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 9119 | sf.headers = NULL; |
| 9120 | sf.trailers = NULL; |
| 9121 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9122 | #ifdef __APPLE__ |
| 9123 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9124 | keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9125 | #else |
| 9126 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9127 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9128 | #endif |
| 9129 | &headers, &trailers, &flags)) |
| 9130 | return NULL; |
| 9131 | if (headers != NULL) { |
| 9132 | if (!PySequence_Check(headers)) { |
| 9133 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9134 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9135 | return NULL; |
| 9136 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9137 | Py_ssize_t i = PySequence_Size(headers); |
| 9138 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9139 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9140 | if (i > INT_MAX) { |
| 9141 | PyErr_SetString(PyExc_OverflowError, |
| 9142 | "sendfile() header is too large"); |
| 9143 | return NULL; |
| 9144 | } |
| 9145 | if (i > 0) { |
| 9146 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9147 | if (iov_setup(&(sf.headers), &hbuf, |
| 9148 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9149 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9150 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9151 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 9152 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 9153 | # define OFF_T_MAX 0x7fffffffffffffff |
| 9154 | if (sbytes >= OFF_T_MAX - blen) { |
| 9155 | PyErr_SetString(PyExc_OverflowError, |
| 9156 | "sendfile() header is too large"); |
| 9157 | return NULL; |
| 9158 | } |
| 9159 | sbytes += blen; |
| 9160 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9161 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9162 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9163 | } |
| 9164 | } |
| 9165 | if (trailers != NULL) { |
| 9166 | if (!PySequence_Check(trailers)) { |
| 9167 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9168 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9169 | return NULL; |
| 9170 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9171 | Py_ssize_t i = PySequence_Size(trailers); |
| 9172 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9173 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9174 | if (i > INT_MAX) { |
| 9175 | PyErr_SetString(PyExc_OverflowError, |
| 9176 | "sendfile() trailer is too large"); |
| 9177 | return NULL; |
| 9178 | } |
| 9179 | if (i > 0) { |
| 9180 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9181 | if (iov_setup(&(sf.trailers), &tbuf, |
| 9182 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9183 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9184 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9185 | } |
| 9186 | } |
| 9187 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9188 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9189 | do { |
| 9190 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9191 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9192 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9193 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9194 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9195 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9196 | Py_END_ALLOW_THREADS |
| 9197 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9198 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9199 | |
| 9200 | if (sf.headers != NULL) |
| 9201 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 9202 | if (sf.trailers != NULL) |
| 9203 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 9204 | |
| 9205 | if (ret < 0) { |
| 9206 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 9207 | if (sbytes != 0) { |
| 9208 | // some data has been sent |
| 9209 | goto done; |
| 9210 | } |
| 9211 | else { |
| 9212 | // no data has been sent; upper application is supposed |
| 9213 | // to retry on EAGAIN or EBUSY |
| 9214 | return posix_error(); |
| 9215 | } |
| 9216 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9217 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9218 | } |
| 9219 | goto done; |
| 9220 | |
| 9221 | done: |
| 9222 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 9223 | return Py_BuildValue("l", sbytes); |
| 9224 | #else |
| 9225 | return Py_BuildValue("L", sbytes); |
| 9226 | #endif |
| 9227 | |
| 9228 | #else |
| 9229 | Py_ssize_t count; |
| 9230 | PyObject *offobj; |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9231 | static char *keywords[] = {"out_fd", "in_fd", |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9232 | "offset", "count", NULL}; |
| 9233 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 9234 | keywords, &out, &in, &offobj, &count)) |
| 9235 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9236 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9237 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9238 | do { |
| 9239 | Py_BEGIN_ALLOW_THREADS |
| 9240 | ret = sendfile(out, in, NULL, count); |
| 9241 | Py_END_ALLOW_THREADS |
| 9242 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9243 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9244 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9245 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9246 | } |
| 9247 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9248 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9249 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9250 | |
| 9251 | do { |
| 9252 | Py_BEGIN_ALLOW_THREADS |
| 9253 | ret = sendfile(out, in, &offset, count); |
| 9254 | Py_END_ALLOW_THREADS |
| 9255 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9256 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9257 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9258 | return Py_BuildValue("n", ret); |
| 9259 | #endif |
| 9260 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9261 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9262 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9263 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9264 | #if defined(__APPLE__) |
| 9265 | /*[clinic input] |
| 9266 | os._fcopyfile |
| 9267 | |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9268 | in_fd: int |
| 9269 | out_fd: int |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9270 | flags: int |
| 9271 | / |
| 9272 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9273 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9274 | [clinic start generated code]*/ |
| 9275 | |
| 9276 | static PyObject * |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9277 | os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags) |
| 9278 | /*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9279 | { |
| 9280 | int ret; |
| 9281 | |
| 9282 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9283 | ret = fcopyfile(in_fd, out_fd, NULL, flags); |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9284 | Py_END_ALLOW_THREADS |
| 9285 | if (ret < 0) |
| 9286 | return posix_error(); |
| 9287 | Py_RETURN_NONE; |
| 9288 | } |
| 9289 | #endif |
| 9290 | |
| 9291 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9292 | /*[clinic input] |
| 9293 | os.fstat |
| 9294 | |
| 9295 | fd : int |
| 9296 | |
| 9297 | Perform a stat system call on the given file descriptor. |
| 9298 | |
| 9299 | Like stat(), but for an open file descriptor. |
| 9300 | Equivalent to os.stat(fd). |
| 9301 | [clinic start generated code]*/ |
| 9302 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9303 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9304 | os_fstat_impl(PyObject *module, int fd) |
| 9305 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9306 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9307 | STRUCT_STAT st; |
| 9308 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9309 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9310 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9311 | do { |
| 9312 | Py_BEGIN_ALLOW_THREADS |
| 9313 | res = FSTAT(fd, &st); |
| 9314 | Py_END_ALLOW_THREADS |
| 9315 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9316 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9317 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9318 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9319 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9320 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9321 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9322 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9323 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9324 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9325 | } |
| 9326 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9327 | |
| 9328 | /*[clinic input] |
| 9329 | os.isatty -> bool |
| 9330 | fd: int |
| 9331 | / |
| 9332 | |
| 9333 | Return True if the fd is connected to a terminal. |
| 9334 | |
| 9335 | Return True if the file descriptor is an open file descriptor |
| 9336 | connected to the slave end of a terminal. |
| 9337 | [clinic start generated code]*/ |
| 9338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9339 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9340 | os_isatty_impl(PyObject *module, int fd) |
| 9341 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9342 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9343 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9344 | _Py_BEGIN_SUPPRESS_IPH |
| 9345 | return_value = isatty(fd); |
| 9346 | _Py_END_SUPPRESS_IPH |
| 9347 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9348 | } |
| 9349 | |
| 9350 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9351 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9352 | /*[clinic input] |
| 9353 | os.pipe |
| 9354 | |
| 9355 | Create a pipe. |
| 9356 | |
| 9357 | Returns a tuple of two file descriptors: |
| 9358 | (read_fd, write_fd) |
| 9359 | [clinic start generated code]*/ |
| 9360 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9361 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9362 | os_pipe_impl(PyObject *module) |
| 9363 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9364 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9365 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9366 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9367 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9368 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9369 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9370 | #else |
| 9371 | int res; |
| 9372 | #endif |
| 9373 | |
| 9374 | #ifdef MS_WINDOWS |
| 9375 | attr.nLength = sizeof(attr); |
| 9376 | attr.lpSecurityDescriptor = NULL; |
| 9377 | attr.bInheritHandle = FALSE; |
| 9378 | |
| 9379 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9380 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9381 | ok = CreatePipe(&read, &write, &attr, 0); |
| 9382 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9383 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 9384 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9385 | if (fds[0] == -1 || fds[1] == -1) { |
| 9386 | CloseHandle(read); |
| 9387 | CloseHandle(write); |
| 9388 | ok = 0; |
| 9389 | } |
| 9390 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9391 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9392 | Py_END_ALLOW_THREADS |
| 9393 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9394 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9395 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9396 | #else |
| 9397 | |
| 9398 | #ifdef HAVE_PIPE2 |
| 9399 | Py_BEGIN_ALLOW_THREADS |
| 9400 | res = pipe2(fds, O_CLOEXEC); |
| 9401 | Py_END_ALLOW_THREADS |
| 9402 | |
| 9403 | if (res != 0 && errno == ENOSYS) |
| 9404 | { |
| 9405 | #endif |
| 9406 | Py_BEGIN_ALLOW_THREADS |
| 9407 | res = pipe(fds); |
| 9408 | Py_END_ALLOW_THREADS |
| 9409 | |
| 9410 | if (res == 0) { |
| 9411 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9412 | close(fds[0]); |
| 9413 | close(fds[1]); |
| 9414 | return NULL; |
| 9415 | } |
| 9416 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9417 | close(fds[0]); |
| 9418 | close(fds[1]); |
| 9419 | return NULL; |
| 9420 | } |
| 9421 | } |
| 9422 | #ifdef HAVE_PIPE2 |
| 9423 | } |
| 9424 | #endif |
| 9425 | |
| 9426 | if (res != 0) |
| 9427 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9428 | #endif /* !MS_WINDOWS */ |
| 9429 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9430 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9431 | #endif /* HAVE_PIPE */ |
| 9432 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9433 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9434 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9435 | /*[clinic input] |
| 9436 | os.pipe2 |
| 9437 | |
| 9438 | flags: int |
| 9439 | / |
| 9440 | |
| 9441 | Create a pipe with flags set atomically. |
| 9442 | |
| 9443 | Returns a tuple of two file descriptors: |
| 9444 | (read_fd, write_fd) |
| 9445 | |
| 9446 | flags can be constructed by ORing together one or more of these values: |
| 9447 | O_NONBLOCK, O_CLOEXEC. |
| 9448 | [clinic start generated code]*/ |
| 9449 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9450 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9451 | os_pipe2_impl(PyObject *module, int flags) |
| 9452 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9453 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9454 | int fds[2]; |
| 9455 | int res; |
| 9456 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9457 | res = pipe2(fds, flags); |
| 9458 | if (res != 0) |
| 9459 | return posix_error(); |
| 9460 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9461 | } |
| 9462 | #endif /* HAVE_PIPE2 */ |
| 9463 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9464 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9465 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9466 | /*[clinic input] |
| 9467 | os.writev -> Py_ssize_t |
| 9468 | fd: int |
| 9469 | buffers: object |
| 9470 | / |
| 9471 | |
| 9472 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9473 | |
| 9474 | Returns the total number of bytes written. |
| 9475 | buffers must be a sequence of bytes-like objects. |
| 9476 | [clinic start generated code]*/ |
| 9477 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9478 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9479 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9480 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9481 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9482 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9483 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9484 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9485 | struct iovec *iov; |
| 9486 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9487 | |
| 9488 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9489 | PyErr_SetString(PyExc_TypeError, |
| 9490 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9491 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9492 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9493 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9494 | if (cnt < 0) |
| 9495 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9496 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9497 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9498 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9499 | } |
| 9500 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9501 | do { |
| 9502 | Py_BEGIN_ALLOW_THREADS |
| 9503 | result = writev(fd, iov, cnt); |
| 9504 | Py_END_ALLOW_THREADS |
| 9505 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9506 | |
| 9507 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9508 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9509 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9510 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9511 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9512 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9513 | #endif /* HAVE_WRITEV */ |
| 9514 | |
| 9515 | |
| 9516 | #ifdef HAVE_PWRITE |
| 9517 | /*[clinic input] |
| 9518 | os.pwrite -> Py_ssize_t |
| 9519 | |
| 9520 | fd: int |
| 9521 | buffer: Py_buffer |
| 9522 | offset: Py_off_t |
| 9523 | / |
| 9524 | |
| 9525 | Write bytes to a file descriptor starting at a particular offset. |
| 9526 | |
| 9527 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9528 | the file. Returns the number of bytes writte. Does not change the |
| 9529 | current file offset. |
| 9530 | [clinic start generated code]*/ |
| 9531 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9532 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9533 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9534 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9535 | { |
| 9536 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9537 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9538 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9539 | do { |
| 9540 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9541 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9542 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9543 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9544 | Py_END_ALLOW_THREADS |
| 9545 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9546 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9547 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9548 | posix_error(); |
| 9549 | return size; |
| 9550 | } |
| 9551 | #endif /* HAVE_PWRITE */ |
| 9552 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9553 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9554 | /*[clinic input] |
| 9555 | os.pwritev -> Py_ssize_t |
| 9556 | |
| 9557 | fd: int |
| 9558 | buffers: object |
| 9559 | offset: Py_off_t |
| 9560 | flags: int = 0 |
| 9561 | / |
| 9562 | |
| 9563 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9564 | |
| 9565 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9566 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9567 | buffer is written before proceeding to second, and so on. The operating system may |
| 9568 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9569 | This function writes the contents of each object to the file descriptor and returns |
| 9570 | the total number of bytes written. |
| 9571 | |
| 9572 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9573 | |
| 9574 | - RWF_DSYNC |
| 9575 | - RWF_SYNC |
| 9576 | |
| 9577 | Using non-zero flags requires Linux 4.7 or newer. |
| 9578 | [clinic start generated code]*/ |
| 9579 | |
| 9580 | static Py_ssize_t |
| 9581 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9582 | int flags) |
| 9583 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/ |
| 9584 | { |
| 9585 | Py_ssize_t cnt; |
| 9586 | Py_ssize_t result; |
| 9587 | int async_err = 0; |
| 9588 | struct iovec *iov; |
| 9589 | Py_buffer *buf; |
| 9590 | |
| 9591 | if (!PySequence_Check(buffers)) { |
| 9592 | PyErr_SetString(PyExc_TypeError, |
| 9593 | "pwritev() arg 2 must be a sequence"); |
| 9594 | return -1; |
| 9595 | } |
| 9596 | |
| 9597 | cnt = PySequence_Size(buffers); |
| 9598 | if (cnt < 0) { |
| 9599 | return -1; |
| 9600 | } |
| 9601 | |
| 9602 | #ifndef HAVE_PWRITEV2 |
| 9603 | if(flags != 0) { |
| 9604 | argument_unavailable_error("pwritev2", "flags"); |
| 9605 | return -1; |
| 9606 | } |
| 9607 | #endif |
| 9608 | |
| 9609 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9610 | return -1; |
| 9611 | } |
| 9612 | #ifdef HAVE_PWRITEV2 |
| 9613 | do { |
| 9614 | Py_BEGIN_ALLOW_THREADS |
| 9615 | _Py_BEGIN_SUPPRESS_IPH |
| 9616 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9617 | _Py_END_SUPPRESS_IPH |
| 9618 | Py_END_ALLOW_THREADS |
| 9619 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9620 | #else |
| 9621 | do { |
| 9622 | Py_BEGIN_ALLOW_THREADS |
| 9623 | _Py_BEGIN_SUPPRESS_IPH |
| 9624 | result = pwritev(fd, iov, cnt, offset); |
| 9625 | _Py_END_SUPPRESS_IPH |
| 9626 | Py_END_ALLOW_THREADS |
| 9627 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9628 | #endif |
| 9629 | |
| 9630 | iov_cleanup(iov, buf, cnt); |
| 9631 | if (result < 0) { |
| 9632 | if (!async_err) { |
| 9633 | posix_error(); |
| 9634 | } |
| 9635 | return -1; |
| 9636 | } |
| 9637 | |
| 9638 | return result; |
| 9639 | } |
| 9640 | #endif /* HAVE_PWRITEV */ |
| 9641 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9642 | #ifdef HAVE_COPY_FILE_RANGE |
| 9643 | /*[clinic input] |
| 9644 | |
| 9645 | os.copy_file_range |
| 9646 | src: int |
| 9647 | Source file descriptor. |
| 9648 | dst: int |
| 9649 | Destination file descriptor. |
| 9650 | count: Py_ssize_t |
| 9651 | Number of bytes to copy. |
| 9652 | offset_src: object = None |
| 9653 | Starting offset in src. |
| 9654 | offset_dst: object = None |
| 9655 | Starting offset in dst. |
| 9656 | |
| 9657 | Copy count bytes from one file descriptor to another. |
| 9658 | |
| 9659 | If offset_src is None, then src is read from the current position; |
| 9660 | respectively for offset_dst. |
| 9661 | [clinic start generated code]*/ |
| 9662 | |
| 9663 | static PyObject * |
| 9664 | os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, |
| 9665 | PyObject *offset_src, PyObject *offset_dst) |
| 9666 | /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ |
| 9667 | { |
| 9668 | off_t offset_src_val, offset_dst_val; |
| 9669 | off_t *p_offset_src = NULL; |
| 9670 | off_t *p_offset_dst = NULL; |
| 9671 | Py_ssize_t ret; |
| 9672 | int async_err = 0; |
| 9673 | /* The flags argument is provided to allow |
| 9674 | * for future extensions and currently must be to 0. */ |
| 9675 | int flags = 0; |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9676 | |
| 9677 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9678 | if (count < 0) { |
| 9679 | PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); |
| 9680 | return NULL; |
| 9681 | } |
| 9682 | |
| 9683 | if (offset_src != Py_None) { |
| 9684 | if (!Py_off_t_converter(offset_src, &offset_src_val)) { |
| 9685 | return NULL; |
| 9686 | } |
| 9687 | p_offset_src = &offset_src_val; |
| 9688 | } |
| 9689 | |
| 9690 | if (offset_dst != Py_None) { |
| 9691 | if (!Py_off_t_converter(offset_dst, &offset_dst_val)) { |
| 9692 | return NULL; |
| 9693 | } |
| 9694 | p_offset_dst = &offset_dst_val; |
| 9695 | } |
| 9696 | |
| 9697 | do { |
| 9698 | Py_BEGIN_ALLOW_THREADS |
| 9699 | ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags); |
| 9700 | Py_END_ALLOW_THREADS |
| 9701 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9702 | |
| 9703 | if (ret < 0) { |
| 9704 | return (!async_err) ? posix_error() : NULL; |
| 9705 | } |
| 9706 | |
| 9707 | return PyLong_FromSsize_t(ret); |
| 9708 | } |
| 9709 | #endif /* HAVE_COPY_FILE_RANGE*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9710 | |
| 9711 | #ifdef HAVE_MKFIFO |
| 9712 | /*[clinic input] |
| 9713 | os.mkfifo |
| 9714 | |
| 9715 | path: path_t |
| 9716 | mode: int=0o666 |
| 9717 | * |
| 9718 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9719 | |
| 9720 | Create a "fifo" (a POSIX named pipe). |
| 9721 | |
| 9722 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9723 | and path should be relative; path will then be relative to that directory. |
| 9724 | dir_fd may not be implemented on your platform. |
| 9725 | If it is unavailable, using it will raise a NotImplementedError. |
| 9726 | [clinic start generated code]*/ |
| 9727 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9728 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9729 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9730 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9731 | { |
| 9732 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9733 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9734 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9735 | do { |
| 9736 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9737 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9738 | if (dir_fd != DEFAULT_DIR_FD) |
| 9739 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9740 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9741 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9742 | result = mkfifo(path->narrow, mode); |
| 9743 | Py_END_ALLOW_THREADS |
| 9744 | } while (result != 0 && errno == EINTR && |
| 9745 | !(async_err = PyErr_CheckSignals())); |
| 9746 | if (result != 0) |
| 9747 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9748 | |
| 9749 | Py_RETURN_NONE; |
| 9750 | } |
| 9751 | #endif /* HAVE_MKFIFO */ |
| 9752 | |
| 9753 | |
| 9754 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9755 | /*[clinic input] |
| 9756 | os.mknod |
| 9757 | |
| 9758 | path: path_t |
| 9759 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9760 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9761 | * |
| 9762 | dir_fd: dir_fd(requires='mknodat')=None |
| 9763 | |
| 9764 | Create a node in the file system. |
| 9765 | |
| 9766 | Create a node in the file system (file, device special file or named pipe) |
| 9767 | at path. mode specifies both the permissions to use and the |
| 9768 | type of node to be created, being combined (bitwise OR) with one of |
| 9769 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 9770 | device defines the newly created device special file (probably using |
| 9771 | os.makedev()). Otherwise device is ignored. |
| 9772 | |
| 9773 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9774 | and path should be relative; path will then be relative to that directory. |
| 9775 | dir_fd may not be implemented on your platform. |
| 9776 | If it is unavailable, using it will raise a NotImplementedError. |
| 9777 | [clinic start generated code]*/ |
| 9778 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9779 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9780 | os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9781 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9782 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9783 | { |
| 9784 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9785 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9786 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9787 | do { |
| 9788 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9789 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9790 | if (dir_fd != DEFAULT_DIR_FD) |
| 9791 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 9792 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9793 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9794 | result = mknod(path->narrow, mode, device); |
| 9795 | Py_END_ALLOW_THREADS |
| 9796 | } while (result != 0 && errno == EINTR && |
| 9797 | !(async_err = PyErr_CheckSignals())); |
| 9798 | if (result != 0) |
| 9799 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9800 | |
| 9801 | Py_RETURN_NONE; |
| 9802 | } |
| 9803 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 9804 | |
| 9805 | |
| 9806 | #ifdef HAVE_DEVICE_MACROS |
| 9807 | /*[clinic input] |
| 9808 | os.major -> unsigned_int |
| 9809 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9810 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9811 | / |
| 9812 | |
| 9813 | Extracts a device major number from a raw device number. |
| 9814 | [clinic start generated code]*/ |
| 9815 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9816 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9817 | os_major_impl(PyObject *module, dev_t device) |
| 9818 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9819 | { |
| 9820 | return major(device); |
| 9821 | } |
| 9822 | |
| 9823 | |
| 9824 | /*[clinic input] |
| 9825 | os.minor -> unsigned_int |
| 9826 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9827 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9828 | / |
| 9829 | |
| 9830 | Extracts a device minor number from a raw device number. |
| 9831 | [clinic start generated code]*/ |
| 9832 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9833 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9834 | os_minor_impl(PyObject *module, dev_t device) |
| 9835 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9836 | { |
| 9837 | return minor(device); |
| 9838 | } |
| 9839 | |
| 9840 | |
| 9841 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9842 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9843 | |
| 9844 | major: int |
| 9845 | minor: int |
| 9846 | / |
| 9847 | |
| 9848 | Composes a raw device number from the major and minor device numbers. |
| 9849 | [clinic start generated code]*/ |
| 9850 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9851 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9852 | os_makedev_impl(PyObject *module, int major, int minor) |
| 9853 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9854 | { |
| 9855 | return makedev(major, minor); |
| 9856 | } |
| 9857 | #endif /* HAVE_DEVICE_MACROS */ |
| 9858 | |
| 9859 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9860 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9861 | /*[clinic input] |
| 9862 | os.ftruncate |
| 9863 | |
| 9864 | fd: int |
| 9865 | length: Py_off_t |
| 9866 | / |
| 9867 | |
| 9868 | Truncate a file, specified by file descriptor, to a specific length. |
| 9869 | [clinic start generated code]*/ |
| 9870 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9871 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9872 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 9873 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9874 | { |
| 9875 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9876 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9877 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 9878 | if (PySys_Audit("os.truncate", "in", fd, length) < 0) { |
| 9879 | return NULL; |
| 9880 | } |
| 9881 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9882 | do { |
| 9883 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9884 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9885 | #ifdef MS_WINDOWS |
| 9886 | result = _chsize_s(fd, length); |
| 9887 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9888 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9889 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9890 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9891 | Py_END_ALLOW_THREADS |
| 9892 | } while (result != 0 && errno == EINTR && |
| 9893 | !(async_err = PyErr_CheckSignals())); |
| 9894 | if (result != 0) |
| 9895 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9896 | Py_RETURN_NONE; |
| 9897 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9898 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9899 | |
| 9900 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9901 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9902 | /*[clinic input] |
| 9903 | os.truncate |
| 9904 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 9905 | length: Py_off_t |
| 9906 | |
| 9907 | Truncate a file, specified by path, to a specific length. |
| 9908 | |
| 9909 | On some platforms, path may also be specified as an open file descriptor. |
| 9910 | If this functionality is unavailable, using it raises an exception. |
| 9911 | [clinic start generated code]*/ |
| 9912 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9913 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9914 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 9915 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9916 | { |
| 9917 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9918 | #ifdef MS_WINDOWS |
| 9919 | int fd; |
| 9920 | #endif |
| 9921 | |
| 9922 | if (path->fd != -1) |
| 9923 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9924 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 9925 | if (PySys_Audit("os.truncate", "On", path->object, length) < 0) { |
| 9926 | return NULL; |
| 9927 | } |
| 9928 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9929 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9930 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9931 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 9932 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 9933 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9934 | result = -1; |
| 9935 | else { |
| 9936 | result = _chsize_s(fd, length); |
| 9937 | close(fd); |
| 9938 | if (result < 0) |
| 9939 | errno = result; |
| 9940 | } |
| 9941 | #else |
| 9942 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9943 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9944 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9945 | Py_END_ALLOW_THREADS |
| 9946 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 9947 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9948 | |
| 9949 | Py_RETURN_NONE; |
| 9950 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9951 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9952 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9953 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9954 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 9955 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 9956 | defined, which is the case in Python on AIX. AIX bug report: |
| 9957 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 9958 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 9959 | # define POSIX_FADVISE_AIX_BUG |
| 9960 | #endif |
| 9961 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9962 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9963 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9964 | /*[clinic input] |
| 9965 | os.posix_fallocate |
| 9966 | |
| 9967 | fd: int |
| 9968 | offset: Py_off_t |
| 9969 | length: Py_off_t |
| 9970 | / |
| 9971 | |
| 9972 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 9973 | |
| 9974 | Ensure that the file specified by fd encompasses a range of bytes |
| 9975 | starting at offset bytes from the beginning and continuing for length bytes. |
| 9976 | [clinic start generated code]*/ |
| 9977 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9978 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9979 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9980 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9981 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9982 | { |
| 9983 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9984 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9985 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9986 | do { |
| 9987 | Py_BEGIN_ALLOW_THREADS |
| 9988 | result = posix_fallocate(fd, offset, length); |
| 9989 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 9990 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9991 | |
| 9992 | if (result == 0) |
| 9993 | Py_RETURN_NONE; |
| 9994 | |
| 9995 | if (async_err) |
| 9996 | return NULL; |
| 9997 | |
| 9998 | errno = result; |
| 9999 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10000 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10001 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10002 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10003 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10004 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10005 | /*[clinic input] |
| 10006 | os.posix_fadvise |
| 10007 | |
| 10008 | fd: int |
| 10009 | offset: Py_off_t |
| 10010 | length: Py_off_t |
| 10011 | advice: int |
| 10012 | / |
| 10013 | |
| 10014 | Announce an intention to access data in a specific pattern. |
| 10015 | |
| 10016 | Announce an intention to access data in a specific pattern, thus allowing |
| 10017 | the kernel to make optimizations. |
| 10018 | The advice applies to the region of the file specified by fd starting at |
| 10019 | offset and continuing for length bytes. |
| 10020 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 10021 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 10022 | POSIX_FADV_DONTNEED. |
| 10023 | [clinic start generated code]*/ |
| 10024 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10025 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10026 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10027 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10028 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10029 | { |
| 10030 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10031 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10032 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10033 | do { |
| 10034 | Py_BEGIN_ALLOW_THREADS |
| 10035 | result = posix_fadvise(fd, offset, length, advice); |
| 10036 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10037 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10038 | |
| 10039 | if (result == 0) |
| 10040 | Py_RETURN_NONE; |
| 10041 | |
| 10042 | if (async_err) |
| 10043 | return NULL; |
| 10044 | |
| 10045 | errno = result; |
| 10046 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10047 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10048 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10049 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10050 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10051 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10052 | static void |
| 10053 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10054 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10055 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 10056 | * this will cause previous value to be collected. This has to |
| 10057 | * happen after the real putenv() call because the old value |
| 10058 | * was still accessible until then. */ |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 10059 | if (PyDict_SetItem(_posixstate_global->posix_putenv_garbage, name, value)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10060 | /* really not much we can do; just leak */ |
| 10061 | PyErr_Clear(); |
| 10062 | else |
| 10063 | Py_DECREF(value); |
| 10064 | } |
| 10065 | |
| 10066 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 10067 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10068 | /*[clinic input] |
| 10069 | os.putenv |
| 10070 | |
| 10071 | name: unicode |
| 10072 | value: unicode |
| 10073 | / |
| 10074 | |
| 10075 | Change or add an environment variable. |
| 10076 | [clinic start generated code]*/ |
| 10077 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10078 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10079 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10080 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10081 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 10082 | const wchar_t *env; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10083 | Py_ssize_t size; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10084 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10085 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 10086 | defining hidden environment variables. */ |
| 10087 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 10088 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 10089 | { |
| 10090 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10091 | return NULL; |
| 10092 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10093 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 10094 | if (unicode == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10095 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10096 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10097 | |
| 10098 | env = PyUnicode_AsUnicodeAndSize(unicode, &size); |
| 10099 | if (env == NULL) |
| 10100 | goto error; |
| 10101 | if (size > _MAX_ENV) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10102 | PyErr_Format(PyExc_ValueError, |
| 10103 | "the environment variable is longer than %u characters", |
| 10104 | _MAX_ENV); |
| 10105 | goto error; |
| 10106 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10107 | if (wcslen(env) != (size_t)size) { |
| 10108 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10109 | goto error; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10110 | } |
| 10111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10112 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10113 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10114 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10115 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10116 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10117 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10118 | Py_RETURN_NONE; |
| 10119 | |
| 10120 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10121 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10122 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10123 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10124 | #else /* MS_WINDOWS */ |
| 10125 | /*[clinic input] |
| 10126 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10127 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10128 | name: FSConverter |
| 10129 | value: FSConverter |
| 10130 | / |
| 10131 | |
| 10132 | Change or add an environment variable. |
| 10133 | [clinic start generated code]*/ |
| 10134 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10135 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10136 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10137 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10138 | { |
| 10139 | PyObject *bytes = NULL; |
| 10140 | char *env; |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10141 | const char *name_string = PyBytes_AS_STRING(name); |
| 10142 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10143 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10144 | if (strchr(name_string, '=') != NULL) { |
| 10145 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10146 | return NULL; |
| 10147 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10148 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 10149 | if (bytes == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10150 | return NULL; |
| 10151 | } |
| 10152 | |
| 10153 | env = PyBytes_AS_STRING(bytes); |
| 10154 | if (putenv(env)) { |
| 10155 | Py_DECREF(bytes); |
| 10156 | return posix_error(); |
| 10157 | } |
| 10158 | |
| 10159 | posix_putenv_garbage_setitem(name, bytes); |
| 10160 | Py_RETURN_NONE; |
| 10161 | } |
| 10162 | #endif /* MS_WINDOWS */ |
| 10163 | #endif /* HAVE_PUTENV */ |
| 10164 | |
| 10165 | |
| 10166 | #ifdef HAVE_UNSETENV |
| 10167 | /*[clinic input] |
| 10168 | os.unsetenv |
| 10169 | name: FSConverter |
| 10170 | / |
| 10171 | |
| 10172 | Delete an environment variable. |
| 10173 | [clinic start generated code]*/ |
| 10174 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10175 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10176 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10177 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10178 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10179 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10180 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10181 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10182 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10183 | #ifdef HAVE_BROKEN_UNSETENV |
| 10184 | unsetenv(PyBytes_AS_STRING(name)); |
| 10185 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10186 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10187 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10188 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10189 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10190 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10191 | /* Remove the key from posix_putenv_garbage; |
| 10192 | * this will cause it to be collected. This has to |
| 10193 | * happen after the real unsetenv() call because the |
| 10194 | * old value was still accessible until then. |
| 10195 | */ |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 10196 | if (PyDict_DelItem(_posixstate(module)->posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10197 | /* really not much we can do; just leak */ |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 10198 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 10199 | return NULL; |
| 10200 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10201 | PyErr_Clear(); |
| 10202 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10203 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10204 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10205 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10206 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10207 | |
| 10208 | /*[clinic input] |
| 10209 | os.strerror |
| 10210 | |
| 10211 | code: int |
| 10212 | / |
| 10213 | |
| 10214 | Translate an error code to a message string. |
| 10215 | [clinic start generated code]*/ |
| 10216 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10217 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10218 | os_strerror_impl(PyObject *module, int code) |
| 10219 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10220 | { |
| 10221 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10222 | if (message == NULL) { |
| 10223 | PyErr_SetString(PyExc_ValueError, |
| 10224 | "strerror() argument out of range"); |
| 10225 | return NULL; |
| 10226 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 10227 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10228 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10229 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10230 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10231 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10232 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10233 | /*[clinic input] |
| 10234 | os.WCOREDUMP -> bool |
| 10235 | |
| 10236 | status: int |
| 10237 | / |
| 10238 | |
| 10239 | Return True if the process returning status was dumped to a core file. |
| 10240 | [clinic start generated code]*/ |
| 10241 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10242 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10243 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 10244 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10245 | { |
| 10246 | WAIT_TYPE wait_status; |
| 10247 | WAIT_STATUS_INT(wait_status) = status; |
| 10248 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10249 | } |
| 10250 | #endif /* WCOREDUMP */ |
| 10251 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10252 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10253 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10254 | /*[clinic input] |
| 10255 | os.WIFCONTINUED -> bool |
| 10256 | |
| 10257 | status: int |
| 10258 | |
| 10259 | Return True if a particular process was continued from a job control stop. |
| 10260 | |
| 10261 | Return True if the process returning status was continued from a |
| 10262 | job control stop. |
| 10263 | [clinic start generated code]*/ |
| 10264 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10265 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10266 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 10267 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10268 | { |
| 10269 | WAIT_TYPE wait_status; |
| 10270 | WAIT_STATUS_INT(wait_status) = status; |
| 10271 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10272 | } |
| 10273 | #endif /* WIFCONTINUED */ |
| 10274 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10275 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10276 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10277 | /*[clinic input] |
| 10278 | os.WIFSTOPPED -> bool |
| 10279 | |
| 10280 | status: int |
| 10281 | |
| 10282 | Return True if the process returning status was stopped. |
| 10283 | [clinic start generated code]*/ |
| 10284 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10285 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10286 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 10287 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10288 | { |
| 10289 | WAIT_TYPE wait_status; |
| 10290 | WAIT_STATUS_INT(wait_status) = status; |
| 10291 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10292 | } |
| 10293 | #endif /* WIFSTOPPED */ |
| 10294 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10295 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10296 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10297 | /*[clinic input] |
| 10298 | os.WIFSIGNALED -> bool |
| 10299 | |
| 10300 | status: int |
| 10301 | |
| 10302 | Return True if the process returning status was terminated by a signal. |
| 10303 | [clinic start generated code]*/ |
| 10304 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10305 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10306 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 10307 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10308 | { |
| 10309 | WAIT_TYPE wait_status; |
| 10310 | WAIT_STATUS_INT(wait_status) = status; |
| 10311 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10312 | } |
| 10313 | #endif /* WIFSIGNALED */ |
| 10314 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10315 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10316 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10317 | /*[clinic input] |
| 10318 | os.WIFEXITED -> bool |
| 10319 | |
| 10320 | status: int |
| 10321 | |
| 10322 | Return True if the process returning status exited via the exit() system call. |
| 10323 | [clinic start generated code]*/ |
| 10324 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10325 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10326 | os_WIFEXITED_impl(PyObject *module, int status) |
| 10327 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10328 | { |
| 10329 | WAIT_TYPE wait_status; |
| 10330 | WAIT_STATUS_INT(wait_status) = status; |
| 10331 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10332 | } |
| 10333 | #endif /* WIFEXITED */ |
| 10334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10335 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10336 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10337 | /*[clinic input] |
| 10338 | os.WEXITSTATUS -> int |
| 10339 | |
| 10340 | status: int |
| 10341 | |
| 10342 | Return the process return code from status. |
| 10343 | [clinic start generated code]*/ |
| 10344 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10345 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10346 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 10347 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10348 | { |
| 10349 | WAIT_TYPE wait_status; |
| 10350 | WAIT_STATUS_INT(wait_status) = status; |
| 10351 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10352 | } |
| 10353 | #endif /* WEXITSTATUS */ |
| 10354 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10355 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10356 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10357 | /*[clinic input] |
| 10358 | os.WTERMSIG -> int |
| 10359 | |
| 10360 | status: int |
| 10361 | |
| 10362 | Return the signal that terminated the process that provided the status value. |
| 10363 | [clinic start generated code]*/ |
| 10364 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10365 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10366 | os_WTERMSIG_impl(PyObject *module, int status) |
| 10367 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10368 | { |
| 10369 | WAIT_TYPE wait_status; |
| 10370 | WAIT_STATUS_INT(wait_status) = status; |
| 10371 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10372 | } |
| 10373 | #endif /* WTERMSIG */ |
| 10374 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10375 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10376 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10377 | /*[clinic input] |
| 10378 | os.WSTOPSIG -> int |
| 10379 | |
| 10380 | status: int |
| 10381 | |
| 10382 | Return the signal that stopped the process that provided the status value. |
| 10383 | [clinic start generated code]*/ |
| 10384 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10385 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10386 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 10387 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10388 | { |
| 10389 | WAIT_TYPE wait_status; |
| 10390 | WAIT_STATUS_INT(wait_status) = status; |
| 10391 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10392 | } |
| 10393 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10394 | #endif /* HAVE_SYS_WAIT_H */ |
| 10395 | |
| 10396 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10397 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10398 | #ifdef _SCO_DS |
| 10399 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 10400 | needed definitions in sys/statvfs.h */ |
| 10401 | #define _SVID3 |
| 10402 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10403 | #include <sys/statvfs.h> |
| 10404 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10405 | static PyObject* |
| 10406 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 10407 | PyObject *StatVFSResultType = _posixstate_global->StatVFSResultType; |
| 10408 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10409 | if (v == NULL) |
| 10410 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10411 | |
| 10412 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10413 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10414 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10415 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 10416 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 10417 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 10418 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 10419 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 10420 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 10421 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10422 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10423 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10424 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10425 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10426 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10427 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10428 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10429 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10430 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10431 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10432 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10433 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10434 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10435 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10436 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10437 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10438 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10439 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10440 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10441 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 10442 | * (issue #32390). */ |
| 10443 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 10444 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 10445 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10446 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10447 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10448 | if (PyErr_Occurred()) { |
| 10449 | Py_DECREF(v); |
| 10450 | return NULL; |
| 10451 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10452 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10453 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10454 | } |
| 10455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10456 | |
| 10457 | /*[clinic input] |
| 10458 | os.fstatvfs |
| 10459 | fd: int |
| 10460 | / |
| 10461 | |
| 10462 | Perform an fstatvfs system call on the given fd. |
| 10463 | |
| 10464 | Equivalent to statvfs(fd). |
| 10465 | [clinic start generated code]*/ |
| 10466 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10467 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10468 | os_fstatvfs_impl(PyObject *module, int fd) |
| 10469 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10470 | { |
| 10471 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10472 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10473 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10474 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10475 | do { |
| 10476 | Py_BEGIN_ALLOW_THREADS |
| 10477 | result = fstatvfs(fd, &st); |
| 10478 | Py_END_ALLOW_THREADS |
| 10479 | } while (result != 0 && errno == EINTR && |
| 10480 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10481 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10482 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10483 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10484 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10485 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10486 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10487 | |
| 10488 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10489 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10490 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10491 | /*[clinic input] |
| 10492 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10493 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10494 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10495 | |
| 10496 | Perform a statvfs system call on the given path. |
| 10497 | |
| 10498 | path may always be specified as a string. |
| 10499 | On some platforms, path may also be specified as an open file descriptor. |
| 10500 | If this functionality is unavailable, using it raises an exception. |
| 10501 | [clinic start generated code]*/ |
| 10502 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10503 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10504 | os_statvfs_impl(PyObject *module, path_t *path) |
| 10505 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10506 | { |
| 10507 | int result; |
| 10508 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10509 | |
| 10510 | Py_BEGIN_ALLOW_THREADS |
| 10511 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10512 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10513 | #ifdef __APPLE__ |
| 10514 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10515 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10516 | fd_specified("statvfs", path->fd); |
| 10517 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10518 | } |
| 10519 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10520 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10521 | } |
| 10522 | else |
| 10523 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10524 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10525 | Py_END_ALLOW_THREADS |
| 10526 | |
| 10527 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10528 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10529 | } |
| 10530 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10531 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10532 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10533 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10534 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10535 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10536 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10537 | /*[clinic input] |
| 10538 | os._getdiskusage |
| 10539 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10540 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10541 | |
| 10542 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10543 | [clinic start generated code]*/ |
| 10544 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10545 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10546 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10547 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10548 | { |
| 10549 | BOOL retval; |
| 10550 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10551 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10552 | |
| 10553 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10554 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10555 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10556 | if (retval == 0) { |
| 10557 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10558 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10559 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10560 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10561 | if (dir_path == NULL) { |
| 10562 | return PyErr_NoMemory(); |
| 10563 | } |
| 10564 | |
| 10565 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10566 | |
| 10567 | if (_dirnameW(dir_path) != -1) { |
| 10568 | Py_BEGIN_ALLOW_THREADS |
| 10569 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10570 | Py_END_ALLOW_THREADS |
| 10571 | } |
| 10572 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10573 | err = GetLastError(); |
| 10574 | PyMem_Free(dir_path); |
| 10575 | if (retval) { |
| 10576 | goto success; |
| 10577 | } |
| 10578 | } |
| 10579 | return PyErr_SetFromWindowsErr(err); |
| 10580 | } |
| 10581 | |
| 10582 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10583 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10584 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10585 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10586 | |
| 10587 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10588 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10589 | * It maps strings representing configuration variable names to |
| 10590 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10591 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10592 | * rarely-used constants. There are three separate tables that use |
| 10593 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10594 | * |
| 10595 | * This code is always included, even if none of the interfaces that |
| 10596 | * need it are included. The #if hackery needed to avoid it would be |
| 10597 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10598 | */ |
| 10599 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10600 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10601 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10602 | }; |
| 10603 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10604 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10605 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10606 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10607 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10608 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10609 | int value = _PyLong_AsInt(arg); |
| 10610 | if (value == -1 && PyErr_Occurred()) |
| 10611 | return 0; |
| 10612 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10613 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10614 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10615 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10616 | /* look up the value in the table using a binary search */ |
| 10617 | size_t lo = 0; |
| 10618 | size_t mid; |
| 10619 | size_t hi = tablesize; |
| 10620 | int cmp; |
| 10621 | const char *confname; |
| 10622 | if (!PyUnicode_Check(arg)) { |
| 10623 | PyErr_SetString(PyExc_TypeError, |
| 10624 | "configuration names must be strings or integers"); |
| 10625 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10626 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10627 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10628 | if (confname == NULL) |
| 10629 | return 0; |
| 10630 | while (lo < hi) { |
| 10631 | mid = (lo + hi) / 2; |
| 10632 | cmp = strcmp(confname, table[mid].name); |
| 10633 | if (cmp < 0) |
| 10634 | hi = mid; |
| 10635 | else if (cmp > 0) |
| 10636 | lo = mid + 1; |
| 10637 | else { |
| 10638 | *valuep = table[mid].value; |
| 10639 | return 1; |
| 10640 | } |
| 10641 | } |
| 10642 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10643 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10644 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10645 | } |
| 10646 | |
| 10647 | |
| 10648 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10649 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10650 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10651 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10652 | #endif |
| 10653 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10654 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10655 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10656 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10657 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10658 | #endif |
| 10659 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10660 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10661 | #endif |
| 10662 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10663 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10664 | #endif |
| 10665 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10666 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10667 | #endif |
| 10668 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10669 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10670 | #endif |
| 10671 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10672 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10673 | #endif |
| 10674 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10675 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10676 | #endif |
| 10677 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10678 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10679 | #endif |
| 10680 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10681 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10682 | #endif |
| 10683 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10684 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10685 | #endif |
| 10686 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10687 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10688 | #endif |
| 10689 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10690 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10691 | #endif |
| 10692 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10693 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10694 | #endif |
| 10695 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10696 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10697 | #endif |
| 10698 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10699 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10700 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10701 | #ifdef _PC_ACL_ENABLED |
| 10702 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10703 | #endif |
| 10704 | #ifdef _PC_MIN_HOLE_SIZE |
| 10705 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10706 | #endif |
| 10707 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10708 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10709 | #endif |
| 10710 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10711 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10712 | #endif |
| 10713 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10714 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10715 | #endif |
| 10716 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10717 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10718 | #endif |
| 10719 | #ifdef _PC_REC_XFER_ALIGN |
| 10720 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10721 | #endif |
| 10722 | #ifdef _PC_SYMLINK_MAX |
| 10723 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10724 | #endif |
| 10725 | #ifdef _PC_XATTR_ENABLED |
| 10726 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10727 | #endif |
| 10728 | #ifdef _PC_XATTR_EXISTS |
| 10729 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10730 | #endif |
| 10731 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10732 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10733 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10734 | }; |
| 10735 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10736 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10737 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10738 | { |
| 10739 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10740 | sizeof(posix_constants_pathconf) |
| 10741 | / sizeof(struct constdef)); |
| 10742 | } |
| 10743 | #endif |
| 10744 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10745 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10746 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10747 | /*[clinic input] |
| 10748 | os.fpathconf -> long |
| 10749 | |
| 10750 | fd: int |
| 10751 | name: path_confname |
| 10752 | / |
| 10753 | |
| 10754 | Return the configuration limit name for the file descriptor fd. |
| 10755 | |
| 10756 | If there is no limit, return -1. |
| 10757 | [clinic start generated code]*/ |
| 10758 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10759 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10760 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 10761 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10762 | { |
| 10763 | long limit; |
| 10764 | |
| 10765 | errno = 0; |
| 10766 | limit = fpathconf(fd, name); |
| 10767 | if (limit == -1 && errno != 0) |
| 10768 | posix_error(); |
| 10769 | |
| 10770 | return limit; |
| 10771 | } |
| 10772 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10773 | |
| 10774 | |
| 10775 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10776 | /*[clinic input] |
| 10777 | os.pathconf -> long |
| 10778 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 10779 | name: path_confname |
| 10780 | |
| 10781 | Return the configuration limit name for the file or directory path. |
| 10782 | |
| 10783 | If there is no limit, return -1. |
| 10784 | On some platforms, path may also be specified as an open file descriptor. |
| 10785 | If this functionality is unavailable, using it raises an exception. |
| 10786 | [clinic start generated code]*/ |
| 10787 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10788 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10789 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 10790 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10791 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10792 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10793 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10794 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10795 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10796 | if (path->fd != -1) |
| 10797 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10798 | else |
| 10799 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10800 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10801 | if (limit == -1 && errno != 0) { |
| 10802 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 10803 | /* could be a path or name problem */ |
| 10804 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10805 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10806 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10807 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10808 | |
| 10809 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10810 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10811 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10812 | |
| 10813 | #ifdef HAVE_CONFSTR |
| 10814 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10815 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10816 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10817 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10818 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10819 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10820 | #endif |
| 10821 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10822 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10823 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10824 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10825 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10826 | #endif |
| 10827 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10828 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10829 | #endif |
| 10830 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10831 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10832 | #endif |
| 10833 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10834 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10835 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10836 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10837 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10838 | #endif |
| 10839 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10840 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10841 | #endif |
| 10842 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10843 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10844 | #endif |
| 10845 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10846 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10847 | #endif |
| 10848 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10849 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10850 | #endif |
| 10851 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10852 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10853 | #endif |
| 10854 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10855 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10856 | #endif |
| 10857 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10858 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10859 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10860 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10861 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10862 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10863 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10864 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10865 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10866 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10867 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10868 | #endif |
| 10869 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10870 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10871 | #endif |
| 10872 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10873 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10874 | #endif |
| 10875 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10876 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10877 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10878 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10879 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10880 | #endif |
| 10881 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10882 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10883 | #endif |
| 10884 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10885 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10886 | #endif |
| 10887 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10888 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10889 | #endif |
| 10890 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10891 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10892 | #endif |
| 10893 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10894 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10895 | #endif |
| 10896 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10897 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10898 | #endif |
| 10899 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10900 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10901 | #endif |
| 10902 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10903 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10904 | #endif |
| 10905 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10906 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10907 | #endif |
| 10908 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10909 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10910 | #endif |
| 10911 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10912 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10913 | #endif |
| 10914 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10915 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10916 | #endif |
| 10917 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10918 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10919 | #endif |
| 10920 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10921 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10922 | #endif |
| 10923 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10924 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10925 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10926 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10927 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10928 | #endif |
| 10929 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10930 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10931 | #endif |
| 10932 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10933 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10934 | #endif |
| 10935 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10936 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10937 | #endif |
| 10938 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10939 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10940 | #endif |
| 10941 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10942 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10943 | #endif |
| 10944 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10945 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10946 | #endif |
| 10947 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10948 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10949 | #endif |
| 10950 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10951 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10952 | #endif |
| 10953 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10954 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10955 | #endif |
| 10956 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10957 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10958 | #endif |
| 10959 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10960 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10961 | #endif |
| 10962 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10963 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10964 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10965 | }; |
| 10966 | |
| 10967 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10968 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10969 | { |
| 10970 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 10971 | sizeof(posix_constants_confstr) |
| 10972 | / sizeof(struct constdef)); |
| 10973 | } |
| 10974 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10975 | |
| 10976 | /*[clinic input] |
| 10977 | os.confstr |
| 10978 | |
| 10979 | name: confstr_confname |
| 10980 | / |
| 10981 | |
| 10982 | Return a string-valued system configuration variable. |
| 10983 | [clinic start generated code]*/ |
| 10984 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10985 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10986 | os_confstr_impl(PyObject *module, int name) |
| 10987 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10988 | { |
| 10989 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10990 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 10991 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10992 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 10993 | errno = 0; |
| 10994 | len = confstr(name, buffer, sizeof(buffer)); |
| 10995 | if (len == 0) { |
| 10996 | if (errno) { |
| 10997 | posix_error(); |
| 10998 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10999 | } |
| 11000 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11001 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11002 | } |
| 11003 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11004 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11005 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11006 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11007 | char *buf = PyMem_Malloc(len); |
| 11008 | if (buf == NULL) |
| 11009 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11010 | len2 = confstr(name, buf, len); |
| 11011 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 11012 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11013 | PyMem_Free(buf); |
| 11014 | } |
| 11015 | else |
| 11016 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11017 | return result; |
| 11018 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11019 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11020 | |
| 11021 | |
| 11022 | #ifdef HAVE_SYSCONF |
| 11023 | static struct constdef posix_constants_sysconf[] = { |
| 11024 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11025 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11026 | #endif |
| 11027 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11028 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11029 | #endif |
| 11030 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11031 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11032 | #endif |
| 11033 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11034 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11035 | #endif |
| 11036 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11037 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11038 | #endif |
| 11039 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11040 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11041 | #endif |
| 11042 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11043 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11044 | #endif |
| 11045 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11046 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11047 | #endif |
| 11048 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11049 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11050 | #endif |
| 11051 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11052 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11053 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11054 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11055 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11056 | #endif |
| 11057 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11058 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11059 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11060 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11061 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11062 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11063 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11064 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11065 | #endif |
| 11066 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11067 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11068 | #endif |
| 11069 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11070 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11071 | #endif |
| 11072 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11073 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11074 | #endif |
| 11075 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11076 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11077 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11078 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11079 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11080 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11081 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11082 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11083 | #endif |
| 11084 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11085 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11086 | #endif |
| 11087 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11088 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11089 | #endif |
| 11090 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11091 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11092 | #endif |
| 11093 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11094 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11095 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11096 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11097 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11098 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11099 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11100 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11101 | #endif |
| 11102 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11103 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11104 | #endif |
| 11105 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11106 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11107 | #endif |
| 11108 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11109 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11110 | #endif |
| 11111 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11112 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11113 | #endif |
| 11114 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11115 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11116 | #endif |
| 11117 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11118 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11119 | #endif |
| 11120 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11121 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11122 | #endif |
| 11123 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11124 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11125 | #endif |
| 11126 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11127 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11128 | #endif |
| 11129 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11130 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11131 | #endif |
| 11132 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11133 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11134 | #endif |
| 11135 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11136 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11137 | #endif |
| 11138 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11139 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11140 | #endif |
| 11141 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11142 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11143 | #endif |
| 11144 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11145 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11146 | #endif |
| 11147 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11148 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11149 | #endif |
| 11150 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11151 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11152 | #endif |
| 11153 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11154 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11155 | #endif |
| 11156 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11157 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11158 | #endif |
| 11159 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11160 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11161 | #endif |
| 11162 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11163 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11164 | #endif |
| 11165 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11166 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11167 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11168 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11169 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11170 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11171 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11172 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11173 | #endif |
| 11174 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11175 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11176 | #endif |
| 11177 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11178 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11179 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11180 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11181 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11182 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11183 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11184 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11185 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11186 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11187 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11188 | #endif |
| 11189 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11190 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11191 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11192 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11193 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11194 | #endif |
| 11195 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11196 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11197 | #endif |
| 11198 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11199 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11200 | #endif |
| 11201 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11202 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11203 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11204 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11205 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11206 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11207 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11208 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11209 | #endif |
| 11210 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11211 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11212 | #endif |
| 11213 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11214 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11215 | #endif |
| 11216 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11217 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11218 | #endif |
| 11219 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11220 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11221 | #endif |
| 11222 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11223 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11224 | #endif |
| 11225 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11226 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11227 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11228 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11229 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11230 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11231 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11232 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11233 | #endif |
| 11234 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11235 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11236 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11237 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11238 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11239 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11240 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11241 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11242 | #endif |
| 11243 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11244 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11245 | #endif |
| 11246 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11247 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11248 | #endif |
| 11249 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11250 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11251 | #endif |
| 11252 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11253 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11254 | #endif |
| 11255 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11256 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11257 | #endif |
| 11258 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11259 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11260 | #endif |
| 11261 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11262 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11263 | #endif |
| 11264 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11265 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11266 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11267 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11268 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11269 | #endif |
| 11270 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11271 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11272 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11273 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11274 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11275 | #endif |
| 11276 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11277 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11278 | #endif |
| 11279 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11280 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11281 | #endif |
| 11282 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11283 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11284 | #endif |
| 11285 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11286 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11287 | #endif |
| 11288 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11289 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11290 | #endif |
| 11291 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11292 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11293 | #endif |
| 11294 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11295 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11296 | #endif |
| 11297 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11298 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11299 | #endif |
| 11300 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11301 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11302 | #endif |
| 11303 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11304 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11305 | #endif |
| 11306 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11307 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11308 | #endif |
| 11309 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11310 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11311 | #endif |
| 11312 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11313 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11314 | #endif |
| 11315 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11316 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11317 | #endif |
| 11318 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11319 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11320 | #endif |
| 11321 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11322 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11323 | #endif |
| 11324 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11325 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11326 | #endif |
| 11327 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11328 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11329 | #endif |
| 11330 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11331 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11332 | #endif |
| 11333 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11334 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11335 | #endif |
| 11336 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11337 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11338 | #endif |
| 11339 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11340 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11341 | #endif |
| 11342 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11343 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11344 | #endif |
| 11345 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11346 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11347 | #endif |
| 11348 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11349 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11350 | #endif |
| 11351 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11352 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11353 | #endif |
| 11354 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11355 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11356 | #endif |
| 11357 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11358 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11359 | #endif |
| 11360 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11361 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11362 | #endif |
| 11363 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11364 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11365 | #endif |
| 11366 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11367 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11368 | #endif |
| 11369 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11370 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11371 | #endif |
| 11372 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11373 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11374 | #endif |
| 11375 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11376 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11377 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11378 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11379 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11380 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11381 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11382 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11383 | #endif |
| 11384 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11385 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11386 | #endif |
| 11387 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11388 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11389 | #endif |
| 11390 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11391 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11392 | #endif |
| 11393 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11394 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11395 | #endif |
| 11396 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11397 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11398 | #endif |
| 11399 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11400 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11401 | #endif |
| 11402 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11403 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11404 | #endif |
| 11405 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11406 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11407 | #endif |
| 11408 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11409 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11410 | #endif |
| 11411 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11412 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11413 | #endif |
| 11414 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11415 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11416 | #endif |
| 11417 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11418 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11419 | #endif |
| 11420 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11421 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11422 | #endif |
| 11423 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11424 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11425 | #endif |
| 11426 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11427 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11428 | #endif |
| 11429 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11430 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11431 | #endif |
| 11432 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11433 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11434 | #endif |
| 11435 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11436 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11437 | #endif |
| 11438 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11439 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11440 | #endif |
| 11441 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11442 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11443 | #endif |
| 11444 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11445 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11446 | #endif |
| 11447 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11448 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11449 | #endif |
| 11450 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11451 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11452 | #endif |
| 11453 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11454 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11455 | #endif |
| 11456 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11457 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11458 | #endif |
| 11459 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11460 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11461 | #endif |
| 11462 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11463 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11464 | #endif |
| 11465 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11466 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11467 | #endif |
| 11468 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11469 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11470 | #endif |
| 11471 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11472 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11473 | #endif |
| 11474 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11475 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11476 | #endif |
| 11477 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11478 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11479 | #endif |
| 11480 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11481 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11482 | #endif |
| 11483 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11484 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11485 | #endif |
| 11486 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11487 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11488 | #endif |
| 11489 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11490 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11491 | #endif |
| 11492 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11493 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11494 | #endif |
| 11495 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11496 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11497 | #endif |
| 11498 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11499 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11500 | #endif |
| 11501 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11502 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11503 | #endif |
| 11504 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11505 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11506 | #endif |
| 11507 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11508 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11509 | #endif |
| 11510 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11511 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11512 | #endif |
| 11513 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11514 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11515 | #endif |
| 11516 | }; |
| 11517 | |
| 11518 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11519 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11520 | { |
| 11521 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11522 | sizeof(posix_constants_sysconf) |
| 11523 | / sizeof(struct constdef)); |
| 11524 | } |
| 11525 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11526 | |
| 11527 | /*[clinic input] |
| 11528 | os.sysconf -> long |
| 11529 | name: sysconf_confname |
| 11530 | / |
| 11531 | |
| 11532 | Return an integer-valued system configuration variable. |
| 11533 | [clinic start generated code]*/ |
| 11534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11535 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11536 | os_sysconf_impl(PyObject *module, int name) |
| 11537 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11538 | { |
| 11539 | long value; |
| 11540 | |
| 11541 | errno = 0; |
| 11542 | value = sysconf(name); |
| 11543 | if (value == -1 && errno != 0) |
| 11544 | posix_error(); |
| 11545 | return value; |
| 11546 | } |
| 11547 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11548 | |
| 11549 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11550 | /* This code is used to ensure that the tables of configuration value names |
Serhiy Storchaka | 56a6d85 | 2014-12-01 18:28:43 +0200 | [diff] [blame] | 11551 | * are in sorted order as required by conv_confname(), and also to build |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11552 | * the exported dictionaries that are used to publish information about the |
| 11553 | * names available on the host platform. |
| 11554 | * |
| 11555 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11556 | * when used, even for platforms we're not able to test on. It also makes |
| 11557 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11558 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11559 | |
| 11560 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11561 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11562 | { |
| 11563 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11564 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11565 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11566 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11567 | |
| 11568 | return strcmp(c1->name, c2->name); |
| 11569 | } |
| 11570 | |
| 11571 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11572 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11573 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11574 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11575 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11576 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11577 | |
| 11578 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11579 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11580 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11581 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11582 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11583 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11584 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11585 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11586 | Py_XDECREF(o); |
| 11587 | Py_DECREF(d); |
| 11588 | return -1; |
| 11589 | } |
| 11590 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11591 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11592 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11593 | } |
| 11594 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11595 | /* Return -1 on failure, 0 on success. */ |
| 11596 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11597 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11598 | { |
| 11599 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11600 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11601 | sizeof(posix_constants_pathconf) |
| 11602 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11603 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11604 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11605 | #endif |
| 11606 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11607 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11608 | sizeof(posix_constants_confstr) |
| 11609 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11610 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11611 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11612 | #endif |
| 11613 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11614 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11615 | sizeof(posix_constants_sysconf) |
| 11616 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11617 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11618 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11619 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11620 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11621 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11622 | |
| 11623 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11624 | /*[clinic input] |
| 11625 | os.abort |
| 11626 | |
| 11627 | Abort the interpreter immediately. |
| 11628 | |
| 11629 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11630 | on the hosting operating system. This function never returns. |
| 11631 | [clinic start generated code]*/ |
| 11632 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11633 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11634 | os_abort_impl(PyObject *module) |
| 11635 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11636 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11637 | abort(); |
| 11638 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11639 | #ifndef __clang__ |
| 11640 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11641 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11642 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11643 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11644 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11645 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11646 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11647 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11648 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11649 | /* Grab ShellExecute dynamically from shell32 */ |
| 11650 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11651 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11652 | LPCWSTR, INT); |
| 11653 | static int |
| 11654 | check_ShellExecute() |
| 11655 | { |
| 11656 | HINSTANCE hShell32; |
| 11657 | |
| 11658 | /* only recheck */ |
| 11659 | if (-1 == has_ShellExecute) { |
| 11660 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11661 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11662 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11663 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11664 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11665 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11666 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11667 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11668 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11669 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11670 | } else { |
| 11671 | has_ShellExecute = 0; |
| 11672 | } |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 11673 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11674 | } |
| 11675 | return has_ShellExecute; |
| 11676 | } |
| 11677 | |
| 11678 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11679 | /*[clinic input] |
| 11680 | os.startfile |
| 11681 | filepath: path_t |
| 11682 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11683 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11684 | Start a file with its associated application. |
| 11685 | |
| 11686 | When "operation" is not specified or "open", this acts like |
| 11687 | double-clicking the file in Explorer, or giving the file name as an |
| 11688 | argument to the DOS "start" command: the file is opened with whatever |
| 11689 | application (if any) its extension is associated. |
| 11690 | When another "operation" is given, it specifies what should be done with |
| 11691 | the file. A typical operation is "print". |
| 11692 | |
| 11693 | startfile returns as soon as the associated application is launched. |
| 11694 | There is no option to wait for the application to close, and no way |
| 11695 | to retrieve the application's exit status. |
| 11696 | |
| 11697 | The filepath is relative to the current directory. If you want to use |
| 11698 | an absolute path, make sure the first character is not a slash ("/"); |
| 11699 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11700 | [clinic start generated code]*/ |
| 11701 | |
| 11702 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11703 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11704 | const Py_UNICODE *operation) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 11705 | /*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11706 | { |
| 11707 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11708 | |
| 11709 | if(!check_ShellExecute()) { |
| 11710 | /* If the OS doesn't have ShellExecute, return a |
| 11711 | NotImplementedError. */ |
| 11712 | return PyErr_Format(PyExc_NotImplementedError, |
| 11713 | "startfile not available on this platform"); |
| 11714 | } |
| 11715 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11716 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11717 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11718 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11719 | Py_END_ALLOW_THREADS |
| 11720 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11721 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11722 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11723 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11724 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11725 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11726 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11727 | #endif /* MS_WINDOWS */ |
| 11728 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11729 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11730 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11731 | /*[clinic input] |
| 11732 | os.getloadavg |
| 11733 | |
| 11734 | Return average recent system load information. |
| 11735 | |
| 11736 | Return the number of processes in the system run queue averaged over |
| 11737 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11738 | Raises OSError if the load average was unobtainable. |
| 11739 | [clinic start generated code]*/ |
| 11740 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11741 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11742 | os_getloadavg_impl(PyObject *module) |
| 11743 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11744 | { |
| 11745 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11746 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11747 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11748 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11749 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11750 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11751 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11752 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11753 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11754 | |
| 11755 | /*[clinic input] |
| 11756 | os.device_encoding |
| 11757 | fd: int |
| 11758 | |
| 11759 | Return a string describing the encoding of a terminal's file descriptor. |
| 11760 | |
| 11761 | The file descriptor must be attached to a terminal. |
| 11762 | If the device is not a terminal, return None. |
| 11763 | [clinic start generated code]*/ |
| 11764 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11765 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11766 | os_device_encoding_impl(PyObject *module, int fd) |
| 11767 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11768 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11769 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 11770 | } |
| 11771 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11772 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11773 | #ifdef HAVE_SETRESUID |
| 11774 | /*[clinic input] |
| 11775 | os.setresuid |
| 11776 | |
| 11777 | ruid: uid_t |
| 11778 | euid: uid_t |
| 11779 | suid: uid_t |
| 11780 | / |
| 11781 | |
| 11782 | Set the current process's real, effective, and saved user ids. |
| 11783 | [clinic start generated code]*/ |
| 11784 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11785 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11786 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 11787 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11788 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11789 | if (setresuid(ruid, euid, suid) < 0) |
| 11790 | return posix_error(); |
| 11791 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11792 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11793 | #endif /* HAVE_SETRESUID */ |
| 11794 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11795 | |
| 11796 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11797 | /*[clinic input] |
| 11798 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11799 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11800 | rgid: gid_t |
| 11801 | egid: gid_t |
| 11802 | sgid: gid_t |
| 11803 | / |
| 11804 | |
| 11805 | Set the current process's real, effective, and saved group ids. |
| 11806 | [clinic start generated code]*/ |
| 11807 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11808 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11809 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 11810 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11811 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11812 | if (setresgid(rgid, egid, sgid) < 0) |
| 11813 | return posix_error(); |
| 11814 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11815 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11816 | #endif /* HAVE_SETRESGID */ |
| 11817 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11818 | |
| 11819 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11820 | /*[clinic input] |
| 11821 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11822 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11823 | Return a tuple of the current process's real, effective, and saved user ids. |
| 11824 | [clinic start generated code]*/ |
| 11825 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11826 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11827 | os_getresuid_impl(PyObject *module) |
| 11828 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11829 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11830 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11831 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 11832 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11833 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 11834 | _PyLong_FromUid(euid), |
| 11835 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11836 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11837 | #endif /* HAVE_GETRESUID */ |
| 11838 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11839 | |
| 11840 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11841 | /*[clinic input] |
| 11842 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11843 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11844 | Return a tuple of the current process's real, effective, and saved group ids. |
| 11845 | [clinic start generated code]*/ |
| 11846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11847 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11848 | os_getresgid_impl(PyObject *module) |
| 11849 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11850 | { |
| 11851 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11852 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 11853 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11854 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 11855 | _PyLong_FromGid(egid), |
| 11856 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11857 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11858 | #endif /* HAVE_GETRESGID */ |
| 11859 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11860 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11861 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11862 | /*[clinic input] |
| 11863 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11864 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11865 | path: path_t(allow_fd=True) |
| 11866 | attribute: path_t |
| 11867 | * |
| 11868 | follow_symlinks: bool = True |
| 11869 | |
| 11870 | Return the value of extended attribute attribute on path. |
| 11871 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11872 | path may be either a string, a path-like object, or an open file descriptor. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11873 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11874 | link, getxattr will examine the symbolic link itself instead of the file |
| 11875 | the link points to. |
| 11876 | |
| 11877 | [clinic start generated code]*/ |
| 11878 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11879 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11880 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11881 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11882 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11883 | { |
| 11884 | Py_ssize_t i; |
| 11885 | PyObject *buffer = NULL; |
| 11886 | |
| 11887 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 11888 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11889 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11890 | for (i = 0; ; i++) { |
| 11891 | void *ptr; |
| 11892 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11893 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11894 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11895 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11896 | path_error(path); |
| 11897 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11898 | } |
| 11899 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 11900 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11901 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11902 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11903 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11904 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11905 | if (path->fd >= 0) |
| 11906 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11907 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11908 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11909 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11910 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11911 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11912 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11913 | if (result < 0) { |
| 11914 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11915 | if (errno == ERANGE) |
| 11916 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11917 | path_error(path); |
| 11918 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11919 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11920 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11921 | if (result != buffer_size) { |
| 11922 | /* Can only shrink. */ |
| 11923 | _PyBytes_Resize(&buffer, result); |
| 11924 | } |
| 11925 | break; |
| 11926 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11927 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11928 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11929 | } |
| 11930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11931 | |
| 11932 | /*[clinic input] |
| 11933 | os.setxattr |
| 11934 | |
| 11935 | path: path_t(allow_fd=True) |
| 11936 | attribute: path_t |
| 11937 | value: Py_buffer |
| 11938 | flags: int = 0 |
| 11939 | * |
| 11940 | follow_symlinks: bool = True |
| 11941 | |
| 11942 | Set extended attribute attribute on path to value. |
| 11943 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11944 | path may be either a string, a path-like object, or an open file descriptor. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11945 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11946 | link, setxattr will modify the symbolic link itself instead of the file |
| 11947 | the link points to. |
| 11948 | |
| 11949 | [clinic start generated code]*/ |
| 11950 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11951 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11952 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11953 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11954 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11955 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11956 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11957 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11958 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11959 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11960 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11961 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11962 | if (path->fd > -1) |
| 11963 | result = fsetxattr(path->fd, attribute->narrow, |
| 11964 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11965 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11966 | result = setxattr(path->narrow, attribute->narrow, |
| 11967 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11968 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11969 | result = lsetxattr(path->narrow, attribute->narrow, |
| 11970 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11971 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11972 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11973 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11974 | path_error(path); |
| 11975 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11976 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11977 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11978 | Py_RETURN_NONE; |
| 11979 | } |
| 11980 | |
| 11981 | |
| 11982 | /*[clinic input] |
| 11983 | os.removexattr |
| 11984 | |
| 11985 | path: path_t(allow_fd=True) |
| 11986 | attribute: path_t |
| 11987 | * |
| 11988 | follow_symlinks: bool = True |
| 11989 | |
| 11990 | Remove extended attribute attribute on path. |
| 11991 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11992 | path may be either a string, a path-like object, or an open file descriptor. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11993 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11994 | link, removexattr will modify the symbolic link itself instead of the file |
| 11995 | the link points to. |
| 11996 | |
| 11997 | [clinic start generated code]*/ |
| 11998 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11999 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12000 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12001 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12002 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12003 | { |
| 12004 | ssize_t result; |
| 12005 | |
| 12006 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 12007 | return NULL; |
| 12008 | |
| 12009 | Py_BEGIN_ALLOW_THREADS; |
| 12010 | if (path->fd > -1) |
| 12011 | result = fremovexattr(path->fd, attribute->narrow); |
| 12012 | else if (follow_symlinks) |
| 12013 | result = removexattr(path->narrow, attribute->narrow); |
| 12014 | else |
| 12015 | result = lremovexattr(path->narrow, attribute->narrow); |
| 12016 | Py_END_ALLOW_THREADS; |
| 12017 | |
| 12018 | if (result) { |
| 12019 | return path_error(path); |
| 12020 | } |
| 12021 | |
| 12022 | Py_RETURN_NONE; |
| 12023 | } |
| 12024 | |
| 12025 | |
| 12026 | /*[clinic input] |
| 12027 | os.listxattr |
| 12028 | |
| 12029 | path: path_t(allow_fd=True, nullable=True) = None |
| 12030 | * |
| 12031 | follow_symlinks: bool = True |
| 12032 | |
| 12033 | Return a list of extended attributes on path. |
| 12034 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12035 | path may be either None, a string, a path-like object, or an open file descriptor. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12036 | if path is None, listxattr will examine the current directory. |
| 12037 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12038 | link, listxattr will examine the symbolic link itself instead of the file |
| 12039 | the link points to. |
| 12040 | [clinic start generated code]*/ |
| 12041 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12042 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12043 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12044 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12045 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12046 | Py_ssize_t i; |
| 12047 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12048 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12049 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12050 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12051 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12052 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12053 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12054 | name = path->narrow ? path->narrow : "."; |
| 12055 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12056 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12057 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12058 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12059 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12060 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12061 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 12062 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12063 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12064 | break; |
| 12065 | } |
| 12066 | buffer = PyMem_MALLOC(buffer_size); |
| 12067 | if (!buffer) { |
| 12068 | PyErr_NoMemory(); |
| 12069 | break; |
| 12070 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12071 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12072 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12073 | if (path->fd > -1) |
| 12074 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12075 | else if (follow_symlinks) |
| 12076 | length = listxattr(name, buffer, buffer_size); |
| 12077 | else |
| 12078 | length = llistxattr(name, buffer, buffer_size); |
| 12079 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12080 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12081 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12082 | if (errno == ERANGE) { |
| 12083 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 12084 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12085 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12086 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12087 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12088 | break; |
| 12089 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12090 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12091 | result = PyList_New(0); |
| 12092 | if (!result) { |
| 12093 | goto exit; |
| 12094 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12095 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12096 | end = buffer + length; |
| 12097 | for (trace = start = buffer; trace != end; trace++) { |
| 12098 | if (!*trace) { |
| 12099 | int error; |
| 12100 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 12101 | trace - start); |
| 12102 | if (!attribute) { |
| 12103 | Py_DECREF(result); |
| 12104 | result = NULL; |
| 12105 | goto exit; |
| 12106 | } |
| 12107 | error = PyList_Append(result, attribute); |
| 12108 | Py_DECREF(attribute); |
| 12109 | if (error) { |
| 12110 | Py_DECREF(result); |
| 12111 | result = NULL; |
| 12112 | goto exit; |
| 12113 | } |
| 12114 | start = trace + 1; |
| 12115 | } |
| 12116 | } |
| 12117 | break; |
| 12118 | } |
| 12119 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12120 | if (buffer) |
| 12121 | PyMem_FREE(buffer); |
| 12122 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12123 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12124 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12125 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12126 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12127 | /*[clinic input] |
| 12128 | os.urandom |
| 12129 | |
| 12130 | size: Py_ssize_t |
| 12131 | / |
| 12132 | |
| 12133 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 12134 | [clinic start generated code]*/ |
| 12135 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12136 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12137 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 12138 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12139 | { |
| 12140 | PyObject *bytes; |
| 12141 | int result; |
| 12142 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12143 | if (size < 0) |
| 12144 | return PyErr_Format(PyExc_ValueError, |
| 12145 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12146 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 12147 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12148 | return NULL; |
| 12149 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 12150 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12151 | if (result == -1) { |
| 12152 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12153 | return NULL; |
| 12154 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12155 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12156 | } |
| 12157 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 12158 | #ifdef HAVE_MEMFD_CREATE |
| 12159 | /*[clinic input] |
| 12160 | os.memfd_create |
| 12161 | |
| 12162 | name: FSConverter |
| 12163 | flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC |
| 12164 | |
| 12165 | [clinic start generated code]*/ |
| 12166 | |
| 12167 | static PyObject * |
| 12168 | os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags) |
| 12169 | /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/ |
| 12170 | { |
| 12171 | int fd; |
| 12172 | const char *bytes = PyBytes_AS_STRING(name); |
| 12173 | Py_BEGIN_ALLOW_THREADS |
| 12174 | fd = memfd_create(bytes, flags); |
| 12175 | Py_END_ALLOW_THREADS |
| 12176 | if (fd == -1) { |
| 12177 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12178 | } |
| 12179 | return PyLong_FromLong(fd); |
| 12180 | } |
| 12181 | #endif |
| 12182 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12183 | /* Terminal size querying */ |
| 12184 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12185 | PyDoc_STRVAR(TerminalSize_docstring, |
| 12186 | "A tuple of (columns, lines) for holding terminal window size"); |
| 12187 | |
| 12188 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 12189 | {"columns", "width of the terminal window in characters"}, |
| 12190 | {"lines", "height of the terminal window in characters"}, |
| 12191 | {NULL, NULL} |
| 12192 | }; |
| 12193 | |
| 12194 | static PyStructSequence_Desc TerminalSize_desc = { |
| 12195 | "os.terminal_size", |
| 12196 | TerminalSize_docstring, |
| 12197 | TerminalSize_fields, |
| 12198 | 2, |
| 12199 | }; |
| 12200 | |
| 12201 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12202 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12203 | PyDoc_STRVAR(termsize__doc__, |
| 12204 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 12205 | "\n" \ |
| 12206 | "The optional argument fd (default standard output) specifies\n" \ |
| 12207 | "which file descriptor should be queried.\n" \ |
| 12208 | "\n" \ |
| 12209 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 12210 | "is thrown.\n" \ |
| 12211 | "\n" \ |
| 12212 | "This function will only be defined if an implementation is\n" \ |
| 12213 | "available for this system.\n" \ |
| 12214 | "\n" \ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 12215 | "shutil.get_terminal_size is the high-level function which should\n" \ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12216 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 12217 | |
| 12218 | static PyObject* |
| 12219 | get_terminal_size(PyObject *self, PyObject *args) |
| 12220 | { |
| 12221 | int columns, lines; |
| 12222 | PyObject *termsize; |
| 12223 | |
| 12224 | int fd = fileno(stdout); |
| 12225 | /* Under some conditions stdout may not be connected and |
| 12226 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 12227 | * GUI apps don't have valid standard streams by default. |
| 12228 | * |
| 12229 | * If this happens, and the optional fd argument is not present, |
| 12230 | * the ioctl below will fail returning EBADF. This is what we want. |
| 12231 | */ |
| 12232 | |
| 12233 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 12234 | return NULL; |
| 12235 | |
| 12236 | #ifdef TERMSIZE_USE_IOCTL |
| 12237 | { |
| 12238 | struct winsize w; |
| 12239 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 12240 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12241 | columns = w.ws_col; |
| 12242 | lines = w.ws_row; |
| 12243 | } |
| 12244 | #endif /* TERMSIZE_USE_IOCTL */ |
| 12245 | |
| 12246 | #ifdef TERMSIZE_USE_CONIO |
| 12247 | { |
| 12248 | DWORD nhandle; |
| 12249 | HANDLE handle; |
| 12250 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 12251 | switch (fd) { |
| 12252 | case 0: nhandle = STD_INPUT_HANDLE; |
| 12253 | break; |
| 12254 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 12255 | break; |
| 12256 | case 2: nhandle = STD_ERROR_HANDLE; |
| 12257 | break; |
| 12258 | default: |
| 12259 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 12260 | } |
| 12261 | handle = GetStdHandle(nhandle); |
| 12262 | if (handle == NULL) |
| 12263 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 12264 | if (handle == INVALID_HANDLE_VALUE) |
| 12265 | return PyErr_SetFromWindowsErr(0); |
| 12266 | |
| 12267 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 12268 | return PyErr_SetFromWindowsErr(0); |
| 12269 | |
| 12270 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 12271 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 12272 | } |
| 12273 | #endif /* TERMSIZE_USE_CONIO */ |
| 12274 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12275 | PyObject *TerminalSizeType = _posixstate(self)->TerminalSizeType; |
| 12276 | termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12277 | if (termsize == NULL) |
| 12278 | return NULL; |
| 12279 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 12280 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 12281 | if (PyErr_Occurred()) { |
| 12282 | Py_DECREF(termsize); |
| 12283 | return NULL; |
| 12284 | } |
| 12285 | return termsize; |
| 12286 | } |
| 12287 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 12288 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12289 | |
| 12290 | /*[clinic input] |
| 12291 | os.cpu_count |
| 12292 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 12293 | Return the number of CPUs in the system; return None if indeterminable. |
| 12294 | |
| 12295 | This number is not equivalent to the number of CPUs the current process can |
| 12296 | use. The number of usable CPUs can be obtained with |
| 12297 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12298 | [clinic start generated code]*/ |
| 12299 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12300 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12301 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12302 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12303 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12304 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12305 | #ifdef MS_WINDOWS |
Steve Dower | aa92927 | 2019-09-11 16:15:39 +0100 | [diff] [blame] | 12306 | ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12307 | #elif defined(__hpux) |
| 12308 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 12309 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 12310 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12311 | #elif defined(__DragonFly__) || \ |
| 12312 | defined(__OpenBSD__) || \ |
| 12313 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12314 | defined(__NetBSD__) || \ |
| 12315 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12316 | int mib[2]; |
| 12317 | size_t len = sizeof(ncpu); |
| 12318 | mib[0] = CTL_HW; |
| 12319 | mib[1] = HW_NCPU; |
| 12320 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 12321 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12322 | #endif |
| 12323 | if (ncpu >= 1) |
| 12324 | return PyLong_FromLong(ncpu); |
| 12325 | else |
| 12326 | Py_RETURN_NONE; |
| 12327 | } |
| 12328 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12329 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12330 | /*[clinic input] |
| 12331 | os.get_inheritable -> bool |
| 12332 | |
| 12333 | fd: int |
| 12334 | / |
| 12335 | |
| 12336 | Get the close-on-exe flag of the specified file descriptor. |
| 12337 | [clinic start generated code]*/ |
| 12338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12339 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12340 | os_get_inheritable_impl(PyObject *module, int fd) |
| 12341 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12342 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12343 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12344 | _Py_BEGIN_SUPPRESS_IPH |
| 12345 | return_value = _Py_get_inheritable(fd); |
| 12346 | _Py_END_SUPPRESS_IPH |
| 12347 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12348 | } |
| 12349 | |
| 12350 | |
| 12351 | /*[clinic input] |
| 12352 | os.set_inheritable |
| 12353 | fd: int |
| 12354 | inheritable: int |
| 12355 | / |
| 12356 | |
| 12357 | Set the inheritable flag of the specified file descriptor. |
| 12358 | [clinic start generated code]*/ |
| 12359 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12360 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12361 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 12362 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12363 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12364 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12365 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12366 | _Py_BEGIN_SUPPRESS_IPH |
| 12367 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 12368 | _Py_END_SUPPRESS_IPH |
| 12369 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12370 | return NULL; |
| 12371 | Py_RETURN_NONE; |
| 12372 | } |
| 12373 | |
| 12374 | |
| 12375 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12376 | /*[clinic input] |
| 12377 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12378 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12379 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12380 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12381 | Get the close-on-exe flag of the specified file descriptor. |
| 12382 | [clinic start generated code]*/ |
| 12383 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12384 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12385 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12386 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12387 | { |
| 12388 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12389 | |
| 12390 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 12391 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12392 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12393 | } |
| 12394 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12395 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12396 | } |
| 12397 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12399 | /*[clinic input] |
| 12400 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12401 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12402 | inheritable: bool |
| 12403 | / |
| 12404 | |
| 12405 | Set the inheritable flag of the specified handle. |
| 12406 | [clinic start generated code]*/ |
| 12407 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12408 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12409 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12410 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12411 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12412 | { |
| 12413 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12414 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 12415 | PyErr_SetFromWindowsErr(0); |
| 12416 | return NULL; |
| 12417 | } |
| 12418 | Py_RETURN_NONE; |
| 12419 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12420 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12421 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12422 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12423 | /*[clinic input] |
| 12424 | os.get_blocking -> bool |
| 12425 | fd: int |
| 12426 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12427 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12428 | Get the blocking mode of the file descriptor. |
| 12429 | |
| 12430 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 12431 | [clinic start generated code]*/ |
| 12432 | |
| 12433 | static int |
| 12434 | os_get_blocking_impl(PyObject *module, int fd) |
| 12435 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12436 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12437 | int blocking; |
| 12438 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12439 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12440 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12441 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12442 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12443 | } |
| 12444 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12445 | /*[clinic input] |
| 12446 | os.set_blocking |
| 12447 | fd: int |
| 12448 | blocking: bool(accept={int}) |
| 12449 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12450 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12451 | Set the blocking mode of the specified file descriptor. |
| 12452 | |
| 12453 | Set the O_NONBLOCK flag if blocking is False, |
| 12454 | clear the O_NONBLOCK flag otherwise. |
| 12455 | [clinic start generated code]*/ |
| 12456 | |
| 12457 | static PyObject * |
| 12458 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 12459 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12460 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12461 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12462 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12463 | _Py_BEGIN_SUPPRESS_IPH |
| 12464 | result = _Py_set_blocking(fd, blocking); |
| 12465 | _Py_END_SUPPRESS_IPH |
| 12466 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12467 | return NULL; |
| 12468 | Py_RETURN_NONE; |
| 12469 | } |
| 12470 | #endif /* !MS_WINDOWS */ |
| 12471 | |
| 12472 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12473 | /*[clinic input] |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12474 | class os.DirEntry "DirEntry *" "DirEntryType" |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12475 | [clinic start generated code]*/ |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12476 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12477 | |
| 12478 | typedef struct { |
| 12479 | PyObject_HEAD |
| 12480 | PyObject *name; |
| 12481 | PyObject *path; |
| 12482 | PyObject *stat; |
| 12483 | PyObject *lstat; |
| 12484 | #ifdef MS_WINDOWS |
| 12485 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12486 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12487 | int got_file_index; |
| 12488 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12489 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12490 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12491 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12492 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12493 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12494 | #endif |
| 12495 | } DirEntry; |
| 12496 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12497 | static PyObject * |
| 12498 | _disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 12499 | { |
| 12500 | PyErr_Format(PyExc_TypeError, |
| 12501 | "cannot create '%.100s' instances", _PyType_Name(type)); |
| 12502 | return NULL; |
| 12503 | } |
| 12504 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12505 | static void |
| 12506 | DirEntry_dealloc(DirEntry *entry) |
| 12507 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12508 | PyTypeObject *tp = Py_TYPE(entry); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12509 | Py_XDECREF(entry->name); |
| 12510 | Py_XDECREF(entry->path); |
| 12511 | Py_XDECREF(entry->stat); |
| 12512 | Py_XDECREF(entry->lstat); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12513 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 12514 | free_func(entry); |
| 12515 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12516 | } |
| 12517 | |
| 12518 | /* Forward reference */ |
| 12519 | static int |
| 12520 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 12521 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12522 | /*[clinic input] |
| 12523 | os.DirEntry.is_symlink -> bool |
| 12524 | |
| 12525 | Return True if the entry is a symbolic link; cached per entry. |
| 12526 | [clinic start generated code]*/ |
| 12527 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12528 | static int |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12529 | os_DirEntry_is_symlink_impl(DirEntry *self) |
| 12530 | /*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12531 | { |
| 12532 | #ifdef MS_WINDOWS |
| 12533 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12534 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12535 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12536 | if (self->d_type != DT_UNKNOWN) |
| 12537 | return self->d_type == DT_LNK; |
| 12538 | else |
| 12539 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12540 | #else |
| 12541 | /* POSIX without d_type */ |
| 12542 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12543 | #endif |
| 12544 | } |
| 12545 | |
| 12546 | static PyObject * |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12547 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 12548 | { |
| 12549 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12550 | STRUCT_STAT st; |
| 12551 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12552 | |
| 12553 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12554 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12555 | return NULL; |
| 12556 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12557 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12558 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12559 | return NULL; |
| 12560 | const char *path = PyBytes_AS_STRING(ub); |
| 12561 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12562 | #ifdef HAVE_FSTATAT |
| 12563 | result = fstatat(self->dir_fd, path, &st, |
| 12564 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12565 | #else |
| 12566 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12567 | return NULL; |
| 12568 | #endif /* HAVE_FSTATAT */ |
| 12569 | } |
| 12570 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12571 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12572 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12573 | if (follow_symlinks) |
| 12574 | result = STAT(path, &st); |
| 12575 | else |
| 12576 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12577 | } |
| 12578 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12579 | |
| 12580 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12581 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12582 | |
| 12583 | return _pystat_fromstructstat(&st); |
| 12584 | } |
| 12585 | |
| 12586 | static PyObject * |
| 12587 | DirEntry_get_lstat(DirEntry *self) |
| 12588 | { |
| 12589 | if (!self->lstat) { |
| 12590 | #ifdef MS_WINDOWS |
| 12591 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 12592 | #else /* POSIX */ |
| 12593 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 12594 | #endif |
| 12595 | } |
| 12596 | Py_XINCREF(self->lstat); |
| 12597 | return self->lstat; |
| 12598 | } |
| 12599 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12600 | /*[clinic input] |
| 12601 | os.DirEntry.stat |
| 12602 | * |
| 12603 | follow_symlinks: bool = True |
| 12604 | |
| 12605 | Return stat_result object for the entry; cached per entry. |
| 12606 | [clinic start generated code]*/ |
| 12607 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12608 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12609 | os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks) |
| 12610 | /*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12611 | { |
| 12612 | if (!follow_symlinks) |
| 12613 | return DirEntry_get_lstat(self); |
| 12614 | |
| 12615 | if (!self->stat) { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12616 | int result = os_DirEntry_is_symlink_impl(self); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12617 | if (result == -1) |
| 12618 | return NULL; |
| 12619 | else if (result) |
| 12620 | self->stat = DirEntry_fetch_stat(self, 1); |
| 12621 | else |
| 12622 | self->stat = DirEntry_get_lstat(self); |
| 12623 | } |
| 12624 | |
| 12625 | Py_XINCREF(self->stat); |
| 12626 | return self->stat; |
| 12627 | } |
| 12628 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12629 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12630 | static int |
| 12631 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 12632 | { |
| 12633 | PyObject *stat = NULL; |
| 12634 | PyObject *st_mode = NULL; |
| 12635 | long mode; |
| 12636 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12637 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12638 | int is_symlink; |
| 12639 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12640 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12641 | #ifdef MS_WINDOWS |
| 12642 | unsigned long dir_bits; |
| 12643 | #endif |
| 12644 | |
| 12645 | #ifdef MS_WINDOWS |
| 12646 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12647 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12648 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12649 | is_symlink = self->d_type == DT_LNK; |
| 12650 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12651 | #endif |
| 12652 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12653 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12654 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12655 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12656 | stat = os_DirEntry_stat_impl(self, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12657 | if (!stat) { |
| 12658 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12659 | /* If file doesn't exist (anymore), then return False |
| 12660 | (i.e., say it's not a file/directory) */ |
| 12661 | PyErr_Clear(); |
| 12662 | return 0; |
| 12663 | } |
| 12664 | goto error; |
| 12665 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12666 | st_mode = PyObject_GetAttr(stat, _posixstate_global->st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12667 | if (!st_mode) |
| 12668 | goto error; |
| 12669 | |
| 12670 | mode = PyLong_AsLong(st_mode); |
| 12671 | if (mode == -1 && PyErr_Occurred()) |
| 12672 | goto error; |
| 12673 | Py_CLEAR(st_mode); |
| 12674 | Py_CLEAR(stat); |
| 12675 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12676 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12677 | } |
| 12678 | else if (is_symlink) { |
| 12679 | assert(mode_bits != S_IFLNK); |
| 12680 | result = 0; |
| 12681 | } |
| 12682 | else { |
| 12683 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 12684 | #ifdef MS_WINDOWS |
| 12685 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 12686 | if (mode_bits == S_IFDIR) |
| 12687 | result = dir_bits != 0; |
| 12688 | else |
| 12689 | result = dir_bits == 0; |
| 12690 | #else /* POSIX */ |
| 12691 | if (mode_bits == S_IFDIR) |
| 12692 | result = self->d_type == DT_DIR; |
| 12693 | else |
| 12694 | result = self->d_type == DT_REG; |
| 12695 | #endif |
| 12696 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12697 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12698 | |
| 12699 | return result; |
| 12700 | |
| 12701 | error: |
| 12702 | Py_XDECREF(st_mode); |
| 12703 | Py_XDECREF(stat); |
| 12704 | return -1; |
| 12705 | } |
| 12706 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12707 | /*[clinic input] |
| 12708 | os.DirEntry.is_dir -> bool |
| 12709 | * |
| 12710 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12711 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12712 | Return True if the entry is a directory; cached per entry. |
| 12713 | [clinic start generated code]*/ |
| 12714 | |
| 12715 | static int |
| 12716 | os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks) |
| 12717 | /*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/ |
| 12718 | { |
| 12719 | return DirEntry_test_mode(self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12720 | } |
| 12721 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12722 | /*[clinic input] |
| 12723 | os.DirEntry.is_file -> bool |
| 12724 | * |
| 12725 | follow_symlinks: bool = True |
| 12726 | |
| 12727 | Return True if the entry is a file; cached per entry. |
| 12728 | [clinic start generated code]*/ |
| 12729 | |
| 12730 | static int |
| 12731 | os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks) |
| 12732 | /*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12733 | { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12734 | return DirEntry_test_mode(self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12735 | } |
| 12736 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12737 | /*[clinic input] |
| 12738 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12739 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12740 | Return inode of the entry; cached per entry. |
| 12741 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12742 | |
| 12743 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12744 | os_DirEntry_inode_impl(DirEntry *self) |
| 12745 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12746 | { |
| 12747 | #ifdef MS_WINDOWS |
| 12748 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12749 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12750 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12751 | STRUCT_STAT stat; |
| 12752 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12753 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12754 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12755 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12756 | path = PyUnicode_AsUnicode(unicode); |
| 12757 | result = LSTAT(path, &stat); |
| 12758 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12759 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12760 | if (result != 0) |
| 12761 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12762 | |
| 12763 | self->win32_file_index = stat.st_ino; |
| 12764 | self->got_file_index = 1; |
| 12765 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12766 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 12767 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12768 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 12769 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 12770 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12771 | #endif |
| 12772 | } |
| 12773 | |
| 12774 | static PyObject * |
| 12775 | DirEntry_repr(DirEntry *self) |
| 12776 | { |
| 12777 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 12778 | } |
| 12779 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12780 | /*[clinic input] |
| 12781 | os.DirEntry.__fspath__ |
| 12782 | |
| 12783 | Returns the path for the entry. |
| 12784 | [clinic start generated code]*/ |
| 12785 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12786 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12787 | os_DirEntry___fspath___impl(DirEntry *self) |
| 12788 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12789 | { |
| 12790 | Py_INCREF(self->path); |
| 12791 | return self->path; |
| 12792 | } |
| 12793 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12794 | static PyMemberDef DirEntry_members[] = { |
| 12795 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 12796 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 12797 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 12798 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 12799 | {NULL} |
| 12800 | }; |
| 12801 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12802 | #include "clinic/posixmodule.c.h" |
| 12803 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12804 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12805 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 12806 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 12807 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 12808 | OS_DIRENTRY_STAT_METHODDEF |
| 12809 | OS_DIRENTRY_INODE_METHODDEF |
| 12810 | OS_DIRENTRY___FSPATH___METHODDEF |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12811 | {NULL} |
| 12812 | }; |
| 12813 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12814 | static PyType_Slot DirEntryType_slots[] = { |
| 12815 | {Py_tp_new, _disabled_new}, |
| 12816 | {Py_tp_dealloc, DirEntry_dealloc}, |
| 12817 | {Py_tp_repr, DirEntry_repr}, |
| 12818 | {Py_tp_methods, DirEntry_methods}, |
| 12819 | {Py_tp_members, DirEntry_members}, |
| 12820 | {0, 0}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12821 | }; |
| 12822 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12823 | static PyType_Spec DirEntryType_spec = { |
| 12824 | MODNAME ".DirEntry", |
| 12825 | sizeof(DirEntry), |
| 12826 | 0, |
| 12827 | Py_TPFLAGS_DEFAULT, |
| 12828 | DirEntryType_slots |
| 12829 | }; |
| 12830 | |
| 12831 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12832 | #ifdef MS_WINDOWS |
| 12833 | |
| 12834 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12835 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12836 | { |
| 12837 | Py_ssize_t path_len; |
| 12838 | Py_ssize_t size; |
| 12839 | wchar_t *result; |
| 12840 | wchar_t ch; |
| 12841 | |
| 12842 | if (!path_wide) { /* Default arg: "." */ |
| 12843 | path_wide = L"."; |
| 12844 | path_len = 1; |
| 12845 | } |
| 12846 | else { |
| 12847 | path_len = wcslen(path_wide); |
| 12848 | } |
| 12849 | |
| 12850 | /* The +1's are for the path separator and the NUL */ |
| 12851 | size = path_len + 1 + wcslen(filename) + 1; |
| 12852 | result = PyMem_New(wchar_t, size); |
| 12853 | if (!result) { |
| 12854 | PyErr_NoMemory(); |
| 12855 | return NULL; |
| 12856 | } |
| 12857 | wcscpy(result, path_wide); |
| 12858 | if (path_len > 0) { |
| 12859 | ch = result[path_len - 1]; |
| 12860 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 12861 | result[path_len++] = SEP; |
| 12862 | wcscpy(result + path_len, filename); |
| 12863 | } |
| 12864 | return result; |
| 12865 | } |
| 12866 | |
| 12867 | static PyObject * |
| 12868 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 12869 | { |
| 12870 | DirEntry *entry; |
| 12871 | BY_HANDLE_FILE_INFORMATION file_info; |
| 12872 | ULONG reparse_tag; |
| 12873 | wchar_t *joined_path; |
| 12874 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12875 | PyObject *DirEntryType = _posixstate_global->DirEntryType; |
| 12876 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12877 | if (!entry) |
| 12878 | return NULL; |
| 12879 | entry->name = NULL; |
| 12880 | entry->path = NULL; |
| 12881 | entry->stat = NULL; |
| 12882 | entry->lstat = NULL; |
| 12883 | entry->got_file_index = 0; |
| 12884 | |
| 12885 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 12886 | if (!entry->name) |
| 12887 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12888 | if (path->narrow) { |
| 12889 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 12890 | if (!entry->name) |
| 12891 | goto error; |
| 12892 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12893 | |
| 12894 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 12895 | if (!joined_path) |
| 12896 | goto error; |
| 12897 | |
| 12898 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 12899 | PyMem_Free(joined_path); |
| 12900 | if (!entry->path) |
| 12901 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12902 | if (path->narrow) { |
| 12903 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 12904 | if (!entry->path) |
| 12905 | goto error; |
| 12906 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12907 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12908 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12909 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 12910 | |
| 12911 | return (PyObject *)entry; |
| 12912 | |
| 12913 | error: |
| 12914 | Py_DECREF(entry); |
| 12915 | return NULL; |
| 12916 | } |
| 12917 | |
| 12918 | #else /* POSIX */ |
| 12919 | |
| 12920 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12921 | join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12922 | { |
| 12923 | Py_ssize_t path_len; |
| 12924 | Py_ssize_t size; |
| 12925 | char *result; |
| 12926 | |
| 12927 | if (!path_narrow) { /* Default arg: "." */ |
| 12928 | path_narrow = "."; |
| 12929 | path_len = 1; |
| 12930 | } |
| 12931 | else { |
| 12932 | path_len = strlen(path_narrow); |
| 12933 | } |
| 12934 | |
| 12935 | if (filename_len == -1) |
| 12936 | filename_len = strlen(filename); |
| 12937 | |
| 12938 | /* The +1's are for the path separator and the NUL */ |
| 12939 | size = path_len + 1 + filename_len + 1; |
| 12940 | result = PyMem_New(char, size); |
| 12941 | if (!result) { |
| 12942 | PyErr_NoMemory(); |
| 12943 | return NULL; |
| 12944 | } |
| 12945 | strcpy(result, path_narrow); |
| 12946 | if (path_len > 0 && result[path_len - 1] != '/') |
| 12947 | result[path_len++] = '/'; |
| 12948 | strcpy(result + path_len, filename); |
| 12949 | return result; |
| 12950 | } |
| 12951 | |
| 12952 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12953 | DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12954 | ino_t d_ino |
| 12955 | #ifdef HAVE_DIRENT_D_TYPE |
| 12956 | , unsigned char d_type |
| 12957 | #endif |
| 12958 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12959 | { |
| 12960 | DirEntry *entry; |
| 12961 | char *joined_path; |
| 12962 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12963 | PyObject *DirEntryType = _posixstate_global->DirEntryType; |
| 12964 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12965 | if (!entry) |
| 12966 | return NULL; |
| 12967 | entry->name = NULL; |
| 12968 | entry->path = NULL; |
| 12969 | entry->stat = NULL; |
| 12970 | entry->lstat = NULL; |
| 12971 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12972 | if (path->fd != -1) { |
| 12973 | entry->dir_fd = path->fd; |
| 12974 | joined_path = NULL; |
| 12975 | } |
| 12976 | else { |
| 12977 | entry->dir_fd = DEFAULT_DIR_FD; |
| 12978 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 12979 | if (!joined_path) |
| 12980 | goto error; |
| 12981 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12982 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 12983 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12984 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12985 | if (joined_path) |
| 12986 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12987 | } |
| 12988 | else { |
| 12989 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12990 | if (joined_path) |
| 12991 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12992 | } |
| 12993 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12994 | if (!entry->name) |
| 12995 | goto error; |
| 12996 | |
| 12997 | if (path->fd != -1) { |
| 12998 | entry->path = entry->name; |
| 12999 | Py_INCREF(entry->path); |
| 13000 | } |
| 13001 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13002 | goto error; |
| 13003 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13004 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13005 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13006 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13007 | entry->d_ino = d_ino; |
| 13008 | |
| 13009 | return (PyObject *)entry; |
| 13010 | |
| 13011 | error: |
| 13012 | Py_XDECREF(entry); |
| 13013 | return NULL; |
| 13014 | } |
| 13015 | |
| 13016 | #endif |
| 13017 | |
| 13018 | |
| 13019 | typedef struct { |
| 13020 | PyObject_HEAD |
| 13021 | path_t path; |
| 13022 | #ifdef MS_WINDOWS |
| 13023 | HANDLE handle; |
| 13024 | WIN32_FIND_DATAW file_data; |
| 13025 | int first_time; |
| 13026 | #else /* POSIX */ |
| 13027 | DIR *dirp; |
| 13028 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13029 | #ifdef HAVE_FDOPENDIR |
| 13030 | int fd; |
| 13031 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13032 | } ScandirIterator; |
| 13033 | |
| 13034 | #ifdef MS_WINDOWS |
| 13035 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13036 | static int |
| 13037 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13038 | { |
| 13039 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 13040 | } |
| 13041 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13042 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13043 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13044 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13045 | HANDLE handle = iterator->handle; |
| 13046 | |
| 13047 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13048 | return; |
| 13049 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13050 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13051 | Py_BEGIN_ALLOW_THREADS |
| 13052 | FindClose(handle); |
| 13053 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13054 | } |
| 13055 | |
| 13056 | static PyObject * |
| 13057 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13058 | { |
| 13059 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 13060 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13061 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13062 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13063 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13064 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13065 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13066 | |
| 13067 | while (1) { |
| 13068 | if (!iterator->first_time) { |
| 13069 | Py_BEGIN_ALLOW_THREADS |
| 13070 | success = FindNextFileW(iterator->handle, file_data); |
| 13071 | Py_END_ALLOW_THREADS |
| 13072 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13073 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13074 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13075 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13076 | break; |
| 13077 | } |
| 13078 | } |
| 13079 | iterator->first_time = 0; |
| 13080 | |
| 13081 | /* Skip over . and .. */ |
| 13082 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13083 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 13084 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 13085 | if (!entry) |
| 13086 | break; |
| 13087 | return entry; |
| 13088 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13089 | |
| 13090 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13091 | } |
| 13092 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13093 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13094 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13095 | return NULL; |
| 13096 | } |
| 13097 | |
| 13098 | #else /* POSIX */ |
| 13099 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13100 | static int |
| 13101 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13102 | { |
| 13103 | return !iterator->dirp; |
| 13104 | } |
| 13105 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13106 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13107 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13108 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13109 | DIR *dirp = iterator->dirp; |
| 13110 | |
| 13111 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13112 | return; |
| 13113 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13114 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13115 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13116 | #ifdef HAVE_FDOPENDIR |
| 13117 | if (iterator->path.fd != -1) |
| 13118 | rewinddir(dirp); |
| 13119 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13120 | closedir(dirp); |
| 13121 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13122 | return; |
| 13123 | } |
| 13124 | |
| 13125 | static PyObject * |
| 13126 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13127 | { |
| 13128 | struct dirent *direntp; |
| 13129 | Py_ssize_t name_len; |
| 13130 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13131 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13132 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13133 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13134 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13135 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13136 | |
| 13137 | while (1) { |
| 13138 | errno = 0; |
| 13139 | Py_BEGIN_ALLOW_THREADS |
| 13140 | direntp = readdir(iterator->dirp); |
| 13141 | Py_END_ALLOW_THREADS |
| 13142 | |
| 13143 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13144 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13145 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13146 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13147 | break; |
| 13148 | } |
| 13149 | |
| 13150 | /* Skip over . and .. */ |
| 13151 | name_len = NAMLEN(direntp); |
| 13152 | is_dot = direntp->d_name[0] == '.' && |
| 13153 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 13154 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13155 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13156 | name_len, direntp->d_ino |
| 13157 | #ifdef HAVE_DIRENT_D_TYPE |
| 13158 | , direntp->d_type |
| 13159 | #endif |
| 13160 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13161 | if (!entry) |
| 13162 | break; |
| 13163 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13164 | } |
| 13165 | |
| 13166 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13167 | } |
| 13168 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13169 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13170 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13171 | return NULL; |
| 13172 | } |
| 13173 | |
| 13174 | #endif |
| 13175 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13176 | static PyObject * |
| 13177 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 13178 | { |
| 13179 | ScandirIterator_closedir(self); |
| 13180 | Py_RETURN_NONE; |
| 13181 | } |
| 13182 | |
| 13183 | static PyObject * |
| 13184 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 13185 | { |
| 13186 | Py_INCREF(self); |
| 13187 | return self; |
| 13188 | } |
| 13189 | |
| 13190 | static PyObject * |
| 13191 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 13192 | { |
| 13193 | ScandirIterator_closedir(self); |
| 13194 | Py_RETURN_NONE; |
| 13195 | } |
| 13196 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13197 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13198 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 13199 | { |
| 13200 | PyObject *error_type, *error_value, *error_traceback; |
| 13201 | |
| 13202 | /* Save the current exception, if any. */ |
| 13203 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 13204 | |
| 13205 | if (!ScandirIterator_is_closed(iterator)) { |
| 13206 | ScandirIterator_closedir(iterator); |
| 13207 | |
| 13208 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 13209 | "unclosed scandir iterator %R", iterator)) { |
| 13210 | /* Spurious errors can appear at shutdown */ |
| 13211 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 13212 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 13213 | } |
| 13214 | } |
| 13215 | } |
| 13216 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13217 | path_cleanup(&iterator->path); |
| 13218 | |
| 13219 | /* Restore the saved exception. */ |
| 13220 | PyErr_Restore(error_type, error_value, error_traceback); |
| 13221 | } |
| 13222 | |
| 13223 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13224 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 13225 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13226 | PyTypeObject *tp = Py_TYPE(iterator); |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13227 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 13228 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13229 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13230 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 13231 | free_func(iterator); |
| 13232 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13233 | } |
| 13234 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13235 | static PyMethodDef ScandirIterator_methods[] = { |
| 13236 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 13237 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 13238 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 13239 | {NULL} |
| 13240 | }; |
| 13241 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13242 | static PyType_Slot ScandirIteratorType_slots[] = { |
| 13243 | {Py_tp_new, _disabled_new}, |
| 13244 | {Py_tp_dealloc, ScandirIterator_dealloc}, |
| 13245 | {Py_tp_finalize, ScandirIterator_finalize}, |
| 13246 | {Py_tp_iter, PyObject_SelfIter}, |
| 13247 | {Py_tp_iternext, ScandirIterator_iternext}, |
| 13248 | {Py_tp_methods, ScandirIterator_methods}, |
| 13249 | {0, 0}, |
| 13250 | }; |
| 13251 | |
| 13252 | static PyType_Spec ScandirIteratorType_spec = { |
| 13253 | MODNAME ".ScandirIterator", |
| 13254 | sizeof(ScandirIterator), |
| 13255 | 0, |
| 13256 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 13257 | ScandirIteratorType_slots |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13258 | }; |
| 13259 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13260 | /*[clinic input] |
| 13261 | os.scandir |
| 13262 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13263 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13264 | |
| 13265 | Return an iterator of DirEntry objects for given path. |
| 13266 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13267 | path can be specified as either str, bytes, or a path-like object. If path |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13268 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 13269 | all other circumstances they will be str. |
| 13270 | |
| 13271 | If path is None, uses the path='.'. |
| 13272 | [clinic start generated code]*/ |
| 13273 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13274 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13275 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13276 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13277 | { |
| 13278 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13279 | #ifdef MS_WINDOWS |
| 13280 | wchar_t *path_strW; |
| 13281 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13282 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13283 | #ifdef HAVE_FDOPENDIR |
| 13284 | int fd = -1; |
| 13285 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13286 | #endif |
| 13287 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 13288 | if (PySys_Audit("os.scandir", "O", |
| 13289 | path->object ? path->object : Py_None) < 0) { |
| 13290 | return NULL; |
| 13291 | } |
| 13292 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13293 | PyObject *ScandirIteratorType = _posixstate(module)->ScandirIteratorType; |
| 13294 | iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13295 | if (!iterator) |
| 13296 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13297 | |
| 13298 | #ifdef MS_WINDOWS |
| 13299 | iterator->handle = INVALID_HANDLE_VALUE; |
| 13300 | #else |
| 13301 | iterator->dirp = NULL; |
| 13302 | #endif |
| 13303 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13304 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 13305 | /* Move the ownership to iterator->path */ |
| 13306 | path->object = NULL; |
| 13307 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13308 | |
| 13309 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13310 | iterator->first_time = 1; |
| 13311 | |
| 13312 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 13313 | if (!path_strW) |
| 13314 | goto error; |
| 13315 | |
| 13316 | Py_BEGIN_ALLOW_THREADS |
| 13317 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 13318 | Py_END_ALLOW_THREADS |
| 13319 | |
| 13320 | PyMem_Free(path_strW); |
| 13321 | |
| 13322 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 13323 | path_error(&iterator->path); |
| 13324 | goto error; |
| 13325 | } |
| 13326 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13327 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13328 | #ifdef HAVE_FDOPENDIR |
| 13329 | if (path->fd != -1) { |
| 13330 | /* closedir() closes the FD, so we duplicate it */ |
| 13331 | fd = _Py_dup(path->fd); |
| 13332 | if (fd == -1) |
| 13333 | goto error; |
| 13334 | |
| 13335 | Py_BEGIN_ALLOW_THREADS |
| 13336 | iterator->dirp = fdopendir(fd); |
| 13337 | Py_END_ALLOW_THREADS |
| 13338 | } |
| 13339 | else |
| 13340 | #endif |
| 13341 | { |
| 13342 | if (iterator->path.narrow) |
| 13343 | path_str = iterator->path.narrow; |
| 13344 | else |
| 13345 | path_str = "."; |
| 13346 | |
| 13347 | Py_BEGIN_ALLOW_THREADS |
| 13348 | iterator->dirp = opendir(path_str); |
| 13349 | Py_END_ALLOW_THREADS |
| 13350 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13351 | |
| 13352 | if (!iterator->dirp) { |
| 13353 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13354 | #ifdef HAVE_FDOPENDIR |
| 13355 | if (fd != -1) { |
| 13356 | Py_BEGIN_ALLOW_THREADS |
| 13357 | close(fd); |
| 13358 | Py_END_ALLOW_THREADS |
| 13359 | } |
| 13360 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13361 | goto error; |
| 13362 | } |
| 13363 | #endif |
| 13364 | |
| 13365 | return (PyObject *)iterator; |
| 13366 | |
| 13367 | error: |
| 13368 | Py_DECREF(iterator); |
| 13369 | return NULL; |
| 13370 | } |
| 13371 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13372 | /* |
| 13373 | Return the file system path representation of the object. |
| 13374 | |
| 13375 | If the object is str or bytes, then allow it to pass through with |
| 13376 | an incremented refcount. If the object defines __fspath__(), then |
| 13377 | return the result of that method. All other types raise a TypeError. |
| 13378 | */ |
| 13379 | PyObject * |
| 13380 | PyOS_FSPath(PyObject *path) |
| 13381 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13382 | /* For error message reasons, this function is manually inlined in |
| 13383 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13384 | PyObject *func = NULL; |
| 13385 | PyObject *path_repr = NULL; |
| 13386 | |
| 13387 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 13388 | Py_INCREF(path); |
| 13389 | return path; |
| 13390 | } |
| 13391 | |
| 13392 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 13393 | if (NULL == func) { |
| 13394 | return PyErr_Format(PyExc_TypeError, |
| 13395 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13396 | "not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13397 | _PyType_Name(Py_TYPE(path))); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13398 | } |
| 13399 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13400 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13401 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13402 | if (NULL == path_repr) { |
| 13403 | return NULL; |
| 13404 | } |
| 13405 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13406 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 13407 | PyErr_Format(PyExc_TypeError, |
| 13408 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13409 | "not %.200s", _PyType_Name(Py_TYPE(path)), |
| 13410 | _PyType_Name(Py_TYPE(path_repr))); |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13411 | Py_DECREF(path_repr); |
| 13412 | return NULL; |
| 13413 | } |
| 13414 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13415 | return path_repr; |
| 13416 | } |
| 13417 | |
| 13418 | /*[clinic input] |
| 13419 | os.fspath |
| 13420 | |
| 13421 | path: object |
| 13422 | |
| 13423 | Return the file system path representation of the object. |
| 13424 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13425 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 13426 | object defines __fspath__(), then return the result of that method. All other |
| 13427 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13428 | [clinic start generated code]*/ |
| 13429 | |
| 13430 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13431 | os_fspath_impl(PyObject *module, PyObject *path) |
| 13432 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13433 | { |
| 13434 | return PyOS_FSPath(path); |
| 13435 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13436 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13437 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13438 | /*[clinic input] |
| 13439 | os.getrandom |
| 13440 | |
| 13441 | size: Py_ssize_t |
| 13442 | flags: int=0 |
| 13443 | |
| 13444 | Obtain a series of random bytes. |
| 13445 | [clinic start generated code]*/ |
| 13446 | |
| 13447 | static PyObject * |
| 13448 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 13449 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 13450 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13451 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13452 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13453 | |
| 13454 | if (size < 0) { |
| 13455 | errno = EINVAL; |
| 13456 | return posix_error(); |
| 13457 | } |
| 13458 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13459 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13460 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13461 | PyErr_NoMemory(); |
| 13462 | return NULL; |
| 13463 | } |
| 13464 | |
| 13465 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13466 | n = syscall(SYS_getrandom, |
| 13467 | PyBytes_AS_STRING(bytes), |
| 13468 | PyBytes_GET_SIZE(bytes), |
| 13469 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13470 | if (n < 0 && errno == EINTR) { |
| 13471 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13472 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13473 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13474 | |
| 13475 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13476 | continue; |
| 13477 | } |
| 13478 | break; |
| 13479 | } |
| 13480 | |
| 13481 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13482 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13483 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13484 | } |
| 13485 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13486 | if (n != size) { |
| 13487 | _PyBytes_Resize(&bytes, n); |
| 13488 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13489 | |
| 13490 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13491 | |
| 13492 | error: |
| 13493 | Py_DECREF(bytes); |
| 13494 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13495 | } |
| 13496 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13497 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13498 | #ifdef MS_WINDOWS |
| 13499 | /* bpo-36085: Helper functions for managing DLL search directories |
| 13500 | * on win32 |
| 13501 | */ |
| 13502 | |
| 13503 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); |
| 13504 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); |
| 13505 | |
| 13506 | /*[clinic input] |
| 13507 | os._add_dll_directory |
| 13508 | |
| 13509 | path: path_t |
| 13510 | |
| 13511 | Add a path to the DLL search path. |
| 13512 | |
| 13513 | This search path is used when resolving dependencies for imported |
| 13514 | extension modules (the module itself is resolved through sys.path), |
| 13515 | and also by ctypes. |
| 13516 | |
| 13517 | Returns an opaque value that may be passed to os.remove_dll_directory |
| 13518 | to remove this directory from the search path. |
| 13519 | [clinic start generated code]*/ |
| 13520 | |
| 13521 | static PyObject * |
| 13522 | os__add_dll_directory_impl(PyObject *module, path_t *path) |
| 13523 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ |
| 13524 | { |
| 13525 | HMODULE hKernel32; |
| 13526 | PAddDllDirectory AddDllDirectory; |
| 13527 | DLL_DIRECTORY_COOKIE cookie = 0; |
| 13528 | DWORD err = 0; |
| 13529 | |
| 13530 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13531 | infrequent operation, just do it each time. Kernel32 is always |
| 13532 | loaded. */ |
| 13533 | Py_BEGIN_ALLOW_THREADS |
| 13534 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13535 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( |
| 13536 | hKernel32, "AddDllDirectory")) || |
| 13537 | !(cookie = (*AddDllDirectory)(path->wide))) { |
| 13538 | err = GetLastError(); |
| 13539 | } |
| 13540 | Py_END_ALLOW_THREADS |
| 13541 | |
| 13542 | if (err) { |
| 13543 | return win32_error_object_err("add_dll_directory", |
| 13544 | path->object, err); |
| 13545 | } |
| 13546 | |
| 13547 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); |
| 13548 | } |
| 13549 | |
| 13550 | /*[clinic input] |
| 13551 | os._remove_dll_directory |
| 13552 | |
| 13553 | cookie: object |
| 13554 | |
| 13555 | Removes a path from the DLL search path. |
| 13556 | |
| 13557 | The parameter is an opaque value that was returned from |
| 13558 | os.add_dll_directory. You can only remove directories that you added |
| 13559 | yourself. |
| 13560 | [clinic start generated code]*/ |
| 13561 | |
| 13562 | static PyObject * |
| 13563 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) |
| 13564 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ |
| 13565 | { |
| 13566 | HMODULE hKernel32; |
| 13567 | PRemoveDllDirectory RemoveDllDirectory; |
| 13568 | DLL_DIRECTORY_COOKIE cookieValue; |
| 13569 | DWORD err = 0; |
| 13570 | |
| 13571 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { |
| 13572 | PyErr_SetString(PyExc_TypeError, |
| 13573 | "Provided cookie was not returned from os.add_dll_directory"); |
| 13574 | return NULL; |
| 13575 | } |
| 13576 | |
| 13577 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( |
| 13578 | cookie, "DLL directory cookie"); |
| 13579 | |
| 13580 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13581 | infrequent operation, just do it each time. Kernel32 is always |
| 13582 | loaded. */ |
| 13583 | Py_BEGIN_ALLOW_THREADS |
| 13584 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13585 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( |
| 13586 | hKernel32, "RemoveDllDirectory")) || |
| 13587 | !(*RemoveDllDirectory)(cookieValue)) { |
| 13588 | err = GetLastError(); |
| 13589 | } |
| 13590 | Py_END_ALLOW_THREADS |
| 13591 | |
| 13592 | if (err) { |
| 13593 | return win32_error_object_err("remove_dll_directory", |
| 13594 | NULL, err); |
| 13595 | } |
| 13596 | |
| 13597 | if (PyCapsule_SetName(cookie, NULL)) { |
| 13598 | return NULL; |
| 13599 | } |
| 13600 | |
| 13601 | Py_RETURN_NONE; |
| 13602 | } |
| 13603 | |
| 13604 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13605 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13606 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13607 | |
| 13608 | OS_STAT_METHODDEF |
| 13609 | OS_ACCESS_METHODDEF |
| 13610 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13611 | OS_CHDIR_METHODDEF |
| 13612 | OS_CHFLAGS_METHODDEF |
| 13613 | OS_CHMOD_METHODDEF |
| 13614 | OS_FCHMOD_METHODDEF |
| 13615 | OS_LCHMOD_METHODDEF |
| 13616 | OS_CHOWN_METHODDEF |
| 13617 | OS_FCHOWN_METHODDEF |
| 13618 | OS_LCHOWN_METHODDEF |
| 13619 | OS_LCHFLAGS_METHODDEF |
| 13620 | OS_CHROOT_METHODDEF |
| 13621 | OS_CTERMID_METHODDEF |
| 13622 | OS_GETCWD_METHODDEF |
| 13623 | OS_GETCWDB_METHODDEF |
| 13624 | OS_LINK_METHODDEF |
| 13625 | OS_LISTDIR_METHODDEF |
| 13626 | OS_LSTAT_METHODDEF |
| 13627 | OS_MKDIR_METHODDEF |
| 13628 | OS_NICE_METHODDEF |
| 13629 | OS_GETPRIORITY_METHODDEF |
| 13630 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13631 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 13632 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13633 | OS_READLINK_METHODDEF |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 13634 | OS_COPY_FILE_RANGE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13635 | OS_RENAME_METHODDEF |
| 13636 | OS_REPLACE_METHODDEF |
| 13637 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13638 | OS_SYMLINK_METHODDEF |
| 13639 | OS_SYSTEM_METHODDEF |
| 13640 | OS_UMASK_METHODDEF |
| 13641 | OS_UNAME_METHODDEF |
| 13642 | OS_UNLINK_METHODDEF |
| 13643 | OS_REMOVE_METHODDEF |
| 13644 | OS_UTIME_METHODDEF |
| 13645 | OS_TIMES_METHODDEF |
| 13646 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13647 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13648 | OS_EXECV_METHODDEF |
| 13649 | OS_EXECVE_METHODDEF |
| 13650 | OS_SPAWNV_METHODDEF |
| 13651 | OS_SPAWNVE_METHODDEF |
| 13652 | OS_FORK1_METHODDEF |
| 13653 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 13654 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13655 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 13656 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 13657 | OS_SCHED_GETPARAM_METHODDEF |
| 13658 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 13659 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 13660 | OS_SCHED_SETPARAM_METHODDEF |
| 13661 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 13662 | OS_SCHED_YIELD_METHODDEF |
| 13663 | OS_SCHED_SETAFFINITY_METHODDEF |
| 13664 | OS_SCHED_GETAFFINITY_METHODDEF |
| 13665 | OS_OPENPTY_METHODDEF |
| 13666 | OS_FORKPTY_METHODDEF |
| 13667 | OS_GETEGID_METHODDEF |
| 13668 | OS_GETEUID_METHODDEF |
| 13669 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 13670 | #ifdef HAVE_GETGROUPLIST |
| 13671 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 13672 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13673 | OS_GETGROUPS_METHODDEF |
| 13674 | OS_GETPID_METHODDEF |
| 13675 | OS_GETPGRP_METHODDEF |
| 13676 | OS_GETPPID_METHODDEF |
| 13677 | OS_GETUID_METHODDEF |
| 13678 | OS_GETLOGIN_METHODDEF |
| 13679 | OS_KILL_METHODDEF |
| 13680 | OS_KILLPG_METHODDEF |
| 13681 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13682 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13683 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13684 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13685 | OS_SETUID_METHODDEF |
| 13686 | OS_SETEUID_METHODDEF |
| 13687 | OS_SETREUID_METHODDEF |
| 13688 | OS_SETGID_METHODDEF |
| 13689 | OS_SETEGID_METHODDEF |
| 13690 | OS_SETREGID_METHODDEF |
| 13691 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13692 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13693 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13694 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13695 | OS_GETPGID_METHODDEF |
| 13696 | OS_SETPGRP_METHODDEF |
| 13697 | OS_WAIT_METHODDEF |
| 13698 | OS_WAIT3_METHODDEF |
| 13699 | OS_WAIT4_METHODDEF |
| 13700 | OS_WAITID_METHODDEF |
| 13701 | OS_WAITPID_METHODDEF |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 13702 | OS_PIDFD_OPEN_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13703 | OS_GETSID_METHODDEF |
| 13704 | OS_SETSID_METHODDEF |
| 13705 | OS_SETPGID_METHODDEF |
| 13706 | OS_TCGETPGRP_METHODDEF |
| 13707 | OS_TCSETPGRP_METHODDEF |
| 13708 | OS_OPEN_METHODDEF |
| 13709 | OS_CLOSE_METHODDEF |
| 13710 | OS_CLOSERANGE_METHODDEF |
| 13711 | OS_DEVICE_ENCODING_METHODDEF |
| 13712 | OS_DUP_METHODDEF |
| 13713 | OS_DUP2_METHODDEF |
| 13714 | OS_LOCKF_METHODDEF |
| 13715 | OS_LSEEK_METHODDEF |
| 13716 | OS_READ_METHODDEF |
| 13717 | OS_READV_METHODDEF |
| 13718 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13719 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13720 | OS_WRITE_METHODDEF |
| 13721 | OS_WRITEV_METHODDEF |
| 13722 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13723 | OS_PWRITEV_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13724 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 13725 | {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13726 | posix_sendfile__doc__}, |
| 13727 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13728 | OS_FSTAT_METHODDEF |
| 13729 | OS_ISATTY_METHODDEF |
| 13730 | OS_PIPE_METHODDEF |
| 13731 | OS_PIPE2_METHODDEF |
| 13732 | OS_MKFIFO_METHODDEF |
| 13733 | OS_MKNOD_METHODDEF |
| 13734 | OS_MAJOR_METHODDEF |
| 13735 | OS_MINOR_METHODDEF |
| 13736 | OS_MAKEDEV_METHODDEF |
| 13737 | OS_FTRUNCATE_METHODDEF |
| 13738 | OS_TRUNCATE_METHODDEF |
| 13739 | OS_POSIX_FALLOCATE_METHODDEF |
| 13740 | OS_POSIX_FADVISE_METHODDEF |
| 13741 | OS_PUTENV_METHODDEF |
| 13742 | OS_UNSETENV_METHODDEF |
| 13743 | OS_STRERROR_METHODDEF |
| 13744 | OS_FCHDIR_METHODDEF |
| 13745 | OS_FSYNC_METHODDEF |
| 13746 | OS_SYNC_METHODDEF |
| 13747 | OS_FDATASYNC_METHODDEF |
| 13748 | OS_WCOREDUMP_METHODDEF |
| 13749 | OS_WIFCONTINUED_METHODDEF |
| 13750 | OS_WIFSTOPPED_METHODDEF |
| 13751 | OS_WIFSIGNALED_METHODDEF |
| 13752 | OS_WIFEXITED_METHODDEF |
| 13753 | OS_WEXITSTATUS_METHODDEF |
| 13754 | OS_WTERMSIG_METHODDEF |
| 13755 | OS_WSTOPSIG_METHODDEF |
| 13756 | OS_FSTATVFS_METHODDEF |
| 13757 | OS_STATVFS_METHODDEF |
| 13758 | OS_CONFSTR_METHODDEF |
| 13759 | OS_SYSCONF_METHODDEF |
| 13760 | OS_FPATHCONF_METHODDEF |
| 13761 | OS_PATHCONF_METHODDEF |
| 13762 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 13763 | OS__GETFULLPATHNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13764 | OS__GETDISKUSAGE_METHODDEF |
| 13765 | OS__GETFINALPATHNAME_METHODDEF |
| 13766 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 13767 | OS_GETLOADAVG_METHODDEF |
| 13768 | OS_URANDOM_METHODDEF |
| 13769 | OS_SETRESUID_METHODDEF |
| 13770 | OS_SETRESGID_METHODDEF |
| 13771 | OS_GETRESUID_METHODDEF |
| 13772 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 13773 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13774 | OS_GETXATTR_METHODDEF |
| 13775 | OS_SETXATTR_METHODDEF |
| 13776 | OS_REMOVEXATTR_METHODDEF |
| 13777 | OS_LISTXATTR_METHODDEF |
| 13778 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13779 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 13780 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 13781 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13782 | OS_CPU_COUNT_METHODDEF |
| 13783 | OS_GET_INHERITABLE_METHODDEF |
| 13784 | OS_SET_INHERITABLE_METHODDEF |
| 13785 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 13786 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13787 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13788 | OS_GET_BLOCKING_METHODDEF |
| 13789 | OS_SET_BLOCKING_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13790 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13791 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13792 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13793 | OS_GETRANDOM_METHODDEF |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 13794 | OS_MEMFD_CREATE_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13795 | #ifdef MS_WINDOWS |
| 13796 | OS__ADD_DLL_DIRECTORY_METHODDEF |
| 13797 | OS__REMOVE_DLL_DIRECTORY_METHODDEF |
| 13798 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13799 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13800 | }; |
| 13801 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13802 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13803 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13804 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13805 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13806 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13807 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13808 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13809 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13810 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13811 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13812 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13813 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13814 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13815 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13816 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13817 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13818 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13819 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13820 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13821 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13822 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13823 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13824 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13825 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13826 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13827 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13828 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13829 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13830 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13831 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13832 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13833 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13834 | #endif |
| 13835 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13836 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13837 | #endif |
| 13838 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13839 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13840 | #endif |
| 13841 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13842 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13843 | #endif |
| 13844 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13845 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13846 | #endif |
| 13847 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13848 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13849 | #endif |
| 13850 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13851 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13852 | #endif |
| 13853 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13854 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13855 | #endif |
| 13856 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13857 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13858 | #endif |
| 13859 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13860 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13861 | #endif |
| 13862 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13863 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13864 | #endif |
| 13865 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13866 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13867 | #endif |
| 13868 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13869 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13870 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13871 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13872 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13873 | #endif |
| 13874 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13875 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13876 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13877 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13878 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13879 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13880 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13881 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13882 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13883 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13884 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13885 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13886 | #endif |
| 13887 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13888 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13889 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13890 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13891 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13892 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13893 | #endif |
| 13894 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13895 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13896 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13897 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13898 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13899 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13900 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13901 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13902 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 13903 | #ifdef O_TMPFILE |
| 13904 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 13905 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13906 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13907 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13908 | #endif |
| 13909 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13910 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13911 | #endif |
| 13912 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13913 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13914 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13915 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13916 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13917 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13918 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13919 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13920 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13921 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13922 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13923 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13924 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13925 | #endif |
| 13926 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13927 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13928 | #endif |
| 13929 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13930 | /* MS Windows */ |
| 13931 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13932 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13933 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13934 | #endif |
| 13935 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13936 | /* Optimize for short life (keep in memory). */ |
| 13937 | /* MS forgot to define this one with a non-underscore form too. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13938 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13939 | #endif |
| 13940 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13941 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13942 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13943 | #endif |
| 13944 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13945 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13946 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13947 | #endif |
| 13948 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13949 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13950 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13951 | #endif |
| 13952 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13953 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13954 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13955 | /* Send a SIGIO signal whenever input or output |
| 13956 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13957 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 13958 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13959 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13960 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13961 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13962 | #endif |
| 13963 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13964 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13965 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13966 | #endif |
| 13967 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13968 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13969 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13970 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13971 | #ifdef O_NOLINKS |
| 13972 | /* Fails if link count of the named file is greater than 1 */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13973 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13974 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13975 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13976 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13977 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 13978 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 13979 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13980 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13981 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13982 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13983 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13984 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13985 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13986 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13987 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13988 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13989 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13990 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13991 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13992 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13993 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13994 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13995 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13996 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13997 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 13998 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 13999 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14000 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14001 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14002 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14003 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14004 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14005 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14006 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14007 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14008 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14009 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14010 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14011 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14012 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14013 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14014 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14015 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14016 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14017 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14018 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14019 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14020 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14021 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14022 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14023 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14024 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14025 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14026 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14027 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14028 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14029 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14030 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14031 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14032 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 14033 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14034 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14035 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14036 | #endif /* ST_RDONLY */ |
| 14037 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14038 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14039 | #endif /* ST_NOSUID */ |
| 14040 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 14041 | /* GNU extensions */ |
| 14042 | #ifdef ST_NODEV |
| 14043 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 14044 | #endif /* ST_NODEV */ |
| 14045 | #ifdef ST_NOEXEC |
| 14046 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 14047 | #endif /* ST_NOEXEC */ |
| 14048 | #ifdef ST_SYNCHRONOUS |
| 14049 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 14050 | #endif /* ST_SYNCHRONOUS */ |
| 14051 | #ifdef ST_MANDLOCK |
| 14052 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 14053 | #endif /* ST_MANDLOCK */ |
| 14054 | #ifdef ST_WRITE |
| 14055 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 14056 | #endif /* ST_WRITE */ |
| 14057 | #ifdef ST_APPEND |
| 14058 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 14059 | #endif /* ST_APPEND */ |
| 14060 | #ifdef ST_NOATIME |
| 14061 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 14062 | #endif /* ST_NOATIME */ |
| 14063 | #ifdef ST_NODIRATIME |
| 14064 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 14065 | #endif /* ST_NODIRATIME */ |
| 14066 | #ifdef ST_RELATIME |
| 14067 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 14068 | #endif /* ST_RELATIME */ |
| 14069 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14070 | /* FreeBSD sendfile() constants */ |
| 14071 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14072 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14073 | #endif |
| 14074 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14075 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14076 | #endif |
| 14077 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14078 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14079 | #endif |
| 14080 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14081 | /* constants for posix_fadvise */ |
| 14082 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14083 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14084 | #endif |
| 14085 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14086 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14087 | #endif |
| 14088 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14089 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14090 | #endif |
| 14091 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14092 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14093 | #endif |
| 14094 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14095 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14096 | #endif |
| 14097 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14098 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14099 | #endif |
| 14100 | |
| 14101 | /* constants for waitid */ |
| 14102 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14103 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 14104 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 14105 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 14106 | #ifdef P_PIDFD |
| 14107 | if (PyModule_AddIntMacro(m, P_PIDFD)) return -1; |
| 14108 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14109 | #endif |
| 14110 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14111 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14112 | #endif |
| 14113 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14114 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14115 | #endif |
| 14116 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14117 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14118 | #endif |
| 14119 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14120 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14121 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14122 | #ifdef CLD_KILLED |
| 14123 | if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1; |
| 14124 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14125 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14126 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14127 | #endif |
| 14128 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14129 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14130 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14131 | #ifdef CLD_STOPPED |
| 14132 | if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1; |
| 14133 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14134 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14135 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14136 | #endif |
| 14137 | |
| 14138 | /* constants for lockf */ |
| 14139 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14140 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14141 | #endif |
| 14142 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14143 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14144 | #endif |
| 14145 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14146 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14147 | #endif |
| 14148 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14149 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14150 | #endif |
| 14151 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14152 | #ifdef RWF_DSYNC |
| 14153 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 14154 | #endif |
| 14155 | #ifdef RWF_HIPRI |
| 14156 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 14157 | #endif |
| 14158 | #ifdef RWF_SYNC |
| 14159 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 14160 | #endif |
| 14161 | #ifdef RWF_NOWAIT |
| 14162 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 14163 | #endif |
| 14164 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14165 | /* constants for posix_spawn */ |
| 14166 | #ifdef HAVE_POSIX_SPAWN |
| 14167 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 14168 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 14169 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 14170 | #endif |
| 14171 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14172 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14173 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 14174 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14175 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14176 | #endif |
| 14177 | #ifdef HAVE_SPAWNV |
| 14178 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14179 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 14180 | #endif |
| 14181 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14182 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14183 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14184 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14185 | #endif |
| 14186 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14187 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14188 | #endif |
| 14189 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14190 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14191 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14192 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 14193 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14194 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14195 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14196 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14197 | #endif |
| 14198 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14199 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14200 | #endif |
| 14201 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14202 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14203 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14204 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14205 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14206 | #endif |
| 14207 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14208 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14209 | #endif |
| 14210 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14211 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14212 | #endif |
| 14213 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14214 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14215 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14216 | #endif |
| 14217 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 14218 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14219 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 14220 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 14221 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 14222 | #endif |
| 14223 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14224 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14225 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14226 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14227 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14228 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14229 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14230 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14231 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14232 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14233 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14234 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14235 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14236 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14237 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14238 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14239 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14240 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14241 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14242 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14243 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14244 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 14245 | #if HAVE_DECL_RTLD_MEMBER |
| 14246 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 14247 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14248 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14249 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 14250 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 14251 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 14252 | #endif |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14253 | #ifdef HAVE_MEMFD_CREATE |
| 14254 | if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1; |
| 14255 | if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1; |
| 14256 | #ifdef MFD_HUGETLB |
| 14257 | if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14258 | #endif |
| 14259 | #ifdef MFD_HUGE_SHIFT |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14260 | if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14261 | #endif |
| 14262 | #ifdef MFD_HUGE_MASK |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14263 | if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14264 | #endif |
| 14265 | #ifdef MFD_HUGE_64KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14266 | if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14267 | #endif |
| 14268 | #ifdef MFD_HUGE_512KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14269 | if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14270 | #endif |
| 14271 | #ifdef MFD_HUGE_1MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14272 | if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14273 | #endif |
| 14274 | #ifdef MFD_HUGE_2MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14275 | if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14276 | #endif |
| 14277 | #ifdef MFD_HUGE_8MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14278 | if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14279 | #endif |
| 14280 | #ifdef MFD_HUGE_16MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14281 | if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14282 | #endif |
| 14283 | #ifdef MFD_HUGE_32MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14284 | if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14285 | #endif |
| 14286 | #ifdef MFD_HUGE_256MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14287 | if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14288 | #endif |
| 14289 | #ifdef MFD_HUGE_512MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14290 | if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14291 | #endif |
| 14292 | #ifdef MFD_HUGE_1GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14293 | if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14294 | #endif |
| 14295 | #ifdef MFD_HUGE_2GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14296 | if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14297 | #endif |
| 14298 | #ifdef MFD_HUGE_16GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14299 | if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1; |
| 14300 | #endif |
| 14301 | #endif |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14302 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14303 | #if defined(__APPLE__) |
| 14304 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 14305 | #endif |
| 14306 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14307 | #ifdef MS_WINDOWS |
| 14308 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; |
| 14309 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; |
| 14310 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; |
| 14311 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; |
| 14312 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; |
| 14313 | #endif |
| 14314 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14315 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14316 | } |
| 14317 | |
| 14318 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14319 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14320 | PyModuleDef_HEAD_INIT, |
| 14321 | MODNAME, |
| 14322 | posix__doc__, |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14323 | sizeof(_posixstate), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14324 | posix_methods, |
| 14325 | NULL, |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14326 | _posix_traverse, |
| 14327 | _posix_clear, |
| 14328 | _posix_free, |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14329 | }; |
| 14330 | |
| 14331 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14332 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14333 | |
| 14334 | #ifdef HAVE_FACCESSAT |
| 14335 | "HAVE_FACCESSAT", |
| 14336 | #endif |
| 14337 | |
| 14338 | #ifdef HAVE_FCHDIR |
| 14339 | "HAVE_FCHDIR", |
| 14340 | #endif |
| 14341 | |
| 14342 | #ifdef HAVE_FCHMOD |
| 14343 | "HAVE_FCHMOD", |
| 14344 | #endif |
| 14345 | |
| 14346 | #ifdef HAVE_FCHMODAT |
| 14347 | "HAVE_FCHMODAT", |
| 14348 | #endif |
| 14349 | |
| 14350 | #ifdef HAVE_FCHOWN |
| 14351 | "HAVE_FCHOWN", |
| 14352 | #endif |
| 14353 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 14354 | #ifdef HAVE_FCHOWNAT |
| 14355 | "HAVE_FCHOWNAT", |
| 14356 | #endif |
| 14357 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14358 | #ifdef HAVE_FEXECVE |
| 14359 | "HAVE_FEXECVE", |
| 14360 | #endif |
| 14361 | |
| 14362 | #ifdef HAVE_FDOPENDIR |
| 14363 | "HAVE_FDOPENDIR", |
| 14364 | #endif |
| 14365 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14366 | #ifdef HAVE_FPATHCONF |
| 14367 | "HAVE_FPATHCONF", |
| 14368 | #endif |
| 14369 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14370 | #ifdef HAVE_FSTATAT |
| 14371 | "HAVE_FSTATAT", |
| 14372 | #endif |
| 14373 | |
| 14374 | #ifdef HAVE_FSTATVFS |
| 14375 | "HAVE_FSTATVFS", |
| 14376 | #endif |
| 14377 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 14378 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14379 | "HAVE_FTRUNCATE", |
| 14380 | #endif |
| 14381 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14382 | #ifdef HAVE_FUTIMENS |
| 14383 | "HAVE_FUTIMENS", |
| 14384 | #endif |
| 14385 | |
| 14386 | #ifdef HAVE_FUTIMES |
| 14387 | "HAVE_FUTIMES", |
| 14388 | #endif |
| 14389 | |
| 14390 | #ifdef HAVE_FUTIMESAT |
| 14391 | "HAVE_FUTIMESAT", |
| 14392 | #endif |
| 14393 | |
| 14394 | #ifdef HAVE_LINKAT |
| 14395 | "HAVE_LINKAT", |
| 14396 | #endif |
| 14397 | |
| 14398 | #ifdef HAVE_LCHFLAGS |
| 14399 | "HAVE_LCHFLAGS", |
| 14400 | #endif |
| 14401 | |
| 14402 | #ifdef HAVE_LCHMOD |
| 14403 | "HAVE_LCHMOD", |
| 14404 | #endif |
| 14405 | |
| 14406 | #ifdef HAVE_LCHOWN |
| 14407 | "HAVE_LCHOWN", |
| 14408 | #endif |
| 14409 | |
| 14410 | #ifdef HAVE_LSTAT |
| 14411 | "HAVE_LSTAT", |
| 14412 | #endif |
| 14413 | |
| 14414 | #ifdef HAVE_LUTIMES |
| 14415 | "HAVE_LUTIMES", |
| 14416 | #endif |
| 14417 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14418 | #ifdef HAVE_MEMFD_CREATE |
| 14419 | "HAVE_MEMFD_CREATE", |
| 14420 | #endif |
| 14421 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14422 | #ifdef HAVE_MKDIRAT |
| 14423 | "HAVE_MKDIRAT", |
| 14424 | #endif |
| 14425 | |
| 14426 | #ifdef HAVE_MKFIFOAT |
| 14427 | "HAVE_MKFIFOAT", |
| 14428 | #endif |
| 14429 | |
| 14430 | #ifdef HAVE_MKNODAT |
| 14431 | "HAVE_MKNODAT", |
| 14432 | #endif |
| 14433 | |
| 14434 | #ifdef HAVE_OPENAT |
| 14435 | "HAVE_OPENAT", |
| 14436 | #endif |
| 14437 | |
| 14438 | #ifdef HAVE_READLINKAT |
| 14439 | "HAVE_READLINKAT", |
| 14440 | #endif |
| 14441 | |
| 14442 | #ifdef HAVE_RENAMEAT |
| 14443 | "HAVE_RENAMEAT", |
| 14444 | #endif |
| 14445 | |
| 14446 | #ifdef HAVE_SYMLINKAT |
| 14447 | "HAVE_SYMLINKAT", |
| 14448 | #endif |
| 14449 | |
| 14450 | #ifdef HAVE_UNLINKAT |
| 14451 | "HAVE_UNLINKAT", |
| 14452 | #endif |
| 14453 | |
| 14454 | #ifdef HAVE_UTIMENSAT |
| 14455 | "HAVE_UTIMENSAT", |
| 14456 | #endif |
| 14457 | |
| 14458 | #ifdef MS_WINDOWS |
| 14459 | "MS_WINDOWS", |
| 14460 | #endif |
| 14461 | |
| 14462 | NULL |
| 14463 | }; |
| 14464 | |
| 14465 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 14466 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 14467 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14468 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14469 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14470 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14471 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14472 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14473 | m = PyState_FindModule(&posixmodule); |
| 14474 | if (m != NULL) { |
| 14475 | Py_INCREF(m); |
| 14476 | return m; |
| 14477 | } |
| 14478 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14479 | m = PyModule_Create(&posixmodule); |
| 14480 | if (m == NULL) |
| 14481 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14482 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14483 | /* Initialize environ dictionary */ |
| 14484 | v = convertenviron(); |
| 14485 | Py_XINCREF(v); |
| 14486 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 14487 | return NULL; |
| 14488 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14489 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14490 | if (all_ins(m)) |
| 14491 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14492 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14493 | if (setup_confname_tables(m)) |
| 14494 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14495 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14496 | Py_INCREF(PyExc_OSError); |
| 14497 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 14498 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 14499 | #ifdef HAVE_PUTENV |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14500 | /* Save putenv() parameters as values here, so we can collect them when they |
| 14501 | * get re-set with another call for the same key. */ |
| 14502 | _posixstate(m)->posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 14503 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 14504 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14505 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14506 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 14507 | PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc); |
| 14508 | if (WaitidResultType == NULL) { |
| 14509 | return NULL; |
| 14510 | } |
| 14511 | Py_INCREF(WaitidResultType); |
| 14512 | PyModule_AddObject(m, "waitid_result", WaitidResultType); |
| 14513 | _posixstate(m)->WaitidResultType = WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14514 | #endif |
| 14515 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14516 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
| 14517 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 14518 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 14519 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 14520 | PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc); |
| 14521 | if (StatResultType == NULL) { |
| 14522 | return NULL; |
| 14523 | } |
| 14524 | Py_INCREF(StatResultType); |
| 14525 | PyModule_AddObject(m, "stat_result", StatResultType); |
| 14526 | _posixstate(m)->StatResultType = StatResultType; |
| 14527 | structseq_new = ((PyTypeObject *)StatResultType)->tp_new; |
| 14528 | ((PyTypeObject *)StatResultType)->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14529 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14530 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
| 14531 | PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc); |
| 14532 | if (StatVFSResultType == NULL) { |
| 14533 | return NULL; |
| 14534 | } |
| 14535 | Py_INCREF(StatVFSResultType); |
| 14536 | PyModule_AddObject(m, "statvfs_result", StatVFSResultType); |
| 14537 | _posixstate(m)->StatVFSResultType = StatVFSResultType; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14538 | #ifdef NEED_TICKS_PER_SECOND |
| 14539 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14540 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14541 | # elif defined(HZ) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14542 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14543 | # else |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14544 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14545 | # endif |
| 14546 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14547 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14548 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14549 | sched_param_desc.name = MODNAME ".sched_param"; |
| 14550 | PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc); |
| 14551 | if (SchedParamType == NULL) { |
| 14552 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14553 | } |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14554 | Py_INCREF(SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14555 | PyModule_AddObject(m, "sched_param", SchedParamType); |
| 14556 | _posixstate(m)->SchedParamType = SchedParamType; |
| 14557 | ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param; |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14558 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14559 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14560 | /* initialize TerminalSize_info */ |
| 14561 | PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc); |
| 14562 | if (TerminalSizeType == NULL) { |
| 14563 | return NULL; |
| 14564 | } |
| 14565 | Py_INCREF(TerminalSizeType); |
| 14566 | PyModule_AddObject(m, "terminal_size", TerminalSizeType); |
| 14567 | _posixstate(m)->TerminalSizeType = TerminalSizeType; |
| 14568 | |
| 14569 | /* initialize scandir types */ |
| 14570 | PyObject *ScandirIteratorType = PyType_FromSpec(&ScandirIteratorType_spec); |
| 14571 | if (ScandirIteratorType == NULL) { |
| 14572 | return NULL; |
| 14573 | } |
| 14574 | _posixstate(m)->ScandirIteratorType = ScandirIteratorType; |
| 14575 | |
| 14576 | PyObject *DirEntryType = PyType_FromSpec(&DirEntryType_spec); |
| 14577 | if (DirEntryType == NULL) { |
| 14578 | return NULL; |
| 14579 | } |
| 14580 | Py_INCREF(DirEntryType); |
| 14581 | PyModule_AddObject(m, "DirEntry", DirEntryType); |
| 14582 | _posixstate(m)->DirEntryType = DirEntryType; |
| 14583 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14584 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14585 | PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(×_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14586 | if (TimesResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14587 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14588 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14589 | Py_INCREF(TimesResultType); |
| 14590 | PyModule_AddObject(m, "times_result", TimesResultType); |
| 14591 | _posixstate(m)->TimesResultType = TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14592 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14593 | PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14594 | if (UnameResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14595 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14596 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14597 | Py_INCREF(UnameResultType); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14598 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14599 | _posixstate(m)->UnameResultType = (PyObject *)UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14600 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14601 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14602 | /* |
| 14603 | * Step 2 of weak-linking support on Mac OS X. |
| 14604 | * |
| 14605 | * The code below removes functions that are not available on the |
| 14606 | * currently active platform. |
| 14607 | * |
| 14608 | * This block allow one to use a python binary that was build on |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14609 | * OSX 10.4 on OSX 10.3, without losing access to new APIs on |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14610 | * OSX 10.4. |
| 14611 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14612 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14613 | if (fstatvfs == NULL) { |
| 14614 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 14615 | return NULL; |
| 14616 | } |
| 14617 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14618 | #endif /* HAVE_FSTATVFS */ |
| 14619 | |
| 14620 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14621 | if (statvfs == NULL) { |
| 14622 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 14623 | return NULL; |
| 14624 | } |
| 14625 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14626 | #endif /* HAVE_STATVFS */ |
| 14627 | |
| 14628 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14629 | if (lchown == NULL) { |
| 14630 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 14631 | return NULL; |
| 14632 | } |
| 14633 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14634 | #endif /* HAVE_LCHOWN */ |
| 14635 | |
| 14636 | |
| 14637 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14638 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14639 | if ((_posixstate(m)->billion = PyLong_FromLong(1000000000)) == NULL) |
| 14640 | return NULL; |
| 14641 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 14642 | _posixstate(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage"); |
| 14643 | if (_posixstate(m)->struct_rusage == NULL) |
| 14644 | return NULL; |
| 14645 | #endif |
| 14646 | _posixstate(m)->st_mode = PyUnicode_InternFromString("st_mode"); |
| 14647 | if (_posixstate(m)->st_mode == NULL) |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 14648 | return NULL; |
| 14649 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14650 | /* suppress "function not used" warnings */ |
| 14651 | { |
| 14652 | int ignored; |
| 14653 | fd_specified("", -1); |
| 14654 | follow_symlinks_specified("", 1); |
| 14655 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 14656 | dir_fd_converter(Py_None, &ignored); |
| 14657 | dir_fd_unavailable(Py_None, &ignored); |
| 14658 | } |
| 14659 | |
| 14660 | /* |
| 14661 | * provide list of locally available functions |
| 14662 | * so os.py can populate support_* lists |
| 14663 | */ |
| 14664 | list = PyList_New(0); |
| 14665 | if (!list) |
| 14666 | return NULL; |
| 14667 | for (trace = have_functions; *trace; trace++) { |
| 14668 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 14669 | if (!unicode) |
| 14670 | return NULL; |
| 14671 | if (PyList_Append(list, unicode)) |
| 14672 | return NULL; |
| 14673 | Py_DECREF(unicode); |
| 14674 | } |
| 14675 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 14676 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14677 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14678 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14679 | |
| 14680 | #ifdef __cplusplus |
| 14681 | } |
| 14682 | #endif |