Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* POSIX module implementation */ |
| 2 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 3 | /* This file is also used for Windows NT/MS-Win. In that case the |
| 4 | module actually calls itself 'nt', not 'posix', and a few |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 5 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 6 | 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] | 7 | of the compiler used. Different compilers define their own feature |
Victor Stinner | f427a14 | 2014-10-22 12:33:23 +0200 | [diff] [blame] | 8 | test macro, e.g. '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10 | #ifdef __APPLE__ |
| 11 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12 | * 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] | 13 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 14 | * at the end of this file for more information. |
| 15 | */ |
| 16 | # pragma weak lchown |
| 17 | # pragma weak statvfs |
| 18 | # pragma weak fstatvfs |
| 19 | |
| 20 | #endif /* __APPLE__ */ |
| 21 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 22 | #define PY_SSIZE_T_CLEAN |
| 23 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 24 | #include "Python.h" |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 25 | #ifdef MS_WINDOWS |
| 26 | /* include <windows.h> early to avoid conflict with pycore_condvar.h: |
| 27 | |
| 28 | #define WIN32_LEAN_AND_MEAN |
| 29 | #include <windows.h> |
| 30 | |
| 31 | FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */ |
| 32 | # include <windows.h> |
| 33 | #endif |
| 34 | |
pxinwr | 3405e05 | 2020-08-07 13:21:52 +0800 | [diff] [blame] | 35 | #ifdef __VXWORKS__ |
| 36 | # include "pycore_bitutils.h" // _Py_popcount32() |
| 37 | #endif |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 38 | #include "pycore_ceval.h" // _PyEval_ReInitThreads() |
| 39 | #include "pycore_import.h" // _PyImport_ReInitLock() |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 40 | #include "pycore_initconfig.h" // _PyStatus_EXCEPTION() |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 41 | #include "pycore_pystate.h" // _PyInterpreterState_GET() |
| 42 | #include "structmember.h" // PyMemberDef |
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__ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 52 | # undef HAVE_FACCESSAT |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 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 |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 69 | # include <sys/uio.h> |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 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() */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 74 | # include <sys/sysmacros.h> |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 75 | #endif |
| 76 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | #ifdef HAVE_SYS_TYPES_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [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 |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [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 | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 86 | # include <sys/wait.h> // 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 |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 89 | # include <linux/wait.h> // P_PIDFD |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 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 |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [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 |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 97 | # include <fcntl.h> |
| 98 | #endif |
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 |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 101 | # include <grp.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 102 | #endif |
| 103 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 104 | #ifdef HAVE_SYSEXITS_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 105 | # include <sysexits.h> |
| 106 | #endif |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 107 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 108 | #ifdef HAVE_SYS_LOADAVG_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 109 | # include <sys/loadavg.h> |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 110 | #endif |
| 111 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 112 | #ifdef HAVE_SYS_SENDFILE_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 113 | # include <sys/sendfile.h> |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 114 | #endif |
| 115 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 116 | #if defined(__APPLE__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 117 | # include <copyfile.h> |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 118 | #endif |
| 119 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 120 | #ifdef HAVE_SCHED_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 121 | # include <sched.h> |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 122 | #endif |
| 123 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 124 | #ifdef HAVE_COPY_FILE_RANGE |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 125 | # include <unistd.h> |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 126 | #endif |
| 127 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 128 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 129 | # undef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 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__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 133 | # define USE_XATTRS |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 134 | #endif |
| 135 | |
| 136 | #ifdef USE_XATTRS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [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__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 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 |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 147 | # include <dlfcn.h> |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 148 | #endif |
| 149 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 150 | #ifdef __hpux |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 151 | # include <sys/mpctl.h> |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 152 | #endif |
| 153 | |
| 154 | #if defined(__DragonFly__) || \ |
| 155 | defined(__OpenBSD__) || \ |
| 156 | defined(__FreeBSD__) || \ |
| 157 | defined(__NetBSD__) || \ |
| 158 | defined(__APPLE__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 159 | # include <sys/sysctl.h> |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 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 */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 184 | # define HAVE_OPENDIR 1 |
| 185 | # define HAVE_SYSTEM 1 |
| 186 | # include <process.h> |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 187 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 188 | # ifdef _MSC_VER |
| 189 | /* Microsoft compiler */ |
| 190 | # define HAVE_GETPPID 1 |
| 191 | # define HAVE_GETLOGIN 1 |
| 192 | # define HAVE_SPAWNV 1 |
| 193 | # define HAVE_EXECV 1 |
| 194 | # define HAVE_WSPAWNV 1 |
| 195 | # define HAVE_WEXECV 1 |
| 196 | # define HAVE_PIPE 1 |
| 197 | # define HAVE_SYSTEM 1 |
| 198 | # define HAVE_CWAIT 1 |
| 199 | # define HAVE_FSYNC 1 |
| 200 | # define fsync _commit |
| 201 | # else |
| 202 | /* Unix functions that the configure script doesn't check for */ |
| 203 | # ifndef __VXWORKS__ |
| 204 | # define HAVE_EXECV 1 |
| 205 | # define HAVE_FORK 1 |
| 206 | # if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 207 | # define HAVE_FORK1 1 |
| 208 | # endif |
| 209 | # endif |
| 210 | # define HAVE_GETEGID 1 |
| 211 | # define HAVE_GETEUID 1 |
| 212 | # define HAVE_GETGID 1 |
| 213 | # define HAVE_GETPPID 1 |
| 214 | # define HAVE_GETUID 1 |
| 215 | # define HAVE_KILL 1 |
| 216 | # define HAVE_OPENDIR 1 |
| 217 | # define HAVE_PIPE 1 |
| 218 | # define HAVE_SYSTEM 1 |
| 219 | # define HAVE_WAIT 1 |
| 220 | # define HAVE_TTYNAME 1 |
| 221 | # endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 222 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 223 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 224 | _Py_IDENTIFIER(__fspath__); |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 225 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 226 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 227 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 228 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 229 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 230 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 231 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 232 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 233 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 234 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 235 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 236 | (default) */ |
| 237 | extern char *ctermid_r(char *); |
| 238 | #endif |
| 239 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 240 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 241 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 242 | #if defined(__VXWORKS__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 243 | # include <vxCpuLib.h> |
| 244 | # include <rtpLib.h> |
| 245 | # include <wait.h> |
| 246 | # include <taskLib.h> |
| 247 | # ifndef _P_WAIT |
| 248 | # define _P_WAIT 0 |
| 249 | # define _P_NOWAIT 1 |
| 250 | # define _P_NOWAITO 1 |
| 251 | # endif |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 252 | #endif /* __VXWORKS__ */ |
| 253 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 254 | #ifdef HAVE_POSIX_SPAWN |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 255 | # include <spawn.h> |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 256 | #endif |
| 257 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | #ifdef HAVE_UTIME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 259 | # include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 260 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 262 | #ifdef HAVE_SYS_UTIME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 263 | # include <sys/utime.h> |
| 264 | # define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 265 | #endif /* HAVE_SYS_UTIME_H */ |
| 266 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | #ifdef HAVE_SYS_TIMES_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 268 | # include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 269 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | |
| 271 | #ifdef HAVE_SYS_PARAM_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 272 | # include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 273 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | |
| 275 | #ifdef HAVE_SYS_UTSNAME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 276 | # include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 277 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 278 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 279 | #ifdef HAVE_DIRENT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 280 | # include <dirent.h> |
| 281 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 282 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 283 | # if defined(__WATCOMC__) && !defined(__QNX__) |
| 284 | # include <direct.h> |
| 285 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
| 286 | # else |
| 287 | # define dirent direct |
| 288 | # define NAMLEN(dirent) (dirent)->d_namlen |
| 289 | # endif |
| 290 | # ifdef HAVE_SYS_NDIR_H |
| 291 | # include <sys/ndir.h> |
| 292 | # endif |
| 293 | # ifdef HAVE_SYS_DIR_H |
| 294 | # include <sys/dir.h> |
| 295 | # endif |
| 296 | # ifdef HAVE_NDIR_H |
| 297 | # include <ndir.h> |
| 298 | # endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 299 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 301 | #ifdef _MSC_VER |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 302 | # ifdef HAVE_DIRECT_H |
| 303 | # include <direct.h> |
| 304 | # endif |
| 305 | # ifdef HAVE_IO_H |
| 306 | # include <io.h> |
| 307 | # endif |
| 308 | # ifdef HAVE_PROCESS_H |
| 309 | # include <process.h> |
| 310 | # endif |
| 311 | # ifndef IO_REPARSE_TAG_SYMLINK |
| 312 | # define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
| 313 | # endif |
| 314 | # ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 315 | # define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 316 | # endif |
| 317 | # include "osdefs.h" // SEP |
| 318 | # include <malloc.h> |
| 319 | # include <windows.h> |
| 320 | # include <shellapi.h> // ShellExecute() |
| 321 | # include <lmcons.h> // UNLEN |
| 322 | # define HAVE_SYMLINK |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 323 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 324 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 325 | #ifndef MAXPATHLEN |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 326 | # if defined(PATH_MAX) && PATH_MAX > 1024 |
| 327 | # define MAXPATHLEN PATH_MAX |
| 328 | # else |
| 329 | # define MAXPATHLEN 1024 |
| 330 | # endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 331 | #endif /* MAXPATHLEN */ |
| 332 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 333 | #ifdef UNION_WAIT |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 334 | /* Emulate some macros on systems that have a union instead of macros */ |
| 335 | # ifndef WIFEXITED |
| 336 | # define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 337 | # endif |
| 338 | # ifndef WEXITSTATUS |
| 339 | # define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 340 | # endif |
| 341 | # ifndef WTERMSIG |
| 342 | # define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 343 | # endif |
| 344 | # define WAIT_TYPE union wait |
| 345 | # define WAIT_STATUS_INT(s) (s.w_status) |
| 346 | #else |
| 347 | /* !UNION_WAIT */ |
| 348 | # define WAIT_TYPE int |
| 349 | # define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 350 | #endif /* UNION_WAIT */ |
| 351 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 352 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 353 | prototype for it, at least on Solaris -- maybe others as well?). */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 354 | #if defined(HAVE_CTERMID_R) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 355 | # define USE_CTERMID_R |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 356 | #endif |
| 357 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 358 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 359 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 360 | #undef FSTAT |
| 361 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 362 | #ifdef MS_WINDOWS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 363 | # define STAT win32_stat |
| 364 | # define LSTAT win32_lstat |
| 365 | # define FSTAT _Py_fstat_noraise |
| 366 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 367 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 368 | # define STAT stat |
| 369 | # define LSTAT lstat |
| 370 | # define FSTAT fstat |
| 371 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 372 | #endif |
| 373 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 374 | #if defined(MAJOR_IN_MKDEV) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 375 | # include <sys/mkdev.h> |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 376 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 377 | # if defined(MAJOR_IN_SYSMACROS) |
| 378 | # include <sys/sysmacros.h> |
| 379 | # endif |
| 380 | # if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 381 | # include <sys/mkdev.h> |
| 382 | # endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 383 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 384 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 385 | #ifdef MS_WINDOWS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 386 | # define INITFUNC PyInit_nt |
| 387 | # define MODNAME "nt" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 388 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 389 | # define INITFUNC PyInit_posix |
| 390 | # define MODNAME "posix" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 391 | #endif |
| 392 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 393 | #if defined(__sun) |
| 394 | /* Something to implement in autoconf, not present in autoconf 2.69 */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 395 | # define HAVE_STRUCT_STAT_ST_FSTYPE 1 |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 396 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 397 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 398 | /* memfd_create is either defined in sys/mman.h or sys/memfd.h |
| 399 | * linux/memfd.h defines additional flags |
| 400 | */ |
| 401 | #ifdef HAVE_SYS_MMAN_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 402 | # include <sys/mman.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 403 | #endif |
| 404 | #ifdef HAVE_SYS_MEMFD_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 405 | # include <sys/memfd.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 406 | #endif |
| 407 | #ifdef HAVE_LINUX_MEMFD_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 408 | # include <linux/memfd.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 409 | #endif |
| 410 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 411 | #ifdef _Py_MEMORY_SANITIZER |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 412 | # include <sanitizer/msan_interface.h> |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 413 | #endif |
| 414 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 415 | #ifdef HAVE_FORK |
| 416 | static void |
| 417 | run_at_forkers(PyObject *lst, int reverse) |
| 418 | { |
| 419 | Py_ssize_t i; |
| 420 | PyObject *cpy; |
| 421 | |
| 422 | if (lst != NULL) { |
| 423 | assert(PyList_CheckExact(lst)); |
| 424 | |
| 425 | /* Use a list copy in case register_at_fork() is called from |
| 426 | * one of the callbacks. |
| 427 | */ |
| 428 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); |
| 429 | if (cpy == NULL) |
| 430 | PyErr_WriteUnraisable(lst); |
| 431 | else { |
| 432 | if (reverse) |
| 433 | PyList_Reverse(cpy); |
| 434 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { |
| 435 | PyObject *func, *res; |
| 436 | func = PyList_GET_ITEM(cpy, i); |
Jeroen Demeyer | 7f41c8e | 2019-07-04 12:35:31 +0200 | [diff] [blame] | 437 | res = _PyObject_CallNoArg(func); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 438 | if (res == NULL) |
| 439 | PyErr_WriteUnraisable(func); |
| 440 | else |
| 441 | Py_DECREF(res); |
| 442 | } |
| 443 | Py_DECREF(cpy); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void |
| 449 | PyOS_BeforeFork(void) |
| 450 | { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 451 | run_at_forkers(_PyInterpreterState_GET()->before_forkers, 1); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 452 | |
| 453 | _PyImport_AcquireLock(); |
| 454 | } |
| 455 | |
| 456 | void |
| 457 | PyOS_AfterFork_Parent(void) |
| 458 | { |
| 459 | if (_PyImport_ReleaseLock() <= 0) |
| 460 | Py_FatalError("failed releasing import lock after fork"); |
| 461 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 462 | run_at_forkers(_PyInterpreterState_GET()->after_forkers_parent, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | void |
| 466 | PyOS_AfterFork_Child(void) |
| 467 | { |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 468 | PyStatus status; |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 469 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 470 | |
| 471 | status = _PyGILState_Reinit(runtime); |
| 472 | if (_PyStatus_EXCEPTION(status)) { |
| 473 | goto fatal_error; |
| 474 | } |
| 475 | |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 476 | PyThreadState *tstate = _PyThreadState_GET(); |
| 477 | _Py_EnsureTstateNotNULL(tstate); |
| 478 | |
| 479 | status = _PyEval_ReInitThreads(tstate); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 480 | if (_PyStatus_EXCEPTION(status)) { |
| 481 | goto fatal_error; |
| 482 | } |
| 483 | |
| 484 | status = _PyImport_ReInitLock(); |
| 485 | if (_PyStatus_EXCEPTION(status)) { |
| 486 | goto fatal_error; |
| 487 | } |
| 488 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 489 | _PySignal_AfterFork(); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 490 | |
| 491 | status = _PyRuntimeState_ReInitThreads(runtime); |
| 492 | if (_PyStatus_EXCEPTION(status)) { |
| 493 | goto fatal_error; |
| 494 | } |
| 495 | |
| 496 | status = _PyInterpreterState_DeleteExceptMain(runtime); |
| 497 | if (_PyStatus_EXCEPTION(status)) { |
| 498 | goto fatal_error; |
| 499 | } |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 500 | assert(_PyThreadState_GET() == tstate); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 501 | |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 502 | run_at_forkers(tstate->interp->after_forkers_child, 0); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 503 | return; |
| 504 | |
| 505 | fatal_error: |
| 506 | Py_ExitStatusException(status); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static int |
| 510 | register_at_forker(PyObject **lst, PyObject *func) |
| 511 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 512 | if (func == NULL) /* nothing to register? do nothing. */ |
| 513 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 514 | if (*lst == NULL) { |
| 515 | *lst = PyList_New(0); |
| 516 | if (*lst == NULL) |
| 517 | return -1; |
| 518 | } |
| 519 | return PyList_Append(*lst, func); |
| 520 | } |
Victor Stinner | 87255be | 2020-04-07 23:11:49 +0200 | [diff] [blame] | 521 | #endif /* HAVE_FORK */ |
| 522 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 523 | |
| 524 | /* Legacy wrapper */ |
| 525 | void |
| 526 | PyOS_AfterFork(void) |
| 527 | { |
| 528 | #ifdef HAVE_FORK |
| 529 | PyOS_AfterFork_Child(); |
| 530 | #endif |
| 531 | } |
| 532 | |
| 533 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 534 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 535 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 536 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 537 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 538 | ULONG, struct _Py_stat_struct *); |
| 539 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 540 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 541 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 542 | #ifndef MS_WINDOWS |
| 543 | PyObject * |
| 544 | _PyLong_FromUid(uid_t uid) |
| 545 | { |
| 546 | if (uid == (uid_t)-1) |
| 547 | return PyLong_FromLong(-1); |
| 548 | return PyLong_FromUnsignedLong(uid); |
| 549 | } |
| 550 | |
| 551 | PyObject * |
| 552 | _PyLong_FromGid(gid_t gid) |
| 553 | { |
| 554 | if (gid == (gid_t)-1) |
| 555 | return PyLong_FromLong(-1); |
| 556 | return PyLong_FromUnsignedLong(gid); |
| 557 | } |
| 558 | |
| 559 | int |
| 560 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 561 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 562 | uid_t uid; |
| 563 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 564 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 565 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 566 | unsigned long uresult; |
| 567 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 568 | index = _PyNumber_Index(obj); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 569 | if (index == NULL) { |
| 570 | PyErr_Format(PyExc_TypeError, |
| 571 | "uid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 572 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 573 | return 0; |
| 574 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 575 | |
| 576 | /* |
| 577 | * Handling uid_t is complicated for two reasons: |
| 578 | * * Although uid_t is (always?) unsigned, it still |
| 579 | * accepts -1. |
| 580 | * * We don't know its size in advance--it may be |
| 581 | * bigger than an int, or it may be smaller than |
| 582 | * a long. |
| 583 | * |
| 584 | * So a bit of defensive programming is in order. |
| 585 | * Start with interpreting the value passed |
| 586 | * in as a signed long and see if it works. |
| 587 | */ |
| 588 | |
| 589 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 590 | |
| 591 | if (!overflow) { |
| 592 | uid = (uid_t)result; |
| 593 | |
| 594 | if (result == -1) { |
| 595 | if (PyErr_Occurred()) |
| 596 | goto fail; |
| 597 | /* It's a legitimate -1, we're done. */ |
| 598 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 599 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 600 | |
| 601 | /* Any other negative number is disallowed. */ |
| 602 | if (result < 0) |
| 603 | goto underflow; |
| 604 | |
| 605 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 606 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 607 | (long)uid != result) |
| 608 | goto underflow; |
| 609 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 610 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 611 | |
| 612 | if (overflow < 0) |
| 613 | goto underflow; |
| 614 | |
| 615 | /* |
| 616 | * Okay, the value overflowed a signed long. If it |
| 617 | * fits in an *unsigned* long, it may still be okay, |
| 618 | * as uid_t may be unsigned long on this platform. |
| 619 | */ |
| 620 | uresult = PyLong_AsUnsignedLong(index); |
| 621 | if (PyErr_Occurred()) { |
| 622 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 623 | goto overflow; |
| 624 | goto fail; |
| 625 | } |
| 626 | |
| 627 | uid = (uid_t)uresult; |
| 628 | |
| 629 | /* |
| 630 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 631 | * but this value would get interpreted as (uid_t)-1 by chown |
| 632 | * and its siblings. That's not what the user meant! So we |
| 633 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 634 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 635 | */ |
| 636 | if (uid == (uid_t)-1) |
| 637 | goto overflow; |
| 638 | |
| 639 | /* Ensure the value wasn't truncated. */ |
| 640 | if (sizeof(uid_t) < sizeof(long) && |
| 641 | (unsigned long)uid != uresult) |
| 642 | goto overflow; |
| 643 | /* fallthrough */ |
| 644 | |
| 645 | success: |
| 646 | Py_DECREF(index); |
| 647 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 648 | return 1; |
| 649 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 650 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 651 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 652 | "uid is less than minimum"); |
| 653 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 654 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 655 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 656 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 657 | "uid is greater than maximum"); |
| 658 | /* fallthrough */ |
| 659 | |
| 660 | fail: |
| 661 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | int |
| 666 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 667 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 668 | gid_t gid; |
| 669 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 670 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 671 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 672 | unsigned long uresult; |
| 673 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 674 | index = _PyNumber_Index(obj); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 675 | if (index == NULL) { |
| 676 | PyErr_Format(PyExc_TypeError, |
| 677 | "gid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 678 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 679 | return 0; |
| 680 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 681 | |
| 682 | /* |
| 683 | * Handling gid_t is complicated for two reasons: |
| 684 | * * Although gid_t is (always?) unsigned, it still |
| 685 | * accepts -1. |
| 686 | * * We don't know its size in advance--it may be |
| 687 | * bigger than an int, or it may be smaller than |
| 688 | * a long. |
| 689 | * |
| 690 | * So a bit of defensive programming is in order. |
| 691 | * Start with interpreting the value passed |
| 692 | * in as a signed long and see if it works. |
| 693 | */ |
| 694 | |
| 695 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 696 | |
| 697 | if (!overflow) { |
| 698 | gid = (gid_t)result; |
| 699 | |
| 700 | if (result == -1) { |
| 701 | if (PyErr_Occurred()) |
| 702 | goto fail; |
| 703 | /* It's a legitimate -1, we're done. */ |
| 704 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 705 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 706 | |
| 707 | /* Any other negative number is disallowed. */ |
| 708 | if (result < 0) { |
| 709 | goto underflow; |
| 710 | } |
| 711 | |
| 712 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 713 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 714 | (long)gid != result) |
| 715 | goto underflow; |
| 716 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 717 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 718 | |
| 719 | if (overflow < 0) |
| 720 | goto underflow; |
| 721 | |
| 722 | /* |
| 723 | * Okay, the value overflowed a signed long. If it |
| 724 | * fits in an *unsigned* long, it may still be okay, |
| 725 | * as gid_t may be unsigned long on this platform. |
| 726 | */ |
| 727 | uresult = PyLong_AsUnsignedLong(index); |
| 728 | if (PyErr_Occurred()) { |
| 729 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 730 | goto overflow; |
| 731 | goto fail; |
| 732 | } |
| 733 | |
| 734 | gid = (gid_t)uresult; |
| 735 | |
| 736 | /* |
| 737 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 738 | * but this value would get interpreted as (gid_t)-1 by chown |
| 739 | * and its siblings. That's not what the user meant! So we |
| 740 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 741 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 742 | */ |
| 743 | if (gid == (gid_t)-1) |
| 744 | goto overflow; |
| 745 | |
| 746 | /* Ensure the value wasn't truncated. */ |
| 747 | if (sizeof(gid_t) < sizeof(long) && |
| 748 | (unsigned long)gid != uresult) |
| 749 | goto overflow; |
| 750 | /* fallthrough */ |
| 751 | |
| 752 | success: |
| 753 | Py_DECREF(index); |
| 754 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 755 | return 1; |
| 756 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 757 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 758 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 759 | "gid is less than minimum"); |
| 760 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 761 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 762 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 763 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 764 | "gid is greater than maximum"); |
| 765 | /* fallthrough */ |
| 766 | |
| 767 | fail: |
| 768 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 769 | return 0; |
| 770 | } |
| 771 | #endif /* MS_WINDOWS */ |
| 772 | |
| 773 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 774 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 775 | |
| 776 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 777 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 778 | static int |
| 779 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 780 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 781 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 782 | if (PyErr_Occurred()) |
| 783 | return 0; |
| 784 | return 1; |
| 785 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 786 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 787 | |
| 788 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 789 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 790 | /* |
| 791 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 792 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 793 | * which doesn't play nicely with all the initializer lines in this file that |
| 794 | * look like this: |
| 795 | * int dir_fd = DEFAULT_DIR_FD; |
| 796 | */ |
| 797 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 798 | #else |
| 799 | #define DEFAULT_DIR_FD (-100) |
| 800 | #endif |
| 801 | |
| 802 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 803 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 804 | { |
| 805 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 806 | long long_value; |
| 807 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 808 | PyObject *index = _PyNumber_Index(o); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 809 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 810 | return 0; |
| 811 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 812 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 813 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 814 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 815 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 816 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 817 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 818 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 819 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 820 | return 0; |
| 821 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 822 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 823 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 824 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 825 | return 0; |
| 826 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 827 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 828 | *p = (int)long_value; |
| 829 | return 1; |
| 830 | } |
| 831 | |
| 832 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 833 | dir_fd_converter(PyObject *o, void *p) |
| 834 | { |
| 835 | if (o == Py_None) { |
| 836 | *(int *)p = DEFAULT_DIR_FD; |
| 837 | return 1; |
| 838 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 839 | else if (PyIndex_Check(o)) { |
| 840 | return _fd_converter(o, (int *)p); |
| 841 | } |
| 842 | else { |
| 843 | PyErr_Format(PyExc_TypeError, |
| 844 | "argument should be integer or None, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 845 | _PyType_Name(Py_TYPE(o))); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 846 | return 0; |
| 847 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 850 | typedef struct { |
| 851 | PyObject *billion; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 852 | PyObject *DirEntryType; |
| 853 | PyObject *ScandirIteratorType; |
| 854 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 855 | PyObject *SchedParamType; |
| 856 | #endif |
| 857 | PyObject *StatResultType; |
| 858 | PyObject *StatVFSResultType; |
| 859 | PyObject *TerminalSizeType; |
| 860 | PyObject *TimesResultType; |
| 861 | PyObject *UnameResultType; |
| 862 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 863 | PyObject *WaitidResultType; |
| 864 | #endif |
| 865 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 866 | PyObject *struct_rusage; |
| 867 | #endif |
| 868 | PyObject *st_mode; |
| 869 | } _posixstate; |
| 870 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 871 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 872 | static inline _posixstate* |
| 873 | get_posix_state(PyObject *module) |
| 874 | { |
| 875 | void *state = PyModule_GetState(module); |
| 876 | assert(state != NULL); |
| 877 | return (_posixstate *)state; |
| 878 | } |
| 879 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 880 | /* |
| 881 | * A PyArg_ParseTuple "converter" function |
| 882 | * that handles filesystem paths in the manner |
| 883 | * preferred by the os module. |
| 884 | * |
| 885 | * path_converter accepts (Unicode) strings and their |
| 886 | * subclasses, and bytes and their subclasses. What |
| 887 | * it does with the argument depends on the platform: |
| 888 | * |
| 889 | * * On Windows, if we get a (Unicode) string we |
| 890 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 891 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 892 | * |
| 893 | * * On all other platforms, strings are encoded |
| 894 | * to bytes using PyUnicode_FSConverter, then we |
| 895 | * extract the char * from the bytes object and |
| 896 | * return that. |
| 897 | * |
| 898 | * path_converter also optionally accepts signed |
| 899 | * integers (representing open file descriptors) instead |
| 900 | * of path strings. |
| 901 | * |
| 902 | * Input fields: |
| 903 | * path.nullable |
| 904 | * If nonzero, the path is permitted to be None. |
| 905 | * path.allow_fd |
| 906 | * If nonzero, the path is permitted to be a file handle |
| 907 | * (a signed int) instead of a string. |
| 908 | * path.function_name |
| 909 | * If non-NULL, path_converter will use that as the name |
| 910 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 911 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 912 | * path.argument_name |
| 913 | * If non-NULL, path_converter will use that as the name |
| 914 | * of the parameter in error messages. |
| 915 | * (If path.argument_name is NULL it uses "path".) |
| 916 | * |
| 917 | * Output fields: |
| 918 | * path.wide |
| 919 | * Points to the path if it was expressed as Unicode |
| 920 | * and was not encoded. (Only used on Windows.) |
| 921 | * path.narrow |
| 922 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 923 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 924 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 925 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 926 | * path.fd |
| 927 | * Contains a file descriptor if path.accept_fd was true |
| 928 | * and the caller provided a signed integer instead of any |
| 929 | * sort of string. |
| 930 | * |
| 931 | * WARNING: if your "path" parameter is optional, and is |
| 932 | * unspecified, path_converter will never get called. |
| 933 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 934 | * yourself! |
| 935 | * path.length |
| 936 | * The length of the path in characters, if specified as |
| 937 | * a string. |
| 938 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 939 | * The original object passed in (if get a PathLike object, |
| 940 | * the result of PyOS_FSPath() is treated as the original object). |
| 941 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 942 | * path.cleanup |
| 943 | * For internal use only. May point to a temporary object. |
| 944 | * (Pay no attention to the man behind the curtain.) |
| 945 | * |
| 946 | * At most one of path.wide or path.narrow will be non-NULL. |
| 947 | * If path was None and path.nullable was set, |
| 948 | * or if path was an integer and path.allow_fd was set, |
| 949 | * both path.wide and path.narrow will be NULL |
| 950 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 951 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 952 | * path_converter takes care to not write to the path_t |
| 953 | * unless it's successful. However it must reset the |
| 954 | * "cleanup" field each time it's called. |
| 955 | * |
| 956 | * Use as follows: |
| 957 | * path_t path; |
| 958 | * memset(&path, 0, sizeof(path)); |
| 959 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 960 | * // ... use values from path ... |
| 961 | * path_cleanup(&path); |
| 962 | * |
| 963 | * (Note that if PyArg_Parse fails you don't need to call |
| 964 | * path_cleanup(). However it is safe to do so.) |
| 965 | */ |
| 966 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 967 | const char *function_name; |
| 968 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 969 | int nullable; |
| 970 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 971 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 972 | #ifdef MS_WINDOWS |
| 973 | BOOL narrow; |
| 974 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 975 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 976 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 977 | int fd; |
| 978 | Py_ssize_t length; |
| 979 | PyObject *object; |
| 980 | PyObject *cleanup; |
| 981 | } path_t; |
| 982 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 983 | #ifdef MS_WINDOWS |
| 984 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 985 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 986 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 987 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 988 | {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] | 989 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 990 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 991 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 992 | path_cleanup(path_t *path) |
| 993 | { |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 994 | #if !USE_UNICODE_WCHAR_CACHE |
| 995 | wchar_t *wide = (wchar_t *)path->wide; |
| 996 | path->wide = NULL; |
| 997 | PyMem_Free(wide); |
| 998 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 999 | Py_CLEAR(path->object); |
| 1000 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1004 | path_converter(PyObject *o, void *p) |
| 1005 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1006 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1007 | PyObject *bytes = NULL; |
| 1008 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1009 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1010 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1011 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1012 | PyObject *wo = NULL; |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1013 | wchar_t *wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1014 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1015 | |
| 1016 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 1017 | PyErr_Format(exc, "%s%s" fmt, \ |
| 1018 | path->function_name ? path->function_name : "", \ |
| 1019 | path->function_name ? ": " : "", \ |
| 1020 | path->argument_name ? path->argument_name : "path") |
| 1021 | |
| 1022 | /* Py_CLEANUP_SUPPORTED support */ |
| 1023 | if (o == NULL) { |
| 1024 | path_cleanup(path); |
| 1025 | return 1; |
| 1026 | } |
| 1027 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1028 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1029 | path->object = path->cleanup = NULL; |
| 1030 | /* path->object owns a reference to the original object */ |
| 1031 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1032 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1033 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1034 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1035 | #ifdef MS_WINDOWS |
| 1036 | path->narrow = FALSE; |
| 1037 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1038 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1039 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1040 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1041 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1044 | /* Only call this here so that we don't treat the return value of |
| 1045 | os.fspath() as an fd or buffer. */ |
| 1046 | is_index = path->allow_fd && PyIndex_Check(o); |
| 1047 | is_buffer = PyObject_CheckBuffer(o); |
| 1048 | is_bytes = PyBytes_Check(o); |
| 1049 | is_unicode = PyUnicode_Check(o); |
| 1050 | |
| 1051 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 1052 | /* Inline PyOS_FSPath() for better error messages. */ |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1053 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1054 | |
| 1055 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 1056 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1057 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1058 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1059 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1060 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1061 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1062 | goto error_exit; |
| 1063 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1064 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1065 | is_unicode = 1; |
| 1066 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1067 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1068 | is_bytes = 1; |
| 1069 | } |
| 1070 | else { |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1071 | PyErr_Format(PyExc_TypeError, |
| 1072 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1073 | "not %.200s", _PyType_Name(Py_TYPE(o)), |
| 1074 | _PyType_Name(Py_TYPE(res))); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1075 | Py_DECREF(res); |
| 1076 | goto error_exit; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1077 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1078 | |
| 1079 | /* still owns a reference to the original object */ |
| 1080 | Py_DECREF(o); |
| 1081 | o = res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1085 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1086 | #if USE_UNICODE_WCHAR_CACHE |
| 1087 | _Py_COMP_DIAG_PUSH |
| 1088 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1089 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1090 | _Py_COMP_DIAG_POP |
| 1091 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 1092 | wide = PyUnicode_AsWideCharString(o, &length); |
| 1093 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1094 | if (!wide) { |
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 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1097 | if (length > 32767) { |
| 1098 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1099 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1100 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1101 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1102 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1103 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1104 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1105 | |
| 1106 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1107 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1108 | path->fd = -1; |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1109 | #if !USE_UNICODE_WCHAR_CACHE |
| 1110 | wide = NULL; |
| 1111 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1112 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1113 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1114 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1115 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1116 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1117 | #endif |
| 1118 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1119 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1120 | bytes = o; |
| 1121 | Py_INCREF(bytes); |
| 1122 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1123 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1124 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1125 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1126 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1127 | "%s%s%s should be %s, not %.200s", |
| 1128 | path->function_name ? path->function_name : "", |
| 1129 | path->function_name ? ": " : "", |
| 1130 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1131 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1132 | "integer or None" : |
| 1133 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1134 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1135 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1136 | _PyType_Name(Py_TYPE(o)))) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1137 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1138 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1139 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1140 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1141 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1142 | } |
| 1143 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1144 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1145 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1146 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1147 | } |
| 1148 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1149 | #ifdef MS_WINDOWS |
| 1150 | path->narrow = FALSE; |
| 1151 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1152 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1153 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1154 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1155 | } |
| 1156 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1157 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1158 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1159 | path->function_name ? path->function_name : "", |
| 1160 | path->function_name ? ": " : "", |
| 1161 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1162 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1163 | "integer or None" : |
| 1164 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1165 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1166 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1167 | _PyType_Name(Py_TYPE(o))); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1168 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1171 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1172 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1173 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1174 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1175 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1178 | #ifdef MS_WINDOWS |
| 1179 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1180 | narrow, |
| 1181 | length |
| 1182 | ); |
| 1183 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1184 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1187 | #if USE_UNICODE_WCHAR_CACHE |
| 1188 | _Py_COMP_DIAG_PUSH |
| 1189 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1190 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1191 | _Py_COMP_DIAG_POP |
| 1192 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 1193 | wide = PyUnicode_AsWideCharString(wo, &length); |
| 1194 | Py_DECREF(wo); |
| 1195 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1196 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1197 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1198 | } |
| 1199 | if (length > 32767) { |
| 1200 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1201 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1202 | } |
| 1203 | if (wcslen(wide) != length) { |
| 1204 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1205 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1206 | } |
| 1207 | path->wide = wide; |
| 1208 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1209 | Py_DECREF(bytes); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1210 | #if USE_UNICODE_WCHAR_CACHE |
| 1211 | path->cleanup = wo; |
| 1212 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 1213 | wide = NULL; |
| 1214 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1215 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1216 | path->wide = NULL; |
| 1217 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1218 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1219 | /* Still a reference owned by path->object, don't have to |
| 1220 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1221 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1222 | } |
| 1223 | else { |
| 1224 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1225 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1226 | #endif |
| 1227 | path->fd = -1; |
| 1228 | |
| 1229 | success_exit: |
| 1230 | path->length = length; |
| 1231 | path->object = o; |
| 1232 | return Py_CLEANUP_SUPPORTED; |
| 1233 | |
| 1234 | error_exit: |
| 1235 | Py_XDECREF(o); |
| 1236 | Py_XDECREF(bytes); |
| 1237 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1238 | #if USE_UNICODE_WCHAR_CACHE |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1239 | Py_XDECREF(wo); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1240 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 1241 | PyMem_Free(wide); |
| 1242 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1243 | #endif |
| 1244 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1248 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1249 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1250 | PyErr_Format(PyExc_NotImplementedError, |
| 1251 | "%s%s%s unavailable on this platform", |
| 1252 | (function_name != NULL) ? function_name : "", |
| 1253 | (function_name != NULL) ? ": ": "", |
| 1254 | argument_name); |
| 1255 | } |
| 1256 | |
| 1257 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1258 | dir_fd_unavailable(PyObject *o, void *p) |
| 1259 | { |
| 1260 | int dir_fd; |
| 1261 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1262 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1263 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1264 | argument_unavailable_error(NULL, "dir_fd"); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | *(int *)p = dir_fd; |
| 1268 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1272 | fd_specified(const char *function_name, int fd) |
| 1273 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1274 | if (fd == -1) |
| 1275 | return 0; |
| 1276 | |
| 1277 | argument_unavailable_error(function_name, "fd"); |
| 1278 | return 1; |
| 1279 | } |
| 1280 | |
| 1281 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1282 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1283 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1284 | if (follow_symlinks) |
| 1285 | return 0; |
| 1286 | |
| 1287 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1288 | return 1; |
| 1289 | } |
| 1290 | |
| 1291 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1292 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1293 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1294 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1295 | #ifndef MS_WINDOWS |
| 1296 | && !path->narrow |
| 1297 | #endif |
| 1298 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1299 | PyErr_Format(PyExc_ValueError, |
| 1300 | "%s: can't specify dir_fd without matching path", |
| 1301 | function_name); |
| 1302 | return 1; |
| 1303 | } |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
| 1307 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1308 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1309 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1310 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1311 | PyErr_Format(PyExc_ValueError, |
| 1312 | "%s: can't specify both dir_fd and fd", |
| 1313 | function_name); |
| 1314 | return 1; |
| 1315 | } |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1320 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1321 | int follow_symlinks) |
| 1322 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1323 | if ((fd > 0) && (!follow_symlinks)) { |
| 1324 | PyErr_Format(PyExc_ValueError, |
| 1325 | "%s: cannot use fd and follow_symlinks together", |
| 1326 | function_name); |
| 1327 | return 1; |
| 1328 | } |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1333 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1334 | int follow_symlinks) |
| 1335 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1336 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1337 | PyErr_Format(PyExc_ValueError, |
| 1338 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1339 | function_name); |
| 1340 | return 1; |
| 1341 | } |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1345 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1346 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1347 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1348 | typedef off_t Py_off_t; |
| 1349 | #endif |
| 1350 | |
| 1351 | static int |
| 1352 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1353 | { |
| 1354 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1355 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1356 | #else |
| 1357 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1358 | #endif |
| 1359 | if (PyErr_Occurred()) |
| 1360 | return 0; |
| 1361 | return 1; |
| 1362 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1363 | |
| 1364 | static PyObject * |
| 1365 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1366 | { |
| 1367 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1368 | return PyLong_FromLongLong(offset); |
| 1369 | #else |
| 1370 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1371 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1372 | } |
| 1373 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1374 | #ifdef HAVE_SIGSET_T |
| 1375 | /* Convert an iterable of integers to a sigset. |
| 1376 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1377 | int |
| 1378 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1379 | { |
| 1380 | sigset_t *mask = (sigset_t *)addr; |
| 1381 | PyObject *iterator, *item; |
| 1382 | long signum; |
| 1383 | int overflow; |
| 1384 | |
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1385 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
| 1386 | if (sigemptyset(mask) < (0)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1387 | /* Probably only if mask == NULL. */ |
| 1388 | PyErr_SetFromErrno(PyExc_OSError); |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | iterator = PyObject_GetIter(obj); |
| 1393 | if (iterator == NULL) { |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1398 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1399 | Py_DECREF(item); |
| 1400 | if (signum <= 0 || signum >= NSIG) { |
| 1401 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1402 | PyErr_Format(PyExc_ValueError, |
| 1403 | "signal number %ld out of range", signum); |
| 1404 | } |
| 1405 | goto error; |
| 1406 | } |
| 1407 | if (sigaddset(mask, (int)signum)) { |
| 1408 | if (errno != EINVAL) { |
| 1409 | /* Probably impossible */ |
| 1410 | PyErr_SetFromErrno(PyExc_OSError); |
| 1411 | goto error; |
| 1412 | } |
| 1413 | /* For backwards compatibility, allow idioms such as |
| 1414 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1415 | */ |
| 1416 | const char msg[] = |
| 1417 | "invalid signal number %ld, please use valid_signals()"; |
| 1418 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1419 | goto error; |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | if (!PyErr_Occurred()) { |
| 1424 | Py_DECREF(iterator); |
| 1425 | return 1; |
| 1426 | } |
| 1427 | |
| 1428 | error: |
| 1429 | Py_DECREF(iterator); |
| 1430 | return 0; |
| 1431 | } |
| 1432 | #endif /* HAVE_SIGSET_T */ |
| 1433 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1434 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1435 | |
| 1436 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1437 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1438 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1439 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1440 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1441 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1442 | |
| 1443 | if (0 == DeviceIoControl( |
| 1444 | reparse_point_handle, |
| 1445 | FSCTL_GET_REPARSE_POINT, |
| 1446 | NULL, 0, /* in buffer */ |
| 1447 | target_buffer, sizeof(target_buffer), |
| 1448 | &n_bytes_returned, |
| 1449 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1450 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1451 | |
| 1452 | if (reparse_tag) |
| 1453 | *reparse_tag = rdb->ReparseTag; |
| 1454 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1455 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1456 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1457 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1458 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1459 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1460 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1461 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1462 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1463 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1464 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1465 | */ |
| 1466 | #include <crt_externs.h> |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1467 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1468 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1469 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1470 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1471 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1472 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1473 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1474 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1475 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1476 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1477 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1478 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1479 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1480 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1481 | d = PyDict_New(); |
| 1482 | if (d == NULL) |
| 1483 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1484 | #ifdef MS_WINDOWS |
| 1485 | /* _wenviron must be initialized in this way if the program is started |
| 1486 | through main() instead of wmain(). */ |
| 1487 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1488 | e = _wenviron; |
Benoit Hudson | 723f71a | 2019-12-06 14:15:03 -0500 | [diff] [blame] | 1489 | #elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
| 1490 | /* environ is not accessible as an extern in a shared object on OSX; use |
| 1491 | _NSGetEnviron to resolve it. The value changes if you add environment |
| 1492 | variables between calls to Py_Initialize, so don't cache the value. */ |
| 1493 | e = *_NSGetEnviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1494 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1495 | e = environ; |
| 1496 | #endif |
| 1497 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1498 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1499 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1500 | PyObject *k; |
| 1501 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1502 | #ifdef MS_WINDOWS |
| 1503 | const wchar_t *p = wcschr(*e, L'='); |
| 1504 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1505 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1506 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1507 | if (p == NULL) |
| 1508 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1509 | #ifdef MS_WINDOWS |
| 1510 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1511 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1512 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1513 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1514 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1515 | Py_DECREF(d); |
| 1516 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1517 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1518 | #ifdef MS_WINDOWS |
| 1519 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1520 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1521 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1522 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1523 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1524 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1525 | Py_DECREF(d); |
| 1526 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1527 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1528 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1529 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1530 | Py_DECREF(v); |
| 1531 | Py_DECREF(k); |
| 1532 | Py_DECREF(d); |
| 1533 | return NULL; |
| 1534 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1535 | } |
| 1536 | Py_DECREF(k); |
| 1537 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1538 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1539 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1542 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1543 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1544 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1545 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1546 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1547 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1548 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1549 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1550 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1551 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1552 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1553 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1554 | /* XXX We should pass the function name along in the future. |
| 1555 | (winreg.c also wants to pass the function name.) |
| 1556 | This would however require an additional param to the |
| 1557 | Windows error object, which is non-trivial. |
| 1558 | */ |
| 1559 | errno = GetLastError(); |
| 1560 | if (filename) |
| 1561 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1562 | else |
| 1563 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1564 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1565 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1566 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1567 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1568 | { |
| 1569 | /* XXX - see win32_error for comments on 'function' */ |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1570 | if (filename) |
| 1571 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1572 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1573 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1574 | filename); |
| 1575 | else |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1576 | return PyErr_SetFromWindowsErr(err); |
| 1577 | } |
| 1578 | |
| 1579 | static PyObject * |
| 1580 | win32_error_object(const char* function, PyObject* filename) |
| 1581 | { |
| 1582 | errno = GetLastError(); |
| 1583 | return win32_error_object_err(function, filename, errno); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1584 | } |
| 1585 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1586 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1587 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1588 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1589 | posix_path_object_error(PyObject *path) |
| 1590 | { |
| 1591 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1592 | } |
| 1593 | |
| 1594 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1595 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1596 | { |
| 1597 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1598 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1599 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1600 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1601 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1602 | #endif |
| 1603 | } |
| 1604 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1605 | static PyObject * |
| 1606 | path_object_error2(PyObject *path, PyObject *path2) |
| 1607 | { |
| 1608 | #ifdef MS_WINDOWS |
| 1609 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1610 | PyExc_OSError, 0, path, path2); |
| 1611 | #else |
| 1612 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1613 | #endif |
| 1614 | } |
| 1615 | |
| 1616 | static PyObject * |
| 1617 | path_error(path_t *path) |
| 1618 | { |
| 1619 | return path_object_error(path->object); |
| 1620 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1621 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1622 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1623 | posix_path_error(path_t *path) |
| 1624 | { |
| 1625 | return posix_path_object_error(path->object); |
| 1626 | } |
| 1627 | |
| 1628 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1629 | path_error2(path_t *path, path_t *path2) |
| 1630 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1631 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1635 | /* POSIX generic methods */ |
| 1636 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1637 | static PyObject * |
| 1638 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1639 | { |
| 1640 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1641 | int async_err = 0; |
| 1642 | |
| 1643 | do { |
| 1644 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1645 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1646 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1647 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1648 | Py_END_ALLOW_THREADS |
| 1649 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1650 | if (res != 0) |
| 1651 | return (!async_err) ? posix_error() : NULL; |
| 1652 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1653 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1654 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1655 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1656 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1657 | /* This is a reimplementation of the C library's chdir function, |
| 1658 | but one that produces Win32 errors instead of DOS error codes. |
| 1659 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1660 | it also needs to set "magic" environment variables indicating |
| 1661 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1662 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1663 | win32_wchdir(LPCWSTR path) |
| 1664 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1665 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1666 | int result; |
| 1667 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1668 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1669 | if(!SetCurrentDirectoryW(path)) |
| 1670 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1671 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1672 | if (!result) |
| 1673 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1674 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1675 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1676 | if (!new_path) { |
| 1677 | SetLastError(ERROR_OUTOFMEMORY); |
| 1678 | return FALSE; |
| 1679 | } |
| 1680 | result = GetCurrentDirectoryW(result, new_path); |
| 1681 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1682 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1683 | return FALSE; |
| 1684 | } |
| 1685 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1686 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1687 | wcsncmp(new_path, L"//", 2) == 0); |
| 1688 | if (!is_unc_like_path) { |
| 1689 | env[1] = new_path[0]; |
| 1690 | result = SetEnvironmentVariableW(env, new_path); |
| 1691 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1692 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1693 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1694 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1695 | } |
| 1696 | #endif |
| 1697 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1698 | #ifdef MS_WINDOWS |
| 1699 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1700 | - time stamps are restricted to second resolution |
| 1701 | - file modification times suffer from forth-and-back conversions between |
| 1702 | UTC and local time |
| 1703 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1704 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1705 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1706 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1707 | #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1708 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1709 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1710 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1711 | BY_HANDLE_FILE_INFORMATION *info, |
| 1712 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1713 | { |
| 1714 | memset(info, 0, sizeof(*info)); |
| 1715 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1716 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1717 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1718 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1719 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1720 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1721 | /* info->nNumberOfLinks = 1; */ |
| 1722 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1723 | *reparse_tag = pFileData->dwReserved0; |
| 1724 | else |
| 1725 | *reparse_tag = 0; |
| 1726 | } |
| 1727 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1728 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1729 | 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] | 1730 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1731 | HANDLE hFindFile; |
| 1732 | WIN32_FIND_DATAW FileData; |
| 1733 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1734 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1735 | return FALSE; |
| 1736 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1737 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1738 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1741 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1742 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1743 | BOOL traverse) |
| 1744 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1745 | HANDLE hFile; |
| 1746 | BY_HANDLE_FILE_INFORMATION fileInfo; |
| 1747 | FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 }; |
| 1748 | DWORD fileType, error; |
| 1749 | BOOL isUnhandledTag = FALSE; |
| 1750 | int retval = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1751 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1752 | DWORD access = FILE_READ_ATTRIBUTES; |
| 1753 | DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */ |
| 1754 | if (!traverse) { |
| 1755 | flags |= FILE_FLAG_OPEN_REPARSE_POINT; |
| 1756 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1757 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1758 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1759 | if (hFile == INVALID_HANDLE_VALUE) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1760 | /* Either the path doesn't exist, or the caller lacks access. */ |
| 1761 | error = GetLastError(); |
| 1762 | switch (error) { |
| 1763 | case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */ |
| 1764 | case ERROR_SHARING_VIOLATION: /* It's a paging file. */ |
| 1765 | /* Try reading the parent directory. */ |
| 1766 | if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) { |
| 1767 | /* Cannot read the parent directory. */ |
| 1768 | SetLastError(error); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1769 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1770 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1771 | if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1772 | if (traverse || |
| 1773 | !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1774 | /* The stat call has to traverse but cannot, so fail. */ |
| 1775 | SetLastError(error); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1776 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1777 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1778 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1779 | break; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1780 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1781 | case ERROR_INVALID_PARAMETER: |
| 1782 | /* \\.\con requires read or write access. */ |
| 1783 | hFile = CreateFileW(path, access | GENERIC_READ, |
| 1784 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1785 | OPEN_EXISTING, flags, NULL); |
| 1786 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1787 | SetLastError(error); |
| 1788 | return -1; |
| 1789 | } |
| 1790 | break; |
| 1791 | |
| 1792 | case ERROR_CANT_ACCESS_FILE: |
| 1793 | /* bpo37834: open unhandled reparse points if traverse fails. */ |
| 1794 | if (traverse) { |
| 1795 | traverse = FALSE; |
| 1796 | isUnhandledTag = TRUE; |
| 1797 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, |
| 1798 | flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 1799 | } |
| 1800 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1801 | SetLastError(error); |
| 1802 | return -1; |
| 1803 | } |
| 1804 | break; |
| 1805 | |
| 1806 | default: |
| 1807 | return -1; |
| 1808 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1809 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1810 | |
| 1811 | if (hFile != INVALID_HANDLE_VALUE) { |
| 1812 | /* Handle types other than files on disk. */ |
| 1813 | fileType = GetFileType(hFile); |
| 1814 | if (fileType != FILE_TYPE_DISK) { |
| 1815 | if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) { |
| 1816 | retval = -1; |
| 1817 | goto cleanup; |
| 1818 | } |
| 1819 | DWORD fileAttributes = GetFileAttributesW(path); |
| 1820 | memset(result, 0, sizeof(*result)); |
| 1821 | if (fileAttributes != INVALID_FILE_ATTRIBUTES && |
| 1822 | fileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 1823 | /* \\.\pipe\ or \\.\mailslot\ */ |
| 1824 | result->st_mode = _S_IFDIR; |
| 1825 | } else if (fileType == FILE_TYPE_CHAR) { |
| 1826 | /* \\.\nul */ |
| 1827 | result->st_mode = _S_IFCHR; |
| 1828 | } else if (fileType == FILE_TYPE_PIPE) { |
| 1829 | /* \\.\pipe\spam */ |
| 1830 | result->st_mode = _S_IFIFO; |
| 1831 | } |
| 1832 | /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */ |
| 1833 | goto cleanup; |
| 1834 | } |
| 1835 | |
| 1836 | /* Query the reparse tag, and traverse a non-link. */ |
| 1837 | if (!traverse) { |
| 1838 | if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, |
| 1839 | &tagInfo, sizeof(tagInfo))) { |
| 1840 | /* Allow devices that do not support FileAttributeTagInfo. */ |
| 1841 | switch (GetLastError()) { |
| 1842 | case ERROR_INVALID_PARAMETER: |
| 1843 | case ERROR_INVALID_FUNCTION: |
| 1844 | case ERROR_NOT_SUPPORTED: |
| 1845 | tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL; |
| 1846 | tagInfo.ReparseTag = 0; |
| 1847 | break; |
| 1848 | default: |
| 1849 | retval = -1; |
| 1850 | goto cleanup; |
| 1851 | } |
| 1852 | } else if (tagInfo.FileAttributes & |
| 1853 | FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1854 | if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1855 | if (isUnhandledTag) { |
| 1856 | /* Traversing previously failed for either this link |
| 1857 | or its target. */ |
| 1858 | SetLastError(ERROR_CANT_ACCESS_FILE); |
| 1859 | retval = -1; |
| 1860 | goto cleanup; |
| 1861 | } |
| 1862 | /* Traverse a non-link, but not if traversing already failed |
| 1863 | for an unhandled tag. */ |
| 1864 | } else if (!isUnhandledTag) { |
| 1865 | CloseHandle(hFile); |
| 1866 | return win32_xstat_impl(path, result, TRUE); |
| 1867 | } |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | if (!GetFileInformationByHandle(hFile, &fileInfo)) { |
| 1872 | switch (GetLastError()) { |
| 1873 | case ERROR_INVALID_PARAMETER: |
| 1874 | case ERROR_INVALID_FUNCTION: |
| 1875 | case ERROR_NOT_SUPPORTED: |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1876 | /* Volumes and physical disks are block devices, e.g. |
| 1877 | \\.\C: and \\.\PhysicalDrive0. */ |
| 1878 | memset(result, 0, sizeof(*result)); |
| 1879 | result->st_mode = 0x6000; /* S_IFBLK */ |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1880 | goto cleanup; |
| 1881 | } |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1882 | retval = -1; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1883 | goto cleanup; |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result); |
| 1888 | |
| 1889 | if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1890 | /* Fix the file execute permissions. This hack sets S_IEXEC if |
| 1891 | the filename has an extension that is commonly used by files |
| 1892 | that CreateProcessW can execute. A real implementation calls |
| 1893 | GetSecurityInfo, OpenThreadToken/OpenProcessToken, and |
| 1894 | AccessCheck to check for generic read, write, and execute |
| 1895 | access. */ |
| 1896 | const wchar_t *fileExtension = wcsrchr(path, '.'); |
| 1897 | if (fileExtension) { |
| 1898 | if (_wcsicmp(fileExtension, L".exe") == 0 || |
| 1899 | _wcsicmp(fileExtension, L".bat") == 0 || |
| 1900 | _wcsicmp(fileExtension, L".cmd") == 0 || |
| 1901 | _wcsicmp(fileExtension, L".com") == 0) { |
| 1902 | result->st_mode |= 0111; |
| 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | cleanup: |
| 1908 | if (hFile != INVALID_HANDLE_VALUE) { |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1909 | /* Preserve last error if we are failing */ |
| 1910 | error = retval ? GetLastError() : 0; |
| 1911 | if (!CloseHandle(hFile)) { |
| 1912 | retval = -1; |
| 1913 | } else if (retval) { |
| 1914 | /* Restore last error */ |
| 1915 | SetLastError(error); |
| 1916 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | return retval; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1923 | 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] | 1924 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1925 | /* Protocol violation: we explicitly clear errno, instead of |
| 1926 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1927 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1928 | errno = 0; |
| 1929 | return code; |
| 1930 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1931 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1932 | |
| 1933 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1934 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1935 | default does not traverse symlinks and instead returns attributes for |
| 1936 | the symlink. |
| 1937 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1938 | Instead, we will open the file (which *does* traverse symlinks by default) |
| 1939 | and GetFileInformationByHandle(). */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1940 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1941 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1942 | 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] | 1943 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1944 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1947 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1948 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1949 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1950 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1953 | #endif /* MS_WINDOWS */ |
| 1954 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1955 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1956 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1957 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1958 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1959 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1960 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1961 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1962 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1963 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1964 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1965 | |
| 1966 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1967 | {"st_mode", "protection bits"}, |
| 1968 | {"st_ino", "inode"}, |
| 1969 | {"st_dev", "device"}, |
| 1970 | {"st_nlink", "number of hard links"}, |
| 1971 | {"st_uid", "user ID of owner"}, |
| 1972 | {"st_gid", "group ID of owner"}, |
| 1973 | {"st_size", "total size, in bytes"}, |
| 1974 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1975 | {NULL, "integer time of last access"}, |
| 1976 | {NULL, "integer time of last modification"}, |
| 1977 | {NULL, "integer time of last change"}, |
| 1978 | {"st_atime", "time of last access"}, |
| 1979 | {"st_mtime", "time of last modification"}, |
| 1980 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1981 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1982 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1983 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1984 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1985 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1986 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1987 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1988 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1989 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1990 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1991 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1992 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1993 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1994 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1995 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1996 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1997 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1998 | #endif |
| 1999 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2000 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2001 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2002 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2003 | {"st_file_attributes", "Windows file attribute bits"}, |
| 2004 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2005 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2006 | {"st_fstype", "Type of filesystem"}, |
| 2007 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2008 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2009 | {"st_reparse_tag", "Windows reparse tag"}, |
| 2010 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2011 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2012 | }; |
| 2013 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2014 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2015 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2016 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2017 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2018 | #endif |
| 2019 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2020 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2021 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 2022 | #else |
| 2023 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 2024 | #endif |
| 2025 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2026 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2027 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 2028 | #else |
| 2029 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 2030 | #endif |
| 2031 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2032 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 2033 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 2034 | #else |
| 2035 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 2036 | #endif |
| 2037 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2038 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2039 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2040 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2041 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2042 | #endif |
| 2043 | |
| 2044 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 2045 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 2046 | #else |
| 2047 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 2048 | #endif |
| 2049 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2050 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2051 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 2052 | #else |
| 2053 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 2054 | #endif |
| 2055 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2056 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2057 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 2058 | #else |
| 2059 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 2060 | #endif |
| 2061 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2062 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2063 | #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1) |
| 2064 | #else |
| 2065 | #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX |
| 2066 | #endif |
| 2067 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2068 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2069 | "stat_result", /* name */ |
| 2070 | stat_result__doc__, /* doc */ |
| 2071 | stat_result_fields, |
| 2072 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2073 | }; |
| 2074 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2075 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2076 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2077 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2078 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2079 | 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] | 2080 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2081 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2082 | |
| 2083 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2084 | {"f_bsize", }, |
| 2085 | {"f_frsize", }, |
| 2086 | {"f_blocks", }, |
| 2087 | {"f_bfree", }, |
| 2088 | {"f_bavail", }, |
| 2089 | {"f_files", }, |
| 2090 | {"f_ffree", }, |
| 2091 | {"f_favail", }, |
| 2092 | {"f_flag", }, |
| 2093 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 2094 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2095 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2096 | }; |
| 2097 | |
| 2098 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2099 | "statvfs_result", /* name */ |
| 2100 | statvfs_result__doc__, /* doc */ |
| 2101 | statvfs_result_fields, |
| 2102 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2103 | }; |
| 2104 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2105 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2106 | PyDoc_STRVAR(waitid_result__doc__, |
| 2107 | "waitid_result: Result from waitid.\n\n\ |
| 2108 | This object may be accessed either as a tuple of\n\ |
| 2109 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2110 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2111 | \n\ |
| 2112 | See os.waitid for more information."); |
| 2113 | |
| 2114 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2115 | {"si_pid", }, |
| 2116 | {"si_uid", }, |
| 2117 | {"si_signo", }, |
| 2118 | {"si_status", }, |
| 2119 | {"si_code", }, |
| 2120 | {0} |
| 2121 | }; |
| 2122 | |
| 2123 | static PyStructSequence_Desc waitid_result_desc = { |
| 2124 | "waitid_result", /* name */ |
| 2125 | waitid_result__doc__, /* doc */ |
| 2126 | waitid_result_fields, |
| 2127 | 5 |
| 2128 | }; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2129 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2130 | static newfunc structseq_new; |
| 2131 | |
| 2132 | static PyObject * |
| 2133 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2134 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2135 | PyStructSequence *result; |
| 2136 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2137 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2138 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2139 | if (!result) |
| 2140 | return NULL; |
| 2141 | /* If we have been initialized from a tuple, |
| 2142 | st_?time might be set to None. Initialize it |
| 2143 | from the int slots. */ |
| 2144 | for (i = 7; i <= 9; i++) { |
| 2145 | if (result->ob_item[i+3] == Py_None) { |
| 2146 | Py_DECREF(Py_None); |
| 2147 | Py_INCREF(result->ob_item[i]); |
| 2148 | result->ob_item[i+3] = result->ob_item[i]; |
| 2149 | } |
| 2150 | } |
| 2151 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2154 | static int |
| 2155 | _posix_clear(PyObject *module) |
| 2156 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2157 | _posixstate *state = get_posix_state(module); |
| 2158 | Py_CLEAR(state->billion); |
| 2159 | Py_CLEAR(state->DirEntryType); |
| 2160 | Py_CLEAR(state->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2161 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2162 | Py_CLEAR(state->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2163 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2164 | Py_CLEAR(state->StatResultType); |
| 2165 | Py_CLEAR(state->StatVFSResultType); |
| 2166 | Py_CLEAR(state->TerminalSizeType); |
| 2167 | Py_CLEAR(state->TimesResultType); |
| 2168 | Py_CLEAR(state->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2169 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2170 | Py_CLEAR(state->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2171 | #endif |
| 2172 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2173 | Py_CLEAR(state->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2174 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2175 | Py_CLEAR(state->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2176 | return 0; |
| 2177 | } |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2178 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2179 | static int |
| 2180 | _posix_traverse(PyObject *module, visitproc visit, void *arg) |
| 2181 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2182 | _posixstate *state = get_posix_state(module); |
| 2183 | Py_VISIT(state->billion); |
| 2184 | Py_VISIT(state->DirEntryType); |
| 2185 | Py_VISIT(state->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2186 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2187 | Py_VISIT(state->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2188 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2189 | Py_VISIT(state->StatResultType); |
| 2190 | Py_VISIT(state->StatVFSResultType); |
| 2191 | Py_VISIT(state->TerminalSizeType); |
| 2192 | Py_VISIT(state->TimesResultType); |
| 2193 | Py_VISIT(state->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2194 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2195 | Py_VISIT(state->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2196 | #endif |
| 2197 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2198 | Py_VISIT(state->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2199 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2200 | Py_VISIT(state->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2201 | return 0; |
| 2202 | } |
| 2203 | |
| 2204 | static void |
| 2205 | _posix_free(void *module) |
| 2206 | { |
| 2207 | _posix_clear((PyObject *)module); |
| 2208 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2209 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2210 | static void |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2211 | fill_time(PyObject *module, PyObject *v, int index, time_t sec, unsigned long nsec) |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2212 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2213 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2214 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2215 | PyObject *s_in_ns = NULL; |
| 2216 | PyObject *ns_total = NULL; |
| 2217 | PyObject *float_s = NULL; |
| 2218 | |
| 2219 | if (!(s && ns_fractional)) |
| 2220 | goto exit; |
| 2221 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2222 | s_in_ns = PyNumber_Multiply(s, get_posix_state(module)->billion); |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2223 | if (!s_in_ns) |
| 2224 | goto exit; |
| 2225 | |
| 2226 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2227 | if (!ns_total) |
| 2228 | goto exit; |
| 2229 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2230 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2231 | if (!float_s) { |
| 2232 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | PyStructSequence_SET_ITEM(v, index, s); |
| 2236 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2237 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2238 | s = NULL; |
| 2239 | float_s = NULL; |
| 2240 | ns_total = NULL; |
| 2241 | exit: |
| 2242 | Py_XDECREF(s); |
| 2243 | Py_XDECREF(ns_fractional); |
| 2244 | Py_XDECREF(s_in_ns); |
| 2245 | Py_XDECREF(ns_total); |
| 2246 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2249 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2250 | (used by posix_stat() and posix_fstat()) */ |
| 2251 | static PyObject* |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2252 | _pystat_fromstructstat(PyObject *module, STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2253 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2254 | unsigned long ansec, mnsec, cnsec; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2255 | PyObject *StatResultType = get_posix_state(module)->StatResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2256 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2257 | if (v == NULL) |
| 2258 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2259 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2260 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2261 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2262 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2263 | #ifdef MS_WINDOWS |
| 2264 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2265 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2266 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2267 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2268 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2269 | #if defined(MS_WINDOWS) |
| 2270 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2271 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2272 | #else |
| 2273 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2274 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2275 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2276 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2277 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2278 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2279 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2280 | ansec = st->st_atim.tv_nsec; |
| 2281 | mnsec = st->st_mtim.tv_nsec; |
| 2282 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2283 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2284 | ansec = st->st_atimespec.tv_nsec; |
| 2285 | mnsec = st->st_mtimespec.tv_nsec; |
| 2286 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2287 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2288 | ansec = st->st_atime_nsec; |
| 2289 | mnsec = st->st_mtime_nsec; |
| 2290 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2291 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2292 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2293 | #endif |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2294 | fill_time(module, v, 7, st->st_atime, ansec); |
| 2295 | fill_time(module, v, 8, st->st_mtime, mnsec); |
| 2296 | fill_time(module, v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2297 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2298 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2299 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2300 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2301 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2302 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2303 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2304 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2305 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2306 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2307 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2308 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2309 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2310 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2311 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2312 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2313 | #endif |
| 2314 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2315 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2316 | PyObject *val; |
| 2317 | unsigned long bsec,bnsec; |
| 2318 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2319 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2320 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2321 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2322 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2323 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2324 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2325 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2326 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2327 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2328 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2329 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2330 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2331 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2332 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2333 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2334 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2335 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2336 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2337 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2338 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2339 | PyUnicode_FromString(st->st_fstype)); |
| 2340 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2341 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2342 | PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX, |
| 2343 | PyLong_FromUnsignedLong(st->st_reparse_tag)); |
| 2344 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2345 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2346 | if (PyErr_Occurred()) { |
| 2347 | Py_DECREF(v); |
| 2348 | return NULL; |
| 2349 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2350 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2351 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2354 | /* POSIX methods */ |
| 2355 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2356 | |
| 2357 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2358 | posix_do_stat(PyObject *module, const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2359 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2360 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2361 | STRUCT_STAT st; |
| 2362 | int result; |
| 2363 | |
| 2364 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2365 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2366 | return NULL; |
| 2367 | #endif |
| 2368 | |
| 2369 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2370 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2371 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2372 | return NULL; |
| 2373 | |
| 2374 | Py_BEGIN_ALLOW_THREADS |
| 2375 | if (path->fd != -1) |
| 2376 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2377 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2378 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2379 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2380 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2381 | result = win32_lstat(path->wide, &st); |
| 2382 | #else |
| 2383 | else |
| 2384 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2385 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2386 | result = LSTAT(path->narrow, &st); |
| 2387 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2388 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2389 | #ifdef HAVE_FSTATAT |
| 2390 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2391 | result = fstatat(dir_fd, path->narrow, &st, |
| 2392 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2393 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2394 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2395 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2396 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2397 | Py_END_ALLOW_THREADS |
| 2398 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2399 | if (result != 0) { |
| 2400 | return path_error(path); |
| 2401 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2402 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2403 | return _pystat_fromstructstat(module, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2406 | /*[python input] |
| 2407 | |
| 2408 | for s in """ |
| 2409 | |
| 2410 | FACCESSAT |
| 2411 | FCHMODAT |
| 2412 | FCHOWNAT |
| 2413 | FSTATAT |
| 2414 | LINKAT |
| 2415 | MKDIRAT |
| 2416 | MKFIFOAT |
| 2417 | MKNODAT |
| 2418 | OPENAT |
| 2419 | READLINKAT |
| 2420 | SYMLINKAT |
| 2421 | UNLINKAT |
| 2422 | |
| 2423 | """.strip().split(): |
| 2424 | s = s.strip() |
| 2425 | print(""" |
| 2426 | #ifdef HAVE_{s} |
| 2427 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2428 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2429 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2430 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2431 | """.rstrip().format(s=s)) |
| 2432 | |
| 2433 | for s in """ |
| 2434 | |
| 2435 | FCHDIR |
| 2436 | FCHMOD |
| 2437 | FCHOWN |
| 2438 | FDOPENDIR |
| 2439 | FEXECVE |
| 2440 | FPATHCONF |
| 2441 | FSTATVFS |
| 2442 | FTRUNCATE |
| 2443 | |
| 2444 | """.strip().split(): |
| 2445 | s = s.strip() |
| 2446 | print(""" |
| 2447 | #ifdef HAVE_{s} |
| 2448 | #define PATH_HAVE_{s} 1 |
| 2449 | #else |
| 2450 | #define PATH_HAVE_{s} 0 |
| 2451 | #endif |
| 2452 | |
| 2453 | """.rstrip().format(s=s)) |
| 2454 | [python start generated code]*/ |
| 2455 | |
| 2456 | #ifdef HAVE_FACCESSAT |
| 2457 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2458 | #else |
| 2459 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2460 | #endif |
| 2461 | |
| 2462 | #ifdef HAVE_FCHMODAT |
| 2463 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2464 | #else |
| 2465 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2466 | #endif |
| 2467 | |
| 2468 | #ifdef HAVE_FCHOWNAT |
| 2469 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2470 | #else |
| 2471 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2472 | #endif |
| 2473 | |
| 2474 | #ifdef HAVE_FSTATAT |
| 2475 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2476 | #else |
| 2477 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2478 | #endif |
| 2479 | |
| 2480 | #ifdef HAVE_LINKAT |
| 2481 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2482 | #else |
| 2483 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2484 | #endif |
| 2485 | |
| 2486 | #ifdef HAVE_MKDIRAT |
| 2487 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2488 | #else |
| 2489 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2490 | #endif |
| 2491 | |
| 2492 | #ifdef HAVE_MKFIFOAT |
| 2493 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2494 | #else |
| 2495 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2496 | #endif |
| 2497 | |
| 2498 | #ifdef HAVE_MKNODAT |
| 2499 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2500 | #else |
| 2501 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2502 | #endif |
| 2503 | |
| 2504 | #ifdef HAVE_OPENAT |
| 2505 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2506 | #else |
| 2507 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2508 | #endif |
| 2509 | |
| 2510 | #ifdef HAVE_READLINKAT |
| 2511 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2512 | #else |
| 2513 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2514 | #endif |
| 2515 | |
| 2516 | #ifdef HAVE_SYMLINKAT |
| 2517 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2518 | #else |
| 2519 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2520 | #endif |
| 2521 | |
| 2522 | #ifdef HAVE_UNLINKAT |
| 2523 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2524 | #else |
| 2525 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2526 | #endif |
| 2527 | |
| 2528 | #ifdef HAVE_FCHDIR |
| 2529 | #define PATH_HAVE_FCHDIR 1 |
| 2530 | #else |
| 2531 | #define PATH_HAVE_FCHDIR 0 |
| 2532 | #endif |
| 2533 | |
| 2534 | #ifdef HAVE_FCHMOD |
| 2535 | #define PATH_HAVE_FCHMOD 1 |
| 2536 | #else |
| 2537 | #define PATH_HAVE_FCHMOD 0 |
| 2538 | #endif |
| 2539 | |
| 2540 | #ifdef HAVE_FCHOWN |
| 2541 | #define PATH_HAVE_FCHOWN 1 |
| 2542 | #else |
| 2543 | #define PATH_HAVE_FCHOWN 0 |
| 2544 | #endif |
| 2545 | |
| 2546 | #ifdef HAVE_FDOPENDIR |
| 2547 | #define PATH_HAVE_FDOPENDIR 1 |
| 2548 | #else |
| 2549 | #define PATH_HAVE_FDOPENDIR 0 |
| 2550 | #endif |
| 2551 | |
| 2552 | #ifdef HAVE_FEXECVE |
| 2553 | #define PATH_HAVE_FEXECVE 1 |
| 2554 | #else |
| 2555 | #define PATH_HAVE_FEXECVE 0 |
| 2556 | #endif |
| 2557 | |
| 2558 | #ifdef HAVE_FPATHCONF |
| 2559 | #define PATH_HAVE_FPATHCONF 1 |
| 2560 | #else |
| 2561 | #define PATH_HAVE_FPATHCONF 0 |
| 2562 | #endif |
| 2563 | |
| 2564 | #ifdef HAVE_FSTATVFS |
| 2565 | #define PATH_HAVE_FSTATVFS 1 |
| 2566 | #else |
| 2567 | #define PATH_HAVE_FSTATVFS 0 |
| 2568 | #endif |
| 2569 | |
| 2570 | #ifdef HAVE_FTRUNCATE |
| 2571 | #define PATH_HAVE_FTRUNCATE 1 |
| 2572 | #else |
| 2573 | #define PATH_HAVE_FTRUNCATE 0 |
| 2574 | #endif |
| 2575 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2576 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2577 | #ifdef MS_WINDOWS |
| 2578 | #undef PATH_HAVE_FTRUNCATE |
| 2579 | #define PATH_HAVE_FTRUNCATE 1 |
| 2580 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2581 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2582 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2583 | |
| 2584 | class path_t_converter(CConverter): |
| 2585 | |
| 2586 | type = "path_t" |
| 2587 | impl_by_reference = True |
| 2588 | parse_by_reference = True |
| 2589 | |
| 2590 | converter = 'path_converter' |
| 2591 | |
| 2592 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2593 | # right now path_t doesn't support default values. |
| 2594 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2595 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2596 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2597 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2598 | if self.c_default not in (None, 'Py_None'): |
| 2599 | 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] | 2600 | |
| 2601 | self.nullable = nullable |
| 2602 | self.allow_fd = allow_fd |
| 2603 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2604 | def pre_render(self): |
| 2605 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2606 | if isinstance(value, str): |
| 2607 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2608 | return str(int(bool(value))) |
| 2609 | |
| 2610 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2611 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2612 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2613 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2614 | strify(self.nullable), |
| 2615 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2616 | ) |
| 2617 | |
| 2618 | def cleanup(self): |
| 2619 | return "path_cleanup(&" + self.name + ");\n" |
| 2620 | |
| 2621 | |
| 2622 | class dir_fd_converter(CConverter): |
| 2623 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2624 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2625 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2626 | if self.default in (unspecified, None): |
| 2627 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2628 | if isinstance(requires, str): |
| 2629 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2630 | else: |
| 2631 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2632 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2633 | class uid_t_converter(CConverter): |
| 2634 | type = "uid_t" |
| 2635 | converter = '_Py_Uid_Converter' |
| 2636 | |
| 2637 | class gid_t_converter(CConverter): |
| 2638 | type = "gid_t" |
| 2639 | converter = '_Py_Gid_Converter' |
| 2640 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2641 | class dev_t_converter(CConverter): |
| 2642 | type = 'dev_t' |
| 2643 | converter = '_Py_Dev_Converter' |
| 2644 | |
| 2645 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2646 | type = 'dev_t' |
| 2647 | conversion_fn = '_PyLong_FromDev' |
| 2648 | unsigned_cast = '(dev_t)' |
| 2649 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2650 | class FSConverter_converter(CConverter): |
| 2651 | type = 'PyObject *' |
| 2652 | converter = 'PyUnicode_FSConverter' |
| 2653 | def converter_init(self): |
| 2654 | if self.default is not unspecified: |
| 2655 | fail("FSConverter_converter does not support default values") |
| 2656 | self.c_default = 'NULL' |
| 2657 | |
| 2658 | def cleanup(self): |
| 2659 | return "Py_XDECREF(" + self.name + ");\n" |
| 2660 | |
| 2661 | class pid_t_converter(CConverter): |
| 2662 | type = 'pid_t' |
| 2663 | format_unit = '" _Py_PARSE_PID "' |
| 2664 | |
| 2665 | class idtype_t_converter(int_converter): |
| 2666 | type = 'idtype_t' |
| 2667 | |
| 2668 | class id_t_converter(CConverter): |
| 2669 | type = 'id_t' |
| 2670 | format_unit = '" _Py_PARSE_PID "' |
| 2671 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2672 | class intptr_t_converter(CConverter): |
| 2673 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2674 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2675 | |
| 2676 | class Py_off_t_converter(CConverter): |
| 2677 | type = 'Py_off_t' |
| 2678 | converter = 'Py_off_t_converter' |
| 2679 | |
| 2680 | class Py_off_t_return_converter(long_return_converter): |
| 2681 | type = 'Py_off_t' |
| 2682 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2683 | |
| 2684 | class path_confname_converter(CConverter): |
| 2685 | type="int" |
| 2686 | converter="conv_path_confname" |
| 2687 | |
| 2688 | class confstr_confname_converter(path_confname_converter): |
| 2689 | converter='conv_confstr_confname' |
| 2690 | |
| 2691 | class sysconf_confname_converter(path_confname_converter): |
| 2692 | converter="conv_sysconf_confname" |
| 2693 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2694 | [python start generated code]*/ |
Serhiy Storchaka | 9975cc5 | 2020-10-09 23:00:45 +0300 | [diff] [blame^] | 2695 | /*[python end generated code: output=da39a3ee5e6b4b0d input=3338733161aa7879]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2696 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2697 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2698 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2699 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2700 | |
| 2701 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2702 | 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] | 2703 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2704 | |
| 2705 | * |
| 2706 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2707 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2708 | If not None, it should be a file descriptor open to a directory, |
| 2709 | and path should be a relative string; path will then be relative to |
| 2710 | that directory. |
| 2711 | |
| 2712 | follow_symlinks: bool = True |
| 2713 | If False, and the last element of the path is a symbolic link, |
| 2714 | stat will examine the symbolic link itself instead of the file |
| 2715 | the link points to. |
| 2716 | |
| 2717 | Perform a stat system call on the given path. |
| 2718 | |
| 2719 | dir_fd and follow_symlinks may not be implemented |
| 2720 | on your platform. If they are unavailable, using them will raise a |
| 2721 | NotImplementedError. |
| 2722 | |
| 2723 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2724 | an open file descriptor. |
| 2725 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2726 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2727 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2728 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2729 | 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] | 2730 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2731 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2732 | return posix_do_stat(module, "stat", path, dir_fd, follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2733 | } |
| 2734 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2735 | |
| 2736 | /*[clinic input] |
| 2737 | os.lstat |
| 2738 | |
| 2739 | path : path_t |
| 2740 | |
| 2741 | * |
| 2742 | |
| 2743 | dir_fd : dir_fd(requires='fstatat') = None |
| 2744 | |
| 2745 | Perform a stat system call on the given path, without following symbolic links. |
| 2746 | |
| 2747 | Like stat(), but do not follow symbolic links. |
| 2748 | Equivalent to stat(path, follow_symlinks=False). |
| 2749 | [clinic start generated code]*/ |
| 2750 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2751 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2752 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2753 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2754 | { |
| 2755 | int follow_symlinks = 0; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2756 | return posix_do_stat(module, "lstat", path, dir_fd, follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2757 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2758 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2759 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2760 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2761 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2762 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2763 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2764 | 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] | 2765 | |
| 2766 | mode: int |
| 2767 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2768 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2769 | |
| 2770 | * |
| 2771 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2772 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2773 | If not None, it should be a file descriptor open to a directory, |
| 2774 | and path should be relative; path will then be relative to that |
| 2775 | directory. |
| 2776 | |
| 2777 | effective_ids: bool = False |
| 2778 | If True, access will use the effective uid/gid instead of |
| 2779 | the real uid/gid. |
| 2780 | |
| 2781 | follow_symlinks: bool = True |
| 2782 | If False, and the last element of the path is a symbolic link, |
| 2783 | access will examine the symbolic link itself instead of the file |
| 2784 | the link points to. |
| 2785 | |
| 2786 | Use the real uid/gid to test for access to a path. |
| 2787 | |
| 2788 | {parameters} |
| 2789 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2790 | on your platform. If they are unavailable, using them will raise a |
| 2791 | NotImplementedError. |
| 2792 | |
| 2793 | Note that most operations will use the effective uid/gid, therefore this |
| 2794 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2795 | has the specified access to the path. |
| 2796 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2797 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2798 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2799 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2800 | 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] | 2801 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2802 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2803 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2804 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2805 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2806 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2807 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2808 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2809 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2810 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2811 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2812 | #ifndef HAVE_FACCESSAT |
| 2813 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2814 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2815 | |
| 2816 | if (effective_ids) { |
| 2817 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2818 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2819 | } |
| 2820 | #endif |
| 2821 | |
| 2822 | #ifdef MS_WINDOWS |
| 2823 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2824 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2825 | Py_END_ALLOW_THREADS |
| 2826 | |
| 2827 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2828 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2829 | * * we didn't get a -1, and |
| 2830 | * * write access wasn't requested, |
| 2831 | * * or the file isn't read-only, |
| 2832 | * * or it's a directory. |
| 2833 | * (Directories cannot be read-only on Windows.) |
| 2834 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2835 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2836 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2837 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2838 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2839 | #else |
| 2840 | |
| 2841 | Py_BEGIN_ALLOW_THREADS |
| 2842 | #ifdef HAVE_FACCESSAT |
| 2843 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2844 | effective_ids || |
| 2845 | !follow_symlinks) { |
| 2846 | int flags = 0; |
| 2847 | if (!follow_symlinks) |
| 2848 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2849 | if (effective_ids) |
| 2850 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2851 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2852 | } |
| 2853 | else |
| 2854 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2855 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2856 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2857 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2858 | #endif |
| 2859 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2860 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2863 | #ifndef F_OK |
| 2864 | #define F_OK 0 |
| 2865 | #endif |
| 2866 | #ifndef R_OK |
| 2867 | #define R_OK 4 |
| 2868 | #endif |
| 2869 | #ifndef W_OK |
| 2870 | #define W_OK 2 |
| 2871 | #endif |
| 2872 | #ifndef X_OK |
| 2873 | #define X_OK 1 |
| 2874 | #endif |
| 2875 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2876 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2877 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2878 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2879 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2880 | |
| 2881 | fd: int |
| 2882 | Integer file descriptor handle. |
| 2883 | |
| 2884 | / |
| 2885 | |
| 2886 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2887 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2888 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2889 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2890 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2891 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2892 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2893 | |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2894 | long size = sysconf(_SC_TTY_NAME_MAX); |
| 2895 | if (size == -1) { |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2896 | return posix_error(); |
| 2897 | } |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2898 | char *buffer = (char *)PyMem_RawMalloc(size); |
| 2899 | if (buffer == NULL) { |
| 2900 | return PyErr_NoMemory(); |
| 2901 | } |
| 2902 | int ret = ttyname_r(fd, buffer, size); |
| 2903 | if (ret != 0) { |
| 2904 | PyMem_RawFree(buffer); |
| 2905 | errno = ret; |
| 2906 | return posix_error(); |
| 2907 | } |
| 2908 | PyObject *res = PyUnicode_DecodeFSDefault(buffer); |
| 2909 | PyMem_RawFree(buffer); |
| 2910 | return res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2911 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2912 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2913 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2914 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2915 | /*[clinic input] |
| 2916 | os.ctermid |
| 2917 | |
| 2918 | Return the name of the controlling terminal for this process. |
| 2919 | [clinic start generated code]*/ |
| 2920 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2921 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2922 | os_ctermid_impl(PyObject *module) |
| 2923 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2924 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2925 | char *ret; |
| 2926 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2927 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2928 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2929 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2930 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2931 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2932 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2933 | if (ret == NULL) |
| 2934 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2935 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2936 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2937 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2938 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2939 | |
| 2940 | /*[clinic input] |
| 2941 | os.chdir |
| 2942 | |
| 2943 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2944 | |
| 2945 | Change the current working directory to the specified path. |
| 2946 | |
| 2947 | path may always be specified as a string. |
| 2948 | On some platforms, path may also be specified as an open file descriptor. |
| 2949 | If this functionality is unavailable, using it raises an exception. |
| 2950 | [clinic start generated code]*/ |
| 2951 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2952 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2953 | os_chdir_impl(PyObject *module, path_t *path) |
| 2954 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2955 | { |
| 2956 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2957 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 2958 | if (PySys_Audit("os.chdir", "(O)", path->object) < 0) { |
| 2959 | return NULL; |
| 2960 | } |
| 2961 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2962 | Py_BEGIN_ALLOW_THREADS |
| 2963 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2964 | /* on unix, success = 0, on windows, success = !0 */ |
| 2965 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2966 | #else |
| 2967 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2968 | if (path->fd != -1) |
| 2969 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2970 | else |
| 2971 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2972 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2973 | #endif |
| 2974 | Py_END_ALLOW_THREADS |
| 2975 | |
| 2976 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2977 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2980 | Py_RETURN_NONE; |
| 2981 | } |
| 2982 | |
| 2983 | |
| 2984 | #ifdef HAVE_FCHDIR |
| 2985 | /*[clinic input] |
| 2986 | os.fchdir |
| 2987 | |
| 2988 | fd: fildes |
| 2989 | |
| 2990 | Change to the directory of the given file descriptor. |
| 2991 | |
| 2992 | fd must be opened on a directory, not a file. |
| 2993 | Equivalent to os.chdir(fd). |
| 2994 | |
| 2995 | [clinic start generated code]*/ |
| 2996 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2997 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2998 | os_fchdir_impl(PyObject *module, int fd) |
| 2999 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3000 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3001 | if (PySys_Audit("os.chdir", "(i)", fd) < 0) { |
| 3002 | return NULL; |
| 3003 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3004 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3005 | } |
| 3006 | #endif /* HAVE_FCHDIR */ |
| 3007 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3008 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3009 | /*[clinic input] |
| 3010 | os.chmod |
| 3011 | |
| 3012 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3013 | 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] | 3014 | On some platforms, path may also be specified as an open file descriptor. |
| 3015 | If this functionality is unavailable, using it raises an exception. |
| 3016 | |
| 3017 | mode: int |
| 3018 | Operating-system mode bitfield. |
| 3019 | |
| 3020 | * |
| 3021 | |
| 3022 | dir_fd : dir_fd(requires='fchmodat') = None |
| 3023 | If not None, it should be a file descriptor open to a directory, |
| 3024 | and path should be relative; path will then be relative to that |
| 3025 | directory. |
| 3026 | |
| 3027 | follow_symlinks: bool = True |
| 3028 | If False, and the last element of the path is a symbolic link, |
| 3029 | chmod will modify the symbolic link itself instead of the file |
| 3030 | the link points to. |
| 3031 | |
| 3032 | Change the access permissions of a file. |
| 3033 | |
| 3034 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3035 | an open file descriptor. |
| 3036 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3037 | If they are unavailable, using them will raise a NotImplementedError. |
| 3038 | |
| 3039 | [clinic start generated code]*/ |
| 3040 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3041 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3042 | 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] | 3043 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3044 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3045 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3046 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3047 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3048 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3049 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3050 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3051 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3052 | #ifdef HAVE_FCHMODAT |
| 3053 | int fchmodat_nofollow_unsupported = 0; |
| 3054 | #endif |
| 3055 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3056 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 3057 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3058 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3059 | #endif |
| 3060 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3061 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, |
| 3062 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3063 | return NULL; |
| 3064 | } |
| 3065 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3066 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3067 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3068 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 3069 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3070 | result = 0; |
| 3071 | else { |
| 3072 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3073 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 3074 | else |
| 3075 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3076 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3077 | } |
| 3078 | Py_END_ALLOW_THREADS |
| 3079 | |
| 3080 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3081 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3082 | } |
| 3083 | #else /* MS_WINDOWS */ |
| 3084 | Py_BEGIN_ALLOW_THREADS |
| 3085 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3086 | if (path->fd != -1) |
| 3087 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3088 | else |
| 3089 | #endif |
| 3090 | #ifdef HAVE_LCHMOD |
| 3091 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3092 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3093 | else |
| 3094 | #endif |
| 3095 | #ifdef HAVE_FCHMODAT |
| 3096 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 3097 | /* |
| 3098 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 3099 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3100 | * and then says it isn't implemented yet. |
| 3101 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3102 | * |
| 3103 | * Once it is supported, os.chmod will automatically |
| 3104 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 3105 | * Until then, we need to be careful what exception we raise. |
| 3106 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3107 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3108 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3109 | /* |
| 3110 | * But wait! We can't throw the exception without allowing threads, |
| 3111 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 3112 | */ |
| 3113 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3114 | result && |
| 3115 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 3116 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3117 | } |
| 3118 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3119 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3120 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3121 | Py_END_ALLOW_THREADS |
| 3122 | |
| 3123 | if (result) { |
| 3124 | #ifdef HAVE_FCHMODAT |
| 3125 | if (fchmodat_nofollow_unsupported) { |
| 3126 | if (dir_fd != DEFAULT_DIR_FD) |
| 3127 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3128 | dir_fd, follow_symlinks); |
| 3129 | else |
| 3130 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 3131 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3132 | } |
| 3133 | else |
| 3134 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3135 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3136 | } |
| 3137 | #endif |
| 3138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3139 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3142 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3143 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3144 | /*[clinic input] |
| 3145 | os.fchmod |
| 3146 | |
| 3147 | fd: int |
| 3148 | mode: int |
| 3149 | |
| 3150 | Change the access permissions of the file given by file descriptor fd. |
| 3151 | |
| 3152 | Equivalent to os.chmod(fd, mode). |
| 3153 | [clinic start generated code]*/ |
| 3154 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3155 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3156 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 3157 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3158 | { |
| 3159 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3160 | int async_err = 0; |
| 3161 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3162 | if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) { |
| 3163 | return NULL; |
| 3164 | } |
| 3165 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3166 | do { |
| 3167 | Py_BEGIN_ALLOW_THREADS |
| 3168 | res = fchmod(fd, mode); |
| 3169 | Py_END_ALLOW_THREADS |
| 3170 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3171 | if (res != 0) |
| 3172 | return (!async_err) ? posix_error() : NULL; |
| 3173 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3174 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3175 | } |
| 3176 | #endif /* HAVE_FCHMOD */ |
| 3177 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3178 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3179 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3180 | /*[clinic input] |
| 3181 | os.lchmod |
| 3182 | |
| 3183 | path: path_t |
| 3184 | mode: int |
| 3185 | |
| 3186 | Change the access permissions of a file, without following symbolic links. |
| 3187 | |
| 3188 | If path is a symlink, this affects the link itself rather than the target. |
| 3189 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 3190 | [clinic start generated code]*/ |
| 3191 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3192 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3193 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 3194 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3195 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3196 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3197 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) { |
| 3198 | return NULL; |
| 3199 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3200 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3201 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3202 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3203 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3204 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3205 | return NULL; |
| 3206 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3207 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3208 | } |
| 3209 | #endif /* HAVE_LCHMOD */ |
| 3210 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3211 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3212 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3213 | /*[clinic input] |
| 3214 | os.chflags |
| 3215 | |
| 3216 | path: path_t |
| 3217 | flags: unsigned_long(bitwise=True) |
| 3218 | follow_symlinks: bool=True |
| 3219 | |
| 3220 | Set file flags. |
| 3221 | |
| 3222 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3223 | link, chflags will change flags on the symbolic link itself instead of the |
| 3224 | file the link points to. |
| 3225 | follow_symlinks may not be implemented on your platform. If it is |
| 3226 | unavailable, using it will raise a NotImplementedError. |
| 3227 | |
| 3228 | [clinic start generated code]*/ |
| 3229 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3230 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3231 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3232 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3233 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3234 | { |
| 3235 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3236 | |
| 3237 | #ifndef HAVE_LCHFLAGS |
| 3238 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3239 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3240 | #endif |
| 3241 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3242 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3243 | return NULL; |
| 3244 | } |
| 3245 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3246 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3247 | #ifdef HAVE_LCHFLAGS |
| 3248 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3249 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3250 | else |
| 3251 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3252 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3253 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3254 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3255 | if (result) |
| 3256 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3257 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3258 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3259 | } |
| 3260 | #endif /* HAVE_CHFLAGS */ |
| 3261 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3262 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3263 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3264 | /*[clinic input] |
| 3265 | os.lchflags |
| 3266 | |
| 3267 | path: path_t |
| 3268 | flags: unsigned_long(bitwise=True) |
| 3269 | |
| 3270 | Set file flags. |
| 3271 | |
| 3272 | This function will not follow symbolic links. |
| 3273 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3274 | [clinic start generated code]*/ |
| 3275 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3276 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3277 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3278 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3279 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3280 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3281 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3282 | return NULL; |
| 3283 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3284 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3285 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3286 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3287 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3288 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3289 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3290 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3291 | } |
| 3292 | #endif /* HAVE_LCHFLAGS */ |
| 3293 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3294 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3295 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3296 | /*[clinic input] |
| 3297 | os.chroot |
| 3298 | path: path_t |
| 3299 | |
| 3300 | Change root directory to path. |
| 3301 | |
| 3302 | [clinic start generated code]*/ |
| 3303 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3304 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3305 | os_chroot_impl(PyObject *module, path_t *path) |
| 3306 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3307 | { |
| 3308 | int res; |
| 3309 | Py_BEGIN_ALLOW_THREADS |
| 3310 | res = chroot(path->narrow); |
| 3311 | Py_END_ALLOW_THREADS |
| 3312 | if (res < 0) |
| 3313 | return path_error(path); |
| 3314 | Py_RETURN_NONE; |
| 3315 | } |
| 3316 | #endif /* HAVE_CHROOT */ |
| 3317 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3318 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3319 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3320 | /*[clinic input] |
| 3321 | os.fsync |
| 3322 | |
| 3323 | fd: fildes |
| 3324 | |
| 3325 | Force write of fd to disk. |
| 3326 | [clinic start generated code]*/ |
| 3327 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3328 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3329 | os_fsync_impl(PyObject *module, int fd) |
| 3330 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3331 | { |
| 3332 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3333 | } |
| 3334 | #endif /* HAVE_FSYNC */ |
| 3335 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3336 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3337 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3338 | /*[clinic input] |
| 3339 | os.sync |
| 3340 | |
| 3341 | Force write of everything to disk. |
| 3342 | [clinic start generated code]*/ |
| 3343 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3344 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3345 | os_sync_impl(PyObject *module) |
| 3346 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3347 | { |
| 3348 | Py_BEGIN_ALLOW_THREADS |
| 3349 | sync(); |
| 3350 | Py_END_ALLOW_THREADS |
| 3351 | Py_RETURN_NONE; |
| 3352 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3353 | #endif /* HAVE_SYNC */ |
| 3354 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3355 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3356 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3357 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3358 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3359 | #endif |
| 3360 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3361 | /*[clinic input] |
| 3362 | os.fdatasync |
| 3363 | |
| 3364 | fd: fildes |
| 3365 | |
| 3366 | Force write of fd to disk without forcing update of metadata. |
| 3367 | [clinic start generated code]*/ |
| 3368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3369 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3370 | os_fdatasync_impl(PyObject *module, int fd) |
| 3371 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3372 | { |
| 3373 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3374 | } |
| 3375 | #endif /* HAVE_FDATASYNC */ |
| 3376 | |
| 3377 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3378 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3379 | /*[clinic input] |
| 3380 | os.chown |
| 3381 | |
| 3382 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3383 | 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] | 3384 | |
| 3385 | uid: uid_t |
| 3386 | |
| 3387 | gid: gid_t |
| 3388 | |
| 3389 | * |
| 3390 | |
| 3391 | dir_fd : dir_fd(requires='fchownat') = None |
| 3392 | If not None, it should be a file descriptor open to a directory, |
| 3393 | and path should be relative; path will then be relative to that |
| 3394 | directory. |
| 3395 | |
| 3396 | follow_symlinks: bool = True |
| 3397 | If False, and the last element of the path is a symbolic link, |
| 3398 | stat will examine the symbolic link itself instead of the file |
| 3399 | the link points to. |
| 3400 | |
| 3401 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3402 | |
| 3403 | path may always be specified as a string. |
| 3404 | On some platforms, path may also be specified as an open file descriptor. |
| 3405 | If this functionality is unavailable, using it raises an exception. |
| 3406 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3407 | and path should be relative; path will then be relative to that directory. |
| 3408 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3409 | link, chown will modify the symbolic link itself instead of the file the |
| 3410 | link points to. |
| 3411 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3412 | an open file descriptor. |
| 3413 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3414 | If they are unavailable, using them will raise a NotImplementedError. |
| 3415 | |
| 3416 | [clinic start generated code]*/ |
| 3417 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3418 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3419 | 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] | 3420 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3421 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3422 | { |
| 3423 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3424 | |
| 3425 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3426 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3427 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3428 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3429 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3430 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3431 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3432 | |
| 3433 | #ifdef __APPLE__ |
| 3434 | /* |
| 3435 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3436 | * (But we still have an lchown symbol because of weak-linking.) |
| 3437 | * It doesn't have fchownat either. So there's no possibility |
| 3438 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3439 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3440 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3441 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3442 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3443 | } |
| 3444 | #endif |
| 3445 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3446 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, |
| 3447 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3448 | return NULL; |
| 3449 | } |
| 3450 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3451 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3452 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3453 | if (path->fd != -1) |
| 3454 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3455 | else |
| 3456 | #endif |
| 3457 | #ifdef HAVE_LCHOWN |
| 3458 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3459 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3460 | else |
| 3461 | #endif |
| 3462 | #ifdef HAVE_FCHOWNAT |
| 3463 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3464 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3465 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3466 | else |
| 3467 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3468 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3469 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3470 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3471 | if (result) |
| 3472 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3473 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3474 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3475 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3476 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3477 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3478 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3479 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3480 | /*[clinic input] |
| 3481 | os.fchown |
| 3482 | |
| 3483 | fd: int |
| 3484 | uid: uid_t |
| 3485 | gid: gid_t |
| 3486 | |
| 3487 | Change the owner and group id of the file specified by file descriptor. |
| 3488 | |
| 3489 | Equivalent to os.chown(fd, uid, gid). |
| 3490 | |
| 3491 | [clinic start generated code]*/ |
| 3492 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3493 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3494 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3495 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3496 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3497 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3498 | int async_err = 0; |
| 3499 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3500 | if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) { |
| 3501 | return NULL; |
| 3502 | } |
| 3503 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3504 | do { |
| 3505 | Py_BEGIN_ALLOW_THREADS |
| 3506 | res = fchown(fd, uid, gid); |
| 3507 | Py_END_ALLOW_THREADS |
| 3508 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3509 | if (res != 0) |
| 3510 | return (!async_err) ? posix_error() : NULL; |
| 3511 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3512 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3513 | } |
| 3514 | #endif /* HAVE_FCHOWN */ |
| 3515 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3516 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3517 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3518 | /*[clinic input] |
| 3519 | os.lchown |
| 3520 | |
| 3521 | path : path_t |
| 3522 | uid: uid_t |
| 3523 | gid: gid_t |
| 3524 | |
| 3525 | Change the owner and group id of path to the numeric uid and gid. |
| 3526 | |
| 3527 | This function will not follow symbolic links. |
| 3528 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3529 | [clinic start generated code]*/ |
| 3530 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3531 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3532 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3533 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3534 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3535 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3536 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) { |
| 3537 | return NULL; |
| 3538 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3539 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3540 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3541 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3542 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3543 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3544 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3545 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3546 | } |
| 3547 | #endif /* HAVE_LCHOWN */ |
| 3548 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3549 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3550 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3551 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3552 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3553 | #ifdef MS_WINDOWS |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3554 | wchar_t wbuf[MAXPATHLEN]; |
| 3555 | wchar_t *wbuf2 = wbuf; |
| 3556 | DWORD len; |
| 3557 | |
| 3558 | Py_BEGIN_ALLOW_THREADS |
| 3559 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
| 3560 | /* If the buffer is large enough, len does not include the |
| 3561 | terminating \0. If the buffer is too small, len includes |
| 3562 | the space needed for the terminator. */ |
| 3563 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | ec3e20a | 2019-06-28 18:01:59 +0200 | [diff] [blame] | 3564 | if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3565 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3566 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3567 | else { |
| 3568 | wbuf2 = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3569 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3570 | if (wbuf2) { |
| 3571 | len = GetCurrentDirectoryW(len, wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3572 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3573 | } |
| 3574 | Py_END_ALLOW_THREADS |
| 3575 | |
| 3576 | if (!wbuf2) { |
| 3577 | PyErr_NoMemory(); |
| 3578 | return NULL; |
| 3579 | } |
| 3580 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3581 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3582 | PyMem_RawFree(wbuf2); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3583 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3584 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3585 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3586 | PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3587 | if (wbuf2 != wbuf) { |
| 3588 | PyMem_RawFree(wbuf2); |
| 3589 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3590 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3591 | if (use_bytes) { |
| 3592 | if (resobj == NULL) { |
| 3593 | return NULL; |
| 3594 | } |
| 3595 | Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj)); |
| 3596 | } |
| 3597 | |
| 3598 | return resobj; |
| 3599 | #else |
| 3600 | const size_t chunk = 1024; |
| 3601 | |
| 3602 | char *buf = NULL; |
| 3603 | char *cwd = NULL; |
| 3604 | size_t buflen = 0; |
| 3605 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3606 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3607 | do { |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3608 | char *newbuf; |
| 3609 | if (buflen <= PY_SSIZE_T_MAX - chunk) { |
| 3610 | buflen += chunk; |
| 3611 | newbuf = PyMem_RawRealloc(buf, buflen); |
| 3612 | } |
| 3613 | else { |
| 3614 | newbuf = NULL; |
| 3615 | } |
| 3616 | if (newbuf == NULL) { |
| 3617 | PyMem_RawFree(buf); |
| 3618 | buf = NULL; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3619 | break; |
| 3620 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3621 | buf = newbuf; |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3622 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3623 | cwd = getcwd(buf, buflen); |
| 3624 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3625 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3626 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3627 | if (buf == NULL) { |
| 3628 | return PyErr_NoMemory(); |
| 3629 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3630 | if (cwd == NULL) { |
| 3631 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3632 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3633 | } |
| 3634 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3635 | PyObject *obj; |
| 3636 | if (use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3637 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3638 | } |
| 3639 | else { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3640 | obj = PyUnicode_DecodeFSDefault(buf); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3641 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3642 | PyMem_RawFree(buf); |
| 3643 | |
| 3644 | return obj; |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3645 | #endif /* !MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3646 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3647 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3648 | |
| 3649 | /*[clinic input] |
| 3650 | os.getcwd |
| 3651 | |
| 3652 | Return a unicode string representing the current working directory. |
| 3653 | [clinic start generated code]*/ |
| 3654 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3655 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3656 | os_getcwd_impl(PyObject *module) |
| 3657 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3658 | { |
| 3659 | return posix_getcwd(0); |
| 3660 | } |
| 3661 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3662 | |
| 3663 | /*[clinic input] |
| 3664 | os.getcwdb |
| 3665 | |
| 3666 | Return a bytes string representing the current working directory. |
| 3667 | [clinic start generated code]*/ |
| 3668 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3669 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3670 | os_getcwdb_impl(PyObject *module) |
| 3671 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3672 | { |
| 3673 | return posix_getcwd(1); |
| 3674 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3675 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3676 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3677 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3678 | #define HAVE_LINK 1 |
| 3679 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3680 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3681 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3682 | /*[clinic input] |
| 3683 | |
| 3684 | os.link |
| 3685 | |
| 3686 | src : path_t |
| 3687 | dst : path_t |
| 3688 | * |
| 3689 | src_dir_fd : dir_fd = None |
| 3690 | dst_dir_fd : dir_fd = None |
| 3691 | follow_symlinks: bool = True |
| 3692 | |
| 3693 | Create a hard link to a file. |
| 3694 | |
| 3695 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3696 | descriptor open to a directory, and the respective path string (src or dst) |
| 3697 | should be relative; the path will then be relative to that directory. |
| 3698 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3699 | link, link will create a link to the symbolic link itself instead of the |
| 3700 | file the link points to. |
| 3701 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3702 | platform. If they are unavailable, using them will raise a |
| 3703 | NotImplementedError. |
| 3704 | [clinic start generated code]*/ |
| 3705 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3706 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3707 | 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] | 3708 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3709 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3710 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3711 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3712 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3713 | #else |
| 3714 | int result; |
| 3715 | #endif |
| 3716 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3717 | #ifndef HAVE_LINKAT |
| 3718 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3719 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3720 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3721 | } |
| 3722 | #endif |
| 3723 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3724 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3725 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3726 | PyErr_SetString(PyExc_NotImplementedError, |
| 3727 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3728 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3729 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3730 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3731 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3732 | if (PySys_Audit("os.link", "OOii", src->object, dst->object, |
| 3733 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 3734 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 3735 | return NULL; |
| 3736 | } |
| 3737 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3738 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3739 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3740 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3741 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3742 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3743 | if (!result) |
| 3744 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3745 | #else |
| 3746 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3747 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3748 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3749 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3750 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3751 | result = linkat(src_dir_fd, src->narrow, |
| 3752 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3753 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3754 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3755 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3756 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3757 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3758 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3759 | if (result) |
| 3760 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3761 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3762 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3763 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3764 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3765 | #endif |
| 3766 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3767 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3768 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3769 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3770 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3771 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3772 | PyObject *v; |
| 3773 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3774 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3775 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3776 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3777 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3778 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3779 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3780 | WIN32_FIND_DATAW wFileData; |
| 3781 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3782 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3783 | if (!path->wide) { /* Default arg: "." */ |
| 3784 | po_wchars = L"."; |
| 3785 | len = 1; |
| 3786 | } else { |
| 3787 | po_wchars = path->wide; |
| 3788 | len = wcslen(path->wide); |
| 3789 | } |
| 3790 | /* The +5 is so we can append "\\*.*\0" */ |
| 3791 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3792 | if (!wnamebuf) { |
| 3793 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3794 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3795 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3796 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3797 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3798 | wchar_t wch = wnamebuf[len-1]; |
| 3799 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3800 | wnamebuf[len++] = SEP; |
| 3801 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3802 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3803 | if ((list = PyList_New(0)) == NULL) { |
| 3804 | goto exit; |
| 3805 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3806 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3807 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3808 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3809 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3810 | int error = GetLastError(); |
| 3811 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3812 | goto exit; |
| 3813 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3814 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3815 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3816 | } |
| 3817 | do { |
| 3818 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3819 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3820 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3821 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3822 | wcslen(wFileData.cFileName)); |
| 3823 | if (path->narrow && v) { |
| 3824 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3825 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3826 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3827 | Py_DECREF(list); |
| 3828 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3829 | break; |
| 3830 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3831 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3832 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3833 | Py_DECREF(list); |
| 3834 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3835 | break; |
| 3836 | } |
| 3837 | Py_DECREF(v); |
| 3838 | } |
| 3839 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3840 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3841 | Py_END_ALLOW_THREADS |
| 3842 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3843 | it got to the end of the directory. */ |
| 3844 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3845 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3846 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3847 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3848 | } |
| 3849 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3850 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3851 | exit: |
| 3852 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3853 | if (FindClose(hFindFile) == FALSE) { |
| 3854 | if (list != NULL) { |
| 3855 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3856 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3857 | } |
| 3858 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3859 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3860 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3861 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3862 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3863 | } /* end of _listdir_windows_no_opendir */ |
| 3864 | |
| 3865 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3866 | |
| 3867 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3868 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3869 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3870 | PyObject *v; |
| 3871 | DIR *dirp = NULL; |
| 3872 | struct dirent *ep; |
| 3873 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3874 | #ifdef HAVE_FDOPENDIR |
| 3875 | int fd = -1; |
| 3876 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3877 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3878 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3879 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3880 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3881 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3882 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3883 | if (fd == -1) |
| 3884 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3885 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3886 | return_str = 1; |
| 3887 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3888 | Py_BEGIN_ALLOW_THREADS |
| 3889 | dirp = fdopendir(fd); |
| 3890 | Py_END_ALLOW_THREADS |
| 3891 | } |
| 3892 | else |
| 3893 | #endif |
| 3894 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3895 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3896 | if (path->narrow) { |
| 3897 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3898 | /* only return bytes if they specified a bytes-like object */ |
| 3899 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3900 | } |
| 3901 | else { |
| 3902 | name = "."; |
| 3903 | return_str = 1; |
| 3904 | } |
| 3905 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3906 | Py_BEGIN_ALLOW_THREADS |
| 3907 | dirp = opendir(name); |
| 3908 | Py_END_ALLOW_THREADS |
| 3909 | } |
| 3910 | |
| 3911 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3912 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3913 | #ifdef HAVE_FDOPENDIR |
| 3914 | if (fd != -1) { |
| 3915 | Py_BEGIN_ALLOW_THREADS |
| 3916 | close(fd); |
| 3917 | Py_END_ALLOW_THREADS |
| 3918 | } |
| 3919 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3920 | goto exit; |
| 3921 | } |
| 3922 | if ((list = PyList_New(0)) == NULL) { |
| 3923 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3924 | } |
| 3925 | for (;;) { |
| 3926 | errno = 0; |
| 3927 | Py_BEGIN_ALLOW_THREADS |
| 3928 | ep = readdir(dirp); |
| 3929 | Py_END_ALLOW_THREADS |
| 3930 | if (ep == NULL) { |
| 3931 | if (errno == 0) { |
| 3932 | break; |
| 3933 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3934 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3935 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3936 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3937 | } |
| 3938 | } |
| 3939 | if (ep->d_name[0] == '.' && |
| 3940 | (NAMLEN(ep) == 1 || |
| 3941 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3942 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3943 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3944 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3945 | else |
| 3946 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3947 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3948 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3949 | break; |
| 3950 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3951 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3952 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3953 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3954 | break; |
| 3955 | } |
| 3956 | Py_DECREF(v); |
| 3957 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3958 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3959 | exit: |
| 3960 | if (dirp != NULL) { |
| 3961 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3962 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3963 | if (fd > -1) |
| 3964 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3965 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3966 | closedir(dirp); |
| 3967 | Py_END_ALLOW_THREADS |
| 3968 | } |
| 3969 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3970 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3971 | } /* end of _posix_listdir */ |
| 3972 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3973 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3974 | |
| 3975 | /*[clinic input] |
| 3976 | os.listdir |
| 3977 | |
| 3978 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3979 | |
| 3980 | Return a list containing the names of the files in the directory. |
| 3981 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3982 | 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] | 3983 | the filenames returned will also be bytes; in all other circumstances |
| 3984 | the filenames returned will be str. |
| 3985 | If path is None, uses the path='.'. |
| 3986 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3987 | the file descriptor must refer to a directory. |
| 3988 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3989 | |
| 3990 | The list is in arbitrary order. It does not include the special |
| 3991 | entries '.' and '..' even if they are present in the directory. |
| 3992 | |
| 3993 | |
| 3994 | [clinic start generated code]*/ |
| 3995 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3996 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3997 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3998 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3999 | { |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 4000 | if (PySys_Audit("os.listdir", "O", |
| 4001 | path->object ? path->object : Py_None) < 0) { |
| 4002 | return NULL; |
| 4003 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4004 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 4005 | return _listdir_windows_no_opendir(path, NULL); |
| 4006 | #else |
| 4007 | return _posix_listdir(path, NULL); |
| 4008 | #endif |
| 4009 | } |
| 4010 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4011 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4012 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 4013 | /*[clinic input] |
| 4014 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4015 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 4016 | path: path_t |
| 4017 | / |
| 4018 | |
| 4019 | [clinic start generated code]*/ |
| 4020 | |
| 4021 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4022 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 4023 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 4024 | { |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4025 | wchar_t *abspath; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4026 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4027 | /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */ |
| 4028 | if (_Py_abspath(path->wide, &abspath) < 0) { |
| 4029 | return win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4030 | } |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4031 | if (abspath == NULL) { |
| 4032 | return PyErr_NoMemory(); |
| 4033 | } |
| 4034 | |
| 4035 | PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath)); |
| 4036 | PyMem_RawFree(abspath); |
| 4037 | if (str == NULL) { |
| 4038 | return NULL; |
| 4039 | } |
| 4040 | if (path->narrow) { |
| 4041 | Py_SETREF(str, PyUnicode_EncodeFSDefault(str)); |
| 4042 | } |
| 4043 | return str; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4044 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4045 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 4046 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4047 | /*[clinic input] |
| 4048 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 4049 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4050 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4051 | / |
| 4052 | |
| 4053 | A helper function for samepath on windows. |
| 4054 | [clinic start generated code]*/ |
| 4055 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4056 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4057 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 4058 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4059 | { |
| 4060 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4061 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 4062 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4063 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4064 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4065 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4066 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4067 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4068 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4069 | 0, /* desired access */ |
| 4070 | 0, /* share mode */ |
| 4071 | NULL, /* security attributes */ |
| 4072 | OPEN_EXISTING, |
| 4073 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 4074 | FILE_FLAG_BACKUP_SEMANTICS, |
| 4075 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4076 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4077 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4078 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4079 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4080 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4081 | |
| 4082 | /* We have a good handle to the target, use it to determine the |
| 4083 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4084 | while (1) { |
| 4085 | Py_BEGIN_ALLOW_THREADS |
| 4086 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 4087 | buf_size, VOLUME_NAME_DOS); |
| 4088 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4089 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4090 | if (!result_length) { |
| 4091 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 4092 | path->object); |
| 4093 | goto cleanup; |
| 4094 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4095 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4096 | if (result_length < buf_size) { |
| 4097 | break; |
| 4098 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4099 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4100 | wchar_t *tmp; |
| 4101 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 4102 | result_length * sizeof(*tmp)); |
| 4103 | if (!tmp) { |
| 4104 | result = PyErr_NoMemory(); |
| 4105 | goto cleanup; |
| 4106 | } |
| 4107 | |
| 4108 | buf_size = result_length; |
| 4109 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4110 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4111 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4112 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4113 | if (result && path->narrow) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4114 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4115 | } |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4116 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4117 | cleanup: |
| 4118 | if (target_path != buf) { |
| 4119 | PyMem_Free(target_path); |
| 4120 | } |
| 4121 | CloseHandle(hFile); |
| 4122 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4123 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 4124 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4125 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4126 | /*[clinic input] |
| 4127 | os._getvolumepathname |
| 4128 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4129 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4130 | |
| 4131 | A helper function for ismount on Win32. |
| 4132 | [clinic start generated code]*/ |
| 4133 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4134 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4135 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 4136 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4137 | { |
| 4138 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4139 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4140 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4141 | BOOL ret; |
| 4142 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4143 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4144 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4145 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 4146 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4147 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4148 | return NULL; |
| 4149 | } |
| 4150 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4151 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4152 | if (mountpath == NULL) |
| 4153 | return PyErr_NoMemory(); |
| 4154 | |
| 4155 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4156 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4157 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4158 | Py_END_ALLOW_THREADS |
| 4159 | |
| 4160 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4161 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4162 | goto exit; |
| 4163 | } |
| 4164 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4165 | if (path->narrow) |
| 4166 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4167 | |
| 4168 | exit: |
| 4169 | PyMem_Free(mountpath); |
| 4170 | return result; |
| 4171 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4172 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4173 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4174 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4175 | |
| 4176 | /*[clinic input] |
| 4177 | os.mkdir |
| 4178 | |
| 4179 | path : path_t |
| 4180 | |
| 4181 | mode: int = 0o777 |
| 4182 | |
| 4183 | * |
| 4184 | |
| 4185 | dir_fd : dir_fd(requires='mkdirat') = None |
| 4186 | |
| 4187 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4188 | |
| 4189 | Create a directory. |
| 4190 | |
| 4191 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4192 | and path should be relative; path will then be relative to that directory. |
| 4193 | dir_fd may not be implemented on your platform. |
| 4194 | If it is unavailable, using it will raise a NotImplementedError. |
| 4195 | |
| 4196 | The mode argument is ignored on Windows. |
| 4197 | [clinic start generated code]*/ |
| 4198 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4199 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4200 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 4201 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4202 | { |
| 4203 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4204 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4205 | if (PySys_Audit("os.mkdir", "Oii", path->object, mode, |
| 4206 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4207 | return NULL; |
| 4208 | } |
| 4209 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4210 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4211 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4212 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4213 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4214 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4215 | if (!result) |
| 4216 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4217 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4218 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4219 | #if HAVE_MKDIRAT |
| 4220 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4221 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4222 | else |
| 4223 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4224 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4225 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4226 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4227 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4228 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4229 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4230 | if (result < 0) |
| 4231 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4232 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4233 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4236 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4237 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4238 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4239 | #include <sys/resource.h> |
| 4240 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4241 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4242 | |
| 4243 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4244 | /*[clinic input] |
| 4245 | os.nice |
| 4246 | |
| 4247 | increment: int |
| 4248 | / |
| 4249 | |
| 4250 | Add increment to the priority of process and return the new priority. |
| 4251 | [clinic start generated code]*/ |
| 4252 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4253 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4254 | os_nice_impl(PyObject *module, int increment) |
| 4255 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4256 | { |
| 4257 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4258 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4259 | /* There are two flavours of 'nice': one that returns the new |
| 4260 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4261 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4262 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4263 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4264 | If we are of the nice family that returns the new priority, we |
| 4265 | need to clear errno before the call, and check if errno is filled |
| 4266 | before calling posix_error() on a returnvalue of -1, because the |
| 4267 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4268 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4269 | errno = 0; |
| 4270 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4271 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4272 | if (value == 0) |
| 4273 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4274 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4275 | if (value == -1 && errno != 0) |
| 4276 | /* either nice() or getpriority() returned an error */ |
| 4277 | return posix_error(); |
| 4278 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4279 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4280 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4281 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4282 | |
| 4283 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4284 | /*[clinic input] |
| 4285 | os.getpriority |
| 4286 | |
| 4287 | which: int |
| 4288 | who: int |
| 4289 | |
| 4290 | Return program scheduling priority. |
| 4291 | [clinic start generated code]*/ |
| 4292 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4293 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4294 | os_getpriority_impl(PyObject *module, int which, int who) |
| 4295 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4296 | { |
| 4297 | int retval; |
| 4298 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4299 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4300 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4301 | if (errno != 0) |
| 4302 | return posix_error(); |
| 4303 | return PyLong_FromLong((long)retval); |
| 4304 | } |
| 4305 | #endif /* HAVE_GETPRIORITY */ |
| 4306 | |
| 4307 | |
| 4308 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4309 | /*[clinic input] |
| 4310 | os.setpriority |
| 4311 | |
| 4312 | which: int |
| 4313 | who: int |
| 4314 | priority: int |
| 4315 | |
| 4316 | Set program scheduling priority. |
| 4317 | [clinic start generated code]*/ |
| 4318 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4319 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4320 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4321 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4322 | { |
| 4323 | int retval; |
| 4324 | |
| 4325 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4326 | if (retval == -1) |
| 4327 | return posix_error(); |
| 4328 | Py_RETURN_NONE; |
| 4329 | } |
| 4330 | #endif /* HAVE_SETPRIORITY */ |
| 4331 | |
| 4332 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4333 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4334 | 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] | 4335 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4336 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4337 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4338 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4339 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4340 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4341 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4342 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4343 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4344 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4345 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4346 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4347 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4348 | #ifndef HAVE_RENAMEAT |
| 4349 | if (dir_fd_specified) { |
| 4350 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4351 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4352 | } |
| 4353 | #endif |
| 4354 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4355 | if (PySys_Audit("os.rename", "OOii", src->object, dst->object, |
| 4356 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 4357 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 4358 | return NULL; |
| 4359 | } |
| 4360 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4361 | #ifdef MS_WINDOWS |
| 4362 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4363 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4364 | Py_END_ALLOW_THREADS |
| 4365 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4366 | if (!result) |
| 4367 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4368 | |
| 4369 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4370 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4371 | PyErr_Format(PyExc_ValueError, |
| 4372 | "%s: src and dst must be the same type", function_name); |
| 4373 | return NULL; |
| 4374 | } |
| 4375 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4376 | Py_BEGIN_ALLOW_THREADS |
| 4377 | #ifdef HAVE_RENAMEAT |
| 4378 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4379 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4380 | else |
| 4381 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4382 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4383 | Py_END_ALLOW_THREADS |
| 4384 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4385 | if (result) |
| 4386 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4387 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4388 | Py_RETURN_NONE; |
| 4389 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4390 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4391 | |
| 4392 | /*[clinic input] |
| 4393 | os.rename |
| 4394 | |
| 4395 | src : path_t |
| 4396 | dst : path_t |
| 4397 | * |
| 4398 | src_dir_fd : dir_fd = None |
| 4399 | dst_dir_fd : dir_fd = None |
| 4400 | |
| 4401 | Rename a file or directory. |
| 4402 | |
| 4403 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4404 | descriptor open to a directory, and the respective path string (src or dst) |
| 4405 | should be relative; the path will then be relative to that directory. |
| 4406 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4407 | If they are unavailable, using them will raise a NotImplementedError. |
| 4408 | [clinic start generated code]*/ |
| 4409 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4410 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4411 | 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] | 4412 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4413 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4414 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4415 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4416 | } |
| 4417 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4418 | |
| 4419 | /*[clinic input] |
| 4420 | os.replace = os.rename |
| 4421 | |
| 4422 | Rename a file or directory, overwriting the destination. |
| 4423 | |
| 4424 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4425 | descriptor open to a directory, and the respective path string (src or dst) |
| 4426 | should be relative; the path will then be relative to that directory. |
| 4427 | 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] | 4428 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4429 | [clinic start generated code]*/ |
| 4430 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4431 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4432 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4433 | int dst_dir_fd) |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4434 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4435 | { |
| 4436 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4437 | } |
| 4438 | |
| 4439 | |
| 4440 | /*[clinic input] |
| 4441 | os.rmdir |
| 4442 | |
| 4443 | path: path_t |
| 4444 | * |
| 4445 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4446 | |
| 4447 | Remove a directory. |
| 4448 | |
| 4449 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4450 | and path should be relative; path will then be relative to that directory. |
| 4451 | dir_fd may not be implemented on your platform. |
| 4452 | If it is unavailable, using it will raise a NotImplementedError. |
| 4453 | [clinic start generated code]*/ |
| 4454 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4455 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4456 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4457 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4458 | { |
| 4459 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4460 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4461 | if (PySys_Audit("os.rmdir", "Oi", path->object, |
| 4462 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4463 | return NULL; |
| 4464 | } |
| 4465 | |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4466 | Py_BEGIN_ALLOW_THREADS |
| 4467 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4468 | /* Windows, success=1, UNIX, success=0 */ |
| 4469 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4470 | #else |
| 4471 | #ifdef HAVE_UNLINKAT |
| 4472 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4473 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4474 | else |
| 4475 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4476 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4477 | #endif |
| 4478 | Py_END_ALLOW_THREADS |
| 4479 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4480 | if (result) |
| 4481 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4482 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4483 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4486 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4487 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4488 | #ifdef MS_WINDOWS |
| 4489 | /*[clinic input] |
| 4490 | os.system -> long |
| 4491 | |
| 4492 | command: Py_UNICODE |
| 4493 | |
| 4494 | Execute the command in a subshell. |
| 4495 | [clinic start generated code]*/ |
| 4496 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4497 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4498 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4499 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4500 | { |
| 4501 | long result; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4502 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4503 | if (PySys_Audit("os.system", "(u)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4504 | return -1; |
| 4505 | } |
| 4506 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4507 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4508 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4509 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4510 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4511 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4512 | return result; |
| 4513 | } |
| 4514 | #else /* MS_WINDOWS */ |
| 4515 | /*[clinic input] |
| 4516 | os.system -> long |
| 4517 | |
| 4518 | command: FSConverter |
| 4519 | |
| 4520 | Execute the command in a subshell. |
| 4521 | [clinic start generated code]*/ |
| 4522 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4523 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4524 | os_system_impl(PyObject *module, PyObject *command) |
| 4525 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4526 | { |
| 4527 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4528 | const char *bytes = PyBytes_AsString(command); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4529 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4530 | if (PySys_Audit("os.system", "(O)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4531 | return -1; |
| 4532 | } |
| 4533 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4534 | Py_BEGIN_ALLOW_THREADS |
| 4535 | result = system(bytes); |
| 4536 | Py_END_ALLOW_THREADS |
| 4537 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4538 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4539 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4540 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4541 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4542 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4543 | /*[clinic input] |
| 4544 | os.umask |
| 4545 | |
| 4546 | mask: int |
| 4547 | / |
| 4548 | |
| 4549 | Set the current numeric umask and return the previous umask. |
| 4550 | [clinic start generated code]*/ |
| 4551 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4552 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4553 | os_umask_impl(PyObject *module, int mask) |
| 4554 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4555 | { |
| 4556 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4557 | if (i < 0) |
| 4558 | return posix_error(); |
| 4559 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4562 | #ifdef MS_WINDOWS |
| 4563 | |
| 4564 | /* override the default DeleteFileW behavior so that directory |
| 4565 | symlinks can be removed with this function, the same as with |
| 4566 | Unix symlinks */ |
| 4567 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4568 | { |
| 4569 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4570 | WIN32_FIND_DATAW find_data; |
| 4571 | HANDLE find_data_handle; |
| 4572 | int is_directory = 0; |
| 4573 | int is_link = 0; |
| 4574 | |
| 4575 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4576 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4577 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4578 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4579 | it is a symlink */ |
| 4580 | if(is_directory && |
| 4581 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4582 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4583 | |
| 4584 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4585 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4586 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4587 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4588 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4589 | FindClose(find_data_handle); |
| 4590 | } |
| 4591 | } |
| 4592 | } |
| 4593 | |
| 4594 | if (is_directory && is_link) |
| 4595 | return RemoveDirectoryW(lpFileName); |
| 4596 | |
| 4597 | return DeleteFileW(lpFileName); |
| 4598 | } |
| 4599 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4600 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4601 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4602 | /*[clinic input] |
| 4603 | os.unlink |
| 4604 | |
| 4605 | path: path_t |
| 4606 | * |
| 4607 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4608 | |
| 4609 | Remove a file (same as remove()). |
| 4610 | |
| 4611 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4612 | and path should be relative; path will then be relative to that directory. |
| 4613 | dir_fd may not be implemented on your platform. |
| 4614 | If it is unavailable, using it will raise a NotImplementedError. |
| 4615 | |
| 4616 | [clinic start generated code]*/ |
| 4617 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4618 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4619 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4620 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4621 | { |
| 4622 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4623 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4624 | if (PySys_Audit("os.remove", "Oi", path->object, |
| 4625 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4626 | return NULL; |
| 4627 | } |
| 4628 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4629 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4630 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4631 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4632 | /* Windows, success=1, UNIX, success=0 */ |
| 4633 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4634 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4635 | #ifdef HAVE_UNLINKAT |
| 4636 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4637 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4638 | else |
| 4639 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4640 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4641 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4642 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4643 | Py_END_ALLOW_THREADS |
| 4644 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4645 | if (result) |
| 4646 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4647 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4648 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4649 | } |
| 4650 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4651 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4652 | /*[clinic input] |
| 4653 | os.remove = os.unlink |
| 4654 | |
| 4655 | Remove a file (same as unlink()). |
| 4656 | |
| 4657 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4658 | and path should be relative; path will then be relative to that directory. |
| 4659 | dir_fd may not be implemented on your platform. |
| 4660 | If it is unavailable, using it will raise a NotImplementedError. |
| 4661 | [clinic start generated code]*/ |
| 4662 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4663 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4664 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4665 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4666 | { |
| 4667 | return os_unlink_impl(module, path, dir_fd); |
| 4668 | } |
| 4669 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4670 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4671 | static PyStructSequence_Field uname_result_fields[] = { |
| 4672 | {"sysname", "operating system name"}, |
| 4673 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4674 | {"release", "operating system release"}, |
| 4675 | {"version", "operating system version"}, |
| 4676 | {"machine", "hardware identifier"}, |
| 4677 | {NULL} |
| 4678 | }; |
| 4679 | |
| 4680 | PyDoc_STRVAR(uname_result__doc__, |
| 4681 | "uname_result: Result from os.uname().\n\n\ |
| 4682 | This object may be accessed either as a tuple of\n\ |
| 4683 | (sysname, nodename, release, version, machine),\n\ |
| 4684 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4685 | \n\ |
| 4686 | See os.uname for more information."); |
| 4687 | |
| 4688 | static PyStructSequence_Desc uname_result_desc = { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4689 | MODNAME ".uname_result", /* name */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4690 | uname_result__doc__, /* doc */ |
| 4691 | uname_result_fields, |
| 4692 | 5 |
| 4693 | }; |
| 4694 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4695 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4696 | /*[clinic input] |
| 4697 | os.uname |
| 4698 | |
| 4699 | Return an object identifying the current operating system. |
| 4700 | |
| 4701 | The object behaves like a named tuple with the following fields: |
| 4702 | (sysname, nodename, release, version, machine) |
| 4703 | |
| 4704 | [clinic start generated code]*/ |
| 4705 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4706 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4707 | os_uname_impl(PyObject *module) |
| 4708 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4709 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4710 | struct utsname u; |
| 4711 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4712 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4713 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4714 | Py_BEGIN_ALLOW_THREADS |
| 4715 | res = uname(&u); |
| 4716 | Py_END_ALLOW_THREADS |
| 4717 | if (res < 0) |
| 4718 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4719 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 4720 | PyObject *UnameResultType = get_posix_state(module)->UnameResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4721 | value = PyStructSequence_New((PyTypeObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4722 | if (value == NULL) |
| 4723 | return NULL; |
| 4724 | |
| 4725 | #define SET(i, field) \ |
| 4726 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4727 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4728 | if (!o) { \ |
| 4729 | Py_DECREF(value); \ |
| 4730 | return NULL; \ |
| 4731 | } \ |
| 4732 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4733 | } \ |
| 4734 | |
| 4735 | SET(0, u.sysname); |
| 4736 | SET(1, u.nodename); |
| 4737 | SET(2, u.release); |
| 4738 | SET(3, u.version); |
| 4739 | SET(4, u.machine); |
| 4740 | |
| 4741 | #undef SET |
| 4742 | |
| 4743 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4744 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4745 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4746 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4747 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4748 | |
| 4749 | typedef struct { |
| 4750 | int now; |
| 4751 | time_t atime_s; |
| 4752 | long atime_ns; |
| 4753 | time_t mtime_s; |
| 4754 | long mtime_ns; |
| 4755 | } utime_t; |
| 4756 | |
| 4757 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4758 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4759 | * they also intentionally leak the declaration of a pointer named "time" |
| 4760 | */ |
| 4761 | #define UTIME_TO_TIMESPEC \ |
| 4762 | struct timespec ts[2]; \ |
| 4763 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4764 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4765 | time = NULL; \ |
| 4766 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4767 | ts[0].tv_sec = ut->atime_s; \ |
| 4768 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4769 | ts[1].tv_sec = ut->mtime_s; \ |
| 4770 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4771 | time = ts; \ |
| 4772 | } \ |
| 4773 | |
| 4774 | #define UTIME_TO_TIMEVAL \ |
| 4775 | struct timeval tv[2]; \ |
| 4776 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4777 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4778 | time = NULL; \ |
| 4779 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4780 | tv[0].tv_sec = ut->atime_s; \ |
| 4781 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4782 | tv[1].tv_sec = ut->mtime_s; \ |
| 4783 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4784 | time = tv; \ |
| 4785 | } \ |
| 4786 | |
| 4787 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4788 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4789 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4790 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4791 | time = NULL; \ |
| 4792 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4793 | u.actime = ut->atime_s; \ |
| 4794 | u.modtime = ut->mtime_s; \ |
| 4795 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | #define UTIME_TO_TIME_T \ |
| 4799 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4800 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4801 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4802 | time = NULL; \ |
| 4803 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4804 | timet[0] = ut->atime_s; \ |
| 4805 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4806 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4807 | } \ |
| 4808 | |
| 4809 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4810 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4811 | |
| 4812 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4813 | 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] | 4814 | { |
| 4815 | #ifdef HAVE_UTIMENSAT |
| 4816 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4817 | UTIME_TO_TIMESPEC; |
| 4818 | return utimensat(dir_fd, path, time, flags); |
| 4819 | #elif defined(HAVE_FUTIMESAT) |
| 4820 | UTIME_TO_TIMEVAL; |
| 4821 | /* |
| 4822 | * follow_symlinks will never be false here; |
| 4823 | * we only allow !follow_symlinks and dir_fd together |
| 4824 | * if we have utimensat() |
| 4825 | */ |
| 4826 | assert(follow_symlinks); |
| 4827 | return futimesat(dir_fd, path, time); |
| 4828 | #endif |
| 4829 | } |
| 4830 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4831 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4832 | #else |
| 4833 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4834 | #endif |
| 4835 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4836 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4837 | |
| 4838 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4839 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4840 | { |
| 4841 | #ifdef HAVE_FUTIMENS |
| 4842 | UTIME_TO_TIMESPEC; |
| 4843 | return futimens(fd, time); |
| 4844 | #else |
| 4845 | UTIME_TO_TIMEVAL; |
| 4846 | return futimes(fd, time); |
| 4847 | #endif |
| 4848 | } |
| 4849 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4850 | #define PATH_UTIME_HAVE_FD 1 |
| 4851 | #else |
| 4852 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4853 | #endif |
| 4854 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4855 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4856 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4857 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4858 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4859 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4860 | |
| 4861 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4862 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4863 | { |
| 4864 | #ifdef HAVE_UTIMENSAT |
| 4865 | UTIME_TO_TIMESPEC; |
| 4866 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4867 | #else |
| 4868 | UTIME_TO_TIMEVAL; |
| 4869 | return lutimes(path, time); |
| 4870 | #endif |
| 4871 | } |
| 4872 | |
| 4873 | #endif |
| 4874 | |
| 4875 | #ifndef MS_WINDOWS |
| 4876 | |
| 4877 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4878 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4879 | { |
| 4880 | #ifdef HAVE_UTIMENSAT |
| 4881 | UTIME_TO_TIMESPEC; |
| 4882 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4883 | #elif defined(HAVE_UTIMES) |
| 4884 | UTIME_TO_TIMEVAL; |
| 4885 | return utimes(path, time); |
| 4886 | #elif defined(HAVE_UTIME_H) |
| 4887 | UTIME_TO_UTIMBUF; |
| 4888 | return utime(path, time); |
| 4889 | #else |
| 4890 | UTIME_TO_TIME_T; |
| 4891 | return utime(path, time); |
| 4892 | #endif |
| 4893 | } |
| 4894 | |
| 4895 | #endif |
| 4896 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4897 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4898 | split_py_long_to_s_and_ns(PyObject *module, PyObject *py_long, time_t *s, long *ns) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4899 | { |
| 4900 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4901 | PyObject *divmod; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4902 | divmod = PyNumber_Divmod(py_long, get_posix_state(module)->billion); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4903 | if (!divmod) |
| 4904 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4905 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4906 | PyErr_Format(PyExc_TypeError, |
| 4907 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4908 | _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod))); |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4909 | goto exit; |
| 4910 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4911 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4912 | if ((*s == -1) && PyErr_Occurred()) |
| 4913 | goto exit; |
| 4914 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4915 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4916 | goto exit; |
| 4917 | |
| 4918 | result = 1; |
| 4919 | exit: |
| 4920 | Py_XDECREF(divmod); |
| 4921 | return result; |
| 4922 | } |
| 4923 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4924 | |
| 4925 | /*[clinic input] |
| 4926 | os.utime |
| 4927 | |
| 4928 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4929 | times: object = None |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4930 | * |
| 4931 | ns: object = NULL |
| 4932 | dir_fd: dir_fd(requires='futimensat') = None |
| 4933 | follow_symlinks: bool=True |
| 4934 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4935 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4936 | |
| 4937 | Set the access and modified time of path. |
| 4938 | |
| 4939 | path may always be specified as a string. |
| 4940 | On some platforms, path may also be specified as an open file descriptor. |
| 4941 | If this functionality is unavailable, using it raises an exception. |
| 4942 | |
| 4943 | If times is not None, it must be a tuple (atime, mtime); |
| 4944 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4945 | 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] | 4946 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4947 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4948 | 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] | 4949 | Specifying tuples for both times and ns is an error. |
| 4950 | |
| 4951 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4952 | and path should be relative; path will then be relative to that directory. |
| 4953 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4954 | link, utime will modify the symbolic link itself instead of the file the |
| 4955 | link points to. |
| 4956 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4957 | as an open file descriptor. |
| 4958 | dir_fd and follow_symlinks may not be available on your platform. |
| 4959 | If they are unavailable, using them will raise a NotImplementedError. |
| 4960 | |
| 4961 | [clinic start generated code]*/ |
| 4962 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4963 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4964 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4965 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4966 | /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4967 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4968 | #ifdef MS_WINDOWS |
| 4969 | HANDLE hFile; |
| 4970 | FILETIME atime, mtime; |
| 4971 | #else |
| 4972 | int result; |
| 4973 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4974 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4975 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4976 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4977 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4978 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4979 | if (times != Py_None && ns) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4980 | PyErr_SetString(PyExc_ValueError, |
| 4981 | "utime: you may specify either 'times'" |
| 4982 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4983 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4984 | } |
| 4985 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4986 | if (times != Py_None) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4987 | time_t a_sec, m_sec; |
| 4988 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4989 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4990 | PyErr_SetString(PyExc_TypeError, |
| 4991 | "utime: 'times' must be either" |
| 4992 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4993 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4994 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4995 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4996 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4997 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4998 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4999 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5000 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 5001 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 5002 | utime.atime_s = a_sec; |
| 5003 | utime.atime_ns = a_nsec; |
| 5004 | utime.mtime_s = m_sec; |
| 5005 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5006 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5007 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5008 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5009 | PyErr_SetString(PyExc_TypeError, |
| 5010 | "utime: 'ns' must be a tuple of two ints"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5011 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5012 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5013 | utime.now = 0; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5014 | if (!split_py_long_to_s_and_ns(module, PyTuple_GET_ITEM(ns, 0), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5015 | &utime.atime_s, &utime.atime_ns) || |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5016 | !split_py_long_to_s_and_ns(module, PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5017 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5018 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 5019 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5020 | } |
| 5021 | else { |
| 5022 | /* times and ns are both None/unspecified. use "now". */ |
| 5023 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5024 | } |
| 5025 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 5026 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5027 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5028 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5029 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 5030 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5031 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 5032 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 5033 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5034 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5035 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5036 | #if !defined(HAVE_UTIMENSAT) |
| 5037 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 5038 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5039 | "utime: cannot use dir_fd and follow_symlinks " |
| 5040 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5041 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5042 | } |
| 5043 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5044 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5045 | if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None, |
| 5046 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 5047 | return NULL; |
| 5048 | } |
| 5049 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5050 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5051 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5052 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 5053 | NULL, OPEN_EXISTING, |
| 5054 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5055 | Py_END_ALLOW_THREADS |
| 5056 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5057 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5058 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 5059 | } |
| 5060 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5061 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 5062 | GetSystemTimeAsFileTime(&mtime); |
| 5063 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5064 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5065 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 5066 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 5067 | _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] | 5068 | } |
| 5069 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 5070 | /* Avoid putting the file name into the error here, |
| 5071 | as that may confuse the user into believing that |
| 5072 | something is wrong with the file, when it also |
| 5073 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 5074 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5075 | CloseHandle(hFile); |
| 5076 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5077 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5078 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5079 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5080 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 5081 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 5082 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5083 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5084 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5085 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 5086 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5087 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5088 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5089 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5090 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5091 | else |
| 5092 | #endif |
| 5093 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5094 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5095 | if (path->fd != -1) |
| 5096 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5097 | else |
| 5098 | #endif |
| 5099 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5100 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5101 | |
| 5102 | Py_END_ALLOW_THREADS |
| 5103 | |
| 5104 | if (result < 0) { |
| 5105 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5106 | posix_error(); |
| 5107 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5108 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5109 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5110 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5111 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5112 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5113 | } |
| 5114 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5115 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5116 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5117 | |
| 5118 | /*[clinic input] |
| 5119 | os._exit |
| 5120 | |
| 5121 | status: int |
| 5122 | |
| 5123 | Exit to the system with specified status, without normal exit processing. |
| 5124 | [clinic start generated code]*/ |
| 5125 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5126 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5127 | os__exit_impl(PyObject *module, int status) |
| 5128 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5129 | { |
| 5130 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5131 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5132 | } |
| 5133 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5134 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5135 | #define EXECV_CHAR wchar_t |
| 5136 | #else |
| 5137 | #define EXECV_CHAR char |
| 5138 | #endif |
| 5139 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5140 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5141 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5142 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5143 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5144 | Py_ssize_t i; |
| 5145 | for (i = 0; i < count; i++) |
| 5146 | PyMem_Free(array[i]); |
| 5147 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5148 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5149 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5150 | static int |
| 5151 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5152 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5153 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5154 | PyObject *ub; |
| 5155 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5156 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5157 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5158 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5159 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 5160 | if (*out) |
| 5161 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5162 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5163 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5164 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5165 | size = PyBytes_GET_SIZE(ub); |
| 5166 | *out = PyMem_Malloc(size + 1); |
| 5167 | if (*out) { |
| 5168 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 5169 | result = 1; |
| 5170 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5171 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5172 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5173 | Py_DECREF(ub); |
| 5174 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5175 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5176 | #endif |
| 5177 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5178 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5179 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5180 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5181 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5182 | Py_ssize_t i, pos, envc; |
| 5183 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5184 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5185 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5186 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5187 | i = PyMapping_Size(env); |
| 5188 | if (i < 0) |
| 5189 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5190 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5191 | if (envlist == NULL) { |
| 5192 | PyErr_NoMemory(); |
| 5193 | return NULL; |
| 5194 | } |
| 5195 | envc = 0; |
| 5196 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5197 | if (!keys) |
| 5198 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5199 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5200 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5201 | goto error; |
| 5202 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5203 | PyErr_Format(PyExc_TypeError, |
| 5204 | "env.keys() or env.values() is not a list"); |
| 5205 | goto error; |
| 5206 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5207 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5208 | for (pos = 0; pos < i; pos++) { |
| 5209 | key = PyList_GetItem(keys, pos); |
| 5210 | val = PyList_GetItem(vals, pos); |
| 5211 | if (!key || !val) |
| 5212 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5213 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5214 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5215 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 5216 | goto error; |
| 5217 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 5218 | Py_DECREF(key2); |
| 5219 | goto error; |
| 5220 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5221 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 5222 | defining hidden environment variables. */ |
| 5223 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 5224 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 5225 | { |
| 5226 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5227 | Py_DECREF(key2); |
| 5228 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5229 | goto error; |
| 5230 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5231 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 5232 | #else |
| 5233 | if (!PyUnicode_FSConverter(key, &key2)) |
| 5234 | goto error; |
| 5235 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 5236 | Py_DECREF(key2); |
| 5237 | goto error; |
| 5238 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5239 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 5240 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 5241 | { |
| 5242 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5243 | Py_DECREF(key2); |
| 5244 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5245 | goto error; |
| 5246 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5247 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 5248 | PyBytes_AS_STRING(val2)); |
| 5249 | #endif |
| 5250 | Py_DECREF(key2); |
| 5251 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5252 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5253 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5254 | |
| 5255 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 5256 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5257 | goto error; |
| 5258 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5259 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5260 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5261 | } |
| 5262 | Py_DECREF(vals); |
| 5263 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5264 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5265 | envlist[envc] = 0; |
| 5266 | *envc_ptr = envc; |
| 5267 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5268 | |
| 5269 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5270 | Py_XDECREF(keys); |
| 5271 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5272 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5273 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5274 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5275 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5276 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5277 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5278 | { |
| 5279 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5280 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5281 | if (argvlist == NULL) { |
| 5282 | PyErr_NoMemory(); |
| 5283 | return NULL; |
| 5284 | } |
| 5285 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5286 | PyObject* item = PySequence_ITEM(argv, i); |
| 5287 | if (item == NULL) |
| 5288 | goto fail; |
| 5289 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5290 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5291 | goto fail; |
| 5292 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5293 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5294 | } |
| 5295 | argvlist[*argc] = NULL; |
| 5296 | return argvlist; |
| 5297 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5298 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5299 | free_string_array(argvlist, *argc); |
| 5300 | return NULL; |
| 5301 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5302 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5303 | #endif |
| 5304 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5305 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5306 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5307 | /*[clinic input] |
| 5308 | os.execv |
| 5309 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5310 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5311 | Path of executable file. |
| 5312 | argv: object |
| 5313 | Tuple or list of strings. |
| 5314 | / |
| 5315 | |
| 5316 | Execute an executable path with arguments, replacing current process. |
| 5317 | [clinic start generated code]*/ |
| 5318 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5319 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5320 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5321 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5322 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5323 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5324 | Py_ssize_t argc; |
| 5325 | |
| 5326 | /* execv has two arguments: (path, argv), where |
| 5327 | argv is a list or tuple of strings. */ |
| 5328 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5329 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5330 | PyErr_SetString(PyExc_TypeError, |
| 5331 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5332 | return NULL; |
| 5333 | } |
| 5334 | argc = PySequence_Size(argv); |
| 5335 | if (argc < 1) { |
| 5336 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5337 | return NULL; |
| 5338 | } |
| 5339 | |
| 5340 | argvlist = parse_arglist(argv, &argc); |
| 5341 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5342 | return NULL; |
| 5343 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5344 | if (!argvlist[0][0]) { |
| 5345 | PyErr_SetString(PyExc_ValueError, |
| 5346 | "execv() arg 2 first element cannot be empty"); |
| 5347 | free_string_array(argvlist, argc); |
| 5348 | return NULL; |
| 5349 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5350 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5351 | if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5352 | free_string_array(argvlist, argc); |
| 5353 | return NULL; |
| 5354 | } |
| 5355 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5356 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5357 | #ifdef HAVE_WEXECV |
| 5358 | _wexecv(path->wide, argvlist); |
| 5359 | #else |
| 5360 | execv(path->narrow, argvlist); |
| 5361 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5362 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5363 | |
| 5364 | /* If we get here it's definitely an error */ |
| 5365 | |
| 5366 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5367 | return posix_error(); |
| 5368 | } |
| 5369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5370 | |
| 5371 | /*[clinic input] |
| 5372 | os.execve |
| 5373 | |
| 5374 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5375 | Path of executable file. |
| 5376 | argv: object |
| 5377 | Tuple or list of strings. |
| 5378 | env: object |
| 5379 | Dictionary of strings mapping to strings. |
| 5380 | |
| 5381 | Execute an executable path with arguments, replacing current process. |
| 5382 | [clinic start generated code]*/ |
| 5383 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5384 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5385 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5386 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5387 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5388 | EXECV_CHAR **argvlist = NULL; |
| 5389 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5390 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5391 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5392 | /* execve has three arguments: (path, argv, env), where |
| 5393 | argv is a list or tuple of strings and env is a dictionary |
| 5394 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5395 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5396 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5397 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5398 | "execve: argv must be a tuple or list"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5399 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5400 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5401 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5402 | if (argc < 1) { |
| 5403 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5404 | return NULL; |
| 5405 | } |
| 5406 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5407 | if (!PyMapping_Check(env)) { |
| 5408 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5409 | "execve: environment must be a mapping object"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5410 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5411 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5412 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5413 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5414 | if (argvlist == NULL) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5415 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5416 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5417 | if (!argvlist[0][0]) { |
| 5418 | PyErr_SetString(PyExc_ValueError, |
| 5419 | "execve: argv first element cannot be empty"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5420 | goto fail_0; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5421 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5422 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5423 | envlist = parse_envlist(env, &envc); |
| 5424 | if (envlist == NULL) |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5425 | goto fail_0; |
| 5426 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5427 | if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5428 | goto fail_1; |
| 5429 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5430 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5431 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5432 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5433 | if (path->fd > -1) |
| 5434 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5435 | else |
| 5436 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5437 | #ifdef HAVE_WEXECV |
| 5438 | _wexecve(path->wide, argvlist, envlist); |
| 5439 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5440 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5441 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5442 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5443 | |
| 5444 | /* If we get here it's definitely an error */ |
| 5445 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5446 | posix_path_error(path); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5447 | fail_1: |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5448 | free_string_array(envlist, envc); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5449 | fail_0: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5450 | if (argvlist) |
| 5451 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5452 | return NULL; |
| 5453 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5454 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5455 | #endif /* HAVE_EXECV */ |
| 5456 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5457 | #ifdef HAVE_POSIX_SPAWN |
| 5458 | |
| 5459 | enum posix_spawn_file_actions_identifier { |
| 5460 | POSIX_SPAWN_OPEN, |
| 5461 | POSIX_SPAWN_CLOSE, |
| 5462 | POSIX_SPAWN_DUP2 |
| 5463 | }; |
| 5464 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5465 | #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] | 5466 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5467 | convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5468 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5469 | |
| 5470 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5471 | parse_posix_spawn_flags(PyObject *module, const char *func_name, PyObject *setpgroup, |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5472 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5473 | PyObject *setsigdef, PyObject *scheduler, |
| 5474 | posix_spawnattr_t *attrp) |
| 5475 | { |
| 5476 | long all_flags = 0; |
| 5477 | |
| 5478 | errno = posix_spawnattr_init(attrp); |
| 5479 | if (errno) { |
| 5480 | posix_error(); |
| 5481 | return -1; |
| 5482 | } |
| 5483 | |
| 5484 | if (setpgroup) { |
| 5485 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5486 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5487 | goto fail; |
| 5488 | } |
| 5489 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5490 | if (errno) { |
| 5491 | posix_error(); |
| 5492 | goto fail; |
| 5493 | } |
| 5494 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5495 | } |
| 5496 | |
| 5497 | if (resetids) { |
| 5498 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5499 | } |
| 5500 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5501 | if (setsid) { |
| 5502 | #ifdef POSIX_SPAWN_SETSID |
| 5503 | all_flags |= POSIX_SPAWN_SETSID; |
| 5504 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5505 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5506 | #else |
| 5507 | argument_unavailable_error(func_name, "setsid"); |
| 5508 | return -1; |
| 5509 | #endif |
| 5510 | } |
| 5511 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5512 | if (setsigmask) { |
| 5513 | sigset_t set; |
| 5514 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5515 | goto fail; |
| 5516 | } |
| 5517 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5518 | if (errno) { |
| 5519 | posix_error(); |
| 5520 | goto fail; |
| 5521 | } |
| 5522 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5523 | } |
| 5524 | |
| 5525 | if (setsigdef) { |
| 5526 | sigset_t set; |
| 5527 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5528 | goto fail; |
| 5529 | } |
| 5530 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5531 | if (errno) { |
| 5532 | posix_error(); |
| 5533 | goto fail; |
| 5534 | } |
| 5535 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5536 | } |
| 5537 | |
| 5538 | if (scheduler) { |
| 5539 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5540 | PyObject *py_schedpolicy; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5541 | PyObject *schedparam_obj; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5542 | struct sched_param schedparam; |
| 5543 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5544 | if (!PyArg_ParseTuple(scheduler, "OO" |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5545 | ";A scheduler tuple must have two elements", |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5546 | &py_schedpolicy, &schedparam_obj)) { |
| 5547 | goto fail; |
| 5548 | } |
| 5549 | if (!convert_sched_param(module, schedparam_obj, &schedparam)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5550 | goto fail; |
| 5551 | } |
| 5552 | if (py_schedpolicy != Py_None) { |
| 5553 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5554 | |
| 5555 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5556 | goto fail; |
| 5557 | } |
| 5558 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5559 | if (errno) { |
| 5560 | posix_error(); |
| 5561 | goto fail; |
| 5562 | } |
| 5563 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5564 | } |
| 5565 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5566 | if (errno) { |
| 5567 | posix_error(); |
| 5568 | goto fail; |
| 5569 | } |
| 5570 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5571 | #else |
| 5572 | PyErr_SetString(PyExc_NotImplementedError, |
| 5573 | "The scheduler option is not supported in this system."); |
| 5574 | goto fail; |
| 5575 | #endif |
| 5576 | } |
| 5577 | |
| 5578 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5579 | if (errno) { |
| 5580 | posix_error(); |
| 5581 | goto fail; |
| 5582 | } |
| 5583 | |
| 5584 | return 0; |
| 5585 | |
| 5586 | fail: |
| 5587 | (void)posix_spawnattr_destroy(attrp); |
| 5588 | return -1; |
| 5589 | } |
| 5590 | |
| 5591 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5592 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5593 | posix_spawn_file_actions_t *file_actionsp, |
| 5594 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5595 | { |
| 5596 | PyObject *seq; |
| 5597 | PyObject *file_action = NULL; |
| 5598 | PyObject *tag_obj; |
| 5599 | |
| 5600 | seq = PySequence_Fast(file_actions, |
| 5601 | "file_actions must be a sequence or None"); |
| 5602 | if (seq == NULL) { |
| 5603 | return -1; |
| 5604 | } |
| 5605 | |
| 5606 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5607 | if (errno) { |
| 5608 | posix_error(); |
| 5609 | Py_DECREF(seq); |
| 5610 | return -1; |
| 5611 | } |
| 5612 | |
Zackery Spytz | d52a83a | 2019-06-26 14:54:20 -0600 | [diff] [blame] | 5613 | 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] | 5614 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5615 | Py_INCREF(file_action); |
| 5616 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5617 | PyErr_SetString(PyExc_TypeError, |
| 5618 | "Each file_actions element must be a non-empty tuple"); |
| 5619 | goto fail; |
| 5620 | } |
| 5621 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5622 | if (tag == -1 && PyErr_Occurred()) { |
| 5623 | goto fail; |
| 5624 | } |
| 5625 | |
| 5626 | /* Populate the file_actions object */ |
| 5627 | switch (tag) { |
| 5628 | case POSIX_SPAWN_OPEN: { |
| 5629 | int fd, oflag; |
| 5630 | PyObject *path; |
| 5631 | unsigned long mode; |
| 5632 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5633 | ";A open file_action tuple must have 5 elements", |
| 5634 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5635 | &oflag, &mode)) |
| 5636 | { |
| 5637 | goto fail; |
| 5638 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5639 | if (PyList_Append(temp_buffer, path)) { |
| 5640 | Py_DECREF(path); |
| 5641 | goto fail; |
| 5642 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5643 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5644 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5645 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5646 | if (errno) { |
| 5647 | posix_error(); |
| 5648 | goto fail; |
| 5649 | } |
| 5650 | break; |
| 5651 | } |
| 5652 | case POSIX_SPAWN_CLOSE: { |
| 5653 | int fd; |
| 5654 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5655 | ";A close file_action tuple must have 2 elements", |
| 5656 | &tag_obj, &fd)) |
| 5657 | { |
| 5658 | goto fail; |
| 5659 | } |
| 5660 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5661 | if (errno) { |
| 5662 | posix_error(); |
| 5663 | goto fail; |
| 5664 | } |
| 5665 | break; |
| 5666 | } |
| 5667 | case POSIX_SPAWN_DUP2: { |
| 5668 | int fd1, fd2; |
| 5669 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5670 | ";A dup2 file_action tuple must have 3 elements", |
| 5671 | &tag_obj, &fd1, &fd2)) |
| 5672 | { |
| 5673 | goto fail; |
| 5674 | } |
| 5675 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5676 | fd1, fd2); |
| 5677 | if (errno) { |
| 5678 | posix_error(); |
| 5679 | goto fail; |
| 5680 | } |
| 5681 | break; |
| 5682 | } |
| 5683 | default: { |
| 5684 | PyErr_SetString(PyExc_TypeError, |
| 5685 | "Unknown file_actions identifier"); |
| 5686 | goto fail; |
| 5687 | } |
| 5688 | } |
| 5689 | Py_DECREF(file_action); |
| 5690 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5691 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5692 | Py_DECREF(seq); |
| 5693 | return 0; |
| 5694 | |
| 5695 | fail: |
| 5696 | Py_DECREF(seq); |
| 5697 | Py_DECREF(file_action); |
| 5698 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5699 | return -1; |
| 5700 | } |
| 5701 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5702 | |
| 5703 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5704 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 5705 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5706 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5707 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5708 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5709 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5710 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5711 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5712 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5713 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5714 | posix_spawnattr_t attr; |
| 5715 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5716 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5717 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5718 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5719 | pid_t pid; |
| 5720 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5721 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5722 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5723 | 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] | 5724 | like posix.environ. */ |
| 5725 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5726 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5727 | PyErr_Format(PyExc_TypeError, |
| 5728 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5729 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5730 | } |
| 5731 | argc = PySequence_Size(argv); |
| 5732 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5733 | PyErr_Format(PyExc_ValueError, |
| 5734 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5735 | return NULL; |
| 5736 | } |
| 5737 | |
| 5738 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5739 | PyErr_Format(PyExc_TypeError, |
| 5740 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5741 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5742 | } |
| 5743 | |
| 5744 | argvlist = parse_arglist(argv, &argc); |
| 5745 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5746 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5747 | } |
| 5748 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5749 | PyErr_Format(PyExc_ValueError, |
| 5750 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5751 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
| 5754 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5755 | if (envlist == NULL) { |
| 5756 | goto exit; |
| 5757 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5758 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 5759 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5760 | /* There is a bug in old versions of glibc that makes some of the |
| 5761 | * helper functions for manipulating file actions not copy the provided |
| 5762 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5763 | * copy the value of path for some old versions of glibc (<2.20). |
| 5764 | * The use of temp_buffer here is a workaround that keeps the |
| 5765 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5766 | * Check https://bugs.python.org/issue33630 and |
| 5767 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5768 | temp_buffer = PyList_New(0); |
| 5769 | if (!temp_buffer) { |
| 5770 | goto exit; |
| 5771 | } |
| 5772 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5773 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5774 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5775 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5776 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5777 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5778 | if (parse_posix_spawn_flags(module, func_name, setpgroup, resetids, setsid, |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5779 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5780 | goto exit; |
| 5781 | } |
| 5782 | attrp = &attr; |
| 5783 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5784 | if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5785 | goto exit; |
| 5786 | } |
| 5787 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5788 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5789 | #ifdef HAVE_POSIX_SPAWNP |
| 5790 | if (use_posix_spawnp) { |
| 5791 | err_code = posix_spawnp(&pid, path->narrow, |
| 5792 | file_actionsp, attrp, argvlist, envlist); |
| 5793 | } |
| 5794 | else |
| 5795 | #endif /* HAVE_POSIX_SPAWNP */ |
| 5796 | { |
| 5797 | err_code = posix_spawn(&pid, path->narrow, |
| 5798 | file_actionsp, attrp, argvlist, envlist); |
| 5799 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5800 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5801 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5802 | if (err_code) { |
| 5803 | errno = err_code; |
| 5804 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5805 | goto exit; |
| 5806 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5807 | #ifdef _Py_MEMORY_SANITIZER |
| 5808 | __msan_unpoison(&pid, sizeof(pid)); |
| 5809 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5810 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5811 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5812 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5813 | if (file_actionsp) { |
| 5814 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5815 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5816 | if (attrp) { |
| 5817 | (void)posix_spawnattr_destroy(attrp); |
| 5818 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5819 | if (envlist) { |
| 5820 | free_string_array(envlist, envc); |
| 5821 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5822 | if (argvlist) { |
| 5823 | free_string_array(argvlist, argc); |
| 5824 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5825 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5826 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5827 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5828 | |
| 5829 | |
| 5830 | /*[clinic input] |
| 5831 | |
| 5832 | os.posix_spawn |
| 5833 | path: path_t |
| 5834 | Path of executable file. |
| 5835 | argv: object |
| 5836 | Tuple or list of strings. |
| 5837 | env: object |
| 5838 | Dictionary of strings mapping to strings. |
| 5839 | / |
| 5840 | * |
| 5841 | file_actions: object(c_default='NULL') = () |
| 5842 | A sequence of file action tuples. |
| 5843 | setpgroup: object = NULL |
| 5844 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5845 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5846 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 5847 | setsid: bool(accept={int}) = False |
| 5848 | 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] | 5849 | setsigmask: object(c_default='NULL') = () |
| 5850 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5851 | setsigdef: object(c_default='NULL') = () |
| 5852 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5853 | scheduler: object = NULL |
| 5854 | A tuple with the scheduler policy (optional) and parameters. |
| 5855 | |
| 5856 | Execute the program specified by path in a new process. |
| 5857 | [clinic start generated code]*/ |
| 5858 | |
| 5859 | static PyObject * |
| 5860 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5861 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5862 | PyObject *setpgroup, int resetids, int setsid, |
| 5863 | PyObject *setsigmask, PyObject *setsigdef, |
| 5864 | PyObject *scheduler) |
| 5865 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5866 | { |
| 5867 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5868 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5869 | scheduler); |
| 5870 | } |
| 5871 | #endif /* HAVE_POSIX_SPAWN */ |
| 5872 | |
| 5873 | |
| 5874 | |
| 5875 | #ifdef HAVE_POSIX_SPAWNP |
| 5876 | /*[clinic input] |
| 5877 | |
| 5878 | os.posix_spawnp |
| 5879 | path: path_t |
| 5880 | Path of executable file. |
| 5881 | argv: object |
| 5882 | Tuple or list of strings. |
| 5883 | env: object |
| 5884 | Dictionary of strings mapping to strings. |
| 5885 | / |
| 5886 | * |
| 5887 | file_actions: object(c_default='NULL') = () |
| 5888 | A sequence of file action tuples. |
| 5889 | setpgroup: object = NULL |
| 5890 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5891 | resetids: bool(accept={int}) = False |
| 5892 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5893 | setsid: bool(accept={int}) = False |
| 5894 | 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] | 5895 | setsigmask: object(c_default='NULL') = () |
| 5896 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5897 | setsigdef: object(c_default='NULL') = () |
| 5898 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5899 | scheduler: object = NULL |
| 5900 | A tuple with the scheduler policy (optional) and parameters. |
| 5901 | |
| 5902 | Execute the program specified by path in a new process. |
| 5903 | [clinic start generated code]*/ |
| 5904 | |
| 5905 | static PyObject * |
| 5906 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5907 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5908 | PyObject *setpgroup, int resetids, int setsid, |
| 5909 | PyObject *setsigmask, PyObject *setsigdef, |
| 5910 | PyObject *scheduler) |
| 5911 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5912 | { |
| 5913 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5914 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5915 | scheduler); |
| 5916 | } |
| 5917 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5918 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5919 | #ifdef HAVE_RTPSPAWN |
| 5920 | static intptr_t |
| 5921 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], |
| 5922 | const char *envp[]) |
| 5923 | { |
| 5924 | RTP_ID rtpid; |
| 5925 | int status; |
| 5926 | pid_t res; |
| 5927 | int async_err = 0; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5928 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5929 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
| 5930 | uStackSize=0 cannot be used, the default stack size is too small for |
| 5931 | Python. */ |
| 5932 | if (envp) { |
| 5933 | rtpid = rtpSpawn(rtpFileName, argv, envp, |
| 5934 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5935 | } |
| 5936 | else { |
| 5937 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, |
| 5938 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5939 | } |
| 5940 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { |
| 5941 | do { |
| 5942 | res = waitpid((pid_t)rtpid, &status, 0); |
| 5943 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 5944 | |
| 5945 | if (res < 0) |
| 5946 | return RTP_ID_ERROR; |
| 5947 | return ((intptr_t)status); |
| 5948 | } |
| 5949 | return ((intptr_t)rtpid); |
| 5950 | } |
| 5951 | #endif |
| 5952 | |
| 5953 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5954 | /*[clinic input] |
| 5955 | os.spawnv |
| 5956 | |
| 5957 | mode: int |
| 5958 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5959 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5960 | Path of executable file. |
| 5961 | argv: object |
| 5962 | Tuple or list of strings. |
| 5963 | / |
| 5964 | |
| 5965 | Execute the program specified by path in a new process. |
| 5966 | [clinic start generated code]*/ |
| 5967 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5968 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5969 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5970 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5971 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5972 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5973 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5974 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5975 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5976 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5977 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5978 | /* spawnv has three arguments: (mode, path, argv), where |
| 5979 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5980 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5981 | if (PyList_Check(argv)) { |
| 5982 | argc = PyList_Size(argv); |
| 5983 | getitem = PyList_GetItem; |
| 5984 | } |
| 5985 | else if (PyTuple_Check(argv)) { |
| 5986 | argc = PyTuple_Size(argv); |
| 5987 | getitem = PyTuple_GetItem; |
| 5988 | } |
| 5989 | else { |
| 5990 | PyErr_SetString(PyExc_TypeError, |
| 5991 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5992 | return NULL; |
| 5993 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5994 | if (argc == 0) { |
| 5995 | PyErr_SetString(PyExc_ValueError, |
| 5996 | "spawnv() arg 2 cannot be empty"); |
| 5997 | return NULL; |
| 5998 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5999 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6000 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6001 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6002 | return PyErr_NoMemory(); |
| 6003 | } |
| 6004 | for (i = 0; i < argc; i++) { |
| 6005 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 6006 | &argvlist[i])) { |
| 6007 | free_string_array(argvlist, i); |
| 6008 | PyErr_SetString( |
| 6009 | PyExc_TypeError, |
| 6010 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6011 | return NULL; |
| 6012 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 6013 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 6014 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 6015 | PyErr_SetString( |
| 6016 | PyExc_ValueError, |
| 6017 | "spawnv() arg 2 first element cannot be empty"); |
| 6018 | return NULL; |
| 6019 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6020 | } |
| 6021 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6022 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6023 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6024 | if (mode == _OLD_P_OVERLAY) |
| 6025 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6026 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6027 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6028 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6029 | Py_None) < 0) { |
| 6030 | free_string_array(argvlist, argc); |
| 6031 | return NULL; |
| 6032 | } |
| 6033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6034 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6035 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6036 | #ifdef HAVE_WSPAWNV |
| 6037 | spawnval = _wspawnv(mode, path->wide, argvlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6038 | #elif defined(HAVE_RTPSPAWN) |
| 6039 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6040 | #else |
| 6041 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 6042 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6043 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6044 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6045 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6046 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6047 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6048 | if (spawnval == -1) |
| 6049 | return posix_error(); |
| 6050 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6051 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6054 | /*[clinic input] |
| 6055 | os.spawnve |
| 6056 | |
| 6057 | mode: int |
| 6058 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6059 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6060 | Path of executable file. |
| 6061 | argv: object |
| 6062 | Tuple or list of strings. |
| 6063 | env: object |
| 6064 | Dictionary of strings mapping to strings. |
| 6065 | / |
| 6066 | |
| 6067 | Execute the program specified by path in a new process. |
| 6068 | [clinic start generated code]*/ |
| 6069 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6070 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6071 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6072 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6073 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6074 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6075 | EXECV_CHAR **argvlist; |
| 6076 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6077 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 6078 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6079 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6080 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6081 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6082 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6083 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 6084 | argv is a list or tuple of strings and env is a dictionary |
| 6085 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6086 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6087 | if (PyList_Check(argv)) { |
| 6088 | argc = PyList_Size(argv); |
| 6089 | getitem = PyList_GetItem; |
| 6090 | } |
| 6091 | else if (PyTuple_Check(argv)) { |
| 6092 | argc = PyTuple_Size(argv); |
| 6093 | getitem = PyTuple_GetItem; |
| 6094 | } |
| 6095 | else { |
| 6096 | PyErr_SetString(PyExc_TypeError, |
| 6097 | "spawnve() arg 2 must be a tuple or list"); |
| 6098 | goto fail_0; |
| 6099 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 6100 | if (argc == 0) { |
| 6101 | PyErr_SetString(PyExc_ValueError, |
| 6102 | "spawnve() arg 2 cannot be empty"); |
| 6103 | goto fail_0; |
| 6104 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6105 | if (!PyMapping_Check(env)) { |
| 6106 | PyErr_SetString(PyExc_TypeError, |
| 6107 | "spawnve() arg 3 must be a mapping object"); |
| 6108 | goto fail_0; |
| 6109 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6110 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6111 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6112 | if (argvlist == NULL) { |
| 6113 | PyErr_NoMemory(); |
| 6114 | goto fail_0; |
| 6115 | } |
| 6116 | for (i = 0; i < argc; i++) { |
| 6117 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 6118 | &argvlist[i])) |
| 6119 | { |
| 6120 | lastarg = i; |
| 6121 | goto fail_1; |
| 6122 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6123 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6124 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6125 | PyErr_SetString( |
| 6126 | PyExc_ValueError, |
| 6127 | "spawnv() arg 2 first element cannot be empty"); |
| 6128 | goto fail_1; |
| 6129 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6130 | } |
| 6131 | lastarg = argc; |
| 6132 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6133 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6134 | envlist = parse_envlist(env, &envc); |
| 6135 | if (envlist == NULL) |
| 6136 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6137 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6138 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6139 | if (mode == _OLD_P_OVERLAY) |
| 6140 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6141 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6142 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6143 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6144 | goto fail_2; |
| 6145 | } |
| 6146 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6147 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6148 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6149 | #ifdef HAVE_WSPAWNV |
| 6150 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6151 | #elif defined(HAVE_RTPSPAWN) |
| 6152 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, |
| 6153 | (const char **)envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6154 | #else |
| 6155 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 6156 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6157 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6158 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6159 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6160 | if (spawnval == -1) |
| 6161 | (void) posix_error(); |
| 6162 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6163 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6164 | |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6165 | fail_2: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6166 | while (--envc >= 0) |
| 6167 | PyMem_DEL(envlist[envc]); |
| 6168 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 6169 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6170 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6171 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6172 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6173 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6174 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6175 | #endif /* HAVE_SPAWNV */ |
| 6176 | |
| 6177 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6178 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6179 | |
| 6180 | /* Helper function to validate arguments. |
| 6181 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 6182 | If obj is non-NULL it must be callable. */ |
| 6183 | static int |
| 6184 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 6185 | { |
| 6186 | if (obj && !PyCallable_Check(obj)) { |
| 6187 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6188 | obj_name, _PyType_Name(Py_TYPE(obj))); |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6189 | return -1; |
| 6190 | } |
| 6191 | return 0; |
| 6192 | } |
| 6193 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6194 | /*[clinic input] |
| 6195 | os.register_at_fork |
| 6196 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6197 | * |
| 6198 | before: object=NULL |
| 6199 | A callable to be called in the parent before the fork() syscall. |
| 6200 | after_in_child: object=NULL |
| 6201 | A callable to be called in the child after fork(). |
| 6202 | after_in_parent: object=NULL |
| 6203 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6204 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6205 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6206 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6207 | 'before' callbacks are called in reverse order. |
| 6208 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6209 | |
| 6210 | [clinic start generated code]*/ |
| 6211 | |
| 6212 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6213 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 6214 | PyObject *after_in_child, PyObject *after_in_parent) |
| 6215 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6216 | { |
| 6217 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6218 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6219 | if (!before && !after_in_child && !after_in_parent) { |
| 6220 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 6221 | return NULL; |
| 6222 | } |
| 6223 | if (check_null_or_callable(before, "before") || |
| 6224 | check_null_or_callable(after_in_child, "after_in_child") || |
| 6225 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6226 | return NULL; |
| 6227 | } |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6228 | interp = _PyInterpreterState_GET(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6229 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6230 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6231 | return NULL; |
| 6232 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6233 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6234 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6235 | } |
| 6236 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 6237 | return NULL; |
| 6238 | } |
| 6239 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6240 | } |
| 6241 | #endif /* HAVE_FORK */ |
| 6242 | |
| 6243 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6244 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6245 | /*[clinic input] |
| 6246 | os.fork1 |
| 6247 | |
| 6248 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 6249 | |
| 6250 | Return 0 to child process and PID of child to parent process. |
| 6251 | [clinic start generated code]*/ |
| 6252 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6253 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6254 | os_fork1_impl(PyObject *module) |
| 6255 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6256 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6257 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6258 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6259 | if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6260 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6261 | return NULL; |
| 6262 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6263 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6264 | pid = fork1(); |
| 6265 | if (pid == 0) { |
| 6266 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6267 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6268 | } else { |
| 6269 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6270 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6271 | } |
| 6272 | if (pid == -1) |
| 6273 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6274 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6275 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6276 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6277 | |
| 6278 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6279 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6280 | /*[clinic input] |
| 6281 | os.fork |
| 6282 | |
| 6283 | Fork a child process. |
| 6284 | |
| 6285 | Return 0 to child process and PID of child to parent process. |
| 6286 | [clinic start generated code]*/ |
| 6287 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6288 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6289 | os_fork_impl(PyObject *module) |
| 6290 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6291 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6292 | pid_t pid; |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 6293 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 6294 | if (interp->config._isolated_interpreter) { |
| 6295 | PyErr_SetString(PyExc_RuntimeError, |
| 6296 | "fork not supported for isolated subinterpreters"); |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6297 | return NULL; |
| 6298 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6299 | if (PySys_Audit("os.fork", NULL) < 0) { |
| 6300 | return NULL; |
| 6301 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6302 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6303 | pid = fork(); |
| 6304 | if (pid == 0) { |
| 6305 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6306 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6307 | } else { |
| 6308 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6309 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6310 | } |
| 6311 | if (pid == -1) |
| 6312 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6313 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6314 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6315 | #endif /* HAVE_FORK */ |
| 6316 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6317 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6318 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6319 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6320 | /*[clinic input] |
| 6321 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6322 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6323 | policy: int |
| 6324 | |
| 6325 | Get the maximum scheduling priority for policy. |
| 6326 | [clinic start generated code]*/ |
| 6327 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6328 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6329 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 6330 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6331 | { |
| 6332 | int max; |
| 6333 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6334 | max = sched_get_priority_max(policy); |
| 6335 | if (max < 0) |
| 6336 | return posix_error(); |
| 6337 | return PyLong_FromLong(max); |
| 6338 | } |
| 6339 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6340 | |
| 6341 | /*[clinic input] |
| 6342 | os.sched_get_priority_min |
| 6343 | |
| 6344 | policy: int |
| 6345 | |
| 6346 | Get the minimum scheduling priority for policy. |
| 6347 | [clinic start generated code]*/ |
| 6348 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6349 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6350 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 6351 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6352 | { |
| 6353 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6354 | if (min < 0) |
| 6355 | return posix_error(); |
| 6356 | return PyLong_FromLong(min); |
| 6357 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6358 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 6359 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6360 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6361 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 6362 | /*[clinic input] |
| 6363 | os.sched_getscheduler |
| 6364 | pid: pid_t |
| 6365 | / |
| 6366 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6367 | Get the scheduling policy for the process identified by pid. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6368 | |
| 6369 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6370 | [clinic start generated code]*/ |
| 6371 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6372 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6373 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6374 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6375 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6376 | int policy; |
| 6377 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6378 | policy = sched_getscheduler(pid); |
| 6379 | if (policy < 0) |
| 6380 | return posix_error(); |
| 6381 | return PyLong_FromLong(policy); |
| 6382 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6383 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6384 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6385 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6386 | #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] | 6387 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6388 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6389 | |
| 6390 | @classmethod |
| 6391 | os.sched_param.__new__ |
| 6392 | |
| 6393 | sched_priority: object |
| 6394 | A scheduling parameter. |
| 6395 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6396 | Currently has only one field: sched_priority |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6397 | [clinic start generated code]*/ |
| 6398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6399 | static PyObject * |
| 6400 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6401 | /*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6402 | { |
| 6403 | PyObject *res; |
| 6404 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6405 | res = PyStructSequence_New(type); |
| 6406 | if (!res) |
| 6407 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6408 | Py_INCREF(sched_priority); |
| 6409 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6410 | return res; |
| 6411 | } |
| 6412 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6413 | PyDoc_VAR(os_sched_param__doc__); |
| 6414 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6415 | static PyStructSequence_Field sched_param_fields[] = { |
| 6416 | {"sched_priority", "the scheduling priority"}, |
| 6417 | {0} |
| 6418 | }; |
| 6419 | |
| 6420 | static PyStructSequence_Desc sched_param_desc = { |
| 6421 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6422 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6423 | sched_param_fields, |
| 6424 | 1 |
| 6425 | }; |
| 6426 | |
| 6427 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6428 | convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6429 | { |
| 6430 | long priority; |
| 6431 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6432 | if (!Py_IS_TYPE(param, (PyTypeObject *)get_posix_state(module)->SchedParamType)) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6433 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6434 | return 0; |
| 6435 | } |
| 6436 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6437 | if (priority == -1 && PyErr_Occurred()) |
| 6438 | return 0; |
| 6439 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6440 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6441 | return 0; |
| 6442 | } |
| 6443 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6444 | return 1; |
| 6445 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6446 | #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] | 6447 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6448 | |
| 6449 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6450 | /*[clinic input] |
| 6451 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6452 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6453 | pid: pid_t |
| 6454 | policy: int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6455 | param as param_obj: object |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6456 | / |
| 6457 | |
| 6458 | Set the scheduling policy for the process identified by pid. |
| 6459 | |
| 6460 | If pid is 0, the calling process is changed. |
| 6461 | param is an instance of sched_param. |
| 6462 | [clinic start generated code]*/ |
| 6463 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6464 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6465 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6466 | PyObject *param_obj) |
| 6467 | /*[clinic end generated code: output=cde27faa55dc993e input=73013d731bd8fbe9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6468 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6469 | struct sched_param param; |
| 6470 | if (!convert_sched_param(module, param_obj, ¶m)) { |
| 6471 | return NULL; |
| 6472 | } |
| 6473 | |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6474 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6475 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6476 | ** scheduling policy under Solaris/Illumos, and others. |
| 6477 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6478 | */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6479 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6480 | return posix_error(); |
| 6481 | Py_RETURN_NONE; |
| 6482 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6483 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6484 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6485 | |
| 6486 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6487 | /*[clinic input] |
| 6488 | os.sched_getparam |
| 6489 | pid: pid_t |
| 6490 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6491 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6492 | Returns scheduling parameters for the process identified by pid. |
| 6493 | |
| 6494 | If pid is 0, returns parameters for the calling process. |
| 6495 | Return value is an instance of sched_param. |
| 6496 | [clinic start generated code]*/ |
| 6497 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6498 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6499 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6500 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6501 | { |
| 6502 | struct sched_param param; |
| 6503 | PyObject *result; |
| 6504 | PyObject *priority; |
| 6505 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6506 | if (sched_getparam(pid, ¶m)) |
| 6507 | return posix_error(); |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6508 | PyObject *SchedParamType = get_posix_state(module)->SchedParamType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6509 | result = PyStructSequence_New((PyTypeObject *)SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6510 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6511 | return NULL; |
| 6512 | priority = PyLong_FromLong(param.sched_priority); |
| 6513 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6514 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6515 | return NULL; |
| 6516 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6517 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6518 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6519 | } |
| 6520 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6521 | |
| 6522 | /*[clinic input] |
| 6523 | os.sched_setparam |
| 6524 | pid: pid_t |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6525 | param as param_obj: object |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6526 | / |
| 6527 | |
| 6528 | Set scheduling parameters for the process identified by pid. |
| 6529 | |
| 6530 | If pid is 0, sets parameters for the calling process. |
| 6531 | param should be an instance of sched_param. |
| 6532 | [clinic start generated code]*/ |
| 6533 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6534 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6535 | os_sched_setparam_impl(PyObject *module, pid_t pid, PyObject *param_obj) |
| 6536 | /*[clinic end generated code: output=f19fe020a53741c1 input=27b98337c8b2dcc7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6537 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6538 | struct sched_param param; |
| 6539 | if (!convert_sched_param(module, param_obj, ¶m)) { |
| 6540 | return NULL; |
| 6541 | } |
| 6542 | |
| 6543 | if (sched_setparam(pid, ¶m)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6544 | return posix_error(); |
| 6545 | Py_RETURN_NONE; |
| 6546 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6547 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6548 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6549 | |
| 6550 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6551 | /*[clinic input] |
| 6552 | os.sched_rr_get_interval -> double |
| 6553 | pid: pid_t |
| 6554 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6555 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6556 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6557 | |
| 6558 | Value returned is a float. |
| 6559 | [clinic start generated code]*/ |
| 6560 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6561 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6562 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6563 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6564 | { |
| 6565 | struct timespec interval; |
| 6566 | if (sched_rr_get_interval(pid, &interval)) { |
| 6567 | posix_error(); |
| 6568 | return -1.0; |
| 6569 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6570 | #ifdef _Py_MEMORY_SANITIZER |
| 6571 | __msan_unpoison(&interval, sizeof(interval)); |
| 6572 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6573 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6574 | } |
| 6575 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6576 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6577 | |
| 6578 | /*[clinic input] |
| 6579 | os.sched_yield |
| 6580 | |
| 6581 | Voluntarily relinquish the CPU. |
| 6582 | [clinic start generated code]*/ |
| 6583 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6584 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6585 | os_sched_yield_impl(PyObject *module) |
| 6586 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6587 | { |
| 6588 | if (sched_yield()) |
| 6589 | return posix_error(); |
| 6590 | Py_RETURN_NONE; |
| 6591 | } |
| 6592 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6593 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6594 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6595 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6596 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6597 | /*[clinic input] |
| 6598 | os.sched_setaffinity |
| 6599 | pid: pid_t |
| 6600 | mask : object |
| 6601 | / |
| 6602 | |
| 6603 | Set the CPU affinity of the process identified by pid to mask. |
| 6604 | |
| 6605 | mask should be an iterable of integers identifying CPUs. |
| 6606 | [clinic start generated code]*/ |
| 6607 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6608 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6609 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6610 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6611 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6612 | int ncpus; |
| 6613 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6614 | cpu_set_t *cpu_set = NULL; |
| 6615 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6616 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6617 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6618 | if (iterator == NULL) |
| 6619 | return NULL; |
| 6620 | |
| 6621 | ncpus = NCPUS_START; |
| 6622 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6623 | cpu_set = CPU_ALLOC(ncpus); |
| 6624 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6625 | PyErr_NoMemory(); |
| 6626 | goto error; |
| 6627 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6628 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6629 | |
| 6630 | while ((item = PyIter_Next(iterator))) { |
| 6631 | long cpu; |
| 6632 | if (!PyLong_Check(item)) { |
| 6633 | PyErr_Format(PyExc_TypeError, |
| 6634 | "expected an iterator of ints, " |
| 6635 | "but iterator yielded %R", |
| 6636 | Py_TYPE(item)); |
| 6637 | Py_DECREF(item); |
| 6638 | goto error; |
| 6639 | } |
| 6640 | cpu = PyLong_AsLong(item); |
| 6641 | Py_DECREF(item); |
| 6642 | if (cpu < 0) { |
| 6643 | if (!PyErr_Occurred()) |
| 6644 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6645 | goto error; |
| 6646 | } |
| 6647 | if (cpu > INT_MAX - 1) { |
| 6648 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6649 | goto error; |
| 6650 | } |
| 6651 | if (cpu >= ncpus) { |
| 6652 | /* Grow CPU mask to fit the CPU number */ |
| 6653 | int newncpus = ncpus; |
| 6654 | cpu_set_t *newmask; |
| 6655 | size_t newsetsize; |
| 6656 | while (newncpus <= cpu) { |
| 6657 | if (newncpus > INT_MAX / 2) |
| 6658 | newncpus = cpu + 1; |
| 6659 | else |
| 6660 | newncpus = newncpus * 2; |
| 6661 | } |
| 6662 | newmask = CPU_ALLOC(newncpus); |
| 6663 | if (newmask == NULL) { |
| 6664 | PyErr_NoMemory(); |
| 6665 | goto error; |
| 6666 | } |
| 6667 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6668 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6669 | memcpy(newmask, cpu_set, setsize); |
| 6670 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6671 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6672 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6673 | ncpus = newncpus; |
| 6674 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6675 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6676 | } |
Brandt Bucher | 45a30af | 2019-06-27 09:10:57 -0700 | [diff] [blame] | 6677 | if (PyErr_Occurred()) { |
| 6678 | goto error; |
| 6679 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6680 | Py_CLEAR(iterator); |
| 6681 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6682 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6683 | posix_error(); |
| 6684 | goto error; |
| 6685 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6686 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6687 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6688 | |
| 6689 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6690 | if (cpu_set) |
| 6691 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6692 | Py_XDECREF(iterator); |
| 6693 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6694 | } |
| 6695 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6696 | |
| 6697 | /*[clinic input] |
| 6698 | os.sched_getaffinity |
| 6699 | pid: pid_t |
| 6700 | / |
| 6701 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6702 | 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] | 6703 | |
| 6704 | The affinity is returned as a set of CPU identifiers. |
| 6705 | [clinic start generated code]*/ |
| 6706 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6707 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6708 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6709 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6710 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6711 | int cpu, ncpus, count; |
| 6712 | size_t setsize; |
| 6713 | cpu_set_t *mask = NULL; |
| 6714 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6715 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6716 | ncpus = NCPUS_START; |
| 6717 | while (1) { |
| 6718 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6719 | mask = CPU_ALLOC(ncpus); |
| 6720 | if (mask == NULL) |
| 6721 | return PyErr_NoMemory(); |
| 6722 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6723 | break; |
| 6724 | CPU_FREE(mask); |
| 6725 | if (errno != EINVAL) |
| 6726 | return posix_error(); |
| 6727 | if (ncpus > INT_MAX / 2) { |
| 6728 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6729 | "a large enough CPU set"); |
| 6730 | return NULL; |
| 6731 | } |
| 6732 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6733 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6734 | |
| 6735 | res = PySet_New(NULL); |
| 6736 | if (res == NULL) |
| 6737 | goto error; |
| 6738 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6739 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6740 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6741 | --count; |
| 6742 | if (cpu_num == NULL) |
| 6743 | goto error; |
| 6744 | if (PySet_Add(res, cpu_num)) { |
| 6745 | Py_DECREF(cpu_num); |
| 6746 | goto error; |
| 6747 | } |
| 6748 | Py_DECREF(cpu_num); |
| 6749 | } |
| 6750 | } |
| 6751 | CPU_FREE(mask); |
| 6752 | return res; |
| 6753 | |
| 6754 | error: |
| 6755 | if (mask) |
| 6756 | CPU_FREE(mask); |
| 6757 | Py_XDECREF(res); |
| 6758 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6759 | } |
| 6760 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6761 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6762 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6763 | #endif /* HAVE_SCHED_H */ |
| 6764 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6765 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6766 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6767 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6768 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6769 | #define DEV_PTY_FILE "/dev/ptc" |
| 6770 | #define HAVE_DEV_PTMX |
| 6771 | #else |
| 6772 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6773 | #endif |
| 6774 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6775 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6776 | #ifdef HAVE_PTY_H |
| 6777 | #include <pty.h> |
| 6778 | #else |
| 6779 | #ifdef HAVE_LIBUTIL_H |
| 6780 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6781 | #else |
| 6782 | #ifdef HAVE_UTIL_H |
| 6783 | #include <util.h> |
| 6784 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6785 | #endif /* HAVE_LIBUTIL_H */ |
| 6786 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6787 | #ifdef HAVE_STROPTS_H |
| 6788 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6789 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6790 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6791 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6792 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6793 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6794 | /*[clinic input] |
| 6795 | os.openpty |
| 6796 | |
| 6797 | Open a pseudo-terminal. |
| 6798 | |
| 6799 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6800 | for both the master and slave ends. |
| 6801 | [clinic start generated code]*/ |
| 6802 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6803 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6804 | os_openpty_impl(PyObject *module) |
| 6805 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6806 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6807 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6808 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6809 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6810 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6811 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6812 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6813 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6814 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6815 | #endif |
| 6816 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6817 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6818 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6819 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6820 | goto posix_error; |
| 6821 | |
| 6822 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6823 | goto error; |
| 6824 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6825 | goto error; |
| 6826 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6827 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6828 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6829 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6830 | goto posix_error; |
| 6831 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6832 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6833 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6834 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6835 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6836 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6837 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6838 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6839 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6840 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6841 | goto posix_error; |
| 6842 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6843 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6844 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6845 | /* change permission of slave */ |
| 6846 | if (grantpt(master_fd) < 0) { |
| 6847 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6848 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6849 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6850 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6851 | /* unlock slave */ |
| 6852 | if (unlockpt(master_fd) < 0) { |
| 6853 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6854 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6855 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6856 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6857 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6858 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6859 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6860 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6861 | goto posix_error; |
| 6862 | |
| 6863 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6864 | if (slave_fd == -1) |
| 6865 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6866 | |
| 6867 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6868 | goto posix_error; |
| 6869 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6870 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6871 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6872 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6873 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6874 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6875 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6876 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6877 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6878 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6879 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6880 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6881 | posix_error: |
| 6882 | posix_error(); |
| 6883 | error: |
| 6884 | if (master_fd != -1) |
| 6885 | close(master_fd); |
| 6886 | if (slave_fd != -1) |
| 6887 | close(slave_fd); |
| 6888 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6889 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6890 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6891 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6892 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6893 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6894 | /*[clinic input] |
| 6895 | os.forkpty |
| 6896 | |
| 6897 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6898 | |
| 6899 | Returns a tuple of (pid, master_fd). |
| 6900 | Like fork(), return pid of 0 to the child process, |
| 6901 | and pid of child to the parent process. |
| 6902 | To both, return fd of newly opened pseudo-terminal. |
| 6903 | [clinic start generated code]*/ |
| 6904 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6905 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6906 | os_forkpty_impl(PyObject *module) |
| 6907 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6908 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6909 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6910 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6911 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6912 | if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6913 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6914 | return NULL; |
| 6915 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6916 | if (PySys_Audit("os.forkpty", NULL) < 0) { |
| 6917 | return NULL; |
| 6918 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6919 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6920 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6921 | if (pid == 0) { |
| 6922 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6923 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6924 | } else { |
| 6925 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6926 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6927 | } |
| 6928 | if (pid == -1) |
| 6929 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6930 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6931 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6932 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6933 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6934 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6935 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6936 | /*[clinic input] |
| 6937 | os.getegid |
| 6938 | |
| 6939 | Return the current process's effective group id. |
| 6940 | [clinic start generated code]*/ |
| 6941 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6942 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6943 | os_getegid_impl(PyObject *module) |
| 6944 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6945 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6946 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6947 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6948 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6949 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6950 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6951 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6952 | /*[clinic input] |
| 6953 | os.geteuid |
| 6954 | |
| 6955 | Return the current process's effective user id. |
| 6956 | [clinic start generated code]*/ |
| 6957 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6958 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6959 | os_geteuid_impl(PyObject *module) |
| 6960 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6961 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6962 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6963 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6964 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6965 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6966 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6967 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6968 | /*[clinic input] |
| 6969 | os.getgid |
| 6970 | |
| 6971 | Return the current process's group id. |
| 6972 | [clinic start generated code]*/ |
| 6973 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6974 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6975 | os_getgid_impl(PyObject *module) |
| 6976 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6977 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6978 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6979 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6980 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6981 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6982 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6983 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6984 | /*[clinic input] |
| 6985 | os.getpid |
| 6986 | |
| 6987 | Return the current process id. |
| 6988 | [clinic start generated code]*/ |
| 6989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6990 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6991 | os_getpid_impl(PyObject *module) |
| 6992 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6993 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6994 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6995 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6996 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6997 | |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6998 | #ifdef NGROUPS_MAX |
| 6999 | #define MAX_GROUPS NGROUPS_MAX |
| 7000 | #else |
| 7001 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 7002 | #define MAX_GROUPS 64 |
| 7003 | #endif |
| 7004 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7005 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7006 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7007 | #ifdef __APPLE__ |
| 7008 | /*[clinic input] |
| 7009 | os.getgrouplist |
| 7010 | |
| 7011 | user: str |
| 7012 | username to lookup |
| 7013 | group as basegid: int |
| 7014 | base group id of the user |
| 7015 | / |
| 7016 | |
| 7017 | Returns a list of groups to which a user belongs. |
| 7018 | [clinic start generated code]*/ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7019 | |
| 7020 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7021 | os_getgrouplist_impl(PyObject *module, const char *user, int basegid) |
| 7022 | /*[clinic end generated code: output=6e734697b8c26de0 input=f8d870374b09a490]*/ |
| 7023 | #else |
| 7024 | /*[clinic input] |
| 7025 | os.getgrouplist |
| 7026 | |
| 7027 | user: str |
| 7028 | username to lookup |
| 7029 | group as basegid: gid_t |
| 7030 | base group id of the user |
| 7031 | / |
| 7032 | |
| 7033 | Returns a list of groups to which a user belongs. |
| 7034 | [clinic start generated code]*/ |
| 7035 | |
| 7036 | static PyObject * |
| 7037 | os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid) |
| 7038 | /*[clinic end generated code: output=0ebd7fb70115575b input=cc61d5c20b08958d]*/ |
| 7039 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7040 | { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7041 | int i, ngroups; |
| 7042 | PyObject *list; |
| 7043 | #ifdef __APPLE__ |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7044 | int *groups; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7045 | #else |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7046 | gid_t *groups; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7047 | #endif |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 7048 | |
| 7049 | /* |
| 7050 | * NGROUPS_MAX is defined by POSIX.1 as the maximum |
| 7051 | * number of supplimental groups a users can belong to. |
| 7052 | * We have to increment it by one because |
| 7053 | * getgrouplist() returns both the supplemental groups |
| 7054 | * and the primary group, i.e. all of the groups the |
| 7055 | * user belongs to. |
| 7056 | */ |
| 7057 | ngroups = 1 + MAX_GROUPS; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7058 | |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7059 | while (1) { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7060 | #ifdef __APPLE__ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7061 | groups = PyMem_New(int, ngroups); |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7062 | #else |
| 7063 | groups = PyMem_New(gid_t, ngroups); |
| 7064 | #endif |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7065 | if (groups == NULL) { |
| 7066 | return PyErr_NoMemory(); |
| 7067 | } |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7068 | |
| 7069 | int old_ngroups = ngroups; |
| 7070 | if (getgrouplist(user, basegid, groups, &ngroups) != -1) { |
| 7071 | /* Success */ |
| 7072 | break; |
| 7073 | } |
| 7074 | |
| 7075 | /* getgrouplist() fails if the group list is too small */ |
| 7076 | PyMem_Free(groups); |
| 7077 | |
| 7078 | if (ngroups > old_ngroups) { |
| 7079 | /* If the group list is too small, the glibc implementation of |
| 7080 | getgrouplist() sets ngroups to the total number of groups and |
| 7081 | returns -1. */ |
| 7082 | } |
| 7083 | else { |
| 7084 | /* Double the group list size */ |
| 7085 | if (ngroups > INT_MAX / 2) { |
| 7086 | return PyErr_NoMemory(); |
| 7087 | } |
| 7088 | ngroups *= 2; |
| 7089 | } |
| 7090 | |
| 7091 | /* Retry getgrouplist() with a larger group list */ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7092 | } |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7093 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 7094 | #ifdef _Py_MEMORY_SANITIZER |
| 7095 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 7096 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 7097 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 7098 | #endif |
| 7099 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7100 | list = PyList_New(ngroups); |
| 7101 | if (list == NULL) { |
| 7102 | PyMem_Del(groups); |
| 7103 | return NULL; |
| 7104 | } |
| 7105 | |
| 7106 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7107 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7108 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7109 | #else |
| 7110 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 7111 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7112 | if (o == NULL) { |
| 7113 | Py_DECREF(list); |
| 7114 | PyMem_Del(groups); |
| 7115 | return NULL; |
| 7116 | } |
| 7117 | PyList_SET_ITEM(list, i, o); |
| 7118 | } |
| 7119 | |
| 7120 | PyMem_Del(groups); |
| 7121 | |
| 7122 | return list; |
| 7123 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7124 | #endif /* HAVE_GETGROUPLIST */ |
| 7125 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7126 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7127 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7128 | /*[clinic input] |
| 7129 | os.getgroups |
| 7130 | |
| 7131 | Return list of supplemental group IDs for the process. |
| 7132 | [clinic start generated code]*/ |
| 7133 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7134 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7135 | os_getgroups_impl(PyObject *module) |
| 7136 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7137 | { |
| 7138 | PyObject *result = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7139 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7140 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7141 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7142 | * This is a helper variable to store the intermediate result when |
| 7143 | * that happens. |
| 7144 | * |
| 7145 | * To keep the code readable the OSX behaviour is unconditional, |
| 7146 | * according to the POSIX spec this should be safe on all unix-y |
| 7147 | * systems. |
| 7148 | */ |
| 7149 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7150 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7151 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7152 | #ifdef __APPLE__ |
| 7153 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 7154 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 7155 | * always first call getgroups with length 0 to get the actual number |
| 7156 | * of groups. |
| 7157 | */ |
| 7158 | n = getgroups(0, NULL); |
| 7159 | if (n < 0) { |
| 7160 | return posix_error(); |
| 7161 | } else if (n <= MAX_GROUPS) { |
| 7162 | /* groups will fit in existing array */ |
| 7163 | alt_grouplist = grouplist; |
| 7164 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7165 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7166 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7167 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7168 | } |
| 7169 | } |
| 7170 | |
| 7171 | n = getgroups(n, alt_grouplist); |
| 7172 | if (n == -1) { |
| 7173 | if (alt_grouplist != grouplist) { |
| 7174 | PyMem_Free(alt_grouplist); |
| 7175 | } |
| 7176 | return posix_error(); |
| 7177 | } |
| 7178 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7179 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7180 | if (n < 0) { |
| 7181 | if (errno == EINVAL) { |
| 7182 | n = getgroups(0, NULL); |
| 7183 | if (n == -1) { |
| 7184 | return posix_error(); |
| 7185 | } |
| 7186 | if (n == 0) { |
| 7187 | /* Avoid malloc(0) */ |
| 7188 | alt_grouplist = grouplist; |
| 7189 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7190 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7191 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7192 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7193 | } |
| 7194 | n = getgroups(n, alt_grouplist); |
| 7195 | if (n == -1) { |
| 7196 | PyMem_Free(alt_grouplist); |
| 7197 | return posix_error(); |
| 7198 | } |
| 7199 | } |
| 7200 | } else { |
| 7201 | return posix_error(); |
| 7202 | } |
| 7203 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7204 | #endif |
| 7205 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7206 | result = PyList_New(n); |
| 7207 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7208 | int i; |
| 7209 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7210 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7211 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7212 | Py_DECREF(result); |
| 7213 | result = NULL; |
| 7214 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7215 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7216 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7217 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7218 | } |
| 7219 | |
| 7220 | if (alt_grouplist != grouplist) { |
| 7221 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7222 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7223 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7224 | return result; |
| 7225 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7226 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7227 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7228 | #ifdef HAVE_INITGROUPS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7229 | #ifdef __APPLE__ |
| 7230 | /*[clinic input] |
| 7231 | os.initgroups |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7232 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7233 | username as oname: FSConverter |
| 7234 | gid: int |
| 7235 | / |
| 7236 | |
| 7237 | Initialize the group access list. |
| 7238 | |
| 7239 | Call the system initgroups() to initialize the group access list with all of |
| 7240 | the groups of which the specified username is a member, plus the specified |
| 7241 | group id. |
| 7242 | [clinic start generated code]*/ |
| 7243 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7244 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7245 | os_initgroups_impl(PyObject *module, PyObject *oname, int gid) |
| 7246 | /*[clinic end generated code: output=7f074d30a425fd3a input=df3d54331b0af204]*/ |
| 7247 | #else |
| 7248 | /*[clinic input] |
| 7249 | os.initgroups |
| 7250 | |
| 7251 | username as oname: FSConverter |
| 7252 | gid: gid_t |
| 7253 | / |
| 7254 | |
| 7255 | Initialize the group access list. |
| 7256 | |
| 7257 | Call the system initgroups() to initialize the group access list with all of |
| 7258 | the groups of which the specified username is a member, plus the specified |
| 7259 | group id. |
| 7260 | [clinic start generated code]*/ |
| 7261 | |
| 7262 | static PyObject * |
| 7263 | os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid) |
| 7264 | /*[clinic end generated code: output=59341244521a9e3f input=0cb91bdc59a4c564]*/ |
| 7265 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7266 | { |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7267 | const char *username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7268 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7269 | if (initgroups(username, gid) == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7270 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7271 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7272 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7273 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7274 | #endif /* HAVE_INITGROUPS */ |
| 7275 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7276 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7277 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7278 | /*[clinic input] |
| 7279 | os.getpgid |
| 7280 | |
| 7281 | pid: pid_t |
| 7282 | |
| 7283 | Call the system call getpgid(), and return the result. |
| 7284 | [clinic start generated code]*/ |
| 7285 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7286 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7287 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 7288 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7289 | { |
| 7290 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7291 | if (pgid < 0) |
| 7292 | return posix_error(); |
| 7293 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7294 | } |
| 7295 | #endif /* HAVE_GETPGID */ |
| 7296 | |
| 7297 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7298 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7299 | /*[clinic input] |
| 7300 | os.getpgrp |
| 7301 | |
| 7302 | Return the current process group id. |
| 7303 | [clinic start generated code]*/ |
| 7304 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7305 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7306 | os_getpgrp_impl(PyObject *module) |
| 7307 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7308 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7309 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7310 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7311 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7312 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7313 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7314 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7315 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7316 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7317 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7318 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7319 | /*[clinic input] |
| 7320 | os.setpgrp |
| 7321 | |
| 7322 | Make the current process the leader of its process group. |
| 7323 | [clinic start generated code]*/ |
| 7324 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7325 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7326 | os_setpgrp_impl(PyObject *module) |
| 7327 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7328 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7329 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7330 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7331 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7332 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7333 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7334 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7335 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7336 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7337 | #endif /* HAVE_SETPGRP */ |
| 7338 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7339 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7340 | |
| 7341 | #ifdef MS_WINDOWS |
| 7342 | #include <tlhelp32.h> |
| 7343 | |
| 7344 | static PyObject* |
| 7345 | win32_getppid() |
| 7346 | { |
| 7347 | HANDLE snapshot; |
| 7348 | pid_t mypid; |
| 7349 | PyObject* result = NULL; |
| 7350 | BOOL have_record; |
| 7351 | PROCESSENTRY32 pe; |
| 7352 | |
| 7353 | mypid = getpid(); /* This function never fails */ |
| 7354 | |
| 7355 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 7356 | if (snapshot == INVALID_HANDLE_VALUE) |
| 7357 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 7358 | |
| 7359 | pe.dwSize = sizeof(pe); |
| 7360 | have_record = Process32First(snapshot, &pe); |
| 7361 | while (have_record) { |
| 7362 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 7363 | /* We could cache the ulong value in a static variable. */ |
| 7364 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 7365 | break; |
| 7366 | } |
| 7367 | |
| 7368 | have_record = Process32Next(snapshot, &pe); |
| 7369 | } |
| 7370 | |
| 7371 | /* If our loop exits and our pid was not found (result will be NULL) |
| 7372 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 7373 | * error anyway, so let's raise it. */ |
| 7374 | if (!result) |
| 7375 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7376 | |
| 7377 | CloseHandle(snapshot); |
| 7378 | |
| 7379 | return result; |
| 7380 | } |
| 7381 | #endif /*MS_WINDOWS*/ |
| 7382 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7383 | |
| 7384 | /*[clinic input] |
| 7385 | os.getppid |
| 7386 | |
| 7387 | Return the parent's process id. |
| 7388 | |
| 7389 | If the parent process has already exited, Windows machines will still |
| 7390 | return its id; others systems will return the id of the 'init' process (1). |
| 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_getppid_impl(PyObject *module) |
| 7395 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7396 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7397 | #ifdef MS_WINDOWS |
| 7398 | return win32_getppid(); |
| 7399 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7400 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7401 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7402 | } |
| 7403 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7404 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7405 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7406 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7407 | /*[clinic input] |
| 7408 | os.getlogin |
| 7409 | |
| 7410 | Return the actual login name. |
| 7411 | [clinic start generated code]*/ |
| 7412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7413 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7414 | os_getlogin_impl(PyObject *module) |
| 7415 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7416 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7417 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7418 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7419 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7420 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7421 | |
| 7422 | if (GetUserNameW(user_name, &num_chars)) { |
| 7423 | /* num_chars is the number of unicode chars plus null terminator */ |
| 7424 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7425 | } |
| 7426 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7427 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7428 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7429 | char *name; |
| 7430 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7431 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7432 | errno = 0; |
| 7433 | name = getlogin(); |
| 7434 | if (name == NULL) { |
| 7435 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7436 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7437 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7438 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7439 | } |
| 7440 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7441 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7442 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7443 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7444 | return result; |
| 7445 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7446 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7447 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7448 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7449 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7450 | /*[clinic input] |
| 7451 | os.getuid |
| 7452 | |
| 7453 | Return the current process's user id. |
| 7454 | [clinic start generated code]*/ |
| 7455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7456 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7457 | os_getuid_impl(PyObject *module) |
| 7458 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7459 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7460 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7461 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7462 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7463 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7464 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7465 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7466 | #define HAVE_KILL |
| 7467 | #endif /* MS_WINDOWS */ |
| 7468 | |
| 7469 | #ifdef HAVE_KILL |
| 7470 | /*[clinic input] |
| 7471 | os.kill |
| 7472 | |
| 7473 | pid: pid_t |
| 7474 | signal: Py_ssize_t |
| 7475 | / |
| 7476 | |
| 7477 | Kill a process with a signal. |
| 7478 | [clinic start generated code]*/ |
| 7479 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7480 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7481 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7482 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7483 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7484 | if (PySys_Audit("os.kill", "in", pid, signal) < 0) { |
| 7485 | return NULL; |
| 7486 | } |
| 7487 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7488 | if (kill(pid, (int)signal) == -1) |
| 7489 | return posix_error(); |
| 7490 | Py_RETURN_NONE; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7491 | #else /* !MS_WINDOWS */ |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7492 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7493 | DWORD sig = (DWORD)signal; |
| 7494 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7495 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7496 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7497 | /* Console processes which share a common console can be sent CTRL+C or |
| 7498 | CTRL+BREAK events, provided they handle said events. */ |
| 7499 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7500 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7501 | err = GetLastError(); |
| 7502 | PyErr_SetFromWindowsErr(err); |
| 7503 | } |
| 7504 | else |
| 7505 | Py_RETURN_NONE; |
| 7506 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7507 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7508 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7509 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7510 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7511 | if (handle == NULL) { |
| 7512 | err = GetLastError(); |
| 7513 | return PyErr_SetFromWindowsErr(err); |
| 7514 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7515 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7516 | if (TerminateProcess(handle, sig) == 0) { |
| 7517 | err = GetLastError(); |
| 7518 | result = PyErr_SetFromWindowsErr(err); |
| 7519 | } else { |
| 7520 | Py_INCREF(Py_None); |
| 7521 | result = Py_None; |
| 7522 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7523 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7524 | CloseHandle(handle); |
| 7525 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7526 | #endif /* !MS_WINDOWS */ |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7527 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7528 | #endif /* HAVE_KILL */ |
| 7529 | |
| 7530 | |
| 7531 | #ifdef HAVE_KILLPG |
| 7532 | /*[clinic input] |
| 7533 | os.killpg |
| 7534 | |
| 7535 | pgid: pid_t |
| 7536 | signal: int |
| 7537 | / |
| 7538 | |
| 7539 | Kill a process group with a signal. |
| 7540 | [clinic start generated code]*/ |
| 7541 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7542 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7543 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7544 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7545 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7546 | if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) { |
| 7547 | return NULL; |
| 7548 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7549 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7550 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7551 | take the same type. Moreover, pid_t is always at least as wide as |
| 7552 | int (else compilation of this module fails), which is safe. */ |
| 7553 | if (killpg(pgid, signal) == -1) |
| 7554 | return posix_error(); |
| 7555 | Py_RETURN_NONE; |
| 7556 | } |
| 7557 | #endif /* HAVE_KILLPG */ |
| 7558 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7559 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7560 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7561 | #ifdef HAVE_SYS_LOCK_H |
| 7562 | #include <sys/lock.h> |
| 7563 | #endif |
| 7564 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7565 | /*[clinic input] |
| 7566 | os.plock |
| 7567 | op: int |
| 7568 | / |
| 7569 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7570 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7571 | [clinic start generated code]*/ |
| 7572 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7573 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7574 | os_plock_impl(PyObject *module, int op) |
| 7575 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7576 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7577 | if (plock(op) == -1) |
| 7578 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7579 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7580 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7581 | #endif /* HAVE_PLOCK */ |
| 7582 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7583 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7584 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7585 | /*[clinic input] |
| 7586 | os.setuid |
| 7587 | |
| 7588 | uid: uid_t |
| 7589 | / |
| 7590 | |
| 7591 | Set the current process's user id. |
| 7592 | [clinic start generated code]*/ |
| 7593 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7594 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7595 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7596 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7597 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7598 | if (setuid(uid) < 0) |
| 7599 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7600 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7601 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7602 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7603 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7604 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7605 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7606 | /*[clinic input] |
| 7607 | os.seteuid |
| 7608 | |
| 7609 | euid: uid_t |
| 7610 | / |
| 7611 | |
| 7612 | Set the current process's effective user id. |
| 7613 | [clinic start generated code]*/ |
| 7614 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7615 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7616 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7617 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7618 | { |
| 7619 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7620 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7621 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7622 | } |
| 7623 | #endif /* HAVE_SETEUID */ |
| 7624 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7625 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7626 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7627 | /*[clinic input] |
| 7628 | os.setegid |
| 7629 | |
| 7630 | egid: gid_t |
| 7631 | / |
| 7632 | |
| 7633 | Set the current process's effective group id. |
| 7634 | [clinic start generated code]*/ |
| 7635 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7636 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7637 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7638 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7639 | { |
| 7640 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7641 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7642 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7643 | } |
| 7644 | #endif /* HAVE_SETEGID */ |
| 7645 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7646 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7647 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7648 | /*[clinic input] |
| 7649 | os.setreuid |
| 7650 | |
| 7651 | ruid: uid_t |
| 7652 | euid: uid_t |
| 7653 | / |
| 7654 | |
| 7655 | Set the current process's real and effective user ids. |
| 7656 | [clinic start generated code]*/ |
| 7657 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7658 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7659 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7660 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7661 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7662 | if (setreuid(ruid, euid) < 0) { |
| 7663 | return posix_error(); |
| 7664 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7665 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7666 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7667 | } |
| 7668 | #endif /* HAVE_SETREUID */ |
| 7669 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7670 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7671 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7672 | /*[clinic input] |
| 7673 | os.setregid |
| 7674 | |
| 7675 | rgid: gid_t |
| 7676 | egid: gid_t |
| 7677 | / |
| 7678 | |
| 7679 | Set the current process's real and effective group ids. |
| 7680 | [clinic start generated code]*/ |
| 7681 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7682 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7683 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7684 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7685 | { |
| 7686 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7687 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7688 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7689 | } |
| 7690 | #endif /* HAVE_SETREGID */ |
| 7691 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7692 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7693 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7694 | /*[clinic input] |
| 7695 | os.setgid |
| 7696 | gid: gid_t |
| 7697 | / |
| 7698 | |
| 7699 | Set the current process's group id. |
| 7700 | [clinic start generated code]*/ |
| 7701 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7702 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7703 | os_setgid_impl(PyObject *module, gid_t gid) |
| 7704 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7705 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7706 | if (setgid(gid) < 0) |
| 7707 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7708 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7709 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7710 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7711 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7712 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7713 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7714 | /*[clinic input] |
| 7715 | os.setgroups |
| 7716 | |
| 7717 | groups: object |
| 7718 | / |
| 7719 | |
| 7720 | Set the groups of the current process to list. |
| 7721 | [clinic start generated code]*/ |
| 7722 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7723 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7724 | os_setgroups(PyObject *module, PyObject *groups) |
| 7725 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7726 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7727 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7728 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7729 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7730 | if (!PySequence_Check(groups)) { |
| 7731 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7732 | return NULL; |
| 7733 | } |
| 7734 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7735 | if (len < 0) { |
| 7736 | return NULL; |
| 7737 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7738 | if (len > MAX_GROUPS) { |
| 7739 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7740 | return NULL; |
| 7741 | } |
| 7742 | for(i = 0; i < len; i++) { |
| 7743 | PyObject *elem; |
| 7744 | elem = PySequence_GetItem(groups, i); |
| 7745 | if (!elem) |
| 7746 | return NULL; |
| 7747 | if (!PyLong_Check(elem)) { |
| 7748 | PyErr_SetString(PyExc_TypeError, |
| 7749 | "groups must be integers"); |
| 7750 | Py_DECREF(elem); |
| 7751 | return NULL; |
| 7752 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7753 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7754 | Py_DECREF(elem); |
| 7755 | return NULL; |
| 7756 | } |
| 7757 | } |
| 7758 | Py_DECREF(elem); |
| 7759 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7760 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7761 | if (setgroups(len, grouplist) < 0) |
| 7762 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7763 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7764 | } |
| 7765 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7766 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7767 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7768 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7769 | wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7770 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7771 | PyObject *result; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7772 | PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7773 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7774 | if (pid == -1) |
| 7775 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7776 | |
Zackery Spytz | 682107c | 2019-09-09 09:48:32 -0600 | [diff] [blame] | 7777 | // If wait succeeded but no child was ready to report status, ru will not |
| 7778 | // have been populated. |
| 7779 | if (pid == 0) { |
| 7780 | memset(ru, 0, sizeof(*ru)); |
| 7781 | } |
| 7782 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7783 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7784 | if (m == NULL) |
| 7785 | return NULL; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7786 | struct_rusage = PyObject_GetAttr(m, get_posix_state(module)->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7787 | Py_DECREF(m); |
| 7788 | if (struct_rusage == NULL) |
| 7789 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7790 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7791 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7792 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
Eddie Elizondo | e4db1f0 | 2019-11-25 19:07:37 -0800 | [diff] [blame] | 7793 | Py_DECREF(struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7794 | if (!result) |
| 7795 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7796 | |
| 7797 | #ifndef doubletime |
| 7798 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7799 | #endif |
| 7800 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7801 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7802 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7803 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7804 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7805 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7806 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7807 | SET_INT(result, 2, ru->ru_maxrss); |
| 7808 | SET_INT(result, 3, ru->ru_ixrss); |
| 7809 | SET_INT(result, 4, ru->ru_idrss); |
| 7810 | SET_INT(result, 5, ru->ru_isrss); |
| 7811 | SET_INT(result, 6, ru->ru_minflt); |
| 7812 | SET_INT(result, 7, ru->ru_majflt); |
| 7813 | SET_INT(result, 8, ru->ru_nswap); |
| 7814 | SET_INT(result, 9, ru->ru_inblock); |
| 7815 | SET_INT(result, 10, ru->ru_oublock); |
| 7816 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7817 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7818 | SET_INT(result, 13, ru->ru_nsignals); |
| 7819 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7820 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7821 | #undef SET_INT |
| 7822 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7823 | if (PyErr_Occurred()) { |
| 7824 | Py_DECREF(result); |
| 7825 | return NULL; |
| 7826 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7827 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7828 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7829 | } |
| 7830 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7831 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7832 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7833 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7834 | /*[clinic input] |
| 7835 | os.wait3 |
| 7836 | |
| 7837 | options: int |
| 7838 | Wait for completion of a child process. |
| 7839 | |
| 7840 | Returns a tuple of information about the child process: |
| 7841 | (pid, status, rusage) |
| 7842 | [clinic start generated code]*/ |
| 7843 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7844 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7845 | os_wait3_impl(PyObject *module, int options) |
| 7846 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7847 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7848 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7849 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7850 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7851 | WAIT_TYPE status; |
| 7852 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7853 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7854 | do { |
| 7855 | Py_BEGIN_ALLOW_THREADS |
| 7856 | pid = wait3(&status, options, &ru); |
| 7857 | Py_END_ALLOW_THREADS |
| 7858 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7859 | if (pid < 0) |
| 7860 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7861 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7862 | return wait_helper(module, pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7863 | } |
| 7864 | #endif /* HAVE_WAIT3 */ |
| 7865 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7866 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7867 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7868 | /*[clinic input] |
| 7869 | |
| 7870 | os.wait4 |
| 7871 | |
| 7872 | pid: pid_t |
| 7873 | options: int |
| 7874 | |
| 7875 | Wait for completion of a specific child process. |
| 7876 | |
| 7877 | Returns a tuple of information about the child process: |
| 7878 | (pid, status, rusage) |
| 7879 | [clinic start generated code]*/ |
| 7880 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7881 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7882 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7883 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7884 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7885 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7886 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7887 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7888 | WAIT_TYPE status; |
| 7889 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7890 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7891 | do { |
| 7892 | Py_BEGIN_ALLOW_THREADS |
| 7893 | res = wait4(pid, &status, options, &ru); |
| 7894 | Py_END_ALLOW_THREADS |
| 7895 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7896 | if (res < 0) |
| 7897 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7898 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7899 | return wait_helper(module, res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7900 | } |
| 7901 | #endif /* HAVE_WAIT4 */ |
| 7902 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7903 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7904 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7905 | /*[clinic input] |
| 7906 | os.waitid |
| 7907 | |
| 7908 | idtype: idtype_t |
| 7909 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7910 | id: id_t |
| 7911 | The id to wait on. |
| 7912 | options: int |
| 7913 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7914 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7915 | / |
| 7916 | |
| 7917 | Returns the result of waiting for a process or processes. |
| 7918 | |
| 7919 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7920 | no children in a waitable state. |
| 7921 | [clinic start generated code]*/ |
| 7922 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7923 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7924 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7925 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7926 | { |
| 7927 | PyObject *result; |
| 7928 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7929 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7930 | siginfo_t si; |
| 7931 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7932 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7933 | do { |
| 7934 | Py_BEGIN_ALLOW_THREADS |
| 7935 | res = waitid(idtype, id, &si, options); |
| 7936 | Py_END_ALLOW_THREADS |
| 7937 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7938 | if (res < 0) |
| 7939 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7940 | |
| 7941 | if (si.si_pid == 0) |
| 7942 | Py_RETURN_NONE; |
| 7943 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 7944 | PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7945 | result = PyStructSequence_New((PyTypeObject *)WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7946 | if (!result) |
| 7947 | return NULL; |
| 7948 | |
| 7949 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7950 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7951 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7952 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7953 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7954 | if (PyErr_Occurred()) { |
| 7955 | Py_DECREF(result); |
| 7956 | return NULL; |
| 7957 | } |
| 7958 | |
| 7959 | return result; |
| 7960 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7961 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7962 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7963 | |
| 7964 | #if defined(HAVE_WAITPID) |
| 7965 | /*[clinic input] |
| 7966 | os.waitpid |
| 7967 | pid: pid_t |
| 7968 | options: int |
| 7969 | / |
| 7970 | |
| 7971 | Wait for completion of a given child process. |
| 7972 | |
| 7973 | Returns a tuple of information regarding the child process: |
| 7974 | (pid, status) |
| 7975 | |
| 7976 | The options argument is ignored on Windows. |
| 7977 | [clinic start generated code]*/ |
| 7978 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7979 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7980 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7981 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7982 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7983 | pid_t res; |
| 7984 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7985 | WAIT_TYPE status; |
| 7986 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7987 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7988 | do { |
| 7989 | Py_BEGIN_ALLOW_THREADS |
| 7990 | res = waitpid(pid, &status, options); |
| 7991 | Py_END_ALLOW_THREADS |
| 7992 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7993 | if (res < 0) |
| 7994 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7995 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7996 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7997 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7998 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7999 | /* 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] | 8000 | /*[clinic input] |
| 8001 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8002 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8003 | options: int |
| 8004 | / |
| 8005 | |
| 8006 | Wait for completion of a given process. |
| 8007 | |
| 8008 | Returns a tuple of information regarding the process: |
| 8009 | (pid, status << 8) |
| 8010 | |
| 8011 | The options argument is ignored on Windows. |
| 8012 | [clinic start generated code]*/ |
| 8013 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8014 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8015 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 8016 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8017 | { |
| 8018 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8019 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8020 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8021 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8022 | do { |
| 8023 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 8024 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8025 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 8026 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8027 | Py_END_ALLOW_THREADS |
| 8028 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8029 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8030 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8031 | |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 8032 | unsigned long long ustatus = (unsigned int)status; |
| 8033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8034 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 8035 | return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8036 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8037 | #endif |
| 8038 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8039 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8040 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8041 | /*[clinic input] |
| 8042 | os.wait |
| 8043 | |
| 8044 | Wait for completion of a child process. |
| 8045 | |
| 8046 | Returns a tuple of information about the child process: |
| 8047 | (pid, status) |
| 8048 | [clinic start generated code]*/ |
| 8049 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8050 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8051 | os_wait_impl(PyObject *module) |
| 8052 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 8053 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8054 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8055 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8056 | WAIT_TYPE status; |
| 8057 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8058 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8059 | do { |
| 8060 | Py_BEGIN_ALLOW_THREADS |
| 8061 | pid = wait(&status); |
| 8062 | Py_END_ALLOW_THREADS |
| 8063 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8064 | if (pid < 0) |
| 8065 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8066 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8067 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8068 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8069 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8070 | |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 8071 | #if defined(__linux__) && defined(__NR_pidfd_open) |
| 8072 | /*[clinic input] |
| 8073 | os.pidfd_open |
| 8074 | pid: pid_t |
| 8075 | flags: unsigned_int = 0 |
| 8076 | |
| 8077 | Return a file descriptor referring to the process *pid*. |
| 8078 | |
| 8079 | The descriptor can be used to perform process management without races and |
| 8080 | signals. |
| 8081 | [clinic start generated code]*/ |
| 8082 | |
| 8083 | static PyObject * |
| 8084 | os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags) |
| 8085 | /*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/ |
| 8086 | { |
| 8087 | int fd = syscall(__NR_pidfd_open, pid, flags); |
| 8088 | if (fd < 0) { |
| 8089 | return posix_error(); |
| 8090 | } |
| 8091 | return PyLong_FromLong(fd); |
| 8092 | } |
| 8093 | #endif |
| 8094 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8095 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8096 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8097 | /*[clinic input] |
| 8098 | os.readlink |
| 8099 | |
| 8100 | path: path_t |
| 8101 | * |
| 8102 | dir_fd: dir_fd(requires='readlinkat') = None |
| 8103 | |
| 8104 | Return a string representing the path to which the symbolic link points. |
| 8105 | |
| 8106 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8107 | and path should be relative; path will then be relative to that directory. |
| 8108 | |
| 8109 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 8110 | using it will raise a NotImplementedError. |
| 8111 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8112 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8113 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8114 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 8115 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8116 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8117 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 8118 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8119 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8120 | |
| 8121 | Py_BEGIN_ALLOW_THREADS |
| 8122 | #ifdef HAVE_READLINKAT |
| 8123 | if (dir_fd != DEFAULT_DIR_FD) |
| 8124 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 8125 | else |
| 8126 | #endif |
| 8127 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 8128 | Py_END_ALLOW_THREADS |
| 8129 | |
| 8130 | if (length < 0) { |
| 8131 | return path_error(path); |
| 8132 | } |
| 8133 | buffer[length] = '\0'; |
| 8134 | |
| 8135 | if (PyUnicode_Check(path->object)) |
| 8136 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 8137 | else |
| 8138 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8139 | #elif defined(MS_WINDOWS) |
| 8140 | DWORD n_bytes_returned; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8141 | DWORD io_result = 0; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8142 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8143 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 8144 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8145 | PyObject *result = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8146 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8147 | /* First get a handle to the reparse point */ |
| 8148 | Py_BEGIN_ALLOW_THREADS |
| 8149 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8150 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8151 | 0, |
| 8152 | 0, |
| 8153 | 0, |
| 8154 | OPEN_EXISTING, |
| 8155 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 8156 | 0); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8157 | if (reparse_point_handle != INVALID_HANDLE_VALUE) { |
| 8158 | /* New call DeviceIoControl to read the reparse point */ |
| 8159 | io_result = DeviceIoControl( |
| 8160 | reparse_point_handle, |
| 8161 | FSCTL_GET_REPARSE_POINT, |
| 8162 | 0, 0, /* in buffer */ |
| 8163 | target_buffer, sizeof(target_buffer), |
| 8164 | &n_bytes_returned, |
| 8165 | 0 /* we're not using OVERLAPPED_IO */ |
| 8166 | ); |
| 8167 | CloseHandle(reparse_point_handle); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8168 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8169 | Py_END_ALLOW_THREADS |
| 8170 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8171 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8172 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8173 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8174 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8175 | wchar_t *name = NULL; |
| 8176 | Py_ssize_t nameLen = 0; |
| 8177 | if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8178 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8179 | name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 8180 | rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 8181 | nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8182 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8183 | else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) |
| 8184 | { |
| 8185 | name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer + |
| 8186 | rdb->MountPointReparseBuffer.SubstituteNameOffset); |
| 8187 | nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
| 8188 | } |
| 8189 | else |
| 8190 | { |
| 8191 | PyErr_SetString(PyExc_ValueError, "not a symbolic link"); |
| 8192 | } |
| 8193 | if (name) { |
| 8194 | if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) { |
| 8195 | /* Our buffer is mutable, so this is okay */ |
| 8196 | name[1] = L'\\'; |
| 8197 | } |
| 8198 | result = PyUnicode_FromWideChar(name, nameLen); |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8199 | if (result && path->narrow) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8200 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
| 8201 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8202 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8203 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8204 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8205 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8206 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8207 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8208 | #if defined(MS_WINDOWS) |
| 8209 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8210 | /* Remove the last portion of the path - return 0 on success */ |
| 8211 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8212 | _dirnameW(WCHAR *path) |
| 8213 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8214 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8215 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 8216 | if (length == MAX_PATH) { |
| 8217 | return -1; |
| 8218 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8219 | |
| 8220 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8221 | for(ptr = path + length; ptr != path; ptr--) { |
| 8222 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8223 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8224 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8225 | } |
| 8226 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8227 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8228 | } |
| 8229 | |
Minmin Gong | 7f21c9a | 2020-05-18 09:17:19 -0700 | [diff] [blame] | 8230 | #endif |
| 8231 | |
| 8232 | #ifdef HAVE_SYMLINK |
| 8233 | |
| 8234 | #if defined(MS_WINDOWS) |
| 8235 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8236 | /* Is this path absolute? */ |
| 8237 | static int |
| 8238 | _is_absW(const WCHAR *path) |
| 8239 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8240 | return path[0] == L'\\' || path[0] == L'/' || |
| 8241 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8242 | } |
| 8243 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8244 | /* join root and rest with a backslash - return 0 on success */ |
| 8245 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8246 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 8247 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8248 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8249 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8250 | } |
| 8251 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8252 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 8253 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8254 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8255 | |
| 8256 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 8257 | return -1; |
| 8258 | } |
| 8259 | |
| 8260 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8261 | } |
| 8262 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8263 | /* Return True if the path at src relative to dest is a directory */ |
| 8264 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8265 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8266 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8267 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 8268 | WCHAR dest_parent[MAX_PATH]; |
| 8269 | WCHAR src_resolved[MAX_PATH] = L""; |
| 8270 | |
| 8271 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8272 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 8273 | _dirnameW(dest_parent)) { |
| 8274 | return 0; |
| 8275 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8276 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8277 | if (_joinW(src_resolved, dest_parent, src)) { |
| 8278 | return 0; |
| 8279 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8280 | return ( |
| 8281 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 8282 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 8283 | ); |
| 8284 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8285 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8286 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8287 | |
| 8288 | /*[clinic input] |
| 8289 | os.symlink |
| 8290 | src: path_t |
| 8291 | dst: path_t |
| 8292 | target_is_directory: bool = False |
| 8293 | * |
| 8294 | dir_fd: dir_fd(requires='symlinkat')=None |
| 8295 | |
| 8296 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 8297 | |
| 8298 | Create a symbolic link pointing to src named dst. |
| 8299 | |
| 8300 | target_is_directory is required on Windows if the target is to be |
| 8301 | interpreted as a directory. (On Windows, symlink requires |
| 8302 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 8303 | target_is_directory is ignored on non-Windows platforms. |
| 8304 | |
| 8305 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8306 | and path should be relative; path will then be relative to that directory. |
| 8307 | dir_fd may not be implemented on your platform. |
| 8308 | If it is unavailable, using it will raise a NotImplementedError. |
| 8309 | |
| 8310 | [clinic start generated code]*/ |
| 8311 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8312 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8313 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8314 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8315 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8316 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8317 | #ifdef MS_WINDOWS |
| 8318 | DWORD result; |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8319 | DWORD flags = 0; |
| 8320 | |
| 8321 | /* Assumed true, set to false if detected to not be available. */ |
| 8322 | static int windows_has_symlink_unprivileged_flag = TRUE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8323 | #else |
| 8324 | int result; |
| 8325 | #endif |
| 8326 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 8327 | if (PySys_Audit("os.symlink", "OOi", src->object, dst->object, |
| 8328 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 8329 | return NULL; |
| 8330 | } |
| 8331 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8332 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8333 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8334 | if (windows_has_symlink_unprivileged_flag) { |
| 8335 | /* Allow non-admin symlinks if system allows it. */ |
| 8336 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 8337 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8338 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8339 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8340 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8341 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
| 8342 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { |
| 8343 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 8344 | } |
| 8345 | |
| 8346 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8347 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8348 | Py_END_ALLOW_THREADS |
| 8349 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8350 | if (windows_has_symlink_unprivileged_flag && !result && |
| 8351 | ERROR_INVALID_PARAMETER == GetLastError()) { |
| 8352 | |
| 8353 | Py_BEGIN_ALLOW_THREADS |
| 8354 | _Py_BEGIN_SUPPRESS_IPH |
| 8355 | /* This error might be caused by |
| 8356 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. |
| 8357 | Try again, and update windows_has_symlink_unprivileged_flag if we |
| 8358 | are successful this time. |
| 8359 | |
| 8360 | NOTE: There is a risk of a race condition here if there are other |
| 8361 | conditions than the flag causing ERROR_INVALID_PARAMETER, and |
| 8362 | another process (or thread) changes that condition in between our |
| 8363 | calls to CreateSymbolicLink. |
| 8364 | */ |
| 8365 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 8366 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
| 8367 | _Py_END_SUPPRESS_IPH |
| 8368 | Py_END_ALLOW_THREADS |
| 8369 | |
| 8370 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { |
| 8371 | windows_has_symlink_unprivileged_flag = FALSE; |
| 8372 | } |
| 8373 | } |
| 8374 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8375 | if (!result) |
| 8376 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8377 | |
| 8378 | #else |
| 8379 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8380 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 8381 | PyErr_SetString(PyExc_ValueError, |
| 8382 | "symlink: src and dst must be the same type"); |
| 8383 | return NULL; |
| 8384 | } |
| 8385 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8386 | Py_BEGIN_ALLOW_THREADS |
| 8387 | #if HAVE_SYMLINKAT |
| 8388 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8389 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8390 | else |
| 8391 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8392 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8393 | Py_END_ALLOW_THREADS |
| 8394 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8395 | if (result) |
| 8396 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8397 | #endif |
| 8398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8399 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8400 | } |
| 8401 | #endif /* HAVE_SYMLINK */ |
| 8402 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8403 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8404 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8405 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8406 | static PyStructSequence_Field times_result_fields[] = { |
| 8407 | {"user", "user time"}, |
| 8408 | {"system", "system time"}, |
| 8409 | {"children_user", "user time of children"}, |
| 8410 | {"children_system", "system time of children"}, |
| 8411 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 8412 | {NULL} |
| 8413 | }; |
| 8414 | |
| 8415 | PyDoc_STRVAR(times_result__doc__, |
| 8416 | "times_result: Result from os.times().\n\n\ |
| 8417 | This object may be accessed either as a tuple of\n\ |
| 8418 | (user, system, children_user, children_system, elapsed),\n\ |
| 8419 | or via the attributes user, system, children_user, children_system,\n\ |
| 8420 | and elapsed.\n\ |
| 8421 | \n\ |
| 8422 | See os.times for more information."); |
| 8423 | |
| 8424 | static PyStructSequence_Desc times_result_desc = { |
| 8425 | "times_result", /* name */ |
| 8426 | times_result__doc__, /* doc */ |
| 8427 | times_result_fields, |
| 8428 | 5 |
| 8429 | }; |
| 8430 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8431 | #ifdef MS_WINDOWS |
| 8432 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 8433 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8434 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8435 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8436 | |
| 8437 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8438 | build_times_result(PyObject *module, double user, double system, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8439 | double children_user, double children_system, |
| 8440 | double elapsed) |
| 8441 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8442 | PyObject *TimesResultType = get_posix_state(module)->TimesResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8443 | PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8444 | if (value == NULL) |
| 8445 | return NULL; |
| 8446 | |
| 8447 | #define SET(i, field) \ |
| 8448 | { \ |
| 8449 | PyObject *o = PyFloat_FromDouble(field); \ |
| 8450 | if (!o) { \ |
| 8451 | Py_DECREF(value); \ |
| 8452 | return NULL; \ |
| 8453 | } \ |
| 8454 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 8455 | } \ |
| 8456 | |
| 8457 | SET(0, user); |
| 8458 | SET(1, system); |
| 8459 | SET(2, children_user); |
| 8460 | SET(3, children_system); |
| 8461 | SET(4, elapsed); |
| 8462 | |
| 8463 | #undef SET |
| 8464 | |
| 8465 | return value; |
| 8466 | } |
| 8467 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8468 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8469 | #ifndef MS_WINDOWS |
| 8470 | #define NEED_TICKS_PER_SECOND |
| 8471 | static long ticks_per_second = -1; |
| 8472 | #endif /* MS_WINDOWS */ |
| 8473 | |
| 8474 | /*[clinic input] |
| 8475 | os.times |
| 8476 | |
| 8477 | Return a collection containing process timing information. |
| 8478 | |
| 8479 | The object returned behaves like a named tuple with these fields: |
| 8480 | (utime, stime, cutime, cstime, elapsed_time) |
| 8481 | All fields are floating point numbers. |
| 8482 | [clinic start generated code]*/ |
| 8483 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8484 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8485 | os_times_impl(PyObject *module) |
| 8486 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8487 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8488 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8489 | FILETIME create, exit, kernel, user; |
| 8490 | HANDLE hProc; |
| 8491 | hProc = GetCurrentProcess(); |
| 8492 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8493 | /* The fields of a FILETIME structure are the hi and lo part |
| 8494 | of a 64-bit value expressed in 100 nanosecond units. |
| 8495 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8496 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8497 | */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8498 | return build_times_result(module, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8499 | (double)(user.dwHighDateTime*429.4967296 + |
| 8500 | user.dwLowDateTime*1e-7), |
| 8501 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8502 | kernel.dwLowDateTime*1e-7), |
| 8503 | (double)0, |
| 8504 | (double)0, |
| 8505 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8506 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8507 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8508 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8509 | |
| 8510 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8511 | struct tms t; |
| 8512 | clock_t c; |
| 8513 | errno = 0; |
| 8514 | c = times(&t); |
| 8515 | if (c == (clock_t) -1) |
| 8516 | return posix_error(); |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8517 | return build_times_result(module, |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8518 | (double)t.tms_utime / ticks_per_second, |
| 8519 | (double)t.tms_stime / ticks_per_second, |
| 8520 | (double)t.tms_cutime / ticks_per_second, |
| 8521 | (double)t.tms_cstime / ticks_per_second, |
| 8522 | (double)c / ticks_per_second); |
| 8523 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8524 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8525 | #endif /* HAVE_TIMES */ |
| 8526 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8527 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8528 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8529 | /*[clinic input] |
| 8530 | os.getsid |
| 8531 | |
| 8532 | pid: pid_t |
| 8533 | / |
| 8534 | |
| 8535 | Call the system call getsid(pid) and return the result. |
| 8536 | [clinic start generated code]*/ |
| 8537 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8538 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8539 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8540 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8541 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8542 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8543 | sid = getsid(pid); |
| 8544 | if (sid < 0) |
| 8545 | return posix_error(); |
| 8546 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8547 | } |
| 8548 | #endif /* HAVE_GETSID */ |
| 8549 | |
| 8550 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8551 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8552 | /*[clinic input] |
| 8553 | os.setsid |
| 8554 | |
| 8555 | Call the system call setsid(). |
| 8556 | [clinic start generated code]*/ |
| 8557 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8558 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8559 | os_setsid_impl(PyObject *module) |
| 8560 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8561 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8562 | if (setsid() < 0) |
| 8563 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8564 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8565 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8566 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8567 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8568 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8569 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8570 | /*[clinic input] |
| 8571 | os.setpgid |
| 8572 | |
| 8573 | pid: pid_t |
| 8574 | pgrp: pid_t |
| 8575 | / |
| 8576 | |
| 8577 | Call the system call setpgid(pid, pgrp). |
| 8578 | [clinic start generated code]*/ |
| 8579 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8580 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8581 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8582 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8583 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8584 | if (setpgid(pid, pgrp) < 0) |
| 8585 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8586 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8587 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8588 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8589 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8590 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8591 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8592 | /*[clinic input] |
| 8593 | os.tcgetpgrp |
| 8594 | |
| 8595 | fd: int |
| 8596 | / |
| 8597 | |
| 8598 | Return the process group associated with the terminal specified by fd. |
| 8599 | [clinic start generated code]*/ |
| 8600 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8601 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8602 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8603 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8604 | { |
| 8605 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8606 | if (pgid < 0) |
| 8607 | return posix_error(); |
| 8608 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8609 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8610 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8611 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8612 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8613 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8614 | /*[clinic input] |
| 8615 | os.tcsetpgrp |
| 8616 | |
| 8617 | fd: int |
| 8618 | pgid: pid_t |
| 8619 | / |
| 8620 | |
| 8621 | Set the process group associated with the terminal specified by fd. |
| 8622 | [clinic start generated code]*/ |
| 8623 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8624 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8625 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8626 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8627 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8628 | if (tcsetpgrp(fd, pgid) < 0) |
| 8629 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8630 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8631 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8632 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8633 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8634 | /* Functions acting on file descriptors */ |
| 8635 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8636 | #ifdef O_CLOEXEC |
| 8637 | extern int _Py_open_cloexec_works; |
| 8638 | #endif |
| 8639 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8640 | |
| 8641 | /*[clinic input] |
| 8642 | os.open -> int |
| 8643 | path: path_t |
| 8644 | flags: int |
| 8645 | mode: int = 0o777 |
| 8646 | * |
| 8647 | dir_fd: dir_fd(requires='openat') = None |
| 8648 | |
| 8649 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8650 | |
| 8651 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8652 | |
| 8653 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8654 | and path should be relative; path will then be relative to that directory. |
| 8655 | dir_fd may not be implemented on your platform. |
| 8656 | If it is unavailable, using it will raise a NotImplementedError. |
| 8657 | [clinic start generated code]*/ |
| 8658 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8659 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8660 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8661 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8662 | { |
| 8663 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8664 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8665 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8666 | #ifdef O_CLOEXEC |
| 8667 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8668 | #elif !defined(MS_WINDOWS) |
| 8669 | int *atomic_flag_works = NULL; |
| 8670 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8671 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8672 | #ifdef MS_WINDOWS |
| 8673 | flags |= O_NOINHERIT; |
| 8674 | #elif defined(O_CLOEXEC) |
| 8675 | flags |= O_CLOEXEC; |
| 8676 | #endif |
| 8677 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 8678 | if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) { |
| 8679 | return -1; |
| 8680 | } |
| 8681 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8682 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8683 | do { |
| 8684 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8685 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8686 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8687 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8688 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8689 | if (dir_fd != DEFAULT_DIR_FD) |
| 8690 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8691 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8692 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8693 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8694 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8695 | Py_END_ALLOW_THREADS |
| 8696 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8697 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8698 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8699 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8700 | if (!async_err) |
| 8701 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8702 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8703 | } |
| 8704 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8705 | #ifndef MS_WINDOWS |
| 8706 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8707 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8708 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8709 | } |
| 8710 | #endif |
| 8711 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8712 | return fd; |
| 8713 | } |
| 8714 | |
| 8715 | |
| 8716 | /*[clinic input] |
| 8717 | os.close |
| 8718 | |
| 8719 | fd: int |
| 8720 | |
| 8721 | Close a file descriptor. |
| 8722 | [clinic start generated code]*/ |
| 8723 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8724 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8725 | os_close_impl(PyObject *module, int fd) |
| 8726 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8727 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8728 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8729 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8730 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8731 | * for more details. |
| 8732 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8733 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8734 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8735 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8736 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8737 | Py_END_ALLOW_THREADS |
| 8738 | if (res < 0) |
| 8739 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8740 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8741 | } |
| 8742 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8743 | |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8744 | #ifdef HAVE_FDWALK |
| 8745 | static int |
| 8746 | _fdwalk_close_func(void *lohi, int fd) |
| 8747 | { |
| 8748 | int lo = ((int *)lohi)[0]; |
| 8749 | int hi = ((int *)lohi)[1]; |
| 8750 | |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8751 | if (fd >= hi) { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8752 | return 1; |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8753 | } |
| 8754 | else if (fd >= lo) { |
| 8755 | /* Ignore errors */ |
| 8756 | (void)close(fd); |
| 8757 | } |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8758 | return 0; |
| 8759 | } |
| 8760 | #endif /* HAVE_FDWALK */ |
| 8761 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8762 | /*[clinic input] |
| 8763 | os.closerange |
| 8764 | |
| 8765 | fd_low: int |
| 8766 | fd_high: int |
| 8767 | / |
| 8768 | |
| 8769 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8770 | [clinic start generated code]*/ |
| 8771 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8772 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8773 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8774 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8775 | { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8776 | #ifdef HAVE_FDWALK |
| 8777 | int lohi[2]; |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8778 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8779 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8780 | _Py_BEGIN_SUPPRESS_IPH |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8781 | #ifdef HAVE_FDWALK |
| 8782 | lohi[0] = Py_MAX(fd_low, 0); |
| 8783 | lohi[1] = fd_high; |
| 8784 | fdwalk(_fdwalk_close_func, lohi); |
| 8785 | #else |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8786 | fd_low = Py_MAX(fd_low, 0); |
| 8787 | #ifdef __FreeBSD__ |
| 8788 | if (fd_high >= sysconf(_SC_OPEN_MAX)) { |
| 8789 | /* Any errors encountered while closing file descriptors are ignored */ |
| 8790 | closefrom(fd_low); |
| 8791 | } |
| 8792 | else |
| 8793 | #endif |
| 8794 | { |
| 8795 | for (int i = fd_low; i < fd_high; i++) { |
| 8796 | /* Ignore errors */ |
| 8797 | (void)close(i); |
| 8798 | } |
| 8799 | } |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8800 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8801 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8802 | Py_END_ALLOW_THREADS |
| 8803 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8804 | } |
| 8805 | |
| 8806 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8807 | /*[clinic input] |
| 8808 | os.dup -> int |
| 8809 | |
| 8810 | fd: int |
| 8811 | / |
| 8812 | |
| 8813 | Return a duplicate of a file descriptor. |
| 8814 | [clinic start generated code]*/ |
| 8815 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8816 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8817 | os_dup_impl(PyObject *module, int fd) |
| 8818 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8819 | { |
| 8820 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8821 | } |
| 8822 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8823 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8824 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8825 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8826 | fd: int |
| 8827 | fd2: int |
| 8828 | inheritable: bool=True |
| 8829 | |
| 8830 | Duplicate file descriptor. |
| 8831 | [clinic start generated code]*/ |
| 8832 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8833 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8834 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8835 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8836 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8837 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8838 | #if defined(HAVE_DUP3) && \ |
| 8839 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8840 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8841 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8842 | #endif |
| 8843 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8844 | if (fd < 0 || fd2 < 0) { |
| 8845 | posix_error(); |
| 8846 | return -1; |
| 8847 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8848 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8849 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8850 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8851 | * upon close(), and therefore below. |
| 8852 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8853 | #ifdef MS_WINDOWS |
| 8854 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8855 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8856 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8857 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8858 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8859 | if (res < 0) { |
| 8860 | posix_error(); |
| 8861 | return -1; |
| 8862 | } |
| 8863 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8864 | |
| 8865 | /* Character files like console cannot be make non-inheritable */ |
| 8866 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8867 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8868 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8869 | } |
| 8870 | |
| 8871 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8872 | Py_BEGIN_ALLOW_THREADS |
| 8873 | if (!inheritable) |
| 8874 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8875 | else |
| 8876 | res = dup2(fd, fd2); |
| 8877 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8878 | if (res < 0) { |
| 8879 | posix_error(); |
| 8880 | return -1; |
| 8881 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8882 | |
| 8883 | #else |
| 8884 | |
| 8885 | #ifdef HAVE_DUP3 |
| 8886 | if (!inheritable && dup3_works != 0) { |
| 8887 | Py_BEGIN_ALLOW_THREADS |
| 8888 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8889 | Py_END_ALLOW_THREADS |
| 8890 | if (res < 0) { |
| 8891 | if (dup3_works == -1) |
| 8892 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8893 | if (dup3_works) { |
| 8894 | posix_error(); |
| 8895 | return -1; |
| 8896 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8897 | } |
| 8898 | } |
| 8899 | |
| 8900 | if (inheritable || dup3_works == 0) |
| 8901 | { |
| 8902 | #endif |
| 8903 | Py_BEGIN_ALLOW_THREADS |
| 8904 | res = dup2(fd, fd2); |
| 8905 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8906 | if (res < 0) { |
| 8907 | posix_error(); |
| 8908 | return -1; |
| 8909 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8910 | |
| 8911 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8912 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8913 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8914 | } |
| 8915 | #ifdef HAVE_DUP3 |
| 8916 | } |
| 8917 | #endif |
| 8918 | |
| 8919 | #endif |
| 8920 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8921 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8922 | } |
| 8923 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8924 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8925 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8926 | /*[clinic input] |
| 8927 | os.lockf |
| 8928 | |
| 8929 | fd: int |
| 8930 | An open file descriptor. |
| 8931 | command: int |
| 8932 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8933 | length: Py_off_t |
| 8934 | The number of bytes to lock, starting at the current position. |
| 8935 | / |
| 8936 | |
| 8937 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8938 | |
| 8939 | [clinic start generated code]*/ |
| 8940 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8941 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8942 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8943 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8944 | { |
| 8945 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8946 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 8947 | if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) { |
| 8948 | return NULL; |
| 8949 | } |
| 8950 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8951 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8952 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8953 | Py_END_ALLOW_THREADS |
| 8954 | |
| 8955 | if (res < 0) |
| 8956 | return posix_error(); |
| 8957 | |
| 8958 | Py_RETURN_NONE; |
| 8959 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8960 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8961 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8962 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8963 | /*[clinic input] |
| 8964 | os.lseek -> Py_off_t |
| 8965 | |
| 8966 | fd: int |
| 8967 | position: Py_off_t |
| 8968 | how: int |
| 8969 | / |
| 8970 | |
| 8971 | Set the position of a file descriptor. Return the new position. |
| 8972 | |
| 8973 | Return the new cursor position in number of bytes |
| 8974 | relative to the beginning of the file. |
| 8975 | [clinic start generated code]*/ |
| 8976 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8977 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8978 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8979 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8980 | { |
| 8981 | Py_off_t result; |
| 8982 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8983 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8984 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8985 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8986 | case 0: how = SEEK_SET; break; |
| 8987 | case 1: how = SEEK_CUR; break; |
| 8988 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8989 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8990 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8991 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8992 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8993 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8994 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8995 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8996 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8997 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8998 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8999 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9000 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9001 | if (result < 0) |
| 9002 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9003 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9004 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9005 | } |
| 9006 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9007 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9008 | /*[clinic input] |
| 9009 | os.read |
| 9010 | fd: int |
| 9011 | length: Py_ssize_t |
| 9012 | / |
| 9013 | |
| 9014 | Read from a file descriptor. Returns a bytes object. |
| 9015 | [clinic start generated code]*/ |
| 9016 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9017 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9018 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 9019 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9020 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9021 | Py_ssize_t n; |
| 9022 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9023 | |
| 9024 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9025 | errno = EINVAL; |
| 9026 | return posix_error(); |
| 9027 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9028 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 9029 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9030 | |
| 9031 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9032 | if (buffer == NULL) |
| 9033 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9034 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9035 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 9036 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9037 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9038 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9039 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9040 | |
| 9041 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9042 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9043 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9044 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9045 | } |
| 9046 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9047 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9048 | || defined(__APPLE__))) \ |
| 9049 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 9050 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9051 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9052 | 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] | 9053 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9054 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9055 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9056 | *iov = PyMem_New(struct iovec, cnt); |
| 9057 | if (*iov == NULL) { |
| 9058 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9059 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9060 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9061 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9062 | *buf = PyMem_New(Py_buffer, cnt); |
| 9063 | if (*buf == NULL) { |
| 9064 | PyMem_Del(*iov); |
| 9065 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9066 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9067 | } |
| 9068 | |
| 9069 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9070 | PyObject *item = PySequence_GetItem(seq, i); |
| 9071 | if (item == NULL) |
| 9072 | goto fail; |
| 9073 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 9074 | Py_DECREF(item); |
| 9075 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9076 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9077 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9078 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9079 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9080 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9081 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9082 | |
| 9083 | fail: |
| 9084 | PyMem_Del(*iov); |
| 9085 | for (j = 0; j < i; j++) { |
| 9086 | PyBuffer_Release(&(*buf)[j]); |
| 9087 | } |
| 9088 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9089 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9090 | } |
| 9091 | |
| 9092 | static void |
| 9093 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 9094 | { |
| 9095 | int i; |
| 9096 | PyMem_Del(iov); |
| 9097 | for (i = 0; i < cnt; i++) { |
| 9098 | PyBuffer_Release(&buf[i]); |
| 9099 | } |
| 9100 | PyMem_Del(buf); |
| 9101 | } |
| 9102 | #endif |
| 9103 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9104 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9105 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9106 | /*[clinic input] |
| 9107 | os.readv -> Py_ssize_t |
| 9108 | |
| 9109 | fd: int |
| 9110 | buffers: object |
| 9111 | / |
| 9112 | |
| 9113 | Read from a file descriptor fd into an iterable of buffers. |
| 9114 | |
| 9115 | The buffers should be mutable buffers accepting bytes. |
| 9116 | readv will transfer data into each buffer until it is full |
| 9117 | and then move on to the next buffer in the sequence to hold |
| 9118 | the rest of the data. |
| 9119 | |
| 9120 | readv returns the total number of bytes read, |
| 9121 | which may be less than the total capacity of all the buffers. |
| 9122 | [clinic start generated code]*/ |
| 9123 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9124 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9125 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 9126 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9127 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9128 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9129 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9130 | struct iovec *iov; |
| 9131 | Py_buffer *buf; |
| 9132 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9133 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9134 | PyErr_SetString(PyExc_TypeError, |
| 9135 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9136 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9137 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9139 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9140 | if (cnt < 0) |
| 9141 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9142 | |
| 9143 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 9144 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9145 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9146 | do { |
| 9147 | Py_BEGIN_ALLOW_THREADS |
| 9148 | n = readv(fd, iov, cnt); |
| 9149 | Py_END_ALLOW_THREADS |
| 9150 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9151 | |
| 9152 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9153 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9154 | if (!async_err) |
| 9155 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9156 | return -1; |
| 9157 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9158 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9159 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9160 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9161 | #endif /* HAVE_READV */ |
| 9162 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9163 | |
| 9164 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9165 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9166 | os.pread |
| 9167 | |
| 9168 | fd: int |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9169 | length: Py_ssize_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9170 | offset: Py_off_t |
| 9171 | / |
| 9172 | |
| 9173 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 9174 | |
| 9175 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 9176 | the beginning of the file. The file offset remains unchanged. |
| 9177 | [clinic start generated code]*/ |
| 9178 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9179 | static PyObject * |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9180 | os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset) |
| 9181 | /*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9182 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9183 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9184 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9185 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9186 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9187 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9188 | errno = EINVAL; |
| 9189 | return posix_error(); |
| 9190 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9191 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9192 | if (buffer == NULL) |
| 9193 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9194 | |
| 9195 | do { |
| 9196 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9197 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9198 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9199 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9200 | Py_END_ALLOW_THREADS |
| 9201 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9202 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9203 | if (n < 0) { |
| 9204 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9205 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9206 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9207 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9208 | _PyBytes_Resize(&buffer, n); |
| 9209 | return buffer; |
| 9210 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9211 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9212 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9213 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 9214 | /*[clinic input] |
| 9215 | os.preadv -> Py_ssize_t |
| 9216 | |
| 9217 | fd: int |
| 9218 | buffers: object |
| 9219 | offset: Py_off_t |
| 9220 | flags: int = 0 |
| 9221 | / |
| 9222 | |
| 9223 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 9224 | |
| 9225 | Combines the functionality of readv() and pread(). As readv(), it will |
| 9226 | transfer data into each buffer until it is full and then move on to the next |
| 9227 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 9228 | specifies the file offset at which the input operation is to be performed. It |
| 9229 | will return the total number of bytes read (which can be less than the total |
| 9230 | capacity of all the objects). |
| 9231 | |
| 9232 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9233 | |
| 9234 | - RWF_HIPRI |
| 9235 | - RWF_NOWAIT |
| 9236 | |
| 9237 | Using non-zero flags requires Linux 4.6 or newer. |
| 9238 | [clinic start generated code]*/ |
| 9239 | |
| 9240 | static Py_ssize_t |
| 9241 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9242 | int flags) |
| 9243 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 9244 | { |
| 9245 | Py_ssize_t cnt, n; |
| 9246 | int async_err = 0; |
| 9247 | struct iovec *iov; |
| 9248 | Py_buffer *buf; |
| 9249 | |
| 9250 | if (!PySequence_Check(buffers)) { |
| 9251 | PyErr_SetString(PyExc_TypeError, |
| 9252 | "preadv2() arg 2 must be a sequence"); |
| 9253 | return -1; |
| 9254 | } |
| 9255 | |
| 9256 | cnt = PySequence_Size(buffers); |
| 9257 | if (cnt < 0) { |
| 9258 | return -1; |
| 9259 | } |
| 9260 | |
| 9261 | #ifndef HAVE_PREADV2 |
| 9262 | if(flags != 0) { |
| 9263 | argument_unavailable_error("preadv2", "flags"); |
| 9264 | return -1; |
| 9265 | } |
| 9266 | #endif |
| 9267 | |
| 9268 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 9269 | return -1; |
| 9270 | } |
| 9271 | #ifdef HAVE_PREADV2 |
| 9272 | do { |
| 9273 | Py_BEGIN_ALLOW_THREADS |
| 9274 | _Py_BEGIN_SUPPRESS_IPH |
| 9275 | n = preadv2(fd, iov, cnt, offset, flags); |
| 9276 | _Py_END_SUPPRESS_IPH |
| 9277 | Py_END_ALLOW_THREADS |
| 9278 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9279 | #else |
| 9280 | do { |
| 9281 | Py_BEGIN_ALLOW_THREADS |
| 9282 | _Py_BEGIN_SUPPRESS_IPH |
| 9283 | n = preadv(fd, iov, cnt, offset); |
| 9284 | _Py_END_SUPPRESS_IPH |
| 9285 | Py_END_ALLOW_THREADS |
| 9286 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9287 | #endif |
| 9288 | |
| 9289 | iov_cleanup(iov, buf, cnt); |
| 9290 | if (n < 0) { |
| 9291 | if (!async_err) { |
| 9292 | posix_error(); |
| 9293 | } |
| 9294 | return -1; |
| 9295 | } |
| 9296 | |
| 9297 | return n; |
| 9298 | } |
| 9299 | #endif /* HAVE_PREADV */ |
| 9300 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9301 | |
| 9302 | /*[clinic input] |
| 9303 | os.write -> Py_ssize_t |
| 9304 | |
| 9305 | fd: int |
| 9306 | data: Py_buffer |
| 9307 | / |
| 9308 | |
| 9309 | Write a bytes object to a file descriptor. |
| 9310 | [clinic start generated code]*/ |
| 9311 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9312 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9313 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 9314 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9315 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9316 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9317 | } |
| 9318 | |
| 9319 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9320 | #ifdef __APPLE__ |
| 9321 | /*[clinic input] |
| 9322 | os.sendfile |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9323 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9324 | out_fd: int |
| 9325 | in_fd: int |
| 9326 | offset: Py_off_t |
| 9327 | count as sbytes: Py_off_t |
| 9328 | headers: object(c_default="NULL") = () |
| 9329 | trailers: object(c_default="NULL") = () |
| 9330 | flags: int = 0 |
| 9331 | |
| 9332 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9333 | [clinic start generated code]*/ |
| 9334 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9335 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9336 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, |
| 9337 | Py_off_t sbytes, PyObject *headers, PyObject *trailers, |
| 9338 | int flags) |
| 9339 | /*[clinic end generated code: output=81c4bcd143f5c82b input=b0d72579d4c69afa]*/ |
| 9340 | #elif defined(__FreeBSD__) || defined(__DragonFly__) |
| 9341 | /*[clinic input] |
| 9342 | os.sendfile |
| 9343 | |
| 9344 | out_fd: int |
| 9345 | in_fd: int |
| 9346 | offset: Py_off_t |
| 9347 | count: Py_ssize_t |
| 9348 | headers: object(c_default="NULL") = () |
| 9349 | trailers: object(c_default="NULL") = () |
| 9350 | flags: int = 0 |
| 9351 | |
| 9352 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9353 | [clinic start generated code]*/ |
| 9354 | |
| 9355 | static PyObject * |
| 9356 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, |
| 9357 | Py_ssize_t count, PyObject *headers, PyObject *trailers, |
| 9358 | int flags) |
| 9359 | /*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/ |
| 9360 | #else |
| 9361 | /*[clinic input] |
| 9362 | os.sendfile |
| 9363 | |
| 9364 | out_fd: int |
| 9365 | in_fd: int |
| 9366 | offset as offobj: object |
| 9367 | count: Py_ssize_t |
| 9368 | |
| 9369 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9370 | [clinic start generated code]*/ |
| 9371 | |
| 9372 | static PyObject * |
| 9373 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj, |
| 9374 | Py_ssize_t count) |
| 9375 | /*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/ |
| 9376 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9377 | { |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9378 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9379 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9380 | |
| 9381 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 9382 | #ifndef __APPLE__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9383 | off_t sbytes; |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9384 | #endif |
| 9385 | Py_buffer *hbuf, *tbuf; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9386 | struct sf_hdtr sf; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9387 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 9388 | sf.headers = NULL; |
| 9389 | sf.trailers = NULL; |
| 9390 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9391 | if (headers != NULL) { |
| 9392 | if (!PySequence_Check(headers)) { |
| 9393 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9394 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9395 | return NULL; |
| 9396 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9397 | Py_ssize_t i = PySequence_Size(headers); |
| 9398 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9399 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9400 | if (i > INT_MAX) { |
| 9401 | PyErr_SetString(PyExc_OverflowError, |
| 9402 | "sendfile() header is too large"); |
| 9403 | return NULL; |
| 9404 | } |
| 9405 | if (i > 0) { |
| 9406 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9407 | if (iov_setup(&(sf.headers), &hbuf, |
| 9408 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9409 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9410 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9411 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 9412 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 9413 | # define OFF_T_MAX 0x7fffffffffffffff |
| 9414 | if (sbytes >= OFF_T_MAX - blen) { |
| 9415 | PyErr_SetString(PyExc_OverflowError, |
| 9416 | "sendfile() header is too large"); |
| 9417 | return NULL; |
| 9418 | } |
| 9419 | sbytes += blen; |
| 9420 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9421 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9422 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9423 | } |
| 9424 | } |
| 9425 | if (trailers != NULL) { |
| 9426 | if (!PySequence_Check(trailers)) { |
| 9427 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9428 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9429 | return NULL; |
| 9430 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9431 | Py_ssize_t i = PySequence_Size(trailers); |
| 9432 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9433 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9434 | if (i > INT_MAX) { |
| 9435 | PyErr_SetString(PyExc_OverflowError, |
| 9436 | "sendfile() trailer is too large"); |
| 9437 | return NULL; |
| 9438 | } |
| 9439 | if (i > 0) { |
| 9440 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9441 | if (iov_setup(&(sf.trailers), &tbuf, |
| 9442 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9443 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9444 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9445 | } |
| 9446 | } |
| 9447 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9448 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9449 | do { |
| 9450 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9451 | #ifdef __APPLE__ |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9452 | ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9453 | #else |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9454 | ret = sendfile(in_fd, out_fd, offset, count, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9455 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9456 | Py_END_ALLOW_THREADS |
| 9457 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9458 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9459 | |
| 9460 | if (sf.headers != NULL) |
| 9461 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 9462 | if (sf.trailers != NULL) |
| 9463 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 9464 | |
| 9465 | if (ret < 0) { |
| 9466 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 9467 | if (sbytes != 0) { |
| 9468 | // some data has been sent |
| 9469 | goto done; |
| 9470 | } |
| 9471 | else { |
| 9472 | // no data has been sent; upper application is supposed |
| 9473 | // to retry on EAGAIN or EBUSY |
| 9474 | return posix_error(); |
| 9475 | } |
| 9476 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9477 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9478 | } |
| 9479 | goto done; |
| 9480 | |
| 9481 | done: |
| 9482 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 9483 | return Py_BuildValue("l", sbytes); |
| 9484 | #else |
| 9485 | return Py_BuildValue("L", sbytes); |
| 9486 | #endif |
| 9487 | |
| 9488 | #else |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9489 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9490 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9491 | do { |
| 9492 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9493 | ret = sendfile(out_fd, in_fd, NULL, count); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9494 | Py_END_ALLOW_THREADS |
| 9495 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9496 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9497 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9498 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9499 | } |
| 9500 | #endif |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9501 | off_t offset; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9502 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9503 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9504 | |
Jakub Kulík | 8c0be6f | 2020-09-05 21:10:01 +0200 | [diff] [blame] | 9505 | #if defined(__sun) && defined(__SVR4) |
| 9506 | // On Solaris, sendfile raises EINVAL rather than returning 0 |
| 9507 | // when the offset is equal or bigger than the in_fd size. |
Jakub Kulík | 8c0be6f | 2020-09-05 21:10:01 +0200 | [diff] [blame] | 9508 | struct stat st; |
| 9509 | |
| 9510 | do { |
| 9511 | Py_BEGIN_ALLOW_THREADS |
Jakub Kulík | fa8c9e7 | 2020-09-09 21:29:42 +0200 | [diff] [blame] | 9512 | ret = fstat(in_fd, &st); |
Jakub Kulík | 8c0be6f | 2020-09-05 21:10:01 +0200 | [diff] [blame] | 9513 | Py_END_ALLOW_THREADS |
Jakub Kulík | fa8c9e7 | 2020-09-09 21:29:42 +0200 | [diff] [blame] | 9514 | } while (ret != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Jakub Kulík | 8c0be6f | 2020-09-05 21:10:01 +0200 | [diff] [blame] | 9515 | if (ret < 0) |
| 9516 | return (!async_err) ? posix_error() : NULL; |
| 9517 | |
| 9518 | if (offset >= st.st_size) { |
| 9519 | return Py_BuildValue("i", 0); |
| 9520 | } |
| 9521 | #endif |
| 9522 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9523 | do { |
| 9524 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9525 | ret = sendfile(out_fd, in_fd, &offset, count); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9526 | Py_END_ALLOW_THREADS |
| 9527 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9528 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9529 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9530 | return Py_BuildValue("n", ret); |
| 9531 | #endif |
| 9532 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9533 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9535 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9536 | #if defined(__APPLE__) |
| 9537 | /*[clinic input] |
| 9538 | os._fcopyfile |
| 9539 | |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9540 | in_fd: int |
| 9541 | out_fd: int |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9542 | flags: int |
| 9543 | / |
| 9544 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9545 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9546 | [clinic start generated code]*/ |
| 9547 | |
| 9548 | static PyObject * |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9549 | os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags) |
| 9550 | /*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9551 | { |
| 9552 | int ret; |
| 9553 | |
| 9554 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9555 | ret = fcopyfile(in_fd, out_fd, NULL, flags); |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9556 | Py_END_ALLOW_THREADS |
| 9557 | if (ret < 0) |
| 9558 | return posix_error(); |
| 9559 | Py_RETURN_NONE; |
| 9560 | } |
| 9561 | #endif |
| 9562 | |
| 9563 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9564 | /*[clinic input] |
| 9565 | os.fstat |
| 9566 | |
| 9567 | fd : int |
| 9568 | |
| 9569 | Perform a stat system call on the given file descriptor. |
| 9570 | |
| 9571 | Like stat(), but for an open file descriptor. |
| 9572 | Equivalent to os.stat(fd). |
| 9573 | [clinic start generated code]*/ |
| 9574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9575 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9576 | os_fstat_impl(PyObject *module, int fd) |
| 9577 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9578 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9579 | STRUCT_STAT st; |
| 9580 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9581 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9582 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9583 | do { |
| 9584 | Py_BEGIN_ALLOW_THREADS |
| 9585 | res = FSTAT(fd, &st); |
| 9586 | Py_END_ALLOW_THREADS |
| 9587 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9588 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9589 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9590 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9591 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9592 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9593 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9594 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9595 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 9596 | return _pystat_fromstructstat(module, &st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9597 | } |
| 9598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9599 | |
| 9600 | /*[clinic input] |
| 9601 | os.isatty -> bool |
| 9602 | fd: int |
| 9603 | / |
| 9604 | |
| 9605 | Return True if the fd is connected to a terminal. |
| 9606 | |
| 9607 | Return True if the file descriptor is an open file descriptor |
| 9608 | connected to the slave end of a terminal. |
| 9609 | [clinic start generated code]*/ |
| 9610 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9611 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9612 | os_isatty_impl(PyObject *module, int fd) |
| 9613 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9614 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9615 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9616 | _Py_BEGIN_SUPPRESS_IPH |
| 9617 | return_value = isatty(fd); |
| 9618 | _Py_END_SUPPRESS_IPH |
| 9619 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9620 | } |
| 9621 | |
| 9622 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9623 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9624 | /*[clinic input] |
| 9625 | os.pipe |
| 9626 | |
| 9627 | Create a pipe. |
| 9628 | |
| 9629 | Returns a tuple of two file descriptors: |
| 9630 | (read_fd, write_fd) |
| 9631 | [clinic start generated code]*/ |
| 9632 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9633 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9634 | os_pipe_impl(PyObject *module) |
| 9635 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9636 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9637 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9638 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9639 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9640 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9641 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9642 | #else |
| 9643 | int res; |
| 9644 | #endif |
| 9645 | |
| 9646 | #ifdef MS_WINDOWS |
| 9647 | attr.nLength = sizeof(attr); |
| 9648 | attr.lpSecurityDescriptor = NULL; |
| 9649 | attr.bInheritHandle = FALSE; |
| 9650 | |
| 9651 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9652 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9653 | ok = CreatePipe(&read, &write, &attr, 0); |
| 9654 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9655 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 9656 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9657 | if (fds[0] == -1 || fds[1] == -1) { |
| 9658 | CloseHandle(read); |
| 9659 | CloseHandle(write); |
| 9660 | ok = 0; |
| 9661 | } |
| 9662 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9663 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9664 | Py_END_ALLOW_THREADS |
| 9665 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9666 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9667 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9668 | #else |
| 9669 | |
| 9670 | #ifdef HAVE_PIPE2 |
| 9671 | Py_BEGIN_ALLOW_THREADS |
| 9672 | res = pipe2(fds, O_CLOEXEC); |
| 9673 | Py_END_ALLOW_THREADS |
| 9674 | |
| 9675 | if (res != 0 && errno == ENOSYS) |
| 9676 | { |
| 9677 | #endif |
| 9678 | Py_BEGIN_ALLOW_THREADS |
| 9679 | res = pipe(fds); |
| 9680 | Py_END_ALLOW_THREADS |
| 9681 | |
| 9682 | if (res == 0) { |
| 9683 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9684 | close(fds[0]); |
| 9685 | close(fds[1]); |
| 9686 | return NULL; |
| 9687 | } |
| 9688 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9689 | close(fds[0]); |
| 9690 | close(fds[1]); |
| 9691 | return NULL; |
| 9692 | } |
| 9693 | } |
| 9694 | #ifdef HAVE_PIPE2 |
| 9695 | } |
| 9696 | #endif |
| 9697 | |
| 9698 | if (res != 0) |
| 9699 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9700 | #endif /* !MS_WINDOWS */ |
| 9701 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9702 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9703 | #endif /* HAVE_PIPE */ |
| 9704 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9705 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9706 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9707 | /*[clinic input] |
| 9708 | os.pipe2 |
| 9709 | |
| 9710 | flags: int |
| 9711 | / |
| 9712 | |
| 9713 | Create a pipe with flags set atomically. |
| 9714 | |
| 9715 | Returns a tuple of two file descriptors: |
| 9716 | (read_fd, write_fd) |
| 9717 | |
| 9718 | flags can be constructed by ORing together one or more of these values: |
| 9719 | O_NONBLOCK, O_CLOEXEC. |
| 9720 | [clinic start generated code]*/ |
| 9721 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9722 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9723 | os_pipe2_impl(PyObject *module, int flags) |
| 9724 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9725 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9726 | int fds[2]; |
| 9727 | int res; |
| 9728 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9729 | res = pipe2(fds, flags); |
| 9730 | if (res != 0) |
| 9731 | return posix_error(); |
| 9732 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9733 | } |
| 9734 | #endif /* HAVE_PIPE2 */ |
| 9735 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9736 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9737 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9738 | /*[clinic input] |
| 9739 | os.writev -> Py_ssize_t |
| 9740 | fd: int |
| 9741 | buffers: object |
| 9742 | / |
| 9743 | |
| 9744 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9745 | |
| 9746 | Returns the total number of bytes written. |
| 9747 | buffers must be a sequence of bytes-like objects. |
| 9748 | [clinic start generated code]*/ |
| 9749 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9750 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9751 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9752 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9753 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9754 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9755 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9756 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9757 | struct iovec *iov; |
| 9758 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9759 | |
| 9760 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9761 | PyErr_SetString(PyExc_TypeError, |
| 9762 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9763 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9764 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9765 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9766 | if (cnt < 0) |
| 9767 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9768 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9769 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9770 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9771 | } |
| 9772 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9773 | do { |
| 9774 | Py_BEGIN_ALLOW_THREADS |
| 9775 | result = writev(fd, iov, cnt); |
| 9776 | Py_END_ALLOW_THREADS |
| 9777 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9778 | |
| 9779 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9780 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9781 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9782 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9783 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9784 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9785 | #endif /* HAVE_WRITEV */ |
| 9786 | |
| 9787 | |
| 9788 | #ifdef HAVE_PWRITE |
| 9789 | /*[clinic input] |
| 9790 | os.pwrite -> Py_ssize_t |
| 9791 | |
| 9792 | fd: int |
| 9793 | buffer: Py_buffer |
| 9794 | offset: Py_off_t |
| 9795 | / |
| 9796 | |
| 9797 | Write bytes to a file descriptor starting at a particular offset. |
| 9798 | |
| 9799 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9800 | the file. Returns the number of bytes writte. Does not change the |
| 9801 | current file offset. |
| 9802 | [clinic start generated code]*/ |
| 9803 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9804 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9805 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9806 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9807 | { |
| 9808 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9809 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9810 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9811 | do { |
| 9812 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9813 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9814 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9815 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9816 | Py_END_ALLOW_THREADS |
| 9817 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9818 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9819 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9820 | posix_error(); |
| 9821 | return size; |
| 9822 | } |
| 9823 | #endif /* HAVE_PWRITE */ |
| 9824 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9825 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9826 | /*[clinic input] |
| 9827 | os.pwritev -> Py_ssize_t |
| 9828 | |
| 9829 | fd: int |
| 9830 | buffers: object |
| 9831 | offset: Py_off_t |
| 9832 | flags: int = 0 |
| 9833 | / |
| 9834 | |
| 9835 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9836 | |
| 9837 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9838 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9839 | buffer is written before proceeding to second, and so on. The operating system may |
| 9840 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9841 | This function writes the contents of each object to the file descriptor and returns |
| 9842 | the total number of bytes written. |
| 9843 | |
| 9844 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9845 | |
| 9846 | - RWF_DSYNC |
| 9847 | - RWF_SYNC |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 9848 | - RWF_APPEND |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9849 | |
| 9850 | Using non-zero flags requires Linux 4.7 or newer. |
| 9851 | [clinic start generated code]*/ |
| 9852 | |
| 9853 | static Py_ssize_t |
| 9854 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9855 | int flags) |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 9856 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=35358c327e1a2a8e]*/ |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9857 | { |
| 9858 | Py_ssize_t cnt; |
| 9859 | Py_ssize_t result; |
| 9860 | int async_err = 0; |
| 9861 | struct iovec *iov; |
| 9862 | Py_buffer *buf; |
| 9863 | |
| 9864 | if (!PySequence_Check(buffers)) { |
| 9865 | PyErr_SetString(PyExc_TypeError, |
| 9866 | "pwritev() arg 2 must be a sequence"); |
| 9867 | return -1; |
| 9868 | } |
| 9869 | |
| 9870 | cnt = PySequence_Size(buffers); |
| 9871 | if (cnt < 0) { |
| 9872 | return -1; |
| 9873 | } |
| 9874 | |
| 9875 | #ifndef HAVE_PWRITEV2 |
| 9876 | if(flags != 0) { |
| 9877 | argument_unavailable_error("pwritev2", "flags"); |
| 9878 | return -1; |
| 9879 | } |
| 9880 | #endif |
| 9881 | |
| 9882 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9883 | return -1; |
| 9884 | } |
| 9885 | #ifdef HAVE_PWRITEV2 |
| 9886 | do { |
| 9887 | Py_BEGIN_ALLOW_THREADS |
| 9888 | _Py_BEGIN_SUPPRESS_IPH |
| 9889 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9890 | _Py_END_SUPPRESS_IPH |
| 9891 | Py_END_ALLOW_THREADS |
| 9892 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9893 | #else |
| 9894 | do { |
| 9895 | Py_BEGIN_ALLOW_THREADS |
| 9896 | _Py_BEGIN_SUPPRESS_IPH |
| 9897 | result = pwritev(fd, iov, cnt, offset); |
| 9898 | _Py_END_SUPPRESS_IPH |
| 9899 | Py_END_ALLOW_THREADS |
| 9900 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9901 | #endif |
| 9902 | |
| 9903 | iov_cleanup(iov, buf, cnt); |
| 9904 | if (result < 0) { |
| 9905 | if (!async_err) { |
| 9906 | posix_error(); |
| 9907 | } |
| 9908 | return -1; |
| 9909 | } |
| 9910 | |
| 9911 | return result; |
| 9912 | } |
| 9913 | #endif /* HAVE_PWRITEV */ |
| 9914 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9915 | #ifdef HAVE_COPY_FILE_RANGE |
| 9916 | /*[clinic input] |
| 9917 | |
| 9918 | os.copy_file_range |
| 9919 | src: int |
| 9920 | Source file descriptor. |
| 9921 | dst: int |
| 9922 | Destination file descriptor. |
| 9923 | count: Py_ssize_t |
| 9924 | Number of bytes to copy. |
| 9925 | offset_src: object = None |
| 9926 | Starting offset in src. |
| 9927 | offset_dst: object = None |
| 9928 | Starting offset in dst. |
| 9929 | |
| 9930 | Copy count bytes from one file descriptor to another. |
| 9931 | |
| 9932 | If offset_src is None, then src is read from the current position; |
| 9933 | respectively for offset_dst. |
| 9934 | [clinic start generated code]*/ |
| 9935 | |
| 9936 | static PyObject * |
| 9937 | os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, |
| 9938 | PyObject *offset_src, PyObject *offset_dst) |
| 9939 | /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ |
| 9940 | { |
| 9941 | off_t offset_src_val, offset_dst_val; |
| 9942 | off_t *p_offset_src = NULL; |
| 9943 | off_t *p_offset_dst = NULL; |
| 9944 | Py_ssize_t ret; |
| 9945 | int async_err = 0; |
| 9946 | /* The flags argument is provided to allow |
| 9947 | * for future extensions and currently must be to 0. */ |
| 9948 | int flags = 0; |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9949 | |
| 9950 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9951 | if (count < 0) { |
| 9952 | PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); |
| 9953 | return NULL; |
| 9954 | } |
| 9955 | |
| 9956 | if (offset_src != Py_None) { |
| 9957 | if (!Py_off_t_converter(offset_src, &offset_src_val)) { |
| 9958 | return NULL; |
| 9959 | } |
| 9960 | p_offset_src = &offset_src_val; |
| 9961 | } |
| 9962 | |
| 9963 | if (offset_dst != Py_None) { |
| 9964 | if (!Py_off_t_converter(offset_dst, &offset_dst_val)) { |
| 9965 | return NULL; |
| 9966 | } |
| 9967 | p_offset_dst = &offset_dst_val; |
| 9968 | } |
| 9969 | |
| 9970 | do { |
| 9971 | Py_BEGIN_ALLOW_THREADS |
| 9972 | ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags); |
| 9973 | Py_END_ALLOW_THREADS |
| 9974 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9975 | |
| 9976 | if (ret < 0) { |
| 9977 | return (!async_err) ? posix_error() : NULL; |
| 9978 | } |
| 9979 | |
| 9980 | return PyLong_FromSsize_t(ret); |
| 9981 | } |
| 9982 | #endif /* HAVE_COPY_FILE_RANGE*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9983 | |
| 9984 | #ifdef HAVE_MKFIFO |
| 9985 | /*[clinic input] |
| 9986 | os.mkfifo |
| 9987 | |
| 9988 | path: path_t |
| 9989 | mode: int=0o666 |
| 9990 | * |
| 9991 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9992 | |
| 9993 | Create a "fifo" (a POSIX named pipe). |
| 9994 | |
| 9995 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9996 | and path should be relative; path will then be relative to that directory. |
| 9997 | dir_fd may not be implemented on your platform. |
| 9998 | If it is unavailable, using it will raise a NotImplementedError. |
| 9999 | [clinic start generated code]*/ |
| 10000 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10001 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10002 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 10003 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10004 | { |
| 10005 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10006 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10007 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10008 | do { |
| 10009 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10010 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10011 | if (dir_fd != DEFAULT_DIR_FD) |
| 10012 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 10013 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10014 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10015 | result = mkfifo(path->narrow, mode); |
| 10016 | Py_END_ALLOW_THREADS |
| 10017 | } while (result != 0 && errno == EINTR && |
| 10018 | !(async_err = PyErr_CheckSignals())); |
| 10019 | if (result != 0) |
| 10020 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10021 | |
| 10022 | Py_RETURN_NONE; |
| 10023 | } |
| 10024 | #endif /* HAVE_MKFIFO */ |
| 10025 | |
| 10026 | |
| 10027 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 10028 | /*[clinic input] |
| 10029 | os.mknod |
| 10030 | |
| 10031 | path: path_t |
| 10032 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10033 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10034 | * |
| 10035 | dir_fd: dir_fd(requires='mknodat')=None |
| 10036 | |
| 10037 | Create a node in the file system. |
| 10038 | |
| 10039 | Create a node in the file system (file, device special file or named pipe) |
| 10040 | at path. mode specifies both the permissions to use and the |
| 10041 | type of node to be created, being combined (bitwise OR) with one of |
| 10042 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 10043 | device defines the newly created device special file (probably using |
| 10044 | os.makedev()). Otherwise device is ignored. |
| 10045 | |
| 10046 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 10047 | and path should be relative; path will then be relative to that directory. |
| 10048 | dir_fd may not be implemented on your platform. |
| 10049 | If it is unavailable, using it will raise a NotImplementedError. |
| 10050 | [clinic start generated code]*/ |
| 10051 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10052 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10053 | 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] | 10054 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10055 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10056 | { |
| 10057 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10058 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10059 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10060 | do { |
| 10061 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10062 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10063 | if (dir_fd != DEFAULT_DIR_FD) |
| 10064 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 10065 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10066 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10067 | result = mknod(path->narrow, mode, device); |
| 10068 | Py_END_ALLOW_THREADS |
| 10069 | } while (result != 0 && errno == EINTR && |
| 10070 | !(async_err = PyErr_CheckSignals())); |
| 10071 | if (result != 0) |
| 10072 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10073 | |
| 10074 | Py_RETURN_NONE; |
| 10075 | } |
| 10076 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 10077 | |
| 10078 | |
| 10079 | #ifdef HAVE_DEVICE_MACROS |
| 10080 | /*[clinic input] |
| 10081 | os.major -> unsigned_int |
| 10082 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10083 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10084 | / |
| 10085 | |
| 10086 | Extracts a device major number from a raw device number. |
| 10087 | [clinic start generated code]*/ |
| 10088 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10089 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10090 | os_major_impl(PyObject *module, dev_t device) |
| 10091 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10092 | { |
| 10093 | return major(device); |
| 10094 | } |
| 10095 | |
| 10096 | |
| 10097 | /*[clinic input] |
| 10098 | os.minor -> unsigned_int |
| 10099 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10100 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10101 | / |
| 10102 | |
| 10103 | Extracts a device minor number from a raw device number. |
| 10104 | [clinic start generated code]*/ |
| 10105 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10106 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10107 | os_minor_impl(PyObject *module, dev_t device) |
| 10108 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10109 | { |
| 10110 | return minor(device); |
| 10111 | } |
| 10112 | |
| 10113 | |
| 10114 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10115 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10116 | |
| 10117 | major: int |
| 10118 | minor: int |
| 10119 | / |
| 10120 | |
| 10121 | Composes a raw device number from the major and minor device numbers. |
| 10122 | [clinic start generated code]*/ |
| 10123 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10124 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10125 | os_makedev_impl(PyObject *module, int major, int minor) |
| 10126 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10127 | { |
| 10128 | return makedev(major, minor); |
| 10129 | } |
| 10130 | #endif /* HAVE_DEVICE_MACROS */ |
| 10131 | |
| 10132 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10133 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10134 | /*[clinic input] |
| 10135 | os.ftruncate |
| 10136 | |
| 10137 | fd: int |
| 10138 | length: Py_off_t |
| 10139 | / |
| 10140 | |
| 10141 | Truncate a file, specified by file descriptor, to a specific length. |
| 10142 | [clinic start generated code]*/ |
| 10143 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10144 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10145 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 10146 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10147 | { |
| 10148 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10149 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10150 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10151 | if (PySys_Audit("os.truncate", "in", fd, length) < 0) { |
| 10152 | return NULL; |
| 10153 | } |
| 10154 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10155 | do { |
| 10156 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10157 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10158 | #ifdef MS_WINDOWS |
| 10159 | result = _chsize_s(fd, length); |
| 10160 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10161 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10162 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10163 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10164 | Py_END_ALLOW_THREADS |
| 10165 | } while (result != 0 && errno == EINTR && |
| 10166 | !(async_err = PyErr_CheckSignals())); |
| 10167 | if (result != 0) |
| 10168 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10169 | Py_RETURN_NONE; |
| 10170 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10171 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10172 | |
| 10173 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10174 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10175 | /*[clinic input] |
| 10176 | os.truncate |
| 10177 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 10178 | length: Py_off_t |
| 10179 | |
| 10180 | Truncate a file, specified by path, to a specific length. |
| 10181 | |
| 10182 | On some platforms, path may also be specified as an open file descriptor. |
| 10183 | If this functionality is unavailable, using it raises an exception. |
| 10184 | [clinic start generated code]*/ |
| 10185 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10186 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10187 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 10188 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10189 | { |
| 10190 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10191 | #ifdef MS_WINDOWS |
| 10192 | int fd; |
| 10193 | #endif |
| 10194 | |
| 10195 | if (path->fd != -1) |
| 10196 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10197 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10198 | if (PySys_Audit("os.truncate", "On", path->object, length) < 0) { |
| 10199 | return NULL; |
| 10200 | } |
| 10201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10202 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10203 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10204 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10205 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 10206 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10207 | result = -1; |
| 10208 | else { |
| 10209 | result = _chsize_s(fd, length); |
| 10210 | close(fd); |
| 10211 | if (result < 0) |
| 10212 | errno = result; |
| 10213 | } |
| 10214 | #else |
| 10215 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10216 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10217 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10218 | Py_END_ALLOW_THREADS |
| 10219 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 10220 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10221 | |
| 10222 | Py_RETURN_NONE; |
| 10223 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10224 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10225 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10226 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10227 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 10228 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 10229 | defined, which is the case in Python on AIX. AIX bug report: |
| 10230 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 10231 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 10232 | # define POSIX_FADVISE_AIX_BUG |
| 10233 | #endif |
| 10234 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10235 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10236 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10237 | /*[clinic input] |
| 10238 | os.posix_fallocate |
| 10239 | |
| 10240 | fd: int |
| 10241 | offset: Py_off_t |
| 10242 | length: Py_off_t |
| 10243 | / |
| 10244 | |
| 10245 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 10246 | |
| 10247 | Ensure that the file specified by fd encompasses a range of bytes |
| 10248 | starting at offset bytes from the beginning and continuing for length bytes. |
| 10249 | [clinic start generated code]*/ |
| 10250 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10251 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10252 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10253 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10254 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10255 | { |
| 10256 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10257 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10258 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10259 | do { |
| 10260 | Py_BEGIN_ALLOW_THREADS |
| 10261 | result = posix_fallocate(fd, offset, length); |
| 10262 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10263 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10264 | |
| 10265 | if (result == 0) |
| 10266 | Py_RETURN_NONE; |
| 10267 | |
| 10268 | if (async_err) |
| 10269 | return NULL; |
| 10270 | |
| 10271 | errno = result; |
| 10272 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10273 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10274 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10275 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10276 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10277 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10278 | /*[clinic input] |
| 10279 | os.posix_fadvise |
| 10280 | |
| 10281 | fd: int |
| 10282 | offset: Py_off_t |
| 10283 | length: Py_off_t |
| 10284 | advice: int |
| 10285 | / |
| 10286 | |
| 10287 | Announce an intention to access data in a specific pattern. |
| 10288 | |
| 10289 | Announce an intention to access data in a specific pattern, thus allowing |
| 10290 | the kernel to make optimizations. |
| 10291 | The advice applies to the region of the file specified by fd starting at |
| 10292 | offset and continuing for length bytes. |
| 10293 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 10294 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 10295 | POSIX_FADV_DONTNEED. |
| 10296 | [clinic start generated code]*/ |
| 10297 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10298 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10299 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10300 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10301 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10302 | { |
| 10303 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10304 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10305 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10306 | do { |
| 10307 | Py_BEGIN_ALLOW_THREADS |
| 10308 | result = posix_fadvise(fd, offset, length, advice); |
| 10309 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10310 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10311 | |
| 10312 | if (result == 0) |
| 10313 | Py_RETURN_NONE; |
| 10314 | |
| 10315 | if (async_err) |
| 10316 | return NULL; |
| 10317 | |
| 10318 | errno = result; |
| 10319 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10320 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10321 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10322 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10323 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 10324 | #ifdef MS_WINDOWS |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10325 | static PyObject* |
| 10326 | win32_putenv(PyObject *name, PyObject *value) |
| 10327 | { |
| 10328 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 10329 | defining hidden environment variables. */ |
| 10330 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 10331 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 10332 | { |
| 10333 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10334 | return NULL; |
| 10335 | } |
| 10336 | PyObject *unicode; |
| 10337 | if (value != NULL) { |
| 10338 | unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 10339 | } |
| 10340 | else { |
| 10341 | unicode = PyUnicode_FromFormat("%U=", name); |
| 10342 | } |
| 10343 | if (unicode == NULL) { |
| 10344 | return NULL; |
| 10345 | } |
| 10346 | |
| 10347 | Py_ssize_t size; |
| 10348 | /* PyUnicode_AsWideCharString() rejects embedded null characters */ |
| 10349 | wchar_t *env = PyUnicode_AsWideCharString(unicode, &size); |
| 10350 | Py_DECREF(unicode); |
| 10351 | |
| 10352 | if (env == NULL) { |
| 10353 | return NULL; |
| 10354 | } |
| 10355 | if (size > _MAX_ENV) { |
| 10356 | PyErr_Format(PyExc_ValueError, |
| 10357 | "the environment variable is longer than %u characters", |
| 10358 | _MAX_ENV); |
| 10359 | PyMem_Free(env); |
| 10360 | return NULL; |
| 10361 | } |
| 10362 | |
| 10363 | /* _wputenv() and SetEnvironmentVariableW() update the environment in the |
| 10364 | Process Environment Block (PEB). _wputenv() also updates CRT 'environ' |
| 10365 | and '_wenviron' variables, whereas SetEnvironmentVariableW() does not. |
| 10366 | |
| 10367 | Prefer _wputenv() to be compatible with C libraries using CRT |
| 10368 | variables and CRT functions using these variables (ex: getenv()). */ |
| 10369 | int err = _wputenv(env); |
| 10370 | PyMem_Free(env); |
| 10371 | |
| 10372 | if (err) { |
| 10373 | posix_error(); |
| 10374 | return NULL; |
| 10375 | } |
| 10376 | |
| 10377 | Py_RETURN_NONE; |
| 10378 | } |
| 10379 | #endif |
| 10380 | |
| 10381 | |
| 10382 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10383 | /*[clinic input] |
| 10384 | os.putenv |
| 10385 | |
| 10386 | name: unicode |
| 10387 | value: unicode |
| 10388 | / |
| 10389 | |
| 10390 | Change or add an environment variable. |
| 10391 | [clinic start generated code]*/ |
| 10392 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10393 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10394 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10395 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10396 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10397 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10398 | return NULL; |
| 10399 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10400 | return win32_putenv(name, value); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10401 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10402 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10403 | /*[clinic input] |
| 10404 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10405 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10406 | name: FSConverter |
| 10407 | value: FSConverter |
| 10408 | / |
| 10409 | |
| 10410 | Change or add an environment variable. |
| 10411 | [clinic start generated code]*/ |
| 10412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10413 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10414 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10415 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10416 | { |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10417 | const char *name_string = PyBytes_AS_STRING(name); |
| 10418 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10419 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10420 | if (strchr(name_string, '=') != NULL) { |
| 10421 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10422 | return NULL; |
| 10423 | } |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10424 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10425 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10426 | return NULL; |
| 10427 | } |
| 10428 | |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10429 | if (setenv(name_string, value_string, 1)) { |
| 10430 | return posix_error(); |
| 10431 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10432 | Py_RETURN_NONE; |
| 10433 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10434 | #endif /* !defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10435 | |
| 10436 | |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10437 | #ifdef MS_WINDOWS |
| 10438 | /*[clinic input] |
| 10439 | os.unsetenv |
| 10440 | name: unicode |
| 10441 | / |
| 10442 | |
| 10443 | Delete an environment variable. |
| 10444 | [clinic start generated code]*/ |
| 10445 | |
| 10446 | static PyObject * |
| 10447 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10448 | /*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/ |
| 10449 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10450 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10451 | return NULL; |
| 10452 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10453 | return win32_putenv(name, NULL); |
| 10454 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10455 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10456 | /*[clinic input] |
| 10457 | os.unsetenv |
| 10458 | name: FSConverter |
| 10459 | / |
| 10460 | |
| 10461 | Delete an environment variable. |
| 10462 | [clinic start generated code]*/ |
| 10463 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10464 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10465 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10466 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10467 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10468 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10469 | return NULL; |
| 10470 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10471 | #ifdef HAVE_BROKEN_UNSETENV |
| 10472 | unsetenv(PyBytes_AS_STRING(name)); |
| 10473 | #else |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10474 | int err = unsetenv(PyBytes_AS_STRING(name)); |
| 10475 | if (err) { |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10476 | return posix_error(); |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10477 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10478 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10479 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10480 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10481 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10482 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10483 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10484 | |
| 10485 | /*[clinic input] |
| 10486 | os.strerror |
| 10487 | |
| 10488 | code: int |
| 10489 | / |
| 10490 | |
| 10491 | Translate an error code to a message string. |
| 10492 | [clinic start generated code]*/ |
| 10493 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10494 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10495 | os_strerror_impl(PyObject *module, int code) |
| 10496 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10497 | { |
| 10498 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10499 | if (message == NULL) { |
| 10500 | PyErr_SetString(PyExc_ValueError, |
| 10501 | "strerror() argument out of range"); |
| 10502 | return NULL; |
| 10503 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 10504 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10505 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10506 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10507 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10508 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10509 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10510 | /*[clinic input] |
| 10511 | os.WCOREDUMP -> bool |
| 10512 | |
| 10513 | status: int |
| 10514 | / |
| 10515 | |
| 10516 | Return True if the process returning status was dumped to a core file. |
| 10517 | [clinic start generated code]*/ |
| 10518 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10519 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10520 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 10521 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10522 | { |
| 10523 | WAIT_TYPE wait_status; |
| 10524 | WAIT_STATUS_INT(wait_status) = status; |
| 10525 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10526 | } |
| 10527 | #endif /* WCOREDUMP */ |
| 10528 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10529 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10530 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10531 | /*[clinic input] |
| 10532 | os.WIFCONTINUED -> bool |
| 10533 | |
| 10534 | status: int |
| 10535 | |
| 10536 | Return True if a particular process was continued from a job control stop. |
| 10537 | |
| 10538 | Return True if the process returning status was continued from a |
| 10539 | job control stop. |
| 10540 | [clinic start generated code]*/ |
| 10541 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10542 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10543 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 10544 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10545 | { |
| 10546 | WAIT_TYPE wait_status; |
| 10547 | WAIT_STATUS_INT(wait_status) = status; |
| 10548 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10549 | } |
| 10550 | #endif /* WIFCONTINUED */ |
| 10551 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10552 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10553 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10554 | /*[clinic input] |
| 10555 | os.WIFSTOPPED -> bool |
| 10556 | |
| 10557 | status: int |
| 10558 | |
| 10559 | Return True if the process returning status was stopped. |
| 10560 | [clinic start generated code]*/ |
| 10561 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10562 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10563 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 10564 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10565 | { |
| 10566 | WAIT_TYPE wait_status; |
| 10567 | WAIT_STATUS_INT(wait_status) = status; |
| 10568 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10569 | } |
| 10570 | #endif /* WIFSTOPPED */ |
| 10571 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10572 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10573 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10574 | /*[clinic input] |
| 10575 | os.WIFSIGNALED -> bool |
| 10576 | |
| 10577 | status: int |
| 10578 | |
| 10579 | Return True if the process returning status was terminated by a signal. |
| 10580 | [clinic start generated code]*/ |
| 10581 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10582 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10583 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 10584 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10585 | { |
| 10586 | WAIT_TYPE wait_status; |
| 10587 | WAIT_STATUS_INT(wait_status) = status; |
| 10588 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10589 | } |
| 10590 | #endif /* WIFSIGNALED */ |
| 10591 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10592 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10593 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10594 | /*[clinic input] |
| 10595 | os.WIFEXITED -> bool |
| 10596 | |
| 10597 | status: int |
| 10598 | |
| 10599 | Return True if the process returning status exited via the exit() system call. |
| 10600 | [clinic start generated code]*/ |
| 10601 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10602 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10603 | os_WIFEXITED_impl(PyObject *module, int status) |
| 10604 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10605 | { |
| 10606 | WAIT_TYPE wait_status; |
| 10607 | WAIT_STATUS_INT(wait_status) = status; |
| 10608 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10609 | } |
| 10610 | #endif /* WIFEXITED */ |
| 10611 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10612 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10613 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10614 | /*[clinic input] |
| 10615 | os.WEXITSTATUS -> int |
| 10616 | |
| 10617 | status: int |
| 10618 | |
| 10619 | Return the process return code from status. |
| 10620 | [clinic start generated code]*/ |
| 10621 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10622 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10623 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 10624 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10625 | { |
| 10626 | WAIT_TYPE wait_status; |
| 10627 | WAIT_STATUS_INT(wait_status) = status; |
| 10628 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10629 | } |
| 10630 | #endif /* WEXITSTATUS */ |
| 10631 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10632 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10633 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10634 | /*[clinic input] |
| 10635 | os.WTERMSIG -> int |
| 10636 | |
| 10637 | status: int |
| 10638 | |
| 10639 | Return the signal that terminated the process that provided the status value. |
| 10640 | [clinic start generated code]*/ |
| 10641 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10642 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10643 | os_WTERMSIG_impl(PyObject *module, int status) |
| 10644 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10645 | { |
| 10646 | WAIT_TYPE wait_status; |
| 10647 | WAIT_STATUS_INT(wait_status) = status; |
| 10648 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10649 | } |
| 10650 | #endif /* WTERMSIG */ |
| 10651 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10652 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10653 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10654 | /*[clinic input] |
| 10655 | os.WSTOPSIG -> int |
| 10656 | |
| 10657 | status: int |
| 10658 | |
| 10659 | Return the signal that stopped the process that provided the status value. |
| 10660 | [clinic start generated code]*/ |
| 10661 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10662 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10663 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 10664 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10665 | { |
| 10666 | WAIT_TYPE wait_status; |
| 10667 | WAIT_STATUS_INT(wait_status) = status; |
| 10668 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10669 | } |
| 10670 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10671 | #endif /* HAVE_SYS_WAIT_H */ |
| 10672 | |
| 10673 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10674 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10675 | #ifdef _SCO_DS |
| 10676 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 10677 | needed definitions in sys/statvfs.h */ |
| 10678 | #define _SVID3 |
| 10679 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10680 | #include <sys/statvfs.h> |
| 10681 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10682 | static PyObject* |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10683 | _pystatvfs_fromstructstatvfs(PyObject *module, struct statvfs st) { |
| 10684 | PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 10685 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10686 | if (v == NULL) |
| 10687 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10688 | |
| 10689 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10690 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10691 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10692 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 10693 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 10694 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 10695 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 10696 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 10697 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 10698 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10699 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10700 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10701 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10702 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10703 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10704 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10705 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10706 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10707 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10708 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10709 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10710 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10711 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10712 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10713 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10714 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10715 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10716 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10717 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10718 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 10719 | * (issue #32390). */ |
| 10720 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 10721 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 10722 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10723 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10724 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10725 | if (PyErr_Occurred()) { |
| 10726 | Py_DECREF(v); |
| 10727 | return NULL; |
| 10728 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10729 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10730 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10731 | } |
| 10732 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10733 | |
| 10734 | /*[clinic input] |
| 10735 | os.fstatvfs |
| 10736 | fd: int |
| 10737 | / |
| 10738 | |
| 10739 | Perform an fstatvfs system call on the given fd. |
| 10740 | |
| 10741 | Equivalent to statvfs(fd). |
| 10742 | [clinic start generated code]*/ |
| 10743 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10744 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10745 | os_fstatvfs_impl(PyObject *module, int fd) |
| 10746 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10747 | { |
| 10748 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10749 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10750 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10751 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10752 | do { |
| 10753 | Py_BEGIN_ALLOW_THREADS |
| 10754 | result = fstatvfs(fd, &st); |
| 10755 | Py_END_ALLOW_THREADS |
| 10756 | } while (result != 0 && errno == EINTR && |
| 10757 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10758 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10759 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10760 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10761 | return _pystatvfs_fromstructstatvfs(module, st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10762 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10763 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10764 | |
| 10765 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10766 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10767 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10768 | /*[clinic input] |
| 10769 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10770 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10771 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10772 | |
| 10773 | Perform a statvfs system call on the given path. |
| 10774 | |
| 10775 | path may always be specified as a string. |
| 10776 | On some platforms, path may also be specified as an open file descriptor. |
| 10777 | If this functionality is unavailable, using it raises an exception. |
| 10778 | [clinic start generated code]*/ |
| 10779 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10780 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10781 | os_statvfs_impl(PyObject *module, path_t *path) |
| 10782 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10783 | { |
| 10784 | int result; |
| 10785 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10786 | |
| 10787 | Py_BEGIN_ALLOW_THREADS |
| 10788 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10789 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10790 | #ifdef __APPLE__ |
| 10791 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10792 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10793 | fd_specified("statvfs", path->fd); |
| 10794 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10795 | } |
| 10796 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10797 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10798 | } |
| 10799 | else |
| 10800 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10801 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10802 | Py_END_ALLOW_THREADS |
| 10803 | |
| 10804 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10805 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10806 | } |
| 10807 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10808 | return _pystatvfs_fromstructstatvfs(module, st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10809 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10810 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10811 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10812 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10813 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10814 | /*[clinic input] |
| 10815 | os._getdiskusage |
| 10816 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10817 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10818 | |
| 10819 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10820 | [clinic start generated code]*/ |
| 10821 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10822 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10823 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10824 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10825 | { |
| 10826 | BOOL retval; |
| 10827 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10828 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10829 | |
| 10830 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10831 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10832 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10833 | if (retval == 0) { |
| 10834 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10835 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10836 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10837 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10838 | if (dir_path == NULL) { |
| 10839 | return PyErr_NoMemory(); |
| 10840 | } |
| 10841 | |
| 10842 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10843 | |
| 10844 | if (_dirnameW(dir_path) != -1) { |
| 10845 | Py_BEGIN_ALLOW_THREADS |
| 10846 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10847 | Py_END_ALLOW_THREADS |
| 10848 | } |
| 10849 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10850 | err = GetLastError(); |
| 10851 | PyMem_Free(dir_path); |
| 10852 | if (retval) { |
| 10853 | goto success; |
| 10854 | } |
| 10855 | } |
| 10856 | return PyErr_SetFromWindowsErr(err); |
| 10857 | } |
| 10858 | |
| 10859 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10860 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10861 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10862 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10863 | |
| 10864 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10865 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10866 | * It maps strings representing configuration variable names to |
| 10867 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10868 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10869 | * rarely-used constants. There are three separate tables that use |
| 10870 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10871 | * |
| 10872 | * This code is always included, even if none of the interfaces that |
| 10873 | * need it are included. The #if hackery needed to avoid it would be |
| 10874 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10875 | */ |
| 10876 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10877 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10878 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10879 | }; |
| 10880 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10881 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10882 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10883 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10884 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10885 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10886 | int value = _PyLong_AsInt(arg); |
| 10887 | if (value == -1 && PyErr_Occurred()) |
| 10888 | return 0; |
| 10889 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10890 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10891 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10892 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10893 | /* look up the value in the table using a binary search */ |
| 10894 | size_t lo = 0; |
| 10895 | size_t mid; |
| 10896 | size_t hi = tablesize; |
| 10897 | int cmp; |
| 10898 | const char *confname; |
| 10899 | if (!PyUnicode_Check(arg)) { |
| 10900 | PyErr_SetString(PyExc_TypeError, |
| 10901 | "configuration names must be strings or integers"); |
| 10902 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10903 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10904 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10905 | if (confname == NULL) |
| 10906 | return 0; |
| 10907 | while (lo < hi) { |
| 10908 | mid = (lo + hi) / 2; |
| 10909 | cmp = strcmp(confname, table[mid].name); |
| 10910 | if (cmp < 0) |
| 10911 | hi = mid; |
| 10912 | else if (cmp > 0) |
| 10913 | lo = mid + 1; |
| 10914 | else { |
| 10915 | *valuep = table[mid].value; |
| 10916 | return 1; |
| 10917 | } |
| 10918 | } |
| 10919 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10920 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10921 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10922 | } |
| 10923 | |
| 10924 | |
| 10925 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10926 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10927 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10928 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10929 | #endif |
| 10930 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10931 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10932 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10933 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10934 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10935 | #endif |
| 10936 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10937 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10938 | #endif |
| 10939 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10940 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10941 | #endif |
| 10942 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10943 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10944 | #endif |
| 10945 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10946 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10947 | #endif |
| 10948 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10949 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10950 | #endif |
| 10951 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10952 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10953 | #endif |
| 10954 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10955 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10956 | #endif |
| 10957 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10958 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10959 | #endif |
| 10960 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10961 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10962 | #endif |
| 10963 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10964 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10965 | #endif |
| 10966 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10967 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10968 | #endif |
| 10969 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10970 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10971 | #endif |
| 10972 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10973 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10974 | #endif |
| 10975 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10976 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10977 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10978 | #ifdef _PC_ACL_ENABLED |
| 10979 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10980 | #endif |
| 10981 | #ifdef _PC_MIN_HOLE_SIZE |
| 10982 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10983 | #endif |
| 10984 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10985 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10986 | #endif |
| 10987 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10988 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10989 | #endif |
| 10990 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10991 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10992 | #endif |
| 10993 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10994 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10995 | #endif |
| 10996 | #ifdef _PC_REC_XFER_ALIGN |
| 10997 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10998 | #endif |
| 10999 | #ifdef _PC_SYMLINK_MAX |
| 11000 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 11001 | #endif |
| 11002 | #ifdef _PC_XATTR_ENABLED |
| 11003 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 11004 | #endif |
| 11005 | #ifdef _PC_XATTR_EXISTS |
| 11006 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 11007 | #endif |
| 11008 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 11009 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 11010 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11011 | }; |
| 11012 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11013 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11014 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11015 | { |
| 11016 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 11017 | sizeof(posix_constants_pathconf) |
| 11018 | / sizeof(struct constdef)); |
| 11019 | } |
| 11020 | #endif |
| 11021 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11022 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11023 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11024 | /*[clinic input] |
| 11025 | os.fpathconf -> long |
| 11026 | |
Gregory P. Smith | 3ccb96c | 2020-06-20 15:06:48 -0700 | [diff] [blame] | 11027 | fd: fildes |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11028 | name: path_confname |
| 11029 | / |
| 11030 | |
| 11031 | Return the configuration limit name for the file descriptor fd. |
| 11032 | |
| 11033 | If there is no limit, return -1. |
| 11034 | [clinic start generated code]*/ |
| 11035 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11036 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11037 | os_fpathconf_impl(PyObject *module, int fd, int name) |
Gregory P. Smith | 3ccb96c | 2020-06-20 15:06:48 -0700 | [diff] [blame] | 11038 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5b8d2471cfaae186]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11039 | { |
| 11040 | long limit; |
| 11041 | |
| 11042 | errno = 0; |
| 11043 | limit = fpathconf(fd, name); |
| 11044 | if (limit == -1 && errno != 0) |
| 11045 | posix_error(); |
| 11046 | |
| 11047 | return limit; |
| 11048 | } |
| 11049 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11050 | |
| 11051 | |
| 11052 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11053 | /*[clinic input] |
| 11054 | os.pathconf -> long |
| 11055 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 11056 | name: path_confname |
| 11057 | |
| 11058 | Return the configuration limit name for the file or directory path. |
| 11059 | |
| 11060 | If there is no limit, return -1. |
| 11061 | On some platforms, path may also be specified as an open file descriptor. |
| 11062 | If this functionality is unavailable, using it raises an exception. |
| 11063 | [clinic start generated code]*/ |
| 11064 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11065 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11066 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 11067 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11068 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11069 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11070 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11071 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11072 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11073 | if (path->fd != -1) |
| 11074 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11075 | else |
| 11076 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11077 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11078 | if (limit == -1 && errno != 0) { |
| 11079 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 11080 | /* could be a path or name problem */ |
| 11081 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11082 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11083 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11084 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11085 | |
| 11086 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11087 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11088 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11089 | |
| 11090 | #ifdef HAVE_CONFSTR |
| 11091 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11092 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11093 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11094 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11095 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11096 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11097 | #endif |
| 11098 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11099 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11100 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11101 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11102 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11103 | #endif |
| 11104 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11105 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11106 | #endif |
| 11107 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11108 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11109 | #endif |
| 11110 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11111 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11112 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11113 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11114 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11115 | #endif |
| 11116 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11117 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11118 | #endif |
| 11119 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11120 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11121 | #endif |
| 11122 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11123 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11124 | #endif |
| 11125 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11126 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11127 | #endif |
| 11128 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11129 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11130 | #endif |
| 11131 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11132 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11133 | #endif |
| 11134 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11135 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11136 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11137 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11138 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11139 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11140 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11141 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11142 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11143 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11144 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11145 | #endif |
| 11146 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11147 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11148 | #endif |
| 11149 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11150 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11151 | #endif |
| 11152 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11153 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11154 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11155 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11156 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11157 | #endif |
| 11158 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11159 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11160 | #endif |
| 11161 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11162 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11163 | #endif |
| 11164 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11165 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11166 | #endif |
| 11167 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11168 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11169 | #endif |
| 11170 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11171 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11172 | #endif |
| 11173 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11174 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11175 | #endif |
| 11176 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11177 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11178 | #endif |
| 11179 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11180 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11181 | #endif |
| 11182 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11183 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11184 | #endif |
| 11185 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11186 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11187 | #endif |
| 11188 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11189 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11190 | #endif |
| 11191 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11192 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11193 | #endif |
| 11194 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11195 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11196 | #endif |
| 11197 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11198 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11199 | #endif |
| 11200 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11201 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11202 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11203 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11204 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11205 | #endif |
| 11206 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11207 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11208 | #endif |
| 11209 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11210 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11211 | #endif |
| 11212 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11213 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11214 | #endif |
| 11215 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11216 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11217 | #endif |
| 11218 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11219 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11220 | #endif |
| 11221 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11222 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11223 | #endif |
| 11224 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11225 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11226 | #endif |
| 11227 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11228 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11229 | #endif |
| 11230 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11231 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11232 | #endif |
| 11233 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11234 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11235 | #endif |
| 11236 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11237 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11238 | #endif |
| 11239 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11240 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11241 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11242 | }; |
| 11243 | |
| 11244 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11245 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11246 | { |
| 11247 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 11248 | sizeof(posix_constants_confstr) |
| 11249 | / sizeof(struct constdef)); |
| 11250 | } |
| 11251 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11252 | |
| 11253 | /*[clinic input] |
| 11254 | os.confstr |
| 11255 | |
| 11256 | name: confstr_confname |
| 11257 | / |
| 11258 | |
| 11259 | Return a string-valued system configuration variable. |
| 11260 | [clinic start generated code]*/ |
| 11261 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11262 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11263 | os_confstr_impl(PyObject *module, int name) |
| 11264 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11265 | { |
| 11266 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11267 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11268 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11269 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11270 | errno = 0; |
| 11271 | len = confstr(name, buffer, sizeof(buffer)); |
| 11272 | if (len == 0) { |
| 11273 | if (errno) { |
| 11274 | posix_error(); |
| 11275 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11276 | } |
| 11277 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11278 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11279 | } |
| 11280 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11281 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11282 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11283 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11284 | char *buf = PyMem_Malloc(len); |
| 11285 | if (buf == NULL) |
| 11286 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11287 | len2 = confstr(name, buf, len); |
| 11288 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 11289 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11290 | PyMem_Free(buf); |
| 11291 | } |
| 11292 | else |
| 11293 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11294 | return result; |
| 11295 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11296 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11297 | |
| 11298 | |
| 11299 | #ifdef HAVE_SYSCONF |
| 11300 | static struct constdef posix_constants_sysconf[] = { |
| 11301 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11302 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11303 | #endif |
| 11304 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11305 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11306 | #endif |
| 11307 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11308 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11309 | #endif |
| 11310 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11311 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11312 | #endif |
| 11313 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11314 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11315 | #endif |
| 11316 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11317 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11318 | #endif |
| 11319 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11320 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11321 | #endif |
| 11322 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11323 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11324 | #endif |
| 11325 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11326 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11327 | #endif |
| 11328 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11329 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11330 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11331 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11332 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11333 | #endif |
| 11334 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11335 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11336 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11337 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11338 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11339 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11340 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11341 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11342 | #endif |
| 11343 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11344 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11345 | #endif |
| 11346 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11347 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11348 | #endif |
| 11349 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11350 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11351 | #endif |
| 11352 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11353 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11354 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11355 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11356 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11357 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11358 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11359 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11360 | #endif |
| 11361 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11362 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11363 | #endif |
| 11364 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11365 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11366 | #endif |
| 11367 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11368 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11369 | #endif |
| 11370 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11371 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11372 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11373 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11374 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11375 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11376 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11377 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11378 | #endif |
| 11379 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11380 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11381 | #endif |
| 11382 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11383 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11384 | #endif |
| 11385 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11386 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11387 | #endif |
| 11388 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11389 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11390 | #endif |
| 11391 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11392 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11393 | #endif |
| 11394 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11395 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11396 | #endif |
| 11397 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11398 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11399 | #endif |
| 11400 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11401 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11402 | #endif |
| 11403 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11404 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11405 | #endif |
| 11406 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11407 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11408 | #endif |
| 11409 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11410 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11411 | #endif |
| 11412 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11413 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11414 | #endif |
| 11415 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11416 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11417 | #endif |
| 11418 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11419 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11420 | #endif |
| 11421 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11422 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11423 | #endif |
| 11424 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11425 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11426 | #endif |
| 11427 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11428 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11429 | #endif |
| 11430 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11431 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11432 | #endif |
| 11433 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11434 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11435 | #endif |
| 11436 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11437 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11438 | #endif |
| 11439 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11440 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11441 | #endif |
| 11442 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11443 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11444 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11445 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11446 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11447 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11448 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11449 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11450 | #endif |
| 11451 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11452 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11453 | #endif |
| 11454 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11455 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11456 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11457 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11458 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11459 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11460 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11461 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11462 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11463 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11464 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11465 | #endif |
| 11466 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11467 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11468 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11469 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11470 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11471 | #endif |
| 11472 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11473 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11474 | #endif |
| 11475 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11476 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11477 | #endif |
| 11478 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11479 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11480 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11481 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11482 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11483 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11484 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11485 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11486 | #endif |
| 11487 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11488 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11489 | #endif |
| 11490 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11491 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11492 | #endif |
| 11493 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11494 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11495 | #endif |
| 11496 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11497 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11498 | #endif |
| 11499 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11500 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11501 | #endif |
| 11502 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11503 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11504 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11505 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11506 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11507 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11508 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11509 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11510 | #endif |
| 11511 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11512 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11513 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11514 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11515 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11516 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11517 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11518 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11519 | #endif |
| 11520 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11521 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11522 | #endif |
| 11523 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11524 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11525 | #endif |
| 11526 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11527 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11528 | #endif |
| 11529 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11530 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11531 | #endif |
| 11532 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11533 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11534 | #endif |
| 11535 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11536 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11537 | #endif |
| 11538 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11539 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11540 | #endif |
| 11541 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11542 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11543 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11544 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11545 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11546 | #endif |
| 11547 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11548 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11549 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11550 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11551 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11552 | #endif |
| 11553 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11554 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11555 | #endif |
| 11556 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11557 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11558 | #endif |
| 11559 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11560 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11561 | #endif |
Batuhan Taşkaya | 909f4a3 | 2020-04-05 03:40:49 +0300 | [diff] [blame] | 11562 | #ifdef _SC_AIX_REALMEM |
| 11563 | {"SC_AIX_REALMEM", _SC_AIX_REALMEM}, |
| 11564 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11565 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11566 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11567 | #endif |
| 11568 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11569 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11570 | #endif |
| 11571 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11572 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11573 | #endif |
| 11574 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11575 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11576 | #endif |
| 11577 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11578 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11579 | #endif |
| 11580 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11581 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11582 | #endif |
| 11583 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11584 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11585 | #endif |
| 11586 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11587 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11588 | #endif |
| 11589 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11590 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11591 | #endif |
| 11592 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11593 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11594 | #endif |
| 11595 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11596 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11597 | #endif |
| 11598 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11599 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11600 | #endif |
| 11601 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11602 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11603 | #endif |
| 11604 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11605 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11606 | #endif |
| 11607 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11608 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11609 | #endif |
| 11610 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11611 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11612 | #endif |
| 11613 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11614 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11615 | #endif |
| 11616 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11617 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11618 | #endif |
| 11619 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11620 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11621 | #endif |
| 11622 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11623 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11624 | #endif |
| 11625 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11626 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11627 | #endif |
| 11628 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11629 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11630 | #endif |
| 11631 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11632 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11633 | #endif |
| 11634 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11635 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11636 | #endif |
| 11637 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11638 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11639 | #endif |
| 11640 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11641 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11642 | #endif |
| 11643 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11644 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11645 | #endif |
| 11646 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11647 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11648 | #endif |
| 11649 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11650 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11651 | #endif |
| 11652 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11653 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11654 | #endif |
| 11655 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11656 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11657 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11658 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11659 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11660 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11661 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11662 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11663 | #endif |
| 11664 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11665 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11666 | #endif |
| 11667 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11668 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11669 | #endif |
| 11670 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11671 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11672 | #endif |
| 11673 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11674 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11675 | #endif |
| 11676 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11677 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11678 | #endif |
| 11679 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11680 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11681 | #endif |
| 11682 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11683 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11684 | #endif |
| 11685 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11686 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11687 | #endif |
| 11688 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11689 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11690 | #endif |
| 11691 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11692 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11693 | #endif |
| 11694 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11695 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11696 | #endif |
| 11697 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11698 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11699 | #endif |
| 11700 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11701 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11702 | #endif |
| 11703 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11704 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11705 | #endif |
| 11706 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11707 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11708 | #endif |
| 11709 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11710 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11711 | #endif |
| 11712 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11713 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11714 | #endif |
| 11715 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11716 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11717 | #endif |
| 11718 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11719 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11720 | #endif |
| 11721 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11722 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11723 | #endif |
| 11724 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11725 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11726 | #endif |
| 11727 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11728 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11729 | #endif |
| 11730 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11731 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11732 | #endif |
| 11733 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11734 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11735 | #endif |
| 11736 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11737 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11738 | #endif |
| 11739 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11740 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11741 | #endif |
| 11742 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11743 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11744 | #endif |
| 11745 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11746 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11747 | #endif |
| 11748 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11749 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11750 | #endif |
| 11751 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11752 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11753 | #endif |
| 11754 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11755 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11756 | #endif |
| 11757 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11758 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11759 | #endif |
| 11760 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11761 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11762 | #endif |
| 11763 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11764 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11765 | #endif |
| 11766 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11767 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11768 | #endif |
| 11769 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11770 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11771 | #endif |
| 11772 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11773 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11774 | #endif |
| 11775 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11776 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11777 | #endif |
| 11778 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11779 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11780 | #endif |
| 11781 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11782 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11783 | #endif |
| 11784 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11785 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11786 | #endif |
| 11787 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11788 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11789 | #endif |
| 11790 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11791 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11792 | #endif |
| 11793 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11794 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11795 | #endif |
| 11796 | }; |
| 11797 | |
| 11798 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11799 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11800 | { |
| 11801 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11802 | sizeof(posix_constants_sysconf) |
| 11803 | / sizeof(struct constdef)); |
| 11804 | } |
| 11805 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11806 | |
| 11807 | /*[clinic input] |
| 11808 | os.sysconf -> long |
| 11809 | name: sysconf_confname |
| 11810 | / |
| 11811 | |
| 11812 | Return an integer-valued system configuration variable. |
| 11813 | [clinic start generated code]*/ |
| 11814 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11815 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11816 | os_sysconf_impl(PyObject *module, int name) |
| 11817 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11818 | { |
| 11819 | long value; |
| 11820 | |
| 11821 | errno = 0; |
| 11822 | value = sysconf(name); |
| 11823 | if (value == -1 && errno != 0) |
| 11824 | posix_error(); |
| 11825 | return value; |
| 11826 | } |
| 11827 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11828 | |
| 11829 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11830 | /* 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] | 11831 | * 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] | 11832 | * the exported dictionaries that are used to publish information about the |
| 11833 | * names available on the host platform. |
| 11834 | * |
| 11835 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11836 | * when used, even for platforms we're not able to test on. It also makes |
| 11837 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11838 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11839 | |
| 11840 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11841 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11842 | { |
| 11843 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11844 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11845 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11846 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11847 | |
| 11848 | return strcmp(c1->name, c2->name); |
| 11849 | } |
| 11850 | |
| 11851 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11852 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11853 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11854 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11855 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11856 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11857 | |
| 11858 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11859 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11860 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11861 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11862 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11863 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11864 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11865 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11866 | Py_XDECREF(o); |
| 11867 | Py_DECREF(d); |
| 11868 | return -1; |
| 11869 | } |
| 11870 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11871 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11872 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11873 | } |
| 11874 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11875 | /* Return -1 on failure, 0 on success. */ |
| 11876 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11877 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11878 | { |
| 11879 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11880 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11881 | sizeof(posix_constants_pathconf) |
| 11882 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11883 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11884 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11885 | #endif |
| 11886 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11887 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11888 | sizeof(posix_constants_confstr) |
| 11889 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11890 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11891 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11892 | #endif |
| 11893 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11894 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11895 | sizeof(posix_constants_sysconf) |
| 11896 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11897 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11898 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11899 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11900 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11901 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11902 | |
| 11903 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11904 | /*[clinic input] |
| 11905 | os.abort |
| 11906 | |
| 11907 | Abort the interpreter immediately. |
| 11908 | |
| 11909 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11910 | on the hosting operating system. This function never returns. |
| 11911 | [clinic start generated code]*/ |
| 11912 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11913 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11914 | os_abort_impl(PyObject *module) |
| 11915 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11916 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11917 | abort(); |
| 11918 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11919 | #ifndef __clang__ |
| 11920 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11921 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11922 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11923 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11924 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11925 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11926 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11927 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11928 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11929 | /* Grab ShellExecute dynamically from shell32 */ |
| 11930 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11931 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11932 | LPCWSTR, INT); |
| 11933 | static int |
| 11934 | check_ShellExecute() |
| 11935 | { |
| 11936 | HINSTANCE hShell32; |
| 11937 | |
| 11938 | /* only recheck */ |
| 11939 | if (-1 == has_ShellExecute) { |
| 11940 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11941 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11942 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11943 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11944 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11945 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11946 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11947 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11948 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11949 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11950 | } else { |
| 11951 | has_ShellExecute = 0; |
| 11952 | } |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 11953 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11954 | } |
| 11955 | return has_ShellExecute; |
| 11956 | } |
| 11957 | |
| 11958 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11959 | /*[clinic input] |
| 11960 | os.startfile |
| 11961 | filepath: path_t |
| 11962 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11963 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11964 | Start a file with its associated application. |
| 11965 | |
| 11966 | When "operation" is not specified or "open", this acts like |
| 11967 | double-clicking the file in Explorer, or giving the file name as an |
| 11968 | argument to the DOS "start" command: the file is opened with whatever |
| 11969 | application (if any) its extension is associated. |
| 11970 | When another "operation" is given, it specifies what should be done with |
| 11971 | the file. A typical operation is "print". |
| 11972 | |
| 11973 | startfile returns as soon as the associated application is launched. |
| 11974 | There is no option to wait for the application to close, and no way |
| 11975 | to retrieve the application's exit status. |
| 11976 | |
| 11977 | The filepath is relative to the current directory. If you want to use |
| 11978 | an absolute path, make sure the first character is not a slash ("/"); |
| 11979 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11980 | [clinic start generated code]*/ |
| 11981 | |
| 11982 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11983 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11984 | const Py_UNICODE *operation) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 11985 | /*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11986 | { |
| 11987 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11988 | |
| 11989 | if(!check_ShellExecute()) { |
| 11990 | /* If the OS doesn't have ShellExecute, return a |
| 11991 | NotImplementedError. */ |
| 11992 | return PyErr_Format(PyExc_NotImplementedError, |
| 11993 | "startfile not available on this platform"); |
| 11994 | } |
| 11995 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 11996 | if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 11997 | return NULL; |
| 11998 | } |
| 11999 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12000 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12001 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 12002 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12003 | Py_END_ALLOW_THREADS |
| 12004 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12005 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12006 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 12007 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12008 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12009 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 12010 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12011 | #endif /* MS_WINDOWS */ |
| 12012 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12013 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12014 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12015 | /*[clinic input] |
| 12016 | os.getloadavg |
| 12017 | |
| 12018 | Return average recent system load information. |
| 12019 | |
| 12020 | Return the number of processes in the system run queue averaged over |
| 12021 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 12022 | Raises OSError if the load average was unobtainable. |
| 12023 | [clinic start generated code]*/ |
| 12024 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12025 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12026 | os_getloadavg_impl(PyObject *module) |
| 12027 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12028 | { |
| 12029 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12030 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 12031 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 12032 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12033 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 12034 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12035 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12036 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12037 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12038 | |
| 12039 | /*[clinic input] |
| 12040 | os.device_encoding |
| 12041 | fd: int |
| 12042 | |
| 12043 | Return a string describing the encoding of a terminal's file descriptor. |
| 12044 | |
| 12045 | The file descriptor must be attached to a terminal. |
| 12046 | If the device is not a terminal, return None. |
| 12047 | [clinic start generated code]*/ |
| 12048 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12049 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12050 | os_device_encoding_impl(PyObject *module, int fd) |
| 12051 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12052 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 12053 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 12054 | } |
| 12055 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12056 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12057 | #ifdef HAVE_SETRESUID |
| 12058 | /*[clinic input] |
| 12059 | os.setresuid |
| 12060 | |
| 12061 | ruid: uid_t |
| 12062 | euid: uid_t |
| 12063 | suid: uid_t |
| 12064 | / |
| 12065 | |
| 12066 | Set the current process's real, effective, and saved user ids. |
| 12067 | [clinic start generated code]*/ |
| 12068 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12069 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12070 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 12071 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12072 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12073 | if (setresuid(ruid, euid, suid) < 0) |
| 12074 | return posix_error(); |
| 12075 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12076 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12077 | #endif /* HAVE_SETRESUID */ |
| 12078 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12079 | |
| 12080 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12081 | /*[clinic input] |
| 12082 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12083 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12084 | rgid: gid_t |
| 12085 | egid: gid_t |
| 12086 | sgid: gid_t |
| 12087 | / |
| 12088 | |
| 12089 | Set the current process's real, effective, and saved group ids. |
| 12090 | [clinic start generated code]*/ |
| 12091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12092 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12093 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 12094 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12095 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12096 | if (setresgid(rgid, egid, sgid) < 0) |
| 12097 | return posix_error(); |
| 12098 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12099 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12100 | #endif /* HAVE_SETRESGID */ |
| 12101 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12102 | |
| 12103 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12104 | /*[clinic input] |
| 12105 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12106 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12107 | Return a tuple of the current process's real, effective, and saved user ids. |
| 12108 | [clinic start generated code]*/ |
| 12109 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12110 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12111 | os_getresuid_impl(PyObject *module) |
| 12112 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12113 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12114 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12115 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 12116 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 12117 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 12118 | _PyLong_FromUid(euid), |
| 12119 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12120 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12121 | #endif /* HAVE_GETRESUID */ |
| 12122 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12123 | |
| 12124 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12125 | /*[clinic input] |
| 12126 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12127 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12128 | Return a tuple of the current process's real, effective, and saved group ids. |
| 12129 | [clinic start generated code]*/ |
| 12130 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12131 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12132 | os_getresgid_impl(PyObject *module) |
| 12133 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12134 | { |
| 12135 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12136 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 12137 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 12138 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 12139 | _PyLong_FromGid(egid), |
| 12140 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12141 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12142 | #endif /* HAVE_GETRESGID */ |
| 12143 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12144 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12145 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12146 | /*[clinic input] |
| 12147 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12148 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12149 | path: path_t(allow_fd=True) |
| 12150 | attribute: path_t |
| 12151 | * |
| 12152 | follow_symlinks: bool = True |
| 12153 | |
| 12154 | Return the value of extended attribute attribute on path. |
| 12155 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12156 | 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] | 12157 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12158 | link, getxattr will examine the symbolic link itself instead of the file |
| 12159 | the link points to. |
| 12160 | |
| 12161 | [clinic start generated code]*/ |
| 12162 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12163 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12164 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12165 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12166 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12167 | { |
| 12168 | Py_ssize_t i; |
| 12169 | PyObject *buffer = NULL; |
| 12170 | |
| 12171 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 12172 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12173 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12174 | if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) { |
| 12175 | return NULL; |
| 12176 | } |
| 12177 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12178 | for (i = 0; ; i++) { |
| 12179 | void *ptr; |
| 12180 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12181 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12182 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12183 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12184 | path_error(path); |
| 12185 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12186 | } |
| 12187 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 12188 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12189 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12190 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12191 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12192 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12193 | if (path->fd >= 0) |
| 12194 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12195 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12196 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12197 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12198 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12199 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12200 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12201 | if (result < 0) { |
| 12202 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12203 | if (errno == ERANGE) |
| 12204 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12205 | path_error(path); |
| 12206 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12207 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12208 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12209 | if (result != buffer_size) { |
| 12210 | /* Can only shrink. */ |
| 12211 | _PyBytes_Resize(&buffer, result); |
| 12212 | } |
| 12213 | break; |
| 12214 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12215 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12216 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12217 | } |
| 12218 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12219 | |
| 12220 | /*[clinic input] |
| 12221 | os.setxattr |
| 12222 | |
| 12223 | path: path_t(allow_fd=True) |
| 12224 | attribute: path_t |
| 12225 | value: Py_buffer |
| 12226 | flags: int = 0 |
| 12227 | * |
| 12228 | follow_symlinks: bool = True |
| 12229 | |
| 12230 | Set extended attribute attribute on path to value. |
| 12231 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12232 | 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] | 12233 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12234 | link, setxattr will modify the symbolic link itself instead of the file |
| 12235 | the link points to. |
| 12236 | |
| 12237 | [clinic start generated code]*/ |
| 12238 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12239 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12240 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12241 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12242 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12243 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12244 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12245 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12246 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12247 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12248 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12249 | if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object, |
| 12250 | value->buf, value->len, flags) < 0) { |
| 12251 | return NULL; |
| 12252 | } |
| 12253 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12254 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12255 | if (path->fd > -1) |
| 12256 | result = fsetxattr(path->fd, attribute->narrow, |
| 12257 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12258 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12259 | result = setxattr(path->narrow, attribute->narrow, |
| 12260 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12261 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12262 | result = lsetxattr(path->narrow, attribute->narrow, |
| 12263 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12264 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12265 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12266 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12267 | path_error(path); |
| 12268 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12269 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12270 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12271 | Py_RETURN_NONE; |
| 12272 | } |
| 12273 | |
| 12274 | |
| 12275 | /*[clinic input] |
| 12276 | os.removexattr |
| 12277 | |
| 12278 | path: path_t(allow_fd=True) |
| 12279 | attribute: path_t |
| 12280 | * |
| 12281 | follow_symlinks: bool = True |
| 12282 | |
| 12283 | Remove extended attribute attribute on path. |
| 12284 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12285 | 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] | 12286 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12287 | link, removexattr will modify the symbolic link itself instead of the file |
| 12288 | the link points to. |
| 12289 | |
| 12290 | [clinic start generated code]*/ |
| 12291 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12292 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12293 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12294 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12295 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12296 | { |
| 12297 | ssize_t result; |
| 12298 | |
| 12299 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 12300 | return NULL; |
| 12301 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12302 | if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) { |
| 12303 | return NULL; |
| 12304 | } |
| 12305 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12306 | Py_BEGIN_ALLOW_THREADS; |
| 12307 | if (path->fd > -1) |
| 12308 | result = fremovexattr(path->fd, attribute->narrow); |
| 12309 | else if (follow_symlinks) |
| 12310 | result = removexattr(path->narrow, attribute->narrow); |
| 12311 | else |
| 12312 | result = lremovexattr(path->narrow, attribute->narrow); |
| 12313 | Py_END_ALLOW_THREADS; |
| 12314 | |
| 12315 | if (result) { |
| 12316 | return path_error(path); |
| 12317 | } |
| 12318 | |
| 12319 | Py_RETURN_NONE; |
| 12320 | } |
| 12321 | |
| 12322 | |
| 12323 | /*[clinic input] |
| 12324 | os.listxattr |
| 12325 | |
| 12326 | path: path_t(allow_fd=True, nullable=True) = None |
| 12327 | * |
| 12328 | follow_symlinks: bool = True |
| 12329 | |
| 12330 | Return a list of extended attributes on path. |
| 12331 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12332 | 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] | 12333 | if path is None, listxattr will examine the current directory. |
| 12334 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12335 | link, listxattr will examine the symbolic link itself instead of the file |
| 12336 | the link points to. |
| 12337 | [clinic start generated code]*/ |
| 12338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12339 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12340 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12341 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12342 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12343 | Py_ssize_t i; |
| 12344 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12345 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12346 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12347 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12348 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12349 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12350 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12351 | if (PySys_Audit("os.listxattr", "(O)", |
| 12352 | path->object ? path->object : Py_None) < 0) { |
| 12353 | return NULL; |
| 12354 | } |
| 12355 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12356 | name = path->narrow ? path->narrow : "."; |
| 12357 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12358 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12359 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12360 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12361 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12362 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12363 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 12364 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12365 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12366 | break; |
| 12367 | } |
| 12368 | buffer = PyMem_MALLOC(buffer_size); |
| 12369 | if (!buffer) { |
| 12370 | PyErr_NoMemory(); |
| 12371 | break; |
| 12372 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12373 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12374 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12375 | if (path->fd > -1) |
| 12376 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12377 | else if (follow_symlinks) |
| 12378 | length = listxattr(name, buffer, buffer_size); |
| 12379 | else |
| 12380 | length = llistxattr(name, buffer, buffer_size); |
| 12381 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12382 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12383 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12384 | if (errno == ERANGE) { |
| 12385 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 12386 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12387 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12388 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12389 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12390 | break; |
| 12391 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12392 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12393 | result = PyList_New(0); |
| 12394 | if (!result) { |
| 12395 | goto exit; |
| 12396 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12397 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12398 | end = buffer + length; |
| 12399 | for (trace = start = buffer; trace != end; trace++) { |
| 12400 | if (!*trace) { |
| 12401 | int error; |
| 12402 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 12403 | trace - start); |
| 12404 | if (!attribute) { |
| 12405 | Py_DECREF(result); |
| 12406 | result = NULL; |
| 12407 | goto exit; |
| 12408 | } |
| 12409 | error = PyList_Append(result, attribute); |
| 12410 | Py_DECREF(attribute); |
| 12411 | if (error) { |
| 12412 | Py_DECREF(result); |
| 12413 | result = NULL; |
| 12414 | goto exit; |
| 12415 | } |
| 12416 | start = trace + 1; |
| 12417 | } |
| 12418 | } |
| 12419 | break; |
| 12420 | } |
| 12421 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12422 | if (buffer) |
| 12423 | PyMem_FREE(buffer); |
| 12424 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12425 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12426 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12427 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12428 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12429 | /*[clinic input] |
| 12430 | os.urandom |
| 12431 | |
| 12432 | size: Py_ssize_t |
| 12433 | / |
| 12434 | |
| 12435 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 12436 | [clinic start generated code]*/ |
| 12437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12438 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12439 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 12440 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12441 | { |
| 12442 | PyObject *bytes; |
| 12443 | int result; |
| 12444 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12445 | if (size < 0) |
| 12446 | return PyErr_Format(PyExc_ValueError, |
| 12447 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12448 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 12449 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12450 | return NULL; |
| 12451 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 12452 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12453 | if (result == -1) { |
| 12454 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12455 | return NULL; |
| 12456 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12457 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12458 | } |
| 12459 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 12460 | #ifdef HAVE_MEMFD_CREATE |
| 12461 | /*[clinic input] |
| 12462 | os.memfd_create |
| 12463 | |
| 12464 | name: FSConverter |
| 12465 | flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC |
| 12466 | |
| 12467 | [clinic start generated code]*/ |
| 12468 | |
| 12469 | static PyObject * |
| 12470 | os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags) |
| 12471 | /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/ |
| 12472 | { |
| 12473 | int fd; |
| 12474 | const char *bytes = PyBytes_AS_STRING(name); |
| 12475 | Py_BEGIN_ALLOW_THREADS |
| 12476 | fd = memfd_create(bytes, flags); |
| 12477 | Py_END_ALLOW_THREADS |
| 12478 | if (fd == -1) { |
| 12479 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12480 | } |
| 12481 | return PyLong_FromLong(fd); |
| 12482 | } |
| 12483 | #endif |
| 12484 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12485 | /* Terminal size querying */ |
| 12486 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12487 | PyDoc_STRVAR(TerminalSize_docstring, |
| 12488 | "A tuple of (columns, lines) for holding terminal window size"); |
| 12489 | |
| 12490 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 12491 | {"columns", "width of the terminal window in characters"}, |
| 12492 | {"lines", "height of the terminal window in characters"}, |
| 12493 | {NULL, NULL} |
| 12494 | }; |
| 12495 | |
| 12496 | static PyStructSequence_Desc TerminalSize_desc = { |
| 12497 | "os.terminal_size", |
| 12498 | TerminalSize_docstring, |
| 12499 | TerminalSize_fields, |
| 12500 | 2, |
| 12501 | }; |
| 12502 | |
| 12503 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12504 | /*[clinic input] |
| 12505 | os.get_terminal_size |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12506 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12507 | fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1 |
| 12508 | / |
| 12509 | |
| 12510 | Return the size of the terminal window as (columns, lines). |
| 12511 | |
| 12512 | The optional argument fd (default standard output) specifies |
| 12513 | which file descriptor should be queried. |
| 12514 | |
| 12515 | If the file descriptor is not connected to a terminal, an OSError |
| 12516 | is thrown. |
| 12517 | |
| 12518 | This function will only be defined if an implementation is |
| 12519 | available for this system. |
| 12520 | |
| 12521 | shutil.get_terminal_size is the high-level function which should |
| 12522 | normally be used, os.get_terminal_size is the low-level implementation. |
| 12523 | [clinic start generated code]*/ |
| 12524 | |
| 12525 | static PyObject * |
| 12526 | os_get_terminal_size_impl(PyObject *module, int fd) |
| 12527 | /*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12528 | { |
| 12529 | int columns, lines; |
| 12530 | PyObject *termsize; |
| 12531 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12532 | /* Under some conditions stdout may not be connected and |
| 12533 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 12534 | * GUI apps don't have valid standard streams by default. |
| 12535 | * |
| 12536 | * If this happens, and the optional fd argument is not present, |
| 12537 | * the ioctl below will fail returning EBADF. This is what we want. |
| 12538 | */ |
| 12539 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12540 | #ifdef TERMSIZE_USE_IOCTL |
| 12541 | { |
| 12542 | struct winsize w; |
| 12543 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 12544 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12545 | columns = w.ws_col; |
| 12546 | lines = w.ws_row; |
| 12547 | } |
| 12548 | #endif /* TERMSIZE_USE_IOCTL */ |
| 12549 | |
| 12550 | #ifdef TERMSIZE_USE_CONIO |
| 12551 | { |
| 12552 | DWORD nhandle; |
| 12553 | HANDLE handle; |
| 12554 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 12555 | switch (fd) { |
| 12556 | case 0: nhandle = STD_INPUT_HANDLE; |
| 12557 | break; |
| 12558 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 12559 | break; |
| 12560 | case 2: nhandle = STD_ERROR_HANDLE; |
| 12561 | break; |
| 12562 | default: |
| 12563 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 12564 | } |
| 12565 | handle = GetStdHandle(nhandle); |
| 12566 | if (handle == NULL) |
| 12567 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 12568 | if (handle == INVALID_HANDLE_VALUE) |
| 12569 | return PyErr_SetFromWindowsErr(0); |
| 12570 | |
| 12571 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 12572 | return PyErr_SetFromWindowsErr(0); |
| 12573 | |
| 12574 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 12575 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 12576 | } |
| 12577 | #endif /* TERMSIZE_USE_CONIO */ |
| 12578 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12579 | PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12580 | termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12581 | if (termsize == NULL) |
| 12582 | return NULL; |
| 12583 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 12584 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 12585 | if (PyErr_Occurred()) { |
| 12586 | Py_DECREF(termsize); |
| 12587 | return NULL; |
| 12588 | } |
| 12589 | return termsize; |
| 12590 | } |
| 12591 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 12592 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12593 | |
| 12594 | /*[clinic input] |
| 12595 | os.cpu_count |
| 12596 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 12597 | Return the number of CPUs in the system; return None if indeterminable. |
| 12598 | |
| 12599 | This number is not equivalent to the number of CPUs the current process can |
| 12600 | use. The number of usable CPUs can be obtained with |
| 12601 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12602 | [clinic start generated code]*/ |
| 12603 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12604 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12605 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12606 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12607 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12608 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12609 | #ifdef MS_WINDOWS |
Steve Dower | aa92927 | 2019-09-11 16:15:39 +0100 | [diff] [blame] | 12610 | ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12611 | #elif defined(__hpux) |
| 12612 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 12613 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 12614 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
pxinwr | 3405e05 | 2020-08-07 13:21:52 +0800 | [diff] [blame] | 12615 | #elif defined(__VXWORKS__) |
| 12616 | ncpu = _Py_popcount32(vxCpuEnabledGet()); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12617 | #elif defined(__DragonFly__) || \ |
| 12618 | defined(__OpenBSD__) || \ |
| 12619 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12620 | defined(__NetBSD__) || \ |
| 12621 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12622 | int mib[2]; |
| 12623 | size_t len = sizeof(ncpu); |
| 12624 | mib[0] = CTL_HW; |
| 12625 | mib[1] = HW_NCPU; |
| 12626 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 12627 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12628 | #endif |
| 12629 | if (ncpu >= 1) |
| 12630 | return PyLong_FromLong(ncpu); |
| 12631 | else |
| 12632 | Py_RETURN_NONE; |
| 12633 | } |
| 12634 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12635 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12636 | /*[clinic input] |
| 12637 | os.get_inheritable -> bool |
| 12638 | |
| 12639 | fd: int |
| 12640 | / |
| 12641 | |
| 12642 | Get the close-on-exe flag of the specified file descriptor. |
| 12643 | [clinic start generated code]*/ |
| 12644 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12645 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12646 | os_get_inheritable_impl(PyObject *module, int fd) |
| 12647 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12648 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12649 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12650 | _Py_BEGIN_SUPPRESS_IPH |
| 12651 | return_value = _Py_get_inheritable(fd); |
| 12652 | _Py_END_SUPPRESS_IPH |
| 12653 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12654 | } |
| 12655 | |
| 12656 | |
| 12657 | /*[clinic input] |
| 12658 | os.set_inheritable |
| 12659 | fd: int |
| 12660 | inheritable: int |
| 12661 | / |
| 12662 | |
| 12663 | Set the inheritable flag of the specified file descriptor. |
| 12664 | [clinic start generated code]*/ |
| 12665 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12666 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12667 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 12668 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12669 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12670 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12671 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12672 | _Py_BEGIN_SUPPRESS_IPH |
| 12673 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 12674 | _Py_END_SUPPRESS_IPH |
| 12675 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12676 | return NULL; |
| 12677 | Py_RETURN_NONE; |
| 12678 | } |
| 12679 | |
| 12680 | |
| 12681 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12682 | /*[clinic input] |
| 12683 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12684 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12685 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12686 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12687 | Get the close-on-exe flag of the specified file descriptor. |
| 12688 | [clinic start generated code]*/ |
| 12689 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12690 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12691 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12692 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12693 | { |
| 12694 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12695 | |
| 12696 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 12697 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12698 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12699 | } |
| 12700 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12701 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12702 | } |
| 12703 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12704 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12705 | /*[clinic input] |
| 12706 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12707 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12708 | inheritable: bool |
| 12709 | / |
| 12710 | |
| 12711 | Set the inheritable flag of the specified handle. |
| 12712 | [clinic start generated code]*/ |
| 12713 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12714 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12715 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12716 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12717 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12718 | { |
| 12719 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12720 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 12721 | PyErr_SetFromWindowsErr(0); |
| 12722 | return NULL; |
| 12723 | } |
| 12724 | Py_RETURN_NONE; |
| 12725 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12726 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12727 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12728 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12729 | /*[clinic input] |
| 12730 | os.get_blocking -> bool |
| 12731 | fd: int |
| 12732 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12733 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12734 | Get the blocking mode of the file descriptor. |
| 12735 | |
| 12736 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 12737 | [clinic start generated code]*/ |
| 12738 | |
| 12739 | static int |
| 12740 | os_get_blocking_impl(PyObject *module, int fd) |
| 12741 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12742 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12743 | int blocking; |
| 12744 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12745 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12746 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12747 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12748 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12749 | } |
| 12750 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12751 | /*[clinic input] |
| 12752 | os.set_blocking |
| 12753 | fd: int |
| 12754 | blocking: bool(accept={int}) |
| 12755 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12756 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12757 | Set the blocking mode of the specified file descriptor. |
| 12758 | |
| 12759 | Set the O_NONBLOCK flag if blocking is False, |
| 12760 | clear the O_NONBLOCK flag otherwise. |
| 12761 | [clinic start generated code]*/ |
| 12762 | |
| 12763 | static PyObject * |
| 12764 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 12765 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12766 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12767 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12768 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12769 | _Py_BEGIN_SUPPRESS_IPH |
| 12770 | result = _Py_set_blocking(fd, blocking); |
| 12771 | _Py_END_SUPPRESS_IPH |
| 12772 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12773 | return NULL; |
| 12774 | Py_RETURN_NONE; |
| 12775 | } |
| 12776 | #endif /* !MS_WINDOWS */ |
| 12777 | |
| 12778 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12779 | /*[clinic input] |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12780 | class os.DirEntry "DirEntry *" "DirEntryType" |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12781 | [clinic start generated code]*/ |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12782 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12783 | |
| 12784 | typedef struct { |
| 12785 | PyObject_HEAD |
| 12786 | PyObject *name; |
| 12787 | PyObject *path; |
| 12788 | PyObject *stat; |
| 12789 | PyObject *lstat; |
| 12790 | #ifdef MS_WINDOWS |
| 12791 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12792 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12793 | int got_file_index; |
| 12794 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12795 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12796 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12797 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12798 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12799 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12800 | #endif |
| 12801 | } DirEntry; |
| 12802 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12803 | static PyObject * |
| 12804 | _disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 12805 | { |
| 12806 | PyErr_Format(PyExc_TypeError, |
| 12807 | "cannot create '%.100s' instances", _PyType_Name(type)); |
| 12808 | return NULL; |
| 12809 | } |
| 12810 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12811 | static void |
| 12812 | DirEntry_dealloc(DirEntry *entry) |
| 12813 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12814 | PyTypeObject *tp = Py_TYPE(entry); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12815 | Py_XDECREF(entry->name); |
| 12816 | Py_XDECREF(entry->path); |
| 12817 | Py_XDECREF(entry->stat); |
| 12818 | Py_XDECREF(entry->lstat); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12819 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 12820 | free_func(entry); |
| 12821 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12822 | } |
| 12823 | |
| 12824 | /* Forward reference */ |
| 12825 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12826 | DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self, |
| 12827 | int follow_symlinks, unsigned short mode_bits); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12828 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12829 | /*[clinic input] |
| 12830 | os.DirEntry.is_symlink -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12831 | defining_class: defining_class |
| 12832 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12833 | |
| 12834 | Return True if the entry is a symbolic link; cached per entry. |
| 12835 | [clinic start generated code]*/ |
| 12836 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12837 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12838 | os_DirEntry_is_symlink_impl(DirEntry *self, PyTypeObject *defining_class) |
| 12839 | /*[clinic end generated code: output=293096d589b6d47c input=e9acc5ee4d511113]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12840 | { |
| 12841 | #ifdef MS_WINDOWS |
| 12842 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12843 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12844 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12845 | if (self->d_type != DT_UNKNOWN) |
| 12846 | return self->d_type == DT_LNK; |
| 12847 | else |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12848 | return DirEntry_test_mode(defining_class, self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12849 | #else |
| 12850 | /* POSIX without d_type */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12851 | return DirEntry_test_mode(defining_class, self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12852 | #endif |
| 12853 | } |
| 12854 | |
| 12855 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12856 | DirEntry_fetch_stat(PyObject *module, DirEntry *self, int follow_symlinks) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12857 | { |
| 12858 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12859 | STRUCT_STAT st; |
| 12860 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12861 | |
| 12862 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12863 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12864 | return NULL; |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 12865 | #if USE_UNICODE_WCHAR_CACHE |
| 12866 | _Py_COMP_DIAG_PUSH |
| 12867 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12868 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 12869 | _Py_COMP_DIAG_POP |
| 12870 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 12871 | wchar_t *path = PyUnicode_AsWideCharString(ub, NULL); |
| 12872 | Py_DECREF(ub); |
| 12873 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12874 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12875 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12876 | return NULL; |
| 12877 | const char *path = PyBytes_AS_STRING(ub); |
| 12878 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12879 | #ifdef HAVE_FSTATAT |
| 12880 | result = fstatat(self->dir_fd, path, &st, |
| 12881 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12882 | #else |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 12883 | Py_DECREF(ub); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12884 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12885 | return NULL; |
| 12886 | #endif /* HAVE_FSTATAT */ |
| 12887 | } |
| 12888 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12889 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12890 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12891 | if (follow_symlinks) |
| 12892 | result = STAT(path, &st); |
| 12893 | else |
| 12894 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12895 | } |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 12896 | #if defined(MS_WINDOWS) && !USE_UNICODE_WCHAR_CACHE |
| 12897 | PyMem_Free(path); |
| 12898 | #else /* USE_UNICODE_WCHAR_CACHE */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12899 | Py_DECREF(ub); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 12900 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12901 | |
| 12902 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12903 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12904 | |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12905 | return _pystat_fromstructstat(module, &st); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12906 | } |
| 12907 | |
| 12908 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12909 | DirEntry_get_lstat(PyTypeObject *defining_class, DirEntry *self) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12910 | { |
| 12911 | if (!self->lstat) { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12912 | PyObject *module = PyType_GetModule(defining_class); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12913 | #ifdef MS_WINDOWS |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12914 | self->lstat = _pystat_fromstructstat(module, &self->win32_lstat); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12915 | #else /* POSIX */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12916 | self->lstat = DirEntry_fetch_stat(module, self, 0); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12917 | #endif |
| 12918 | } |
| 12919 | Py_XINCREF(self->lstat); |
| 12920 | return self->lstat; |
| 12921 | } |
| 12922 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12923 | /*[clinic input] |
| 12924 | os.DirEntry.stat |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12925 | defining_class: defining_class |
| 12926 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12927 | * |
| 12928 | follow_symlinks: bool = True |
| 12929 | |
| 12930 | Return stat_result object for the entry; cached per entry. |
| 12931 | [clinic start generated code]*/ |
| 12932 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12933 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12934 | os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class, |
| 12935 | int follow_symlinks) |
| 12936 | /*[clinic end generated code: output=23f803e19c3e780e input=e816273c4e67ee98]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12937 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12938 | if (!follow_symlinks) { |
| 12939 | return DirEntry_get_lstat(defining_class, self); |
| 12940 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12941 | |
| 12942 | if (!self->stat) { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12943 | int result = os_DirEntry_is_symlink_impl(self, defining_class); |
| 12944 | if (result == -1) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12945 | return NULL; |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12946 | } |
| 12947 | if (result) { |
| 12948 | PyObject *module = PyType_GetModule(defining_class); |
| 12949 | self->stat = DirEntry_fetch_stat(module, self, 1); |
| 12950 | } |
| 12951 | else { |
| 12952 | self->stat = DirEntry_get_lstat(defining_class, self); |
| 12953 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12954 | } |
| 12955 | |
| 12956 | Py_XINCREF(self->stat); |
| 12957 | return self->stat; |
| 12958 | } |
| 12959 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12960 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12961 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12962 | DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self, |
| 12963 | int follow_symlinks, unsigned short mode_bits) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12964 | { |
| 12965 | PyObject *stat = NULL; |
| 12966 | PyObject *st_mode = NULL; |
| 12967 | long mode; |
| 12968 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12969 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12970 | int is_symlink; |
| 12971 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12972 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12973 | #ifdef MS_WINDOWS |
| 12974 | unsigned long dir_bits; |
| 12975 | #endif |
| 12976 | |
| 12977 | #ifdef MS_WINDOWS |
| 12978 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12979 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12980 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12981 | is_symlink = self->d_type == DT_LNK; |
| 12982 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12983 | #endif |
| 12984 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12985 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12986 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12987 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12988 | stat = os_DirEntry_stat_impl(self, defining_class, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12989 | if (!stat) { |
| 12990 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12991 | /* If file doesn't exist (anymore), then return False |
| 12992 | (i.e., say it's not a file/directory) */ |
| 12993 | PyErr_Clear(); |
| 12994 | return 0; |
| 12995 | } |
| 12996 | goto error; |
| 12997 | } |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12998 | _posixstate* state = get_posix_state(PyType_GetModule(defining_class)); |
| 12999 | st_mode = PyObject_GetAttr(stat, state->st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13000 | if (!st_mode) |
| 13001 | goto error; |
| 13002 | |
| 13003 | mode = PyLong_AsLong(st_mode); |
| 13004 | if (mode == -1 && PyErr_Occurred()) |
| 13005 | goto error; |
| 13006 | Py_CLEAR(st_mode); |
| 13007 | Py_CLEAR(stat); |
| 13008 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13009 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13010 | } |
| 13011 | else if (is_symlink) { |
| 13012 | assert(mode_bits != S_IFLNK); |
| 13013 | result = 0; |
| 13014 | } |
| 13015 | else { |
| 13016 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 13017 | #ifdef MS_WINDOWS |
| 13018 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 13019 | if (mode_bits == S_IFDIR) |
| 13020 | result = dir_bits != 0; |
| 13021 | else |
| 13022 | result = dir_bits == 0; |
| 13023 | #else /* POSIX */ |
| 13024 | if (mode_bits == S_IFDIR) |
| 13025 | result = self->d_type == DT_DIR; |
| 13026 | else |
| 13027 | result = self->d_type == DT_REG; |
| 13028 | #endif |
| 13029 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13030 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13031 | |
| 13032 | return result; |
| 13033 | |
| 13034 | error: |
| 13035 | Py_XDECREF(st_mode); |
| 13036 | Py_XDECREF(stat); |
| 13037 | return -1; |
| 13038 | } |
| 13039 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13040 | /*[clinic input] |
| 13041 | os.DirEntry.is_dir -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13042 | defining_class: defining_class |
| 13043 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13044 | * |
| 13045 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13046 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13047 | Return True if the entry is a directory; cached per entry. |
| 13048 | [clinic start generated code]*/ |
| 13049 | |
| 13050 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13051 | os_DirEntry_is_dir_impl(DirEntry *self, PyTypeObject *defining_class, |
| 13052 | int follow_symlinks) |
| 13053 | /*[clinic end generated code: output=0cd453b9c0987fdf input=1a4ffd6dec9920cb]*/ |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13054 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13055 | return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13056 | } |
| 13057 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13058 | /*[clinic input] |
| 13059 | os.DirEntry.is_file -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13060 | defining_class: defining_class |
| 13061 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13062 | * |
| 13063 | follow_symlinks: bool = True |
| 13064 | |
| 13065 | Return True if the entry is a file; cached per entry. |
| 13066 | [clinic start generated code]*/ |
| 13067 | |
| 13068 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13069 | os_DirEntry_is_file_impl(DirEntry *self, PyTypeObject *defining_class, |
| 13070 | int follow_symlinks) |
| 13071 | /*[clinic end generated code: output=f7c277ab5ba80908 input=0a64c5a12e802e3b]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13072 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13073 | return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13074 | } |
| 13075 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13076 | /*[clinic input] |
| 13077 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13078 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13079 | Return inode of the entry; cached per entry. |
| 13080 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13081 | |
| 13082 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13083 | os_DirEntry_inode_impl(DirEntry *self) |
| 13084 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13085 | { |
| 13086 | #ifdef MS_WINDOWS |
| 13087 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13088 | PyObject *unicode; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13089 | STRUCT_STAT stat; |
| 13090 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13091 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13092 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13093 | return NULL; |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13094 | #if USE_UNICODE_WCHAR_CACHE |
| 13095 | _Py_COMP_DIAG_PUSH |
| 13096 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
| 13097 | const wchar_t *path = PyUnicode_AsUnicode(unicode); |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13098 | result = LSTAT(path, &stat); |
| 13099 | Py_DECREF(unicode); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13100 | _Py_COMP_DIAG_POP |
| 13101 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 13102 | wchar_t *path = PyUnicode_AsWideCharString(unicode, NULL); |
| 13103 | Py_DECREF(unicode); |
| 13104 | result = LSTAT(path, &stat); |
| 13105 | PyMem_Free(path); |
| 13106 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13107 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13108 | if (result != 0) |
| 13109 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13110 | |
| 13111 | self->win32_file_index = stat.st_ino; |
| 13112 | self->got_file_index = 1; |
| 13113 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 13114 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 13115 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13116 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 13117 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 13118 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13119 | #endif |
| 13120 | } |
| 13121 | |
| 13122 | static PyObject * |
| 13123 | DirEntry_repr(DirEntry *self) |
| 13124 | { |
| 13125 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 13126 | } |
| 13127 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13128 | /*[clinic input] |
| 13129 | os.DirEntry.__fspath__ |
| 13130 | |
| 13131 | Returns the path for the entry. |
| 13132 | [clinic start generated code]*/ |
| 13133 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 13134 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13135 | os_DirEntry___fspath___impl(DirEntry *self) |
| 13136 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 13137 | { |
| 13138 | Py_INCREF(self->path); |
| 13139 | return self->path; |
| 13140 | } |
| 13141 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13142 | static PyMemberDef DirEntry_members[] = { |
| 13143 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 13144 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 13145 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 13146 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 13147 | {NULL} |
| 13148 | }; |
| 13149 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13150 | #include "clinic/posixmodule.c.h" |
| 13151 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13152 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13153 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 13154 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 13155 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 13156 | OS_DIRENTRY_STAT_METHODDEF |
| 13157 | OS_DIRENTRY_INODE_METHODDEF |
| 13158 | OS_DIRENTRY___FSPATH___METHODDEF |
Batuhan Taşkaya | f9dd51e | 2020-04-08 00:37:19 +0300 | [diff] [blame] | 13159 | {"__class_getitem__", (PyCFunction)Py_GenericAlias, |
| 13160 | METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13161 | {NULL} |
| 13162 | }; |
| 13163 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13164 | static PyType_Slot DirEntryType_slots[] = { |
| 13165 | {Py_tp_new, _disabled_new}, |
| 13166 | {Py_tp_dealloc, DirEntry_dealloc}, |
| 13167 | {Py_tp_repr, DirEntry_repr}, |
| 13168 | {Py_tp_methods, DirEntry_methods}, |
| 13169 | {Py_tp_members, DirEntry_members}, |
| 13170 | {0, 0}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13171 | }; |
| 13172 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13173 | static PyType_Spec DirEntryType_spec = { |
| 13174 | MODNAME ".DirEntry", |
| 13175 | sizeof(DirEntry), |
| 13176 | 0, |
| 13177 | Py_TPFLAGS_DEFAULT, |
| 13178 | DirEntryType_slots |
| 13179 | }; |
| 13180 | |
| 13181 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13182 | #ifdef MS_WINDOWS |
| 13183 | |
| 13184 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 13185 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13186 | { |
| 13187 | Py_ssize_t path_len; |
| 13188 | Py_ssize_t size; |
| 13189 | wchar_t *result; |
| 13190 | wchar_t ch; |
| 13191 | |
| 13192 | if (!path_wide) { /* Default arg: "." */ |
| 13193 | path_wide = L"."; |
| 13194 | path_len = 1; |
| 13195 | } |
| 13196 | else { |
| 13197 | path_len = wcslen(path_wide); |
| 13198 | } |
| 13199 | |
| 13200 | /* The +1's are for the path separator and the NUL */ |
| 13201 | size = path_len + 1 + wcslen(filename) + 1; |
| 13202 | result = PyMem_New(wchar_t, size); |
| 13203 | if (!result) { |
| 13204 | PyErr_NoMemory(); |
| 13205 | return NULL; |
| 13206 | } |
| 13207 | wcscpy(result, path_wide); |
| 13208 | if (path_len > 0) { |
| 13209 | ch = result[path_len - 1]; |
| 13210 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 13211 | result[path_len++] = SEP; |
| 13212 | wcscpy(result + path_len, filename); |
| 13213 | } |
| 13214 | return result; |
| 13215 | } |
| 13216 | |
| 13217 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13218 | DirEntry_from_find_data(PyObject *module, path_t *path, WIN32_FIND_DATAW *dataW) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13219 | { |
| 13220 | DirEntry *entry; |
| 13221 | BY_HANDLE_FILE_INFORMATION file_info; |
| 13222 | ULONG reparse_tag; |
| 13223 | wchar_t *joined_path; |
| 13224 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13225 | PyObject *DirEntryType = get_posix_state(module)->DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13226 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13227 | if (!entry) |
| 13228 | return NULL; |
| 13229 | entry->name = NULL; |
| 13230 | entry->path = NULL; |
| 13231 | entry->stat = NULL; |
| 13232 | entry->lstat = NULL; |
| 13233 | entry->got_file_index = 0; |
| 13234 | |
| 13235 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 13236 | if (!entry->name) |
| 13237 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13238 | if (path->narrow) { |
| 13239 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 13240 | if (!entry->name) |
| 13241 | goto error; |
| 13242 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13243 | |
| 13244 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 13245 | if (!joined_path) |
| 13246 | goto error; |
| 13247 | |
| 13248 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 13249 | PyMem_Free(joined_path); |
| 13250 | if (!entry->path) |
| 13251 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13252 | if (path->narrow) { |
| 13253 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 13254 | if (!entry->path) |
| 13255 | goto error; |
| 13256 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13257 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13258 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13259 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 13260 | |
| 13261 | return (PyObject *)entry; |
| 13262 | |
| 13263 | error: |
| 13264 | Py_DECREF(entry); |
| 13265 | return NULL; |
| 13266 | } |
| 13267 | |
| 13268 | #else /* POSIX */ |
| 13269 | |
| 13270 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 13271 | 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] | 13272 | { |
| 13273 | Py_ssize_t path_len; |
| 13274 | Py_ssize_t size; |
| 13275 | char *result; |
| 13276 | |
| 13277 | if (!path_narrow) { /* Default arg: "." */ |
| 13278 | path_narrow = "."; |
| 13279 | path_len = 1; |
| 13280 | } |
| 13281 | else { |
| 13282 | path_len = strlen(path_narrow); |
| 13283 | } |
| 13284 | |
| 13285 | if (filename_len == -1) |
| 13286 | filename_len = strlen(filename); |
| 13287 | |
| 13288 | /* The +1's are for the path separator and the NUL */ |
| 13289 | size = path_len + 1 + filename_len + 1; |
| 13290 | result = PyMem_New(char, size); |
| 13291 | if (!result) { |
| 13292 | PyErr_NoMemory(); |
| 13293 | return NULL; |
| 13294 | } |
| 13295 | strcpy(result, path_narrow); |
| 13296 | if (path_len > 0 && result[path_len - 1] != '/') |
| 13297 | result[path_len++] = '/'; |
| 13298 | strcpy(result + path_len, filename); |
| 13299 | return result; |
| 13300 | } |
| 13301 | |
| 13302 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13303 | DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name, |
| 13304 | Py_ssize_t name_len, ino_t d_ino |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13305 | #ifdef HAVE_DIRENT_D_TYPE |
| 13306 | , unsigned char d_type |
| 13307 | #endif |
| 13308 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13309 | { |
| 13310 | DirEntry *entry; |
| 13311 | char *joined_path; |
| 13312 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13313 | PyObject *DirEntryType = get_posix_state(module)->DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13314 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13315 | if (!entry) |
| 13316 | return NULL; |
| 13317 | entry->name = NULL; |
| 13318 | entry->path = NULL; |
| 13319 | entry->stat = NULL; |
| 13320 | entry->lstat = NULL; |
| 13321 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13322 | if (path->fd != -1) { |
| 13323 | entry->dir_fd = path->fd; |
| 13324 | joined_path = NULL; |
| 13325 | } |
| 13326 | else { |
| 13327 | entry->dir_fd = DEFAULT_DIR_FD; |
| 13328 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 13329 | if (!joined_path) |
| 13330 | goto error; |
| 13331 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13332 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 13333 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13334 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13335 | if (joined_path) |
| 13336 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13337 | } |
| 13338 | else { |
| 13339 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13340 | if (joined_path) |
| 13341 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13342 | } |
| 13343 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13344 | if (!entry->name) |
| 13345 | goto error; |
| 13346 | |
| 13347 | if (path->fd != -1) { |
| 13348 | entry->path = entry->name; |
| 13349 | Py_INCREF(entry->path); |
| 13350 | } |
| 13351 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13352 | goto error; |
| 13353 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13354 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13355 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13356 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13357 | entry->d_ino = d_ino; |
| 13358 | |
| 13359 | return (PyObject *)entry; |
| 13360 | |
| 13361 | error: |
| 13362 | Py_XDECREF(entry); |
| 13363 | return NULL; |
| 13364 | } |
| 13365 | |
| 13366 | #endif |
| 13367 | |
| 13368 | |
| 13369 | typedef struct { |
| 13370 | PyObject_HEAD |
| 13371 | path_t path; |
| 13372 | #ifdef MS_WINDOWS |
| 13373 | HANDLE handle; |
| 13374 | WIN32_FIND_DATAW file_data; |
| 13375 | int first_time; |
| 13376 | #else /* POSIX */ |
| 13377 | DIR *dirp; |
| 13378 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13379 | #ifdef HAVE_FDOPENDIR |
| 13380 | int fd; |
| 13381 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13382 | } ScandirIterator; |
| 13383 | |
| 13384 | #ifdef MS_WINDOWS |
| 13385 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13386 | static int |
| 13387 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13388 | { |
| 13389 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 13390 | } |
| 13391 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13392 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13393 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13394 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13395 | HANDLE handle = iterator->handle; |
| 13396 | |
| 13397 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13398 | return; |
| 13399 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13400 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13401 | Py_BEGIN_ALLOW_THREADS |
| 13402 | FindClose(handle); |
| 13403 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13404 | } |
| 13405 | |
| 13406 | static PyObject * |
| 13407 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13408 | { |
| 13409 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 13410 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13411 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13412 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13413 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13414 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13415 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13416 | |
| 13417 | while (1) { |
| 13418 | if (!iterator->first_time) { |
| 13419 | Py_BEGIN_ALLOW_THREADS |
| 13420 | success = FindNextFileW(iterator->handle, file_data); |
| 13421 | Py_END_ALLOW_THREADS |
| 13422 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13423 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13424 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13425 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13426 | break; |
| 13427 | } |
| 13428 | } |
| 13429 | iterator->first_time = 0; |
| 13430 | |
| 13431 | /* Skip over . and .. */ |
| 13432 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13433 | wcscmp(file_data->cFileName, L"..") != 0) |
| 13434 | { |
| 13435 | PyObject *module = PyType_GetModule(Py_TYPE(iterator)); |
| 13436 | entry = DirEntry_from_find_data(module, &iterator->path, file_data); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13437 | if (!entry) |
| 13438 | break; |
| 13439 | return entry; |
| 13440 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13441 | |
| 13442 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13443 | } |
| 13444 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13445 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13446 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13447 | return NULL; |
| 13448 | } |
| 13449 | |
| 13450 | #else /* POSIX */ |
| 13451 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13452 | static int |
| 13453 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13454 | { |
| 13455 | return !iterator->dirp; |
| 13456 | } |
| 13457 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13458 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13459 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13460 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13461 | DIR *dirp = iterator->dirp; |
| 13462 | |
| 13463 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13464 | return; |
| 13465 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13466 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13467 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13468 | #ifdef HAVE_FDOPENDIR |
| 13469 | if (iterator->path.fd != -1) |
| 13470 | rewinddir(dirp); |
| 13471 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13472 | closedir(dirp); |
| 13473 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13474 | return; |
| 13475 | } |
| 13476 | |
| 13477 | static PyObject * |
| 13478 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13479 | { |
| 13480 | struct dirent *direntp; |
| 13481 | Py_ssize_t name_len; |
| 13482 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13483 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13484 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13485 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13486 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13487 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13488 | |
| 13489 | while (1) { |
| 13490 | errno = 0; |
| 13491 | Py_BEGIN_ALLOW_THREADS |
| 13492 | direntp = readdir(iterator->dirp); |
| 13493 | Py_END_ALLOW_THREADS |
| 13494 | |
| 13495 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13496 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13497 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13498 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13499 | break; |
| 13500 | } |
| 13501 | |
| 13502 | /* Skip over . and .. */ |
| 13503 | name_len = NAMLEN(direntp); |
| 13504 | is_dot = direntp->d_name[0] == '.' && |
| 13505 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 13506 | if (!is_dot) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13507 | PyObject *module = PyType_GetModule(Py_TYPE(iterator)); |
| 13508 | entry = DirEntry_from_posix_info(module, |
| 13509 | &iterator->path, direntp->d_name, |
| 13510 | name_len, direntp->d_ino |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13511 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13512 | , direntp->d_type |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13513 | #endif |
| 13514 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13515 | if (!entry) |
| 13516 | break; |
| 13517 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13518 | } |
| 13519 | |
| 13520 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13521 | } |
| 13522 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13523 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13524 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13525 | return NULL; |
| 13526 | } |
| 13527 | |
| 13528 | #endif |
| 13529 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13530 | static PyObject * |
| 13531 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 13532 | { |
| 13533 | ScandirIterator_closedir(self); |
| 13534 | Py_RETURN_NONE; |
| 13535 | } |
| 13536 | |
| 13537 | static PyObject * |
| 13538 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 13539 | { |
| 13540 | Py_INCREF(self); |
| 13541 | return self; |
| 13542 | } |
| 13543 | |
| 13544 | static PyObject * |
| 13545 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 13546 | { |
| 13547 | ScandirIterator_closedir(self); |
| 13548 | Py_RETURN_NONE; |
| 13549 | } |
| 13550 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13551 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13552 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 13553 | { |
| 13554 | PyObject *error_type, *error_value, *error_traceback; |
| 13555 | |
| 13556 | /* Save the current exception, if any. */ |
| 13557 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 13558 | |
| 13559 | if (!ScandirIterator_is_closed(iterator)) { |
| 13560 | ScandirIterator_closedir(iterator); |
| 13561 | |
| 13562 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 13563 | "unclosed scandir iterator %R", iterator)) { |
| 13564 | /* Spurious errors can appear at shutdown */ |
| 13565 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 13566 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 13567 | } |
| 13568 | } |
| 13569 | } |
| 13570 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13571 | path_cleanup(&iterator->path); |
| 13572 | |
| 13573 | /* Restore the saved exception. */ |
| 13574 | PyErr_Restore(error_type, error_value, error_traceback); |
| 13575 | } |
| 13576 | |
| 13577 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13578 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 13579 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13580 | PyTypeObject *tp = Py_TYPE(iterator); |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13581 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 13582 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13583 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13584 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 13585 | free_func(iterator); |
| 13586 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13587 | } |
| 13588 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13589 | static PyMethodDef ScandirIterator_methods[] = { |
| 13590 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 13591 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 13592 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 13593 | {NULL} |
| 13594 | }; |
| 13595 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13596 | static PyType_Slot ScandirIteratorType_slots[] = { |
| 13597 | {Py_tp_new, _disabled_new}, |
| 13598 | {Py_tp_dealloc, ScandirIterator_dealloc}, |
| 13599 | {Py_tp_finalize, ScandirIterator_finalize}, |
| 13600 | {Py_tp_iter, PyObject_SelfIter}, |
| 13601 | {Py_tp_iternext, ScandirIterator_iternext}, |
| 13602 | {Py_tp_methods, ScandirIterator_methods}, |
| 13603 | {0, 0}, |
| 13604 | }; |
| 13605 | |
| 13606 | static PyType_Spec ScandirIteratorType_spec = { |
| 13607 | MODNAME ".ScandirIterator", |
| 13608 | sizeof(ScandirIterator), |
| 13609 | 0, |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13610 | // bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since |
| 13611 | // PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance. |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13612 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 13613 | ScandirIteratorType_slots |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13614 | }; |
| 13615 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13616 | /*[clinic input] |
| 13617 | os.scandir |
| 13618 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13619 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13620 | |
| 13621 | Return an iterator of DirEntry objects for given path. |
| 13622 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13623 | 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] | 13624 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 13625 | all other circumstances they will be str. |
| 13626 | |
| 13627 | If path is None, uses the path='.'. |
| 13628 | [clinic start generated code]*/ |
| 13629 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13630 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13631 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13632 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13633 | { |
| 13634 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13635 | #ifdef MS_WINDOWS |
| 13636 | wchar_t *path_strW; |
| 13637 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13638 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13639 | #ifdef HAVE_FDOPENDIR |
| 13640 | int fd = -1; |
| 13641 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13642 | #endif |
| 13643 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 13644 | if (PySys_Audit("os.scandir", "O", |
| 13645 | path->object ? path->object : Py_None) < 0) { |
| 13646 | return NULL; |
| 13647 | } |
| 13648 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 13649 | PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13650 | iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13651 | if (!iterator) |
| 13652 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13653 | |
| 13654 | #ifdef MS_WINDOWS |
| 13655 | iterator->handle = INVALID_HANDLE_VALUE; |
| 13656 | #else |
| 13657 | iterator->dirp = NULL; |
| 13658 | #endif |
| 13659 | |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 13660 | /* Move the ownership to iterator->path */ |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13661 | memcpy(&iterator->path, path, sizeof(path_t)); |
| 13662 | memset(path, 0, sizeof(path_t)); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13663 | |
| 13664 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13665 | iterator->first_time = 1; |
| 13666 | |
| 13667 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 13668 | if (!path_strW) |
| 13669 | goto error; |
| 13670 | |
| 13671 | Py_BEGIN_ALLOW_THREADS |
| 13672 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 13673 | Py_END_ALLOW_THREADS |
| 13674 | |
| 13675 | PyMem_Free(path_strW); |
| 13676 | |
| 13677 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 13678 | path_error(&iterator->path); |
| 13679 | goto error; |
| 13680 | } |
| 13681 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13682 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13683 | #ifdef HAVE_FDOPENDIR |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13684 | if (iterator->path.fd != -1) { |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13685 | /* closedir() closes the FD, so we duplicate it */ |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13686 | fd = _Py_dup(iterator->path.fd); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13687 | if (fd == -1) |
| 13688 | goto error; |
| 13689 | |
| 13690 | Py_BEGIN_ALLOW_THREADS |
| 13691 | iterator->dirp = fdopendir(fd); |
| 13692 | Py_END_ALLOW_THREADS |
| 13693 | } |
| 13694 | else |
| 13695 | #endif |
| 13696 | { |
| 13697 | if (iterator->path.narrow) |
| 13698 | path_str = iterator->path.narrow; |
| 13699 | else |
| 13700 | path_str = "."; |
| 13701 | |
| 13702 | Py_BEGIN_ALLOW_THREADS |
| 13703 | iterator->dirp = opendir(path_str); |
| 13704 | Py_END_ALLOW_THREADS |
| 13705 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13706 | |
| 13707 | if (!iterator->dirp) { |
| 13708 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13709 | #ifdef HAVE_FDOPENDIR |
| 13710 | if (fd != -1) { |
| 13711 | Py_BEGIN_ALLOW_THREADS |
| 13712 | close(fd); |
| 13713 | Py_END_ALLOW_THREADS |
| 13714 | } |
| 13715 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13716 | goto error; |
| 13717 | } |
| 13718 | #endif |
| 13719 | |
| 13720 | return (PyObject *)iterator; |
| 13721 | |
| 13722 | error: |
| 13723 | Py_DECREF(iterator); |
| 13724 | return NULL; |
| 13725 | } |
| 13726 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13727 | /* |
| 13728 | Return the file system path representation of the object. |
| 13729 | |
| 13730 | If the object is str or bytes, then allow it to pass through with |
| 13731 | an incremented refcount. If the object defines __fspath__(), then |
| 13732 | return the result of that method. All other types raise a TypeError. |
| 13733 | */ |
| 13734 | PyObject * |
| 13735 | PyOS_FSPath(PyObject *path) |
| 13736 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13737 | /* For error message reasons, this function is manually inlined in |
| 13738 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13739 | PyObject *func = NULL; |
| 13740 | PyObject *path_repr = NULL; |
| 13741 | |
| 13742 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 13743 | Py_INCREF(path); |
| 13744 | return path; |
| 13745 | } |
| 13746 | |
| 13747 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 13748 | if (NULL == func) { |
| 13749 | return PyErr_Format(PyExc_TypeError, |
| 13750 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13751 | "not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13752 | _PyType_Name(Py_TYPE(path))); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13753 | } |
| 13754 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13755 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13756 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13757 | if (NULL == path_repr) { |
| 13758 | return NULL; |
| 13759 | } |
| 13760 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13761 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 13762 | PyErr_Format(PyExc_TypeError, |
| 13763 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13764 | "not %.200s", _PyType_Name(Py_TYPE(path)), |
| 13765 | _PyType_Name(Py_TYPE(path_repr))); |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13766 | Py_DECREF(path_repr); |
| 13767 | return NULL; |
| 13768 | } |
| 13769 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13770 | return path_repr; |
| 13771 | } |
| 13772 | |
| 13773 | /*[clinic input] |
| 13774 | os.fspath |
| 13775 | |
| 13776 | path: object |
| 13777 | |
| 13778 | Return the file system path representation of the object. |
| 13779 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13780 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 13781 | object defines __fspath__(), then return the result of that method. All other |
| 13782 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13783 | [clinic start generated code]*/ |
| 13784 | |
| 13785 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13786 | os_fspath_impl(PyObject *module, PyObject *path) |
| 13787 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13788 | { |
| 13789 | return PyOS_FSPath(path); |
| 13790 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13791 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13792 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13793 | /*[clinic input] |
| 13794 | os.getrandom |
| 13795 | |
| 13796 | size: Py_ssize_t |
| 13797 | flags: int=0 |
| 13798 | |
| 13799 | Obtain a series of random bytes. |
| 13800 | [clinic start generated code]*/ |
| 13801 | |
| 13802 | static PyObject * |
| 13803 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 13804 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 13805 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13806 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13807 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13808 | |
| 13809 | if (size < 0) { |
| 13810 | errno = EINVAL; |
| 13811 | return posix_error(); |
| 13812 | } |
| 13813 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13814 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13815 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13816 | PyErr_NoMemory(); |
| 13817 | return NULL; |
| 13818 | } |
| 13819 | |
| 13820 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13821 | n = syscall(SYS_getrandom, |
| 13822 | PyBytes_AS_STRING(bytes), |
| 13823 | PyBytes_GET_SIZE(bytes), |
| 13824 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13825 | if (n < 0 && errno == EINTR) { |
| 13826 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13827 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13828 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13829 | |
| 13830 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13831 | continue; |
| 13832 | } |
| 13833 | break; |
| 13834 | } |
| 13835 | |
| 13836 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13837 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13838 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13839 | } |
| 13840 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13841 | if (n != size) { |
| 13842 | _PyBytes_Resize(&bytes, n); |
| 13843 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13844 | |
| 13845 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13846 | |
| 13847 | error: |
| 13848 | Py_DECREF(bytes); |
| 13849 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13850 | } |
| 13851 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13852 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13853 | #ifdef MS_WINDOWS |
| 13854 | /* bpo-36085: Helper functions for managing DLL search directories |
| 13855 | * on win32 |
| 13856 | */ |
| 13857 | |
| 13858 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); |
| 13859 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); |
| 13860 | |
| 13861 | /*[clinic input] |
| 13862 | os._add_dll_directory |
| 13863 | |
| 13864 | path: path_t |
| 13865 | |
| 13866 | Add a path to the DLL search path. |
| 13867 | |
| 13868 | This search path is used when resolving dependencies for imported |
| 13869 | extension modules (the module itself is resolved through sys.path), |
| 13870 | and also by ctypes. |
| 13871 | |
| 13872 | Returns an opaque value that may be passed to os.remove_dll_directory |
| 13873 | to remove this directory from the search path. |
| 13874 | [clinic start generated code]*/ |
| 13875 | |
| 13876 | static PyObject * |
| 13877 | os__add_dll_directory_impl(PyObject *module, path_t *path) |
| 13878 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ |
| 13879 | { |
| 13880 | HMODULE hKernel32; |
| 13881 | PAddDllDirectory AddDllDirectory; |
| 13882 | DLL_DIRECTORY_COOKIE cookie = 0; |
| 13883 | DWORD err = 0; |
| 13884 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 13885 | if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) { |
| 13886 | return NULL; |
| 13887 | } |
| 13888 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13889 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13890 | infrequent operation, just do it each time. Kernel32 is always |
| 13891 | loaded. */ |
| 13892 | Py_BEGIN_ALLOW_THREADS |
| 13893 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13894 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( |
| 13895 | hKernel32, "AddDllDirectory")) || |
| 13896 | !(cookie = (*AddDllDirectory)(path->wide))) { |
| 13897 | err = GetLastError(); |
| 13898 | } |
| 13899 | Py_END_ALLOW_THREADS |
| 13900 | |
| 13901 | if (err) { |
| 13902 | return win32_error_object_err("add_dll_directory", |
| 13903 | path->object, err); |
| 13904 | } |
| 13905 | |
| 13906 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); |
| 13907 | } |
| 13908 | |
| 13909 | /*[clinic input] |
| 13910 | os._remove_dll_directory |
| 13911 | |
| 13912 | cookie: object |
| 13913 | |
| 13914 | Removes a path from the DLL search path. |
| 13915 | |
| 13916 | The parameter is an opaque value that was returned from |
| 13917 | os.add_dll_directory. You can only remove directories that you added |
| 13918 | yourself. |
| 13919 | [clinic start generated code]*/ |
| 13920 | |
| 13921 | static PyObject * |
| 13922 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) |
| 13923 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ |
| 13924 | { |
| 13925 | HMODULE hKernel32; |
| 13926 | PRemoveDllDirectory RemoveDllDirectory; |
| 13927 | DLL_DIRECTORY_COOKIE cookieValue; |
| 13928 | DWORD err = 0; |
| 13929 | |
| 13930 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { |
| 13931 | PyErr_SetString(PyExc_TypeError, |
| 13932 | "Provided cookie was not returned from os.add_dll_directory"); |
| 13933 | return NULL; |
| 13934 | } |
| 13935 | |
| 13936 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( |
| 13937 | cookie, "DLL directory cookie"); |
| 13938 | |
| 13939 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13940 | infrequent operation, just do it each time. Kernel32 is always |
| 13941 | loaded. */ |
| 13942 | Py_BEGIN_ALLOW_THREADS |
| 13943 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13944 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( |
| 13945 | hKernel32, "RemoveDllDirectory")) || |
| 13946 | !(*RemoveDllDirectory)(cookieValue)) { |
| 13947 | err = GetLastError(); |
| 13948 | } |
| 13949 | Py_END_ALLOW_THREADS |
| 13950 | |
| 13951 | if (err) { |
| 13952 | return win32_error_object_err("remove_dll_directory", |
| 13953 | NULL, err); |
| 13954 | } |
| 13955 | |
| 13956 | if (PyCapsule_SetName(cookie, NULL)) { |
| 13957 | return NULL; |
| 13958 | } |
| 13959 | |
| 13960 | Py_RETURN_NONE; |
| 13961 | } |
| 13962 | |
| 13963 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13964 | |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13965 | |
| 13966 | /* Only check if WIFEXITED is available: expect that it comes |
| 13967 | with WEXITSTATUS, WIFSIGNALED, etc. |
| 13968 | |
| 13969 | os.waitstatus_to_exitcode() is implemented in C and not in Python, so |
| 13970 | subprocess can safely call it during late Python finalization without |
| 13971 | risking that used os attributes were set to None by _PyImport_Cleanup(). */ |
| 13972 | #if defined(WIFEXITED) || defined(MS_WINDOWS) |
| 13973 | /*[clinic input] |
| 13974 | os.waitstatus_to_exitcode |
| 13975 | |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13976 | status as status_obj: object |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13977 | |
| 13978 | Convert a wait status to an exit code. |
| 13979 | |
| 13980 | On Unix: |
| 13981 | |
| 13982 | * If WIFEXITED(status) is true, return WEXITSTATUS(status). |
| 13983 | * If WIFSIGNALED(status) is true, return -WTERMSIG(status). |
| 13984 | * Otherwise, raise a ValueError. |
| 13985 | |
| 13986 | On Windows, return status shifted right by 8 bits. |
| 13987 | |
| 13988 | On Unix, if the process is being traced or if waitpid() was called with |
| 13989 | WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true. |
| 13990 | This function must not be called if WIFSTOPPED(status) is true. |
| 13991 | [clinic start generated code]*/ |
| 13992 | |
| 13993 | static PyObject * |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13994 | os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj) |
| 13995 | /*[clinic end generated code: output=db50b1b0ba3c7153 input=7fe2d7fdaea3db42]*/ |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13996 | { |
| 13997 | #ifndef MS_WINDOWS |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13998 | int status = _PyLong_AsInt(status_obj); |
| 13999 | if (status == -1 && PyErr_Occurred()) { |
| 14000 | return NULL; |
| 14001 | } |
| 14002 | |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14003 | WAIT_TYPE wait_status; |
| 14004 | WAIT_STATUS_INT(wait_status) = status; |
| 14005 | int exitcode; |
| 14006 | if (WIFEXITED(wait_status)) { |
| 14007 | exitcode = WEXITSTATUS(wait_status); |
| 14008 | /* Sanity check to provide warranty on the function behavior. |
| 14009 | It should not occur in practice */ |
| 14010 | if (exitcode < 0) { |
| 14011 | PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode); |
| 14012 | return NULL; |
| 14013 | } |
| 14014 | } |
| 14015 | else if (WIFSIGNALED(wait_status)) { |
| 14016 | int signum = WTERMSIG(wait_status); |
| 14017 | /* Sanity check to provide warranty on the function behavior. |
| 14018 | It should not occurs in practice */ |
| 14019 | if (signum <= 0) { |
| 14020 | PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum); |
| 14021 | return NULL; |
| 14022 | } |
| 14023 | exitcode = -signum; |
| 14024 | } else if (WIFSTOPPED(wait_status)) { |
| 14025 | /* Status only received if the process is being traced |
| 14026 | or if waitpid() was called with WUNTRACED option. */ |
| 14027 | int signum = WSTOPSIG(wait_status); |
| 14028 | PyErr_Format(PyExc_ValueError, |
| 14029 | "process stopped by delivery of signal %i", |
| 14030 | signum); |
| 14031 | return NULL; |
| 14032 | } |
| 14033 | else { |
| 14034 | PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status); |
| 14035 | return NULL; |
| 14036 | } |
| 14037 | return PyLong_FromLong(exitcode); |
| 14038 | #else |
| 14039 | /* Windows implementation: see os.waitpid() implementation |
| 14040 | which uses _cwait(). */ |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 14041 | unsigned long long status = PyLong_AsUnsignedLongLong(status_obj); |
| 14042 | if (status == (unsigned long long)-1 && PyErr_Occurred()) { |
| 14043 | return NULL; |
| 14044 | } |
| 14045 | |
| 14046 | unsigned long long exitcode = (status >> 8); |
| 14047 | /* ExitProcess() accepts an UINT type: |
| 14048 | reject exit code which doesn't fit in an UINT */ |
| 14049 | if (exitcode > UINT_MAX) { |
| 14050 | PyErr_Format(PyExc_ValueError, "invalid exit code: %llu", exitcode); |
| 14051 | return NULL; |
| 14052 | } |
| 14053 | return PyLong_FromUnsignedLong((unsigned long)exitcode); |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14054 | #endif |
| 14055 | } |
| 14056 | #endif |
| 14057 | |
| 14058 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14059 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 14060 | |
| 14061 | OS_STAT_METHODDEF |
| 14062 | OS_ACCESS_METHODDEF |
| 14063 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14064 | OS_CHDIR_METHODDEF |
| 14065 | OS_CHFLAGS_METHODDEF |
| 14066 | OS_CHMOD_METHODDEF |
| 14067 | OS_FCHMOD_METHODDEF |
| 14068 | OS_LCHMOD_METHODDEF |
| 14069 | OS_CHOWN_METHODDEF |
| 14070 | OS_FCHOWN_METHODDEF |
| 14071 | OS_LCHOWN_METHODDEF |
| 14072 | OS_LCHFLAGS_METHODDEF |
| 14073 | OS_CHROOT_METHODDEF |
| 14074 | OS_CTERMID_METHODDEF |
| 14075 | OS_GETCWD_METHODDEF |
| 14076 | OS_GETCWDB_METHODDEF |
| 14077 | OS_LINK_METHODDEF |
| 14078 | OS_LISTDIR_METHODDEF |
| 14079 | OS_LSTAT_METHODDEF |
| 14080 | OS_MKDIR_METHODDEF |
| 14081 | OS_NICE_METHODDEF |
| 14082 | OS_GETPRIORITY_METHODDEF |
| 14083 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14084 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 14085 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 14086 | OS_READLINK_METHODDEF |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 14087 | OS_COPY_FILE_RANGE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14088 | OS_RENAME_METHODDEF |
| 14089 | OS_REPLACE_METHODDEF |
| 14090 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14091 | OS_SYMLINK_METHODDEF |
| 14092 | OS_SYSTEM_METHODDEF |
| 14093 | OS_UMASK_METHODDEF |
| 14094 | OS_UNAME_METHODDEF |
| 14095 | OS_UNLINK_METHODDEF |
| 14096 | OS_REMOVE_METHODDEF |
| 14097 | OS_UTIME_METHODDEF |
| 14098 | OS_TIMES_METHODDEF |
| 14099 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14100 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14101 | OS_EXECV_METHODDEF |
| 14102 | OS_EXECVE_METHODDEF |
| 14103 | OS_SPAWNV_METHODDEF |
| 14104 | OS_SPAWNVE_METHODDEF |
| 14105 | OS_FORK1_METHODDEF |
| 14106 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 14107 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14108 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 14109 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 14110 | OS_SCHED_GETPARAM_METHODDEF |
| 14111 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 14112 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 14113 | OS_SCHED_SETPARAM_METHODDEF |
| 14114 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 14115 | OS_SCHED_YIELD_METHODDEF |
| 14116 | OS_SCHED_SETAFFINITY_METHODDEF |
| 14117 | OS_SCHED_GETAFFINITY_METHODDEF |
| 14118 | OS_OPENPTY_METHODDEF |
| 14119 | OS_FORKPTY_METHODDEF |
| 14120 | OS_GETEGID_METHODDEF |
| 14121 | OS_GETEUID_METHODDEF |
| 14122 | OS_GETGID_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14123 | OS_GETGROUPLIST_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14124 | OS_GETGROUPS_METHODDEF |
| 14125 | OS_GETPID_METHODDEF |
| 14126 | OS_GETPGRP_METHODDEF |
| 14127 | OS_GETPPID_METHODDEF |
| 14128 | OS_GETUID_METHODDEF |
| 14129 | OS_GETLOGIN_METHODDEF |
| 14130 | OS_KILL_METHODDEF |
| 14131 | OS_KILLPG_METHODDEF |
| 14132 | OS_PLOCK_METHODDEF |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 14133 | OS_STARTFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14134 | OS_SETUID_METHODDEF |
| 14135 | OS_SETEUID_METHODDEF |
| 14136 | OS_SETREUID_METHODDEF |
| 14137 | OS_SETGID_METHODDEF |
| 14138 | OS_SETEGID_METHODDEF |
| 14139 | OS_SETREGID_METHODDEF |
| 14140 | OS_SETGROUPS_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14141 | OS_INITGROUPS_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14142 | OS_GETPGID_METHODDEF |
| 14143 | OS_SETPGRP_METHODDEF |
| 14144 | OS_WAIT_METHODDEF |
| 14145 | OS_WAIT3_METHODDEF |
| 14146 | OS_WAIT4_METHODDEF |
| 14147 | OS_WAITID_METHODDEF |
| 14148 | OS_WAITPID_METHODDEF |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 14149 | OS_PIDFD_OPEN_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14150 | OS_GETSID_METHODDEF |
| 14151 | OS_SETSID_METHODDEF |
| 14152 | OS_SETPGID_METHODDEF |
| 14153 | OS_TCGETPGRP_METHODDEF |
| 14154 | OS_TCSETPGRP_METHODDEF |
| 14155 | OS_OPEN_METHODDEF |
| 14156 | OS_CLOSE_METHODDEF |
| 14157 | OS_CLOSERANGE_METHODDEF |
| 14158 | OS_DEVICE_ENCODING_METHODDEF |
| 14159 | OS_DUP_METHODDEF |
| 14160 | OS_DUP2_METHODDEF |
| 14161 | OS_LOCKF_METHODDEF |
| 14162 | OS_LSEEK_METHODDEF |
| 14163 | OS_READ_METHODDEF |
| 14164 | OS_READV_METHODDEF |
| 14165 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14166 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14167 | OS_WRITE_METHODDEF |
| 14168 | OS_WRITEV_METHODDEF |
| 14169 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14170 | OS_PWRITEV_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14171 | OS_SENDFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14172 | OS_FSTAT_METHODDEF |
| 14173 | OS_ISATTY_METHODDEF |
| 14174 | OS_PIPE_METHODDEF |
| 14175 | OS_PIPE2_METHODDEF |
| 14176 | OS_MKFIFO_METHODDEF |
| 14177 | OS_MKNOD_METHODDEF |
| 14178 | OS_MAJOR_METHODDEF |
| 14179 | OS_MINOR_METHODDEF |
| 14180 | OS_MAKEDEV_METHODDEF |
| 14181 | OS_FTRUNCATE_METHODDEF |
| 14182 | OS_TRUNCATE_METHODDEF |
| 14183 | OS_POSIX_FALLOCATE_METHODDEF |
| 14184 | OS_POSIX_FADVISE_METHODDEF |
| 14185 | OS_PUTENV_METHODDEF |
| 14186 | OS_UNSETENV_METHODDEF |
| 14187 | OS_STRERROR_METHODDEF |
| 14188 | OS_FCHDIR_METHODDEF |
| 14189 | OS_FSYNC_METHODDEF |
| 14190 | OS_SYNC_METHODDEF |
| 14191 | OS_FDATASYNC_METHODDEF |
| 14192 | OS_WCOREDUMP_METHODDEF |
| 14193 | OS_WIFCONTINUED_METHODDEF |
| 14194 | OS_WIFSTOPPED_METHODDEF |
| 14195 | OS_WIFSIGNALED_METHODDEF |
| 14196 | OS_WIFEXITED_METHODDEF |
| 14197 | OS_WEXITSTATUS_METHODDEF |
| 14198 | OS_WTERMSIG_METHODDEF |
| 14199 | OS_WSTOPSIG_METHODDEF |
| 14200 | OS_FSTATVFS_METHODDEF |
| 14201 | OS_STATVFS_METHODDEF |
| 14202 | OS_CONFSTR_METHODDEF |
| 14203 | OS_SYSCONF_METHODDEF |
| 14204 | OS_FPATHCONF_METHODDEF |
| 14205 | OS_PATHCONF_METHODDEF |
| 14206 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 14207 | OS__GETFULLPATHNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14208 | OS__GETDISKUSAGE_METHODDEF |
| 14209 | OS__GETFINALPATHNAME_METHODDEF |
| 14210 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 14211 | OS_GETLOADAVG_METHODDEF |
| 14212 | OS_URANDOM_METHODDEF |
| 14213 | OS_SETRESUID_METHODDEF |
| 14214 | OS_SETRESGID_METHODDEF |
| 14215 | OS_GETRESUID_METHODDEF |
| 14216 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 14217 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14218 | OS_GETXATTR_METHODDEF |
| 14219 | OS_SETXATTR_METHODDEF |
| 14220 | OS_REMOVEXATTR_METHODDEF |
| 14221 | OS_LISTXATTR_METHODDEF |
| 14222 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14223 | OS_GET_TERMINAL_SIZE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14224 | OS_CPU_COUNT_METHODDEF |
| 14225 | OS_GET_INHERITABLE_METHODDEF |
| 14226 | OS_SET_INHERITABLE_METHODDEF |
| 14227 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 14228 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 14229 | OS_GET_BLOCKING_METHODDEF |
| 14230 | OS_SET_BLOCKING_METHODDEF |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 14231 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14232 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14233 | OS_GETRANDOM_METHODDEF |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14234 | OS_MEMFD_CREATE_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14235 | OS__ADD_DLL_DIRECTORY_METHODDEF |
| 14236 | OS__REMOVE_DLL_DIRECTORY_METHODDEF |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14237 | OS_WAITSTATUS_TO_EXITCODE_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14238 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14239 | }; |
| 14240 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14241 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14242 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14243 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14244 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14245 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14246 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14247 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14248 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14249 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14250 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14251 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14252 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14253 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14254 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14255 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14256 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14257 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14258 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14259 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14260 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14261 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14262 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14263 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14264 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14265 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14266 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14267 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14268 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14269 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14270 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14271 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14272 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14273 | #endif |
| 14274 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14275 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14276 | #endif |
| 14277 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14278 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14279 | #endif |
| 14280 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14281 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14282 | #endif |
| 14283 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14284 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14285 | #endif |
| 14286 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14287 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14288 | #endif |
| 14289 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14290 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14291 | #endif |
| 14292 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14293 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14294 | #endif |
| 14295 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14296 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14297 | #endif |
| 14298 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14299 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14300 | #endif |
| 14301 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14302 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14303 | #endif |
| 14304 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14305 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14306 | #endif |
| 14307 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14308 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14309 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14310 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14311 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14312 | #endif |
| 14313 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14314 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14315 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14316 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14317 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14318 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14319 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14320 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14321 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14322 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14323 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14324 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14325 | #endif |
| 14326 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14327 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14328 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14329 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14330 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14331 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14332 | #endif |
| 14333 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14334 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14335 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14336 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14337 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14338 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14339 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14340 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14341 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 14342 | #ifdef O_TMPFILE |
| 14343 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 14344 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14345 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14346 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14347 | #endif |
| 14348 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14349 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14350 | #endif |
| 14351 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14352 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14353 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14354 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14355 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14356 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14357 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14358 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14359 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14360 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14361 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14362 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14363 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14364 | #endif |
| 14365 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14366 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14367 | #endif |
| 14368 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14369 | /* MS Windows */ |
| 14370 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14371 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14372 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14373 | #endif |
| 14374 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14375 | /* Optimize for short life (keep in memory). */ |
| 14376 | /* 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] | 14377 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14378 | #endif |
| 14379 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14380 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14381 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14382 | #endif |
| 14383 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14384 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14385 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14386 | #endif |
| 14387 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14388 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14389 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14390 | #endif |
| 14391 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14392 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14393 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14394 | /* Send a SIGIO signal whenever input or output |
| 14395 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14396 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14397 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14398 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14399 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14400 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14401 | #endif |
| 14402 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14403 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14404 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14405 | #endif |
| 14406 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14407 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14408 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14409 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14410 | #ifdef O_NOLINKS |
| 14411 | /* 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] | 14412 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14413 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14414 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14415 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14416 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14417 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 14418 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14419 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14420 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14421 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14422 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14423 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14424 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14425 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14426 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14427 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14428 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14429 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14430 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14431 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14432 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14433 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14434 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14435 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14436 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14437 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14438 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14439 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14440 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14441 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14442 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14443 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14444 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14445 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14446 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14447 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14448 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14449 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14450 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14451 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14452 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14453 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14454 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14455 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14456 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14457 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14458 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14459 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14460 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14461 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14462 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14463 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14464 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14465 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14466 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14467 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14468 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14469 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14470 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14471 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 14472 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14473 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14474 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14475 | #endif /* ST_RDONLY */ |
| 14476 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14477 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14478 | #endif /* ST_NOSUID */ |
| 14479 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 14480 | /* GNU extensions */ |
| 14481 | #ifdef ST_NODEV |
| 14482 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 14483 | #endif /* ST_NODEV */ |
| 14484 | #ifdef ST_NOEXEC |
| 14485 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 14486 | #endif /* ST_NOEXEC */ |
| 14487 | #ifdef ST_SYNCHRONOUS |
| 14488 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 14489 | #endif /* ST_SYNCHRONOUS */ |
| 14490 | #ifdef ST_MANDLOCK |
| 14491 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 14492 | #endif /* ST_MANDLOCK */ |
| 14493 | #ifdef ST_WRITE |
| 14494 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 14495 | #endif /* ST_WRITE */ |
| 14496 | #ifdef ST_APPEND |
| 14497 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 14498 | #endif /* ST_APPEND */ |
| 14499 | #ifdef ST_NOATIME |
| 14500 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 14501 | #endif /* ST_NOATIME */ |
| 14502 | #ifdef ST_NODIRATIME |
| 14503 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 14504 | #endif /* ST_NODIRATIME */ |
| 14505 | #ifdef ST_RELATIME |
| 14506 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 14507 | #endif /* ST_RELATIME */ |
| 14508 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14509 | /* FreeBSD sendfile() constants */ |
| 14510 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14511 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14512 | #endif |
| 14513 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14514 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14515 | #endif |
| 14516 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14517 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14518 | #endif |
| 14519 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14520 | /* constants for posix_fadvise */ |
| 14521 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14522 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14523 | #endif |
| 14524 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14525 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14526 | #endif |
| 14527 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14528 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14529 | #endif |
| 14530 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14531 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14532 | #endif |
| 14533 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14534 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14535 | #endif |
| 14536 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14537 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14538 | #endif |
| 14539 | |
| 14540 | /* constants for waitid */ |
| 14541 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14542 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 14543 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 14544 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 14545 | #ifdef P_PIDFD |
| 14546 | if (PyModule_AddIntMacro(m, P_PIDFD)) return -1; |
| 14547 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14548 | #endif |
| 14549 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14550 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14551 | #endif |
| 14552 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14553 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14554 | #endif |
| 14555 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14556 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14557 | #endif |
| 14558 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14559 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14560 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14561 | #ifdef CLD_KILLED |
| 14562 | if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1; |
| 14563 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14564 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14565 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14566 | #endif |
| 14567 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14568 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14569 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14570 | #ifdef CLD_STOPPED |
| 14571 | if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1; |
| 14572 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14573 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14574 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14575 | #endif |
| 14576 | |
| 14577 | /* constants for lockf */ |
| 14578 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14579 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14580 | #endif |
| 14581 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14582 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14583 | #endif |
| 14584 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14585 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14586 | #endif |
| 14587 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14588 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14589 | #endif |
| 14590 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14591 | #ifdef RWF_DSYNC |
| 14592 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 14593 | #endif |
| 14594 | #ifdef RWF_HIPRI |
| 14595 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 14596 | #endif |
| 14597 | #ifdef RWF_SYNC |
| 14598 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 14599 | #endif |
| 14600 | #ifdef RWF_NOWAIT |
| 14601 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 14602 | #endif |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 14603 | #ifdef RWF_APPEND |
| 14604 | if (PyModule_AddIntConstant(m, "RWF_APPEND", RWF_APPEND)) return -1; |
| 14605 | #endif |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14606 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14607 | /* constants for posix_spawn */ |
| 14608 | #ifdef HAVE_POSIX_SPAWN |
| 14609 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 14610 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 14611 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 14612 | #endif |
| 14613 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14614 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14615 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 14616 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14617 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14618 | #endif |
| 14619 | #ifdef HAVE_SPAWNV |
| 14620 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14621 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 14622 | #endif |
| 14623 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14624 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14625 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14626 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14627 | #endif |
| 14628 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14629 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14630 | #endif |
| 14631 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14632 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14633 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14634 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 14635 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14636 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14637 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14638 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14639 | #endif |
| 14640 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14641 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14642 | #endif |
| 14643 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14644 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14645 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14646 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14647 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14648 | #endif |
| 14649 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14650 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14651 | #endif |
| 14652 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14653 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14654 | #endif |
| 14655 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14656 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14657 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14658 | #endif |
| 14659 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 14660 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14661 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 14662 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 14663 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 14664 | #endif |
| 14665 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14666 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14667 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14668 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14669 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14670 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14671 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14672 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14673 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14674 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14675 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14676 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14677 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14678 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14679 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14680 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14681 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14682 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14683 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14684 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14685 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14686 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 14687 | #if HAVE_DECL_RTLD_MEMBER |
| 14688 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 14689 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14690 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14691 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 14692 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 14693 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 14694 | #endif |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14695 | #ifdef HAVE_MEMFD_CREATE |
| 14696 | if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1; |
| 14697 | if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1; |
| 14698 | #ifdef MFD_HUGETLB |
| 14699 | if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14700 | #endif |
| 14701 | #ifdef MFD_HUGE_SHIFT |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14702 | if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14703 | #endif |
| 14704 | #ifdef MFD_HUGE_MASK |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14705 | if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14706 | #endif |
| 14707 | #ifdef MFD_HUGE_64KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14708 | if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14709 | #endif |
| 14710 | #ifdef MFD_HUGE_512KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14711 | if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14712 | #endif |
| 14713 | #ifdef MFD_HUGE_1MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14714 | if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14715 | #endif |
| 14716 | #ifdef MFD_HUGE_2MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14717 | if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14718 | #endif |
| 14719 | #ifdef MFD_HUGE_8MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14720 | if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14721 | #endif |
| 14722 | #ifdef MFD_HUGE_16MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14723 | if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14724 | #endif |
| 14725 | #ifdef MFD_HUGE_32MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14726 | if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14727 | #endif |
| 14728 | #ifdef MFD_HUGE_256MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14729 | if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14730 | #endif |
| 14731 | #ifdef MFD_HUGE_512MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14732 | if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14733 | #endif |
| 14734 | #ifdef MFD_HUGE_1GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14735 | if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14736 | #endif |
| 14737 | #ifdef MFD_HUGE_2GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14738 | if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14739 | #endif |
| 14740 | #ifdef MFD_HUGE_16GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14741 | if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1; |
| 14742 | #endif |
| 14743 | #endif |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14744 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14745 | #if defined(__APPLE__) |
| 14746 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 14747 | #endif |
| 14748 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14749 | #ifdef MS_WINDOWS |
| 14750 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; |
| 14751 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; |
| 14752 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; |
| 14753 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; |
| 14754 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; |
| 14755 | #endif |
| 14756 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14757 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14758 | } |
| 14759 | |
| 14760 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14761 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14762 | |
| 14763 | #ifdef HAVE_FACCESSAT |
| 14764 | "HAVE_FACCESSAT", |
| 14765 | #endif |
| 14766 | |
| 14767 | #ifdef HAVE_FCHDIR |
| 14768 | "HAVE_FCHDIR", |
| 14769 | #endif |
| 14770 | |
| 14771 | #ifdef HAVE_FCHMOD |
| 14772 | "HAVE_FCHMOD", |
| 14773 | #endif |
| 14774 | |
| 14775 | #ifdef HAVE_FCHMODAT |
| 14776 | "HAVE_FCHMODAT", |
| 14777 | #endif |
| 14778 | |
| 14779 | #ifdef HAVE_FCHOWN |
| 14780 | "HAVE_FCHOWN", |
| 14781 | #endif |
| 14782 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 14783 | #ifdef HAVE_FCHOWNAT |
| 14784 | "HAVE_FCHOWNAT", |
| 14785 | #endif |
| 14786 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14787 | #ifdef HAVE_FEXECVE |
| 14788 | "HAVE_FEXECVE", |
| 14789 | #endif |
| 14790 | |
| 14791 | #ifdef HAVE_FDOPENDIR |
| 14792 | "HAVE_FDOPENDIR", |
| 14793 | #endif |
| 14794 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14795 | #ifdef HAVE_FPATHCONF |
| 14796 | "HAVE_FPATHCONF", |
| 14797 | #endif |
| 14798 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14799 | #ifdef HAVE_FSTATAT |
| 14800 | "HAVE_FSTATAT", |
| 14801 | #endif |
| 14802 | |
| 14803 | #ifdef HAVE_FSTATVFS |
| 14804 | "HAVE_FSTATVFS", |
| 14805 | #endif |
| 14806 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 14807 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14808 | "HAVE_FTRUNCATE", |
| 14809 | #endif |
| 14810 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14811 | #ifdef HAVE_FUTIMENS |
| 14812 | "HAVE_FUTIMENS", |
| 14813 | #endif |
| 14814 | |
| 14815 | #ifdef HAVE_FUTIMES |
| 14816 | "HAVE_FUTIMES", |
| 14817 | #endif |
| 14818 | |
| 14819 | #ifdef HAVE_FUTIMESAT |
| 14820 | "HAVE_FUTIMESAT", |
| 14821 | #endif |
| 14822 | |
| 14823 | #ifdef HAVE_LINKAT |
| 14824 | "HAVE_LINKAT", |
| 14825 | #endif |
| 14826 | |
| 14827 | #ifdef HAVE_LCHFLAGS |
| 14828 | "HAVE_LCHFLAGS", |
| 14829 | #endif |
| 14830 | |
| 14831 | #ifdef HAVE_LCHMOD |
| 14832 | "HAVE_LCHMOD", |
| 14833 | #endif |
| 14834 | |
| 14835 | #ifdef HAVE_LCHOWN |
| 14836 | "HAVE_LCHOWN", |
| 14837 | #endif |
| 14838 | |
| 14839 | #ifdef HAVE_LSTAT |
| 14840 | "HAVE_LSTAT", |
| 14841 | #endif |
| 14842 | |
| 14843 | #ifdef HAVE_LUTIMES |
| 14844 | "HAVE_LUTIMES", |
| 14845 | #endif |
| 14846 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14847 | #ifdef HAVE_MEMFD_CREATE |
| 14848 | "HAVE_MEMFD_CREATE", |
| 14849 | #endif |
| 14850 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14851 | #ifdef HAVE_MKDIRAT |
| 14852 | "HAVE_MKDIRAT", |
| 14853 | #endif |
| 14854 | |
| 14855 | #ifdef HAVE_MKFIFOAT |
| 14856 | "HAVE_MKFIFOAT", |
| 14857 | #endif |
| 14858 | |
| 14859 | #ifdef HAVE_MKNODAT |
| 14860 | "HAVE_MKNODAT", |
| 14861 | #endif |
| 14862 | |
| 14863 | #ifdef HAVE_OPENAT |
| 14864 | "HAVE_OPENAT", |
| 14865 | #endif |
| 14866 | |
| 14867 | #ifdef HAVE_READLINKAT |
| 14868 | "HAVE_READLINKAT", |
| 14869 | #endif |
| 14870 | |
| 14871 | #ifdef HAVE_RENAMEAT |
| 14872 | "HAVE_RENAMEAT", |
| 14873 | #endif |
| 14874 | |
| 14875 | #ifdef HAVE_SYMLINKAT |
| 14876 | "HAVE_SYMLINKAT", |
| 14877 | #endif |
| 14878 | |
| 14879 | #ifdef HAVE_UNLINKAT |
| 14880 | "HAVE_UNLINKAT", |
| 14881 | #endif |
| 14882 | |
| 14883 | #ifdef HAVE_UTIMENSAT |
| 14884 | "HAVE_UTIMENSAT", |
| 14885 | #endif |
| 14886 | |
| 14887 | #ifdef MS_WINDOWS |
| 14888 | "MS_WINDOWS", |
| 14889 | #endif |
| 14890 | |
| 14891 | NULL |
| 14892 | }; |
| 14893 | |
| 14894 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14895 | static int |
| 14896 | posixmodule_exec(PyObject *m) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14897 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14898 | _posixstate *state = get_posix_state(m); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14899 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14900 | /* Initialize environ dictionary */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14901 | PyObject *v = convertenviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14902 | Py_XINCREF(v); |
| 14903 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14904 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14905 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14906 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14907 | if (all_ins(m)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14908 | return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14909 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14910 | if (setup_confname_tables(m)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14911 | return -1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14912 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14913 | Py_INCREF(PyExc_OSError); |
| 14914 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 14915 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14916 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14917 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 14918 | PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc); |
| 14919 | if (WaitidResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14920 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14921 | } |
| 14922 | Py_INCREF(WaitidResultType); |
| 14923 | PyModule_AddObject(m, "waitid_result", WaitidResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14924 | state->WaitidResultType = WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14925 | #endif |
| 14926 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14927 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
| 14928 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 14929 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 14930 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 14931 | PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc); |
| 14932 | if (StatResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14933 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14934 | } |
| 14935 | Py_INCREF(StatResultType); |
| 14936 | PyModule_AddObject(m, "stat_result", StatResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14937 | state->StatResultType = StatResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14938 | structseq_new = ((PyTypeObject *)StatResultType)->tp_new; |
| 14939 | ((PyTypeObject *)StatResultType)->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14940 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14941 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
| 14942 | PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc); |
| 14943 | if (StatVFSResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14944 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14945 | } |
| 14946 | Py_INCREF(StatVFSResultType); |
| 14947 | PyModule_AddObject(m, "statvfs_result", StatVFSResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14948 | state->StatVFSResultType = StatVFSResultType; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14949 | #ifdef NEED_TICKS_PER_SECOND |
| 14950 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14951 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14952 | # elif defined(HZ) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14953 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14954 | # else |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14955 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14956 | # endif |
| 14957 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14958 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14959 | #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] | 14960 | sched_param_desc.name = MODNAME ".sched_param"; |
| 14961 | PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc); |
| 14962 | if (SchedParamType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14963 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14964 | } |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14965 | Py_INCREF(SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14966 | PyModule_AddObject(m, "sched_param", SchedParamType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14967 | state->SchedParamType = SchedParamType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14968 | ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param; |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14969 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14970 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14971 | /* initialize TerminalSize_info */ |
| 14972 | PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc); |
| 14973 | if (TerminalSizeType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14974 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14975 | } |
| 14976 | Py_INCREF(TerminalSizeType); |
| 14977 | PyModule_AddObject(m, "terminal_size", TerminalSizeType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14978 | state->TerminalSizeType = TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14979 | |
| 14980 | /* initialize scandir types */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14981 | PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14982 | if (ScandirIteratorType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14983 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14984 | } |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14985 | state->ScandirIteratorType = ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14986 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14987 | PyObject *DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14988 | if (DirEntryType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14989 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14990 | } |
| 14991 | Py_INCREF(DirEntryType); |
| 14992 | PyModule_AddObject(m, "DirEntry", DirEntryType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14993 | state->DirEntryType = DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14994 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14995 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14996 | PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(×_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14997 | if (TimesResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14998 | return -1; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14999 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15000 | Py_INCREF(TimesResultType); |
| 15001 | PyModule_AddObject(m, "times_result", TimesResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15002 | state->TimesResultType = TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 15003 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15004 | PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 15005 | if (UnameResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15006 | return -1; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 15007 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15008 | Py_INCREF(UnameResultType); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 15009 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15010 | state->UnameResultType = (PyObject *)UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 15011 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15012 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15013 | /* |
| 15014 | * Step 2 of weak-linking support on Mac OS X. |
| 15015 | * |
| 15016 | * The code below removes functions that are not available on the |
| 15017 | * currently active platform. |
| 15018 | * |
| 15019 | * 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] | 15020 | * 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] | 15021 | * OSX 10.4. |
| 15022 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15023 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15024 | if (fstatvfs == NULL) { |
| 15025 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15026 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15027 | } |
| 15028 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15029 | #endif /* HAVE_FSTATVFS */ |
| 15030 | |
| 15031 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15032 | if (statvfs == NULL) { |
| 15033 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15034 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15035 | } |
| 15036 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15037 | #endif /* HAVE_STATVFS */ |
| 15038 | |
| 15039 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15040 | if (lchown == NULL) { |
| 15041 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15042 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15043 | } |
| 15044 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15045 | #endif /* HAVE_LCHOWN */ |
| 15046 | |
| 15047 | |
| 15048 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 15049 | |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15050 | if ((state->billion = PyLong_FromLong(1000000000)) == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15051 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15052 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15053 | state->struct_rusage = PyUnicode_InternFromString("struct_rusage"); |
| 15054 | if (state->struct_rusage == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15055 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15056 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15057 | state->st_mode = PyUnicode_InternFromString("st_mode"); |
| 15058 | if (state->st_mode == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15059 | return -1; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 15060 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15061 | /* suppress "function not used" warnings */ |
| 15062 | { |
| 15063 | int ignored; |
| 15064 | fd_specified("", -1); |
| 15065 | follow_symlinks_specified("", 1); |
| 15066 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 15067 | dir_fd_converter(Py_None, &ignored); |
| 15068 | dir_fd_unavailable(Py_None, &ignored); |
| 15069 | } |
| 15070 | |
| 15071 | /* |
| 15072 | * provide list of locally available functions |
| 15073 | * so os.py can populate support_* lists |
| 15074 | */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15075 | PyObject *list = PyList_New(0); |
| 15076 | if (!list) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15077 | return -1; |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15078 | } |
| 15079 | for (const char * const *trace = have_functions; *trace; trace++) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15080 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 15081 | if (!unicode) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15082 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15083 | if (PyList_Append(list, unicode)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15084 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15085 | Py_DECREF(unicode); |
| 15086 | } |
| 15087 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 15088 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15089 | return 0; |
| 15090 | } |
| 15091 | |
| 15092 | |
| 15093 | static PyModuleDef_Slot posixmodile_slots[] = { |
| 15094 | {Py_mod_exec, posixmodule_exec}, |
| 15095 | {0, NULL} |
| 15096 | }; |
| 15097 | |
| 15098 | static struct PyModuleDef posixmodule = { |
| 15099 | PyModuleDef_HEAD_INIT, |
| 15100 | .m_name = MODNAME, |
| 15101 | .m_doc = posix__doc__, |
| 15102 | .m_size = sizeof(_posixstate), |
| 15103 | .m_methods = posix_methods, |
| 15104 | .m_slots = posixmodile_slots, |
| 15105 | .m_traverse = _posix_traverse, |
| 15106 | .m_clear = _posix_clear, |
| 15107 | .m_free = _posix_free, |
| 15108 | }; |
| 15109 | |
| 15110 | PyMODINIT_FUNC |
| 15111 | INITFUNC(void) |
| 15112 | { |
| 15113 | return PyModuleDef_Init(&posixmodule); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 15114 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15115 | |
| 15116 | #ifdef __cplusplus |
| 15117 | } |
| 15118 | #endif |