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 | |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 35 | #include "pycore_ceval.h" // _PyEval_ReInitThreads() |
| 36 | #include "pycore_import.h" // _PyImport_ReInitLock() |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 37 | #include "pycore_initconfig.h" // _PyStatus_EXCEPTION() |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 38 | #include "pycore_pystate.h" // _PyInterpreterState_GET() |
| 39 | #include "structmember.h" // PyMemberDef |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 40 | #ifndef MS_WINDOWS |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 41 | # include "posixmodule.h" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 42 | #else |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 43 | # include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 44 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 45 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 46 | /* On android API level 21, 'AT_EACCESS' is not declared although |
| 47 | * HAVE_FACCESSAT is defined. */ |
| 48 | #ifdef __ANDROID__ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 49 | # undef HAVE_FACCESSAT |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 50 | #endif |
| 51 | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | fa76eee | 2016-05-28 21:03:48 +0000 | [diff] [blame] | 52 | #include <stdio.h> /* needed for ctermid() */ |
| 53 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 54 | #ifdef __cplusplus |
| 55 | extern "C" { |
| 56 | #endif |
| 57 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 58 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 59 | "This module provides access to operating system functionality that is\n\ |
| 60 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 61 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 62 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 63 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 64 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 65 | #ifdef HAVE_SYS_UIO_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 66 | # include <sys/uio.h> |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 67 | #endif |
| 68 | |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 69 | #ifdef HAVE_SYS_SYSMACROS_H |
| 70 | /* GNU C Library: major(), minor(), makedev() */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 71 | # include <sys/sysmacros.h> |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 72 | #endif |
| 73 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SYS_TYPES_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 75 | # include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | #endif /* HAVE_SYS_TYPES_H */ |
| 77 | |
| 78 | #ifdef HAVE_SYS_STAT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 79 | # include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 82 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 83 | # include <sys/wait.h> // WNOHANG |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 84 | #endif |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 85 | #ifdef HAVE_LINUX_WAIT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 86 | # include <linux/wait.h> // P_PIDFD |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 87 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 88 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 89 | #ifdef HAVE_SIGNAL_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 90 | # include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 91 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 93 | #ifdef HAVE_FCNTL_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 94 | # include <fcntl.h> |
| 95 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 97 | #ifdef HAVE_GRP_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 98 | # include <grp.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 99 | #endif |
| 100 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 101 | #ifdef HAVE_SYSEXITS_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 102 | # include <sysexits.h> |
| 103 | #endif |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 104 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 105 | #ifdef HAVE_SYS_LOADAVG_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 106 | # include <sys/loadavg.h> |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 107 | #endif |
| 108 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 109 | #ifdef HAVE_SYS_SENDFILE_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 110 | # include <sys/sendfile.h> |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 111 | #endif |
| 112 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 113 | #if defined(__APPLE__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 114 | # include <copyfile.h> |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 115 | #endif |
| 116 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 117 | #ifdef HAVE_SCHED_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 118 | # include <sched.h> |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 119 | #endif |
| 120 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 121 | #ifdef HAVE_COPY_FILE_RANGE |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 122 | # include <unistd.h> |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 123 | #endif |
| 124 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 125 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 126 | # undef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 127 | #endif |
| 128 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 129 | #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] | 130 | # define USE_XATTRS |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 131 | #endif |
| 132 | |
| 133 | #ifdef USE_XATTRS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 134 | # include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 135 | #endif |
| 136 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 137 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 138 | # ifdef HAVE_SYS_SOCKET_H |
| 139 | # include <sys/socket.h> |
| 140 | # endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 141 | #endif |
| 142 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 143 | #ifdef HAVE_DLFCN_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 144 | # include <dlfcn.h> |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 145 | #endif |
| 146 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 147 | #ifdef __hpux |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 148 | # include <sys/mpctl.h> |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 149 | #endif |
| 150 | |
| 151 | #if defined(__DragonFly__) || \ |
| 152 | defined(__OpenBSD__) || \ |
| 153 | defined(__FreeBSD__) || \ |
| 154 | defined(__NetBSD__) || \ |
| 155 | defined(__APPLE__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 156 | # include <sys/sysctl.h> |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 157 | #endif |
| 158 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 159 | #ifdef HAVE_LINUX_RANDOM_H |
| 160 | # include <linux/random.h> |
| 161 | #endif |
| 162 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 163 | # include <sys/syscall.h> |
| 164 | #endif |
| 165 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 166 | #if defined(MS_WINDOWS) |
| 167 | # define TERMSIZE_USE_CONIO |
| 168 | #elif defined(HAVE_SYS_IOCTL_H) |
| 169 | # include <sys/ioctl.h> |
| 170 | # if defined(HAVE_TERMIOS_H) |
| 171 | # include <termios.h> |
| 172 | # endif |
| 173 | # if defined(TIOCGWINSZ) |
| 174 | # define TERMSIZE_USE_IOCTL |
| 175 | # endif |
| 176 | #endif /* MS_WINDOWS */ |
| 177 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 178 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 179 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 180 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 181 | # define HAVE_OPENDIR 1 |
| 182 | # define HAVE_SYSTEM 1 |
| 183 | # include <process.h> |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 184 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 185 | # ifdef _MSC_VER |
| 186 | /* Microsoft compiler */ |
| 187 | # define HAVE_GETPPID 1 |
| 188 | # define HAVE_GETLOGIN 1 |
| 189 | # define HAVE_SPAWNV 1 |
| 190 | # define HAVE_EXECV 1 |
| 191 | # define HAVE_WSPAWNV 1 |
| 192 | # define HAVE_WEXECV 1 |
| 193 | # define HAVE_PIPE 1 |
| 194 | # define HAVE_SYSTEM 1 |
| 195 | # define HAVE_CWAIT 1 |
| 196 | # define HAVE_FSYNC 1 |
| 197 | # define fsync _commit |
| 198 | # else |
| 199 | /* Unix functions that the configure script doesn't check for */ |
| 200 | # ifndef __VXWORKS__ |
| 201 | # define HAVE_EXECV 1 |
| 202 | # define HAVE_FORK 1 |
| 203 | # if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 204 | # define HAVE_FORK1 1 |
| 205 | # endif |
| 206 | # endif |
| 207 | # define HAVE_GETEGID 1 |
| 208 | # define HAVE_GETEUID 1 |
| 209 | # define HAVE_GETGID 1 |
| 210 | # define HAVE_GETPPID 1 |
| 211 | # define HAVE_GETUID 1 |
| 212 | # define HAVE_KILL 1 |
| 213 | # define HAVE_OPENDIR 1 |
| 214 | # define HAVE_PIPE 1 |
| 215 | # define HAVE_SYSTEM 1 |
| 216 | # define HAVE_WAIT 1 |
| 217 | # define HAVE_TTYNAME 1 |
| 218 | # endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 219 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 220 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 221 | _Py_IDENTIFIER(__fspath__); |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 222 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 223 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 224 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 225 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 226 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 227 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 228 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 229 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 230 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 231 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 232 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 233 | (default) */ |
| 234 | extern char *ctermid_r(char *); |
| 235 | #endif |
| 236 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 237 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 238 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 239 | #if defined(__VXWORKS__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 240 | # include <vxCpuLib.h> |
| 241 | # include <rtpLib.h> |
| 242 | # include <wait.h> |
| 243 | # include <taskLib.h> |
| 244 | # ifndef _P_WAIT |
| 245 | # define _P_WAIT 0 |
| 246 | # define _P_NOWAIT 1 |
| 247 | # define _P_NOWAITO 1 |
| 248 | # endif |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 249 | #endif /* __VXWORKS__ */ |
| 250 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 251 | #ifdef HAVE_POSIX_SPAWN |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 252 | # include <spawn.h> |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 253 | #endif |
| 254 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | #ifdef HAVE_UTIME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 256 | # include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 257 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 259 | #ifdef HAVE_SYS_UTIME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 260 | # include <sys/utime.h> |
| 261 | # 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] | 262 | #endif /* HAVE_SYS_UTIME_H */ |
| 263 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 264 | #ifdef HAVE_SYS_TIMES_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 265 | # include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 266 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 267 | |
| 268 | #ifdef HAVE_SYS_PARAM_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 269 | # include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 270 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | |
| 272 | #ifdef HAVE_SYS_UTSNAME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 273 | # include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 274 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 276 | #ifdef HAVE_DIRENT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 277 | # include <dirent.h> |
| 278 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 279 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 280 | # if defined(__WATCOMC__) && !defined(__QNX__) |
| 281 | # include <direct.h> |
| 282 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
| 283 | # else |
| 284 | # define dirent direct |
| 285 | # define NAMLEN(dirent) (dirent)->d_namlen |
| 286 | # endif |
| 287 | # ifdef HAVE_SYS_NDIR_H |
| 288 | # include <sys/ndir.h> |
| 289 | # endif |
| 290 | # ifdef HAVE_SYS_DIR_H |
| 291 | # include <sys/dir.h> |
| 292 | # endif |
| 293 | # ifdef HAVE_NDIR_H |
| 294 | # include <ndir.h> |
| 295 | # endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 296 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 297 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 298 | #ifdef _MSC_VER |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 299 | # ifdef HAVE_DIRECT_H |
| 300 | # include <direct.h> |
| 301 | # endif |
| 302 | # ifdef HAVE_IO_H |
| 303 | # include <io.h> |
| 304 | # endif |
| 305 | # ifdef HAVE_PROCESS_H |
| 306 | # include <process.h> |
| 307 | # endif |
| 308 | # ifndef IO_REPARSE_TAG_SYMLINK |
| 309 | # define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
| 310 | # endif |
| 311 | # ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 312 | # define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 313 | # endif |
| 314 | # include "osdefs.h" // SEP |
| 315 | # include <malloc.h> |
| 316 | # include <windows.h> |
| 317 | # include <shellapi.h> // ShellExecute() |
| 318 | # include <lmcons.h> // UNLEN |
| 319 | # define HAVE_SYMLINK |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 320 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 321 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 322 | #ifndef MAXPATHLEN |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 323 | # if defined(PATH_MAX) && PATH_MAX > 1024 |
| 324 | # define MAXPATHLEN PATH_MAX |
| 325 | # else |
| 326 | # define MAXPATHLEN 1024 |
| 327 | # endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 328 | #endif /* MAXPATHLEN */ |
| 329 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 330 | #ifdef UNION_WAIT |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 331 | /* Emulate some macros on systems that have a union instead of macros */ |
| 332 | # ifndef WIFEXITED |
| 333 | # define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 334 | # endif |
| 335 | # ifndef WEXITSTATUS |
| 336 | # define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 337 | # endif |
| 338 | # ifndef WTERMSIG |
| 339 | # define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 340 | # endif |
| 341 | # define WAIT_TYPE union wait |
| 342 | # define WAIT_STATUS_INT(s) (s.w_status) |
| 343 | #else |
| 344 | /* !UNION_WAIT */ |
| 345 | # define WAIT_TYPE int |
| 346 | # define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 347 | #endif /* UNION_WAIT */ |
| 348 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 349 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 350 | prototype for it, at least on Solaris -- maybe others as well?). */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 351 | #if defined(HAVE_CTERMID_R) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 352 | # define USE_CTERMID_R |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 353 | #endif |
| 354 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 355 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 356 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 357 | #undef FSTAT |
| 358 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 359 | #ifdef MS_WINDOWS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 360 | # define STAT win32_stat |
| 361 | # define LSTAT win32_lstat |
| 362 | # define FSTAT _Py_fstat_noraise |
| 363 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 364 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 365 | # define STAT stat |
| 366 | # define LSTAT lstat |
| 367 | # define FSTAT fstat |
| 368 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 369 | #endif |
| 370 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 371 | #if defined(MAJOR_IN_MKDEV) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 372 | # include <sys/mkdev.h> |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 373 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 374 | # if defined(MAJOR_IN_SYSMACROS) |
| 375 | # include <sys/sysmacros.h> |
| 376 | # endif |
| 377 | # if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 378 | # include <sys/mkdev.h> |
| 379 | # endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 380 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 381 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 382 | #ifdef MS_WINDOWS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 383 | # define INITFUNC PyInit_nt |
| 384 | # define MODNAME "nt" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 385 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 386 | # define INITFUNC PyInit_posix |
| 387 | # define MODNAME "posix" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 388 | #endif |
| 389 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 390 | #if defined(__sun) |
| 391 | /* Something to implement in autoconf, not present in autoconf 2.69 */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 392 | # define HAVE_STRUCT_STAT_ST_FSTYPE 1 |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 393 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 394 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 395 | /* memfd_create is either defined in sys/mman.h or sys/memfd.h |
| 396 | * linux/memfd.h defines additional flags |
| 397 | */ |
| 398 | #ifdef HAVE_SYS_MMAN_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 399 | # include <sys/mman.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 400 | #endif |
| 401 | #ifdef HAVE_SYS_MEMFD_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 402 | # include <sys/memfd.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 403 | #endif |
| 404 | #ifdef HAVE_LINUX_MEMFD_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 405 | # include <linux/memfd.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 406 | #endif |
| 407 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 408 | #ifdef _Py_MEMORY_SANITIZER |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 409 | # include <sanitizer/msan_interface.h> |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 410 | #endif |
| 411 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 412 | #ifdef HAVE_FORK |
| 413 | static void |
| 414 | run_at_forkers(PyObject *lst, int reverse) |
| 415 | { |
| 416 | Py_ssize_t i; |
| 417 | PyObject *cpy; |
| 418 | |
| 419 | if (lst != NULL) { |
| 420 | assert(PyList_CheckExact(lst)); |
| 421 | |
| 422 | /* Use a list copy in case register_at_fork() is called from |
| 423 | * one of the callbacks. |
| 424 | */ |
| 425 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); |
| 426 | if (cpy == NULL) |
| 427 | PyErr_WriteUnraisable(lst); |
| 428 | else { |
| 429 | if (reverse) |
| 430 | PyList_Reverse(cpy); |
| 431 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { |
| 432 | PyObject *func, *res; |
| 433 | func = PyList_GET_ITEM(cpy, i); |
Jeroen Demeyer | 7f41c8e | 2019-07-04 12:35:31 +0200 | [diff] [blame] | 434 | res = _PyObject_CallNoArg(func); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 435 | if (res == NULL) |
| 436 | PyErr_WriteUnraisable(func); |
| 437 | else |
| 438 | Py_DECREF(res); |
| 439 | } |
| 440 | Py_DECREF(cpy); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | void |
| 446 | PyOS_BeforeFork(void) |
| 447 | { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 448 | run_at_forkers(_PyInterpreterState_GET()->before_forkers, 1); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 449 | |
| 450 | _PyImport_AcquireLock(); |
| 451 | } |
| 452 | |
| 453 | void |
| 454 | PyOS_AfterFork_Parent(void) |
| 455 | { |
| 456 | if (_PyImport_ReleaseLock() <= 0) |
| 457 | Py_FatalError("failed releasing import lock after fork"); |
| 458 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 459 | run_at_forkers(_PyInterpreterState_GET()->after_forkers_parent, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | void |
| 463 | PyOS_AfterFork_Child(void) |
| 464 | { |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 465 | PyStatus status; |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 466 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 467 | |
| 468 | status = _PyGILState_Reinit(runtime); |
| 469 | if (_PyStatus_EXCEPTION(status)) { |
| 470 | goto fatal_error; |
| 471 | } |
| 472 | |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 473 | PyThreadState *tstate = _PyThreadState_GET(); |
| 474 | _Py_EnsureTstateNotNULL(tstate); |
| 475 | |
| 476 | status = _PyEval_ReInitThreads(tstate); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 477 | if (_PyStatus_EXCEPTION(status)) { |
| 478 | goto fatal_error; |
| 479 | } |
| 480 | |
| 481 | status = _PyImport_ReInitLock(); |
| 482 | if (_PyStatus_EXCEPTION(status)) { |
| 483 | goto fatal_error; |
| 484 | } |
| 485 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 486 | _PySignal_AfterFork(); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 487 | |
| 488 | status = _PyRuntimeState_ReInitThreads(runtime); |
| 489 | if (_PyStatus_EXCEPTION(status)) { |
| 490 | goto fatal_error; |
| 491 | } |
| 492 | |
| 493 | status = _PyInterpreterState_DeleteExceptMain(runtime); |
| 494 | if (_PyStatus_EXCEPTION(status)) { |
| 495 | goto fatal_error; |
| 496 | } |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 497 | assert(_PyThreadState_GET() == tstate); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 498 | |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 499 | run_at_forkers(tstate->interp->after_forkers_child, 0); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 500 | return; |
| 501 | |
| 502 | fatal_error: |
| 503 | Py_ExitStatusException(status); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static int |
| 507 | register_at_forker(PyObject **lst, PyObject *func) |
| 508 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 509 | if (func == NULL) /* nothing to register? do nothing. */ |
| 510 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 511 | if (*lst == NULL) { |
| 512 | *lst = PyList_New(0); |
| 513 | if (*lst == NULL) |
| 514 | return -1; |
| 515 | } |
| 516 | return PyList_Append(*lst, func); |
| 517 | } |
Victor Stinner | 87255be | 2020-04-07 23:11:49 +0200 | [diff] [blame] | 518 | #endif /* HAVE_FORK */ |
| 519 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 520 | |
| 521 | /* Legacy wrapper */ |
| 522 | void |
| 523 | PyOS_AfterFork(void) |
| 524 | { |
| 525 | #ifdef HAVE_FORK |
| 526 | PyOS_AfterFork_Child(); |
| 527 | #endif |
| 528 | } |
| 529 | |
| 530 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 531 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 532 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 533 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 534 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 535 | ULONG, struct _Py_stat_struct *); |
| 536 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 537 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 538 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 539 | #ifndef MS_WINDOWS |
| 540 | PyObject * |
| 541 | _PyLong_FromUid(uid_t uid) |
| 542 | { |
| 543 | if (uid == (uid_t)-1) |
| 544 | return PyLong_FromLong(-1); |
| 545 | return PyLong_FromUnsignedLong(uid); |
| 546 | } |
| 547 | |
| 548 | PyObject * |
| 549 | _PyLong_FromGid(gid_t gid) |
| 550 | { |
| 551 | if (gid == (gid_t)-1) |
| 552 | return PyLong_FromLong(-1); |
| 553 | return PyLong_FromUnsignedLong(gid); |
| 554 | } |
| 555 | |
| 556 | int |
| 557 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 558 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 559 | uid_t uid; |
| 560 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 561 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 562 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 563 | unsigned long uresult; |
| 564 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 565 | index = _PyNumber_Index(obj); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 566 | if (index == NULL) { |
| 567 | PyErr_Format(PyExc_TypeError, |
| 568 | "uid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 569 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 570 | return 0; |
| 571 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 572 | |
| 573 | /* |
| 574 | * Handling uid_t is complicated for two reasons: |
| 575 | * * Although uid_t is (always?) unsigned, it still |
| 576 | * accepts -1. |
| 577 | * * We don't know its size in advance--it may be |
| 578 | * bigger than an int, or it may be smaller than |
| 579 | * a long. |
| 580 | * |
| 581 | * So a bit of defensive programming is in order. |
| 582 | * Start with interpreting the value passed |
| 583 | * in as a signed long and see if it works. |
| 584 | */ |
| 585 | |
| 586 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 587 | |
| 588 | if (!overflow) { |
| 589 | uid = (uid_t)result; |
| 590 | |
| 591 | if (result == -1) { |
| 592 | if (PyErr_Occurred()) |
| 593 | goto fail; |
| 594 | /* It's a legitimate -1, we're done. */ |
| 595 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 596 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 597 | |
| 598 | /* Any other negative number is disallowed. */ |
| 599 | if (result < 0) |
| 600 | goto underflow; |
| 601 | |
| 602 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 603 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 604 | (long)uid != result) |
| 605 | goto underflow; |
| 606 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 607 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 608 | |
| 609 | if (overflow < 0) |
| 610 | goto underflow; |
| 611 | |
| 612 | /* |
| 613 | * Okay, the value overflowed a signed long. If it |
| 614 | * fits in an *unsigned* long, it may still be okay, |
| 615 | * as uid_t may be unsigned long on this platform. |
| 616 | */ |
| 617 | uresult = PyLong_AsUnsignedLong(index); |
| 618 | if (PyErr_Occurred()) { |
| 619 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 620 | goto overflow; |
| 621 | goto fail; |
| 622 | } |
| 623 | |
| 624 | uid = (uid_t)uresult; |
| 625 | |
| 626 | /* |
| 627 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 628 | * but this value would get interpreted as (uid_t)-1 by chown |
| 629 | * and its siblings. That's not what the user meant! So we |
| 630 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 631 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 632 | */ |
| 633 | if (uid == (uid_t)-1) |
| 634 | goto overflow; |
| 635 | |
| 636 | /* Ensure the value wasn't truncated. */ |
| 637 | if (sizeof(uid_t) < sizeof(long) && |
| 638 | (unsigned long)uid != uresult) |
| 639 | goto overflow; |
| 640 | /* fallthrough */ |
| 641 | |
| 642 | success: |
| 643 | Py_DECREF(index); |
| 644 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 645 | return 1; |
| 646 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 647 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 648 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 649 | "uid is less than minimum"); |
| 650 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 651 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 652 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 653 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 654 | "uid is greater than maximum"); |
| 655 | /* fallthrough */ |
| 656 | |
| 657 | fail: |
| 658 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | int |
| 663 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 664 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 665 | gid_t gid; |
| 666 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 667 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 668 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 669 | unsigned long uresult; |
| 670 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 671 | index = _PyNumber_Index(obj); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 672 | if (index == NULL) { |
| 673 | PyErr_Format(PyExc_TypeError, |
| 674 | "gid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 675 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 676 | return 0; |
| 677 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 678 | |
| 679 | /* |
| 680 | * Handling gid_t is complicated for two reasons: |
| 681 | * * Although gid_t is (always?) unsigned, it still |
| 682 | * accepts -1. |
| 683 | * * We don't know its size in advance--it may be |
| 684 | * bigger than an int, or it may be smaller than |
| 685 | * a long. |
| 686 | * |
| 687 | * So a bit of defensive programming is in order. |
| 688 | * Start with interpreting the value passed |
| 689 | * in as a signed long and see if it works. |
| 690 | */ |
| 691 | |
| 692 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 693 | |
| 694 | if (!overflow) { |
| 695 | gid = (gid_t)result; |
| 696 | |
| 697 | if (result == -1) { |
| 698 | if (PyErr_Occurred()) |
| 699 | goto fail; |
| 700 | /* It's a legitimate -1, we're done. */ |
| 701 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 702 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 703 | |
| 704 | /* Any other negative number is disallowed. */ |
| 705 | if (result < 0) { |
| 706 | goto underflow; |
| 707 | } |
| 708 | |
| 709 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 710 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 711 | (long)gid != result) |
| 712 | goto underflow; |
| 713 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 714 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 715 | |
| 716 | if (overflow < 0) |
| 717 | goto underflow; |
| 718 | |
| 719 | /* |
| 720 | * Okay, the value overflowed a signed long. If it |
| 721 | * fits in an *unsigned* long, it may still be okay, |
| 722 | * as gid_t may be unsigned long on this platform. |
| 723 | */ |
| 724 | uresult = PyLong_AsUnsignedLong(index); |
| 725 | if (PyErr_Occurred()) { |
| 726 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 727 | goto overflow; |
| 728 | goto fail; |
| 729 | } |
| 730 | |
| 731 | gid = (gid_t)uresult; |
| 732 | |
| 733 | /* |
| 734 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 735 | * but this value would get interpreted as (gid_t)-1 by chown |
| 736 | * and its siblings. That's not what the user meant! So we |
| 737 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 738 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 739 | */ |
| 740 | if (gid == (gid_t)-1) |
| 741 | goto overflow; |
| 742 | |
| 743 | /* Ensure the value wasn't truncated. */ |
| 744 | if (sizeof(gid_t) < sizeof(long) && |
| 745 | (unsigned long)gid != uresult) |
| 746 | goto overflow; |
| 747 | /* fallthrough */ |
| 748 | |
| 749 | success: |
| 750 | Py_DECREF(index); |
| 751 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 752 | return 1; |
| 753 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 754 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 755 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 756 | "gid is less than minimum"); |
| 757 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 758 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 759 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 760 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 761 | "gid is greater than maximum"); |
| 762 | /* fallthrough */ |
| 763 | |
| 764 | fail: |
| 765 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 766 | return 0; |
| 767 | } |
| 768 | #endif /* MS_WINDOWS */ |
| 769 | |
| 770 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 771 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 772 | |
| 773 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 774 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 775 | static int |
| 776 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 777 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 778 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 779 | if (PyErr_Occurred()) |
| 780 | return 0; |
| 781 | return 1; |
| 782 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 783 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 784 | |
| 785 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 786 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 787 | /* |
| 788 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 789 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 790 | * which doesn't play nicely with all the initializer lines in this file that |
| 791 | * look like this: |
| 792 | * int dir_fd = DEFAULT_DIR_FD; |
| 793 | */ |
| 794 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 795 | #else |
| 796 | #define DEFAULT_DIR_FD (-100) |
| 797 | #endif |
| 798 | |
| 799 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 800 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 801 | { |
| 802 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 803 | long long_value; |
| 804 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 805 | PyObject *index = _PyNumber_Index(o); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 806 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 807 | return 0; |
| 808 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 809 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 810 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 811 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 812 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 813 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 814 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 815 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 816 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 817 | return 0; |
| 818 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 819 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 820 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 821 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 822 | return 0; |
| 823 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 824 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 825 | *p = (int)long_value; |
| 826 | return 1; |
| 827 | } |
| 828 | |
| 829 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 830 | dir_fd_converter(PyObject *o, void *p) |
| 831 | { |
| 832 | if (o == Py_None) { |
| 833 | *(int *)p = DEFAULT_DIR_FD; |
| 834 | return 1; |
| 835 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 836 | else if (PyIndex_Check(o)) { |
| 837 | return _fd_converter(o, (int *)p); |
| 838 | } |
| 839 | else { |
| 840 | PyErr_Format(PyExc_TypeError, |
| 841 | "argument should be integer or None, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 842 | _PyType_Name(Py_TYPE(o))); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 843 | return 0; |
| 844 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 847 | typedef struct { |
| 848 | PyObject *billion; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 849 | PyObject *DirEntryType; |
| 850 | PyObject *ScandirIteratorType; |
| 851 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 852 | PyObject *SchedParamType; |
| 853 | #endif |
| 854 | PyObject *StatResultType; |
| 855 | PyObject *StatVFSResultType; |
| 856 | PyObject *TerminalSizeType; |
| 857 | PyObject *TimesResultType; |
| 858 | PyObject *UnameResultType; |
| 859 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 860 | PyObject *WaitidResultType; |
| 861 | #endif |
| 862 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 863 | PyObject *struct_rusage; |
| 864 | #endif |
| 865 | PyObject *st_mode; |
| 866 | } _posixstate; |
| 867 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 868 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 869 | static inline _posixstate* |
| 870 | get_posix_state(PyObject *module) |
| 871 | { |
| 872 | void *state = PyModule_GetState(module); |
| 873 | assert(state != NULL); |
| 874 | return (_posixstate *)state; |
| 875 | } |
| 876 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 877 | /* |
| 878 | * A PyArg_ParseTuple "converter" function |
| 879 | * that handles filesystem paths in the manner |
| 880 | * preferred by the os module. |
| 881 | * |
| 882 | * path_converter accepts (Unicode) strings and their |
| 883 | * subclasses, and bytes and their subclasses. What |
| 884 | * it does with the argument depends on the platform: |
| 885 | * |
| 886 | * * On Windows, if we get a (Unicode) string we |
| 887 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 888 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 889 | * |
| 890 | * * On all other platforms, strings are encoded |
| 891 | * to bytes using PyUnicode_FSConverter, then we |
| 892 | * extract the char * from the bytes object and |
| 893 | * return that. |
| 894 | * |
| 895 | * path_converter also optionally accepts signed |
| 896 | * integers (representing open file descriptors) instead |
| 897 | * of path strings. |
| 898 | * |
| 899 | * Input fields: |
| 900 | * path.nullable |
| 901 | * If nonzero, the path is permitted to be None. |
| 902 | * path.allow_fd |
| 903 | * If nonzero, the path is permitted to be a file handle |
| 904 | * (a signed int) instead of a string. |
| 905 | * path.function_name |
| 906 | * If non-NULL, path_converter will use that as the name |
| 907 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 908 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 909 | * path.argument_name |
| 910 | * If non-NULL, path_converter will use that as the name |
| 911 | * of the parameter in error messages. |
| 912 | * (If path.argument_name is NULL it uses "path".) |
| 913 | * |
| 914 | * Output fields: |
| 915 | * path.wide |
| 916 | * Points to the path if it was expressed as Unicode |
| 917 | * and was not encoded. (Only used on Windows.) |
| 918 | * path.narrow |
| 919 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 920 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 921 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 922 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 923 | * path.fd |
| 924 | * Contains a file descriptor if path.accept_fd was true |
| 925 | * and the caller provided a signed integer instead of any |
| 926 | * sort of string. |
| 927 | * |
| 928 | * WARNING: if your "path" parameter is optional, and is |
| 929 | * unspecified, path_converter will never get called. |
| 930 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 931 | * yourself! |
| 932 | * path.length |
| 933 | * The length of the path in characters, if specified as |
| 934 | * a string. |
| 935 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 936 | * The original object passed in (if get a PathLike object, |
| 937 | * the result of PyOS_FSPath() is treated as the original object). |
| 938 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 939 | * path.cleanup |
| 940 | * For internal use only. May point to a temporary object. |
| 941 | * (Pay no attention to the man behind the curtain.) |
| 942 | * |
| 943 | * At most one of path.wide or path.narrow will be non-NULL. |
| 944 | * If path was None and path.nullable was set, |
| 945 | * or if path was an integer and path.allow_fd was set, |
| 946 | * both path.wide and path.narrow will be NULL |
| 947 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 948 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 949 | * path_converter takes care to not write to the path_t |
| 950 | * unless it's successful. However it must reset the |
| 951 | * "cleanup" field each time it's called. |
| 952 | * |
| 953 | * Use as follows: |
| 954 | * path_t path; |
| 955 | * memset(&path, 0, sizeof(path)); |
| 956 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 957 | * // ... use values from path ... |
| 958 | * path_cleanup(&path); |
| 959 | * |
| 960 | * (Note that if PyArg_Parse fails you don't need to call |
| 961 | * path_cleanup(). However it is safe to do so.) |
| 962 | */ |
| 963 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 964 | const char *function_name; |
| 965 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 966 | int nullable; |
| 967 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 968 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 969 | #ifdef MS_WINDOWS |
| 970 | BOOL narrow; |
| 971 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 972 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 973 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 974 | int fd; |
| 975 | Py_ssize_t length; |
| 976 | PyObject *object; |
| 977 | PyObject *cleanup; |
| 978 | } path_t; |
| 979 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 980 | #ifdef MS_WINDOWS |
| 981 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 982 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 983 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 984 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 985 | {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] | 986 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 987 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 988 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 989 | path_cleanup(path_t *path) |
| 990 | { |
| 991 | Py_CLEAR(path->object); |
| 992 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 996 | path_converter(PyObject *o, void *p) |
| 997 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 998 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 999 | PyObject *bytes = NULL; |
| 1000 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1001 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1002 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1003 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1004 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1005 | const wchar_t *wide; |
| 1006 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1007 | |
| 1008 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 1009 | PyErr_Format(exc, "%s%s" fmt, \ |
| 1010 | path->function_name ? path->function_name : "", \ |
| 1011 | path->function_name ? ": " : "", \ |
| 1012 | path->argument_name ? path->argument_name : "path") |
| 1013 | |
| 1014 | /* Py_CLEANUP_SUPPORTED support */ |
| 1015 | if (o == NULL) { |
| 1016 | path_cleanup(path); |
| 1017 | return 1; |
| 1018 | } |
| 1019 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1020 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1021 | path->object = path->cleanup = NULL; |
| 1022 | /* path->object owns a reference to the original object */ |
| 1023 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1024 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1025 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1026 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1027 | #ifdef MS_WINDOWS |
| 1028 | path->narrow = FALSE; |
| 1029 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1030 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1031 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1032 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1033 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1036 | /* Only call this here so that we don't treat the return value of |
| 1037 | os.fspath() as an fd or buffer. */ |
| 1038 | is_index = path->allow_fd && PyIndex_Check(o); |
| 1039 | is_buffer = PyObject_CheckBuffer(o); |
| 1040 | is_bytes = PyBytes_Check(o); |
| 1041 | is_unicode = PyUnicode_Check(o); |
| 1042 | |
| 1043 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 1044 | /* Inline PyOS_FSPath() for better error messages. */ |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1045 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1046 | |
| 1047 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 1048 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1049 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1050 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1051 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1052 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1053 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1054 | goto error_exit; |
| 1055 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1056 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1057 | is_unicode = 1; |
| 1058 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1059 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1060 | is_bytes = 1; |
| 1061 | } |
| 1062 | else { |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1063 | PyErr_Format(PyExc_TypeError, |
| 1064 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1065 | "not %.200s", _PyType_Name(Py_TYPE(o)), |
| 1066 | _PyType_Name(Py_TYPE(res))); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1067 | Py_DECREF(res); |
| 1068 | goto error_exit; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1069 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1070 | |
| 1071 | /* still owns a reference to the original object */ |
| 1072 | Py_DECREF(o); |
| 1073 | o = res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1077 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1078 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1079 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1080 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1081 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1082 | if (length > 32767) { |
| 1083 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1084 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1085 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1086 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1087 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1088 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1089 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1090 | |
| 1091 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1092 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1093 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1094 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1095 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1096 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1097 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1098 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1099 | #endif |
| 1100 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1101 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1102 | bytes = o; |
| 1103 | Py_INCREF(bytes); |
| 1104 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1105 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1106 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1107 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1108 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1109 | "%s%s%s should be %s, not %.200s", |
| 1110 | path->function_name ? path->function_name : "", |
| 1111 | path->function_name ? ": " : "", |
| 1112 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1113 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1114 | "integer or None" : |
| 1115 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1116 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1117 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1118 | _PyType_Name(Py_TYPE(o)))) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1119 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1120 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1121 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1122 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1123 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1124 | } |
| 1125 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1126 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1127 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1128 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1129 | } |
| 1130 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1131 | #ifdef MS_WINDOWS |
| 1132 | path->narrow = FALSE; |
| 1133 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1134 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1135 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1136 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1137 | } |
| 1138 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1139 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1140 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1141 | path->function_name ? path->function_name : "", |
| 1142 | path->function_name ? ": " : "", |
| 1143 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1144 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1145 | "integer or None" : |
| 1146 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1147 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1148 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1149 | _PyType_Name(Py_TYPE(o))); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1150 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1153 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1154 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1155 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1156 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1157 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1160 | #ifdef MS_WINDOWS |
| 1161 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1162 | narrow, |
| 1163 | length |
| 1164 | ); |
| 1165 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1166 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1169 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1170 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1171 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1172 | } |
| 1173 | if (length > 32767) { |
| 1174 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1175 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1176 | } |
| 1177 | if (wcslen(wide) != length) { |
| 1178 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1179 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1180 | } |
| 1181 | path->wide = wide; |
| 1182 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1183 | path->cleanup = wo; |
| 1184 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1185 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1186 | path->wide = NULL; |
| 1187 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1188 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1189 | /* Still a reference owned by path->object, don't have to |
| 1190 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1191 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1192 | } |
| 1193 | else { |
| 1194 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1195 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1196 | #endif |
| 1197 | path->fd = -1; |
| 1198 | |
| 1199 | success_exit: |
| 1200 | path->length = length; |
| 1201 | path->object = o; |
| 1202 | return Py_CLEANUP_SUPPORTED; |
| 1203 | |
| 1204 | error_exit: |
| 1205 | Py_XDECREF(o); |
| 1206 | Py_XDECREF(bytes); |
| 1207 | #ifdef MS_WINDOWS |
| 1208 | Py_XDECREF(wo); |
| 1209 | #endif |
| 1210 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1214 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1215 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1216 | PyErr_Format(PyExc_NotImplementedError, |
| 1217 | "%s%s%s unavailable on this platform", |
| 1218 | (function_name != NULL) ? function_name : "", |
| 1219 | (function_name != NULL) ? ": ": "", |
| 1220 | argument_name); |
| 1221 | } |
| 1222 | |
| 1223 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1224 | dir_fd_unavailable(PyObject *o, void *p) |
| 1225 | { |
| 1226 | int dir_fd; |
| 1227 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1228 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1229 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1230 | argument_unavailable_error(NULL, "dir_fd"); |
| 1231 | return 0; |
| 1232 | } |
| 1233 | *(int *)p = dir_fd; |
| 1234 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1238 | fd_specified(const char *function_name, int fd) |
| 1239 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1240 | if (fd == -1) |
| 1241 | return 0; |
| 1242 | |
| 1243 | argument_unavailable_error(function_name, "fd"); |
| 1244 | return 1; |
| 1245 | } |
| 1246 | |
| 1247 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1248 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1249 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1250 | if (follow_symlinks) |
| 1251 | return 0; |
| 1252 | |
| 1253 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1254 | return 1; |
| 1255 | } |
| 1256 | |
| 1257 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1258 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1259 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1260 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1261 | #ifndef MS_WINDOWS |
| 1262 | && !path->narrow |
| 1263 | #endif |
| 1264 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1265 | PyErr_Format(PyExc_ValueError, |
| 1266 | "%s: can't specify dir_fd without matching path", |
| 1267 | function_name); |
| 1268 | return 1; |
| 1269 | } |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1274 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1275 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1276 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1277 | PyErr_Format(PyExc_ValueError, |
| 1278 | "%s: can't specify both dir_fd and fd", |
| 1279 | function_name); |
| 1280 | return 1; |
| 1281 | } |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1286 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1287 | int follow_symlinks) |
| 1288 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1289 | if ((fd > 0) && (!follow_symlinks)) { |
| 1290 | PyErr_Format(PyExc_ValueError, |
| 1291 | "%s: cannot use fd and follow_symlinks together", |
| 1292 | function_name); |
| 1293 | return 1; |
| 1294 | } |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1299 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1300 | int follow_symlinks) |
| 1301 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1302 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1303 | PyErr_Format(PyExc_ValueError, |
| 1304 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1305 | function_name); |
| 1306 | return 1; |
| 1307 | } |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1311 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1312 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1313 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1314 | typedef off_t Py_off_t; |
| 1315 | #endif |
| 1316 | |
| 1317 | static int |
| 1318 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1319 | { |
| 1320 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1321 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1322 | #else |
| 1323 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1324 | #endif |
| 1325 | if (PyErr_Occurred()) |
| 1326 | return 0; |
| 1327 | return 1; |
| 1328 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1329 | |
| 1330 | static PyObject * |
| 1331 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1332 | { |
| 1333 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1334 | return PyLong_FromLongLong(offset); |
| 1335 | #else |
| 1336 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1337 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1338 | } |
| 1339 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1340 | #ifdef HAVE_SIGSET_T |
| 1341 | /* Convert an iterable of integers to a sigset. |
| 1342 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1343 | int |
| 1344 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1345 | { |
| 1346 | sigset_t *mask = (sigset_t *)addr; |
| 1347 | PyObject *iterator, *item; |
| 1348 | long signum; |
| 1349 | int overflow; |
| 1350 | |
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1351 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
| 1352 | if (sigemptyset(mask) < (0)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1353 | /* Probably only if mask == NULL. */ |
| 1354 | PyErr_SetFromErrno(PyExc_OSError); |
| 1355 | return 0; |
| 1356 | } |
| 1357 | |
| 1358 | iterator = PyObject_GetIter(obj); |
| 1359 | if (iterator == NULL) { |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1364 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1365 | Py_DECREF(item); |
| 1366 | if (signum <= 0 || signum >= NSIG) { |
| 1367 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1368 | PyErr_Format(PyExc_ValueError, |
| 1369 | "signal number %ld out of range", signum); |
| 1370 | } |
| 1371 | goto error; |
| 1372 | } |
| 1373 | if (sigaddset(mask, (int)signum)) { |
| 1374 | if (errno != EINVAL) { |
| 1375 | /* Probably impossible */ |
| 1376 | PyErr_SetFromErrno(PyExc_OSError); |
| 1377 | goto error; |
| 1378 | } |
| 1379 | /* For backwards compatibility, allow idioms such as |
| 1380 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1381 | */ |
| 1382 | const char msg[] = |
| 1383 | "invalid signal number %ld, please use valid_signals()"; |
| 1384 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1385 | goto error; |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | if (!PyErr_Occurred()) { |
| 1390 | Py_DECREF(iterator); |
| 1391 | return 1; |
| 1392 | } |
| 1393 | |
| 1394 | error: |
| 1395 | Py_DECREF(iterator); |
| 1396 | return 0; |
| 1397 | } |
| 1398 | #endif /* HAVE_SIGSET_T */ |
| 1399 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1400 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1401 | |
| 1402 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1403 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1404 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1405 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1406 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1407 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1408 | |
| 1409 | if (0 == DeviceIoControl( |
| 1410 | reparse_point_handle, |
| 1411 | FSCTL_GET_REPARSE_POINT, |
| 1412 | NULL, 0, /* in buffer */ |
| 1413 | target_buffer, sizeof(target_buffer), |
| 1414 | &n_bytes_returned, |
| 1415 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1416 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1417 | |
| 1418 | if (reparse_tag) |
| 1419 | *reparse_tag = rdb->ReparseTag; |
| 1420 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1421 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1422 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1423 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1424 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1425 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1426 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1427 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1428 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1429 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1430 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1431 | */ |
| 1432 | #include <crt_externs.h> |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1433 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1434 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1435 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1436 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1437 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1438 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1439 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1440 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1441 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1442 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1443 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1444 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1445 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1446 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1447 | d = PyDict_New(); |
| 1448 | if (d == NULL) |
| 1449 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1450 | #ifdef MS_WINDOWS |
| 1451 | /* _wenviron must be initialized in this way if the program is started |
| 1452 | through main() instead of wmain(). */ |
| 1453 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1454 | e = _wenviron; |
Benoit Hudson | 723f71a | 2019-12-06 14:15:03 -0500 | [diff] [blame] | 1455 | #elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
| 1456 | /* environ is not accessible as an extern in a shared object on OSX; use |
| 1457 | _NSGetEnviron to resolve it. The value changes if you add environment |
| 1458 | variables between calls to Py_Initialize, so don't cache the value. */ |
| 1459 | e = *_NSGetEnviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1460 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1461 | e = environ; |
| 1462 | #endif |
| 1463 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1464 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1465 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1466 | PyObject *k; |
| 1467 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1468 | #ifdef MS_WINDOWS |
| 1469 | const wchar_t *p = wcschr(*e, L'='); |
| 1470 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1471 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1472 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1473 | if (p == NULL) |
| 1474 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1475 | #ifdef MS_WINDOWS |
| 1476 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1477 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1478 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1479 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1480 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1481 | Py_DECREF(d); |
| 1482 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1483 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1484 | #ifdef MS_WINDOWS |
| 1485 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1486 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1487 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1488 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1489 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1490 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1491 | Py_DECREF(d); |
| 1492 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1493 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1494 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1495 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1496 | Py_DECREF(v); |
| 1497 | Py_DECREF(k); |
| 1498 | Py_DECREF(d); |
| 1499 | return NULL; |
| 1500 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1501 | } |
| 1502 | Py_DECREF(k); |
| 1503 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1504 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1505 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1508 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1509 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1510 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1511 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1512 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1513 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1514 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1515 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1516 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1517 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1518 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1519 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1520 | /* XXX We should pass the function name along in the future. |
| 1521 | (winreg.c also wants to pass the function name.) |
| 1522 | This would however require an additional param to the |
| 1523 | Windows error object, which is non-trivial. |
| 1524 | */ |
| 1525 | errno = GetLastError(); |
| 1526 | if (filename) |
| 1527 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1528 | else |
| 1529 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1530 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1531 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1532 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1533 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1534 | { |
| 1535 | /* XXX - see win32_error for comments on 'function' */ |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1536 | if (filename) |
| 1537 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1538 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1539 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1540 | filename); |
| 1541 | else |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1542 | return PyErr_SetFromWindowsErr(err); |
| 1543 | } |
| 1544 | |
| 1545 | static PyObject * |
| 1546 | win32_error_object(const char* function, PyObject* filename) |
| 1547 | { |
| 1548 | errno = GetLastError(); |
| 1549 | return win32_error_object_err(function, filename, errno); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1550 | } |
| 1551 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1552 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1553 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1554 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1555 | posix_path_object_error(PyObject *path) |
| 1556 | { |
| 1557 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1558 | } |
| 1559 | |
| 1560 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1561 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1562 | { |
| 1563 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1564 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1565 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1566 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1567 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1568 | #endif |
| 1569 | } |
| 1570 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1571 | static PyObject * |
| 1572 | path_object_error2(PyObject *path, PyObject *path2) |
| 1573 | { |
| 1574 | #ifdef MS_WINDOWS |
| 1575 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1576 | PyExc_OSError, 0, path, path2); |
| 1577 | #else |
| 1578 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1579 | #endif |
| 1580 | } |
| 1581 | |
| 1582 | static PyObject * |
| 1583 | path_error(path_t *path) |
| 1584 | { |
| 1585 | return path_object_error(path->object); |
| 1586 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1587 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1588 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1589 | posix_path_error(path_t *path) |
| 1590 | { |
| 1591 | return posix_path_object_error(path->object); |
| 1592 | } |
| 1593 | |
| 1594 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1595 | path_error2(path_t *path, path_t *path2) |
| 1596 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1597 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1601 | /* POSIX generic methods */ |
| 1602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1603 | static int |
| 1604 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1605 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1606 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1607 | int *pointer = (int *)p; |
| 1608 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1609 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1610 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1611 | *pointer = fd; |
| 1612 | return 1; |
| 1613 | } |
| 1614 | |
| 1615 | static PyObject * |
| 1616 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1617 | { |
| 1618 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1619 | int async_err = 0; |
| 1620 | |
| 1621 | do { |
| 1622 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1623 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1624 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1625 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1626 | Py_END_ALLOW_THREADS |
| 1627 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1628 | if (res != 0) |
| 1629 | return (!async_err) ? posix_error() : NULL; |
| 1630 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1631 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1632 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1633 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1634 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1635 | /* This is a reimplementation of the C library's chdir function, |
| 1636 | but one that produces Win32 errors instead of DOS error codes. |
| 1637 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1638 | it also needs to set "magic" environment variables indicating |
| 1639 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1640 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1641 | win32_wchdir(LPCWSTR path) |
| 1642 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1643 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1644 | int result; |
| 1645 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1646 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1647 | if(!SetCurrentDirectoryW(path)) |
| 1648 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1649 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1650 | if (!result) |
| 1651 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1652 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1653 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1654 | if (!new_path) { |
| 1655 | SetLastError(ERROR_OUTOFMEMORY); |
| 1656 | return FALSE; |
| 1657 | } |
| 1658 | result = GetCurrentDirectoryW(result, new_path); |
| 1659 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1660 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1661 | return FALSE; |
| 1662 | } |
| 1663 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1664 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1665 | wcsncmp(new_path, L"//", 2) == 0); |
| 1666 | if (!is_unc_like_path) { |
| 1667 | env[1] = new_path[0]; |
| 1668 | result = SetEnvironmentVariableW(env, new_path); |
| 1669 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1670 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1671 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1672 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1673 | } |
| 1674 | #endif |
| 1675 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1676 | #ifdef MS_WINDOWS |
| 1677 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1678 | - time stamps are restricted to second resolution |
| 1679 | - file modification times suffer from forth-and-back conversions between |
| 1680 | UTC and local time |
| 1681 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1682 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1683 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1684 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1685 | #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1686 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1687 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1688 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1689 | BY_HANDLE_FILE_INFORMATION *info, |
| 1690 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1691 | { |
| 1692 | memset(info, 0, sizeof(*info)); |
| 1693 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1694 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1695 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1696 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1697 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1698 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1699 | /* info->nNumberOfLinks = 1; */ |
| 1700 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1701 | *reparse_tag = pFileData->dwReserved0; |
| 1702 | else |
| 1703 | *reparse_tag = 0; |
| 1704 | } |
| 1705 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1706 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1707 | 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] | 1708 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1709 | HANDLE hFindFile; |
| 1710 | WIN32_FIND_DATAW FileData; |
| 1711 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1712 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1713 | return FALSE; |
| 1714 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1715 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1716 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1719 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1720 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1721 | BOOL traverse) |
| 1722 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1723 | HANDLE hFile; |
| 1724 | BY_HANDLE_FILE_INFORMATION fileInfo; |
| 1725 | FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 }; |
| 1726 | DWORD fileType, error; |
| 1727 | BOOL isUnhandledTag = FALSE; |
| 1728 | int retval = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1729 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1730 | DWORD access = FILE_READ_ATTRIBUTES; |
| 1731 | DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */ |
| 1732 | if (!traverse) { |
| 1733 | flags |= FILE_FLAG_OPEN_REPARSE_POINT; |
| 1734 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1735 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1736 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1737 | if (hFile == INVALID_HANDLE_VALUE) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1738 | /* Either the path doesn't exist, or the caller lacks access. */ |
| 1739 | error = GetLastError(); |
| 1740 | switch (error) { |
| 1741 | case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */ |
| 1742 | case ERROR_SHARING_VIOLATION: /* It's a paging file. */ |
| 1743 | /* Try reading the parent directory. */ |
| 1744 | if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) { |
| 1745 | /* Cannot read the parent directory. */ |
| 1746 | SetLastError(error); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1747 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1748 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1749 | if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1750 | if (traverse || |
| 1751 | !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1752 | /* The stat call has to traverse but cannot, so fail. */ |
| 1753 | SetLastError(error); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1754 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1755 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1756 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1757 | break; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1758 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1759 | case ERROR_INVALID_PARAMETER: |
| 1760 | /* \\.\con requires read or write access. */ |
| 1761 | hFile = CreateFileW(path, access | GENERIC_READ, |
| 1762 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1763 | OPEN_EXISTING, flags, NULL); |
| 1764 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1765 | SetLastError(error); |
| 1766 | return -1; |
| 1767 | } |
| 1768 | break; |
| 1769 | |
| 1770 | case ERROR_CANT_ACCESS_FILE: |
| 1771 | /* bpo37834: open unhandled reparse points if traverse fails. */ |
| 1772 | if (traverse) { |
| 1773 | traverse = FALSE; |
| 1774 | isUnhandledTag = TRUE; |
| 1775 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, |
| 1776 | flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 1777 | } |
| 1778 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1779 | SetLastError(error); |
| 1780 | return -1; |
| 1781 | } |
| 1782 | break; |
| 1783 | |
| 1784 | default: |
| 1785 | return -1; |
| 1786 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1787 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1788 | |
| 1789 | if (hFile != INVALID_HANDLE_VALUE) { |
| 1790 | /* Handle types other than files on disk. */ |
| 1791 | fileType = GetFileType(hFile); |
| 1792 | if (fileType != FILE_TYPE_DISK) { |
| 1793 | if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) { |
| 1794 | retval = -1; |
| 1795 | goto cleanup; |
| 1796 | } |
| 1797 | DWORD fileAttributes = GetFileAttributesW(path); |
| 1798 | memset(result, 0, sizeof(*result)); |
| 1799 | if (fileAttributes != INVALID_FILE_ATTRIBUTES && |
| 1800 | fileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 1801 | /* \\.\pipe\ or \\.\mailslot\ */ |
| 1802 | result->st_mode = _S_IFDIR; |
| 1803 | } else if (fileType == FILE_TYPE_CHAR) { |
| 1804 | /* \\.\nul */ |
| 1805 | result->st_mode = _S_IFCHR; |
| 1806 | } else if (fileType == FILE_TYPE_PIPE) { |
| 1807 | /* \\.\pipe\spam */ |
| 1808 | result->st_mode = _S_IFIFO; |
| 1809 | } |
| 1810 | /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */ |
| 1811 | goto cleanup; |
| 1812 | } |
| 1813 | |
| 1814 | /* Query the reparse tag, and traverse a non-link. */ |
| 1815 | if (!traverse) { |
| 1816 | if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, |
| 1817 | &tagInfo, sizeof(tagInfo))) { |
| 1818 | /* Allow devices that do not support FileAttributeTagInfo. */ |
| 1819 | switch (GetLastError()) { |
| 1820 | case ERROR_INVALID_PARAMETER: |
| 1821 | case ERROR_INVALID_FUNCTION: |
| 1822 | case ERROR_NOT_SUPPORTED: |
| 1823 | tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL; |
| 1824 | tagInfo.ReparseTag = 0; |
| 1825 | break; |
| 1826 | default: |
| 1827 | retval = -1; |
| 1828 | goto cleanup; |
| 1829 | } |
| 1830 | } else if (tagInfo.FileAttributes & |
| 1831 | FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1832 | if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1833 | if (isUnhandledTag) { |
| 1834 | /* Traversing previously failed for either this link |
| 1835 | or its target. */ |
| 1836 | SetLastError(ERROR_CANT_ACCESS_FILE); |
| 1837 | retval = -1; |
| 1838 | goto cleanup; |
| 1839 | } |
| 1840 | /* Traverse a non-link, but not if traversing already failed |
| 1841 | for an unhandled tag. */ |
| 1842 | } else if (!isUnhandledTag) { |
| 1843 | CloseHandle(hFile); |
| 1844 | return win32_xstat_impl(path, result, TRUE); |
| 1845 | } |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | if (!GetFileInformationByHandle(hFile, &fileInfo)) { |
| 1850 | switch (GetLastError()) { |
| 1851 | case ERROR_INVALID_PARAMETER: |
| 1852 | case ERROR_INVALID_FUNCTION: |
| 1853 | case ERROR_NOT_SUPPORTED: |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1854 | /* Volumes and physical disks are block devices, e.g. |
| 1855 | \\.\C: and \\.\PhysicalDrive0. */ |
| 1856 | memset(result, 0, sizeof(*result)); |
| 1857 | result->st_mode = 0x6000; /* S_IFBLK */ |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1858 | goto cleanup; |
| 1859 | } |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1860 | retval = -1; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1861 | goto cleanup; |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result); |
| 1866 | |
| 1867 | if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1868 | /* Fix the file execute permissions. This hack sets S_IEXEC if |
| 1869 | the filename has an extension that is commonly used by files |
| 1870 | that CreateProcessW can execute. A real implementation calls |
| 1871 | GetSecurityInfo, OpenThreadToken/OpenProcessToken, and |
| 1872 | AccessCheck to check for generic read, write, and execute |
| 1873 | access. */ |
| 1874 | const wchar_t *fileExtension = wcsrchr(path, '.'); |
| 1875 | if (fileExtension) { |
| 1876 | if (_wcsicmp(fileExtension, L".exe") == 0 || |
| 1877 | _wcsicmp(fileExtension, L".bat") == 0 || |
| 1878 | _wcsicmp(fileExtension, L".cmd") == 0 || |
| 1879 | _wcsicmp(fileExtension, L".com") == 0) { |
| 1880 | result->st_mode |= 0111; |
| 1881 | } |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | cleanup: |
| 1886 | if (hFile != INVALID_HANDLE_VALUE) { |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1887 | /* Preserve last error if we are failing */ |
| 1888 | error = retval ? GetLastError() : 0; |
| 1889 | if (!CloseHandle(hFile)) { |
| 1890 | retval = -1; |
| 1891 | } else if (retval) { |
| 1892 | /* Restore last error */ |
| 1893 | SetLastError(error); |
| 1894 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | return retval; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1901 | 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] | 1902 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1903 | /* Protocol violation: we explicitly clear errno, instead of |
| 1904 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1905 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1906 | errno = 0; |
| 1907 | return code; |
| 1908 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1909 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1910 | |
| 1911 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1912 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1913 | default does not traverse symlinks and instead returns attributes for |
| 1914 | the symlink. |
| 1915 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1916 | Instead, we will open the file (which *does* traverse symlinks by default) |
| 1917 | and GetFileInformationByHandle(). */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1918 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1919 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1920 | 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] | 1921 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1922 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1925 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1926 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1927 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1928 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1931 | #endif /* MS_WINDOWS */ |
| 1932 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1933 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1934 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1935 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1936 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1937 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1938 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1939 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1940 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1941 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1942 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1943 | |
| 1944 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1945 | {"st_mode", "protection bits"}, |
| 1946 | {"st_ino", "inode"}, |
| 1947 | {"st_dev", "device"}, |
| 1948 | {"st_nlink", "number of hard links"}, |
| 1949 | {"st_uid", "user ID of owner"}, |
| 1950 | {"st_gid", "group ID of owner"}, |
| 1951 | {"st_size", "total size, in bytes"}, |
| 1952 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1953 | {NULL, "integer time of last access"}, |
| 1954 | {NULL, "integer time of last modification"}, |
| 1955 | {NULL, "integer time of last change"}, |
| 1956 | {"st_atime", "time of last access"}, |
| 1957 | {"st_mtime", "time of last modification"}, |
| 1958 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1959 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1960 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1961 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1962 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1963 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1964 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1965 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1966 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1967 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1968 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1969 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1970 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1971 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1972 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1973 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1974 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1975 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1976 | #endif |
| 1977 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1978 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1979 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1980 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1981 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1982 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1983 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1984 | {"st_fstype", "Type of filesystem"}, |
| 1985 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1986 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 1987 | {"st_reparse_tag", "Windows reparse tag"}, |
| 1988 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1989 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1990 | }; |
| 1991 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1992 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1993 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1994 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1995 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1996 | #endif |
| 1997 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1998 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1999 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 2000 | #else |
| 2001 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 2002 | #endif |
| 2003 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2004 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2005 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 2006 | #else |
| 2007 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 2008 | #endif |
| 2009 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2010 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 2011 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 2012 | #else |
| 2013 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 2014 | #endif |
| 2015 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2016 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2017 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2018 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2019 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2020 | #endif |
| 2021 | |
| 2022 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 2023 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 2024 | #else |
| 2025 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 2026 | #endif |
| 2027 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2028 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2029 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 2030 | #else |
| 2031 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 2032 | #endif |
| 2033 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2034 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2035 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 2036 | #else |
| 2037 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 2038 | #endif |
| 2039 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2040 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2041 | #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1) |
| 2042 | #else |
| 2043 | #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX |
| 2044 | #endif |
| 2045 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2046 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2047 | "stat_result", /* name */ |
| 2048 | stat_result__doc__, /* doc */ |
| 2049 | stat_result_fields, |
| 2050 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2051 | }; |
| 2052 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2053 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2054 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2055 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2056 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2057 | 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] | 2058 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2059 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2060 | |
| 2061 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2062 | {"f_bsize", }, |
| 2063 | {"f_frsize", }, |
| 2064 | {"f_blocks", }, |
| 2065 | {"f_bfree", }, |
| 2066 | {"f_bavail", }, |
| 2067 | {"f_files", }, |
| 2068 | {"f_ffree", }, |
| 2069 | {"f_favail", }, |
| 2070 | {"f_flag", }, |
| 2071 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 2072 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2073 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2074 | }; |
| 2075 | |
| 2076 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2077 | "statvfs_result", /* name */ |
| 2078 | statvfs_result__doc__, /* doc */ |
| 2079 | statvfs_result_fields, |
| 2080 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2081 | }; |
| 2082 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2083 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2084 | PyDoc_STRVAR(waitid_result__doc__, |
| 2085 | "waitid_result: Result from waitid.\n\n\ |
| 2086 | This object may be accessed either as a tuple of\n\ |
| 2087 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2088 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2089 | \n\ |
| 2090 | See os.waitid for more information."); |
| 2091 | |
| 2092 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2093 | {"si_pid", }, |
| 2094 | {"si_uid", }, |
| 2095 | {"si_signo", }, |
| 2096 | {"si_status", }, |
| 2097 | {"si_code", }, |
| 2098 | {0} |
| 2099 | }; |
| 2100 | |
| 2101 | static PyStructSequence_Desc waitid_result_desc = { |
| 2102 | "waitid_result", /* name */ |
| 2103 | waitid_result__doc__, /* doc */ |
| 2104 | waitid_result_fields, |
| 2105 | 5 |
| 2106 | }; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2107 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2108 | static newfunc structseq_new; |
| 2109 | |
| 2110 | static PyObject * |
| 2111 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2112 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2113 | PyStructSequence *result; |
| 2114 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2115 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2116 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2117 | if (!result) |
| 2118 | return NULL; |
| 2119 | /* If we have been initialized from a tuple, |
| 2120 | st_?time might be set to None. Initialize it |
| 2121 | from the int slots. */ |
| 2122 | for (i = 7; i <= 9; i++) { |
| 2123 | if (result->ob_item[i+3] == Py_None) { |
| 2124 | Py_DECREF(Py_None); |
| 2125 | Py_INCREF(result->ob_item[i]); |
| 2126 | result->ob_item[i+3] = result->ob_item[i]; |
| 2127 | } |
| 2128 | } |
| 2129 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2132 | static int |
| 2133 | _posix_clear(PyObject *module) |
| 2134 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2135 | _posixstate *state = get_posix_state(module); |
| 2136 | Py_CLEAR(state->billion); |
| 2137 | Py_CLEAR(state->DirEntryType); |
| 2138 | Py_CLEAR(state->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2139 | #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] | 2140 | Py_CLEAR(state->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2141 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2142 | Py_CLEAR(state->StatResultType); |
| 2143 | Py_CLEAR(state->StatVFSResultType); |
| 2144 | Py_CLEAR(state->TerminalSizeType); |
| 2145 | Py_CLEAR(state->TimesResultType); |
| 2146 | Py_CLEAR(state->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2147 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2148 | Py_CLEAR(state->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2149 | #endif |
| 2150 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2151 | Py_CLEAR(state->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2152 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2153 | Py_CLEAR(state->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2154 | return 0; |
| 2155 | } |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2156 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2157 | static int |
| 2158 | _posix_traverse(PyObject *module, visitproc visit, void *arg) |
| 2159 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2160 | _posixstate *state = get_posix_state(module); |
| 2161 | Py_VISIT(state->billion); |
| 2162 | Py_VISIT(state->DirEntryType); |
| 2163 | Py_VISIT(state->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2164 | #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] | 2165 | Py_VISIT(state->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2166 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2167 | Py_VISIT(state->StatResultType); |
| 2168 | Py_VISIT(state->StatVFSResultType); |
| 2169 | Py_VISIT(state->TerminalSizeType); |
| 2170 | Py_VISIT(state->TimesResultType); |
| 2171 | Py_VISIT(state->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2172 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2173 | Py_VISIT(state->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2174 | #endif |
| 2175 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2176 | Py_VISIT(state->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2177 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2178 | Py_VISIT(state->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2179 | return 0; |
| 2180 | } |
| 2181 | |
| 2182 | static void |
| 2183 | _posix_free(void *module) |
| 2184 | { |
| 2185 | _posix_clear((PyObject *)module); |
| 2186 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2187 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2188 | static void |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2189 | 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] | 2190 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2191 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2192 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2193 | PyObject *s_in_ns = NULL; |
| 2194 | PyObject *ns_total = NULL; |
| 2195 | PyObject *float_s = NULL; |
| 2196 | |
| 2197 | if (!(s && ns_fractional)) |
| 2198 | goto exit; |
| 2199 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2200 | s_in_ns = PyNumber_Multiply(s, get_posix_state(module)->billion); |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2201 | if (!s_in_ns) |
| 2202 | goto exit; |
| 2203 | |
| 2204 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2205 | if (!ns_total) |
| 2206 | goto exit; |
| 2207 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2208 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2209 | if (!float_s) { |
| 2210 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | PyStructSequence_SET_ITEM(v, index, s); |
| 2214 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2215 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2216 | s = NULL; |
| 2217 | float_s = NULL; |
| 2218 | ns_total = NULL; |
| 2219 | exit: |
| 2220 | Py_XDECREF(s); |
| 2221 | Py_XDECREF(ns_fractional); |
| 2222 | Py_XDECREF(s_in_ns); |
| 2223 | Py_XDECREF(ns_total); |
| 2224 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2227 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2228 | (used by posix_stat() and posix_fstat()) */ |
| 2229 | static PyObject* |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2230 | _pystat_fromstructstat(PyObject *module, STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2231 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2232 | unsigned long ansec, mnsec, cnsec; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2233 | PyObject *StatResultType = get_posix_state(module)->StatResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2234 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2235 | if (v == NULL) |
| 2236 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2237 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2238 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2239 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2240 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2241 | #ifdef MS_WINDOWS |
| 2242 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2243 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2244 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2245 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2246 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2247 | #if defined(MS_WINDOWS) |
| 2248 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2249 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2250 | #else |
| 2251 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2252 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2253 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2254 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2255 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2256 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2257 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2258 | ansec = st->st_atim.tv_nsec; |
| 2259 | mnsec = st->st_mtim.tv_nsec; |
| 2260 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2261 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2262 | ansec = st->st_atimespec.tv_nsec; |
| 2263 | mnsec = st->st_mtimespec.tv_nsec; |
| 2264 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2265 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2266 | ansec = st->st_atime_nsec; |
| 2267 | mnsec = st->st_mtime_nsec; |
| 2268 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2269 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2270 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2271 | #endif |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2272 | fill_time(module, v, 7, st->st_atime, ansec); |
| 2273 | fill_time(module, v, 8, st->st_mtime, mnsec); |
| 2274 | fill_time(module, v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2275 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2276 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2277 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2278 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2279 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2280 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2281 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2282 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2283 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2284 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2285 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2286 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2287 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2288 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2289 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2290 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2291 | #endif |
| 2292 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2293 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2294 | PyObject *val; |
| 2295 | unsigned long bsec,bnsec; |
| 2296 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2297 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2298 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2299 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2300 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2301 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2302 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2303 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2304 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2305 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2306 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2307 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2308 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2309 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2310 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2311 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2312 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2313 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2314 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2315 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2316 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2317 | PyUnicode_FromString(st->st_fstype)); |
| 2318 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2319 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2320 | PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX, |
| 2321 | PyLong_FromUnsignedLong(st->st_reparse_tag)); |
| 2322 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2323 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2324 | if (PyErr_Occurred()) { |
| 2325 | Py_DECREF(v); |
| 2326 | return NULL; |
| 2327 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2328 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2329 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2332 | /* POSIX methods */ |
| 2333 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2334 | |
| 2335 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2336 | posix_do_stat(PyObject *module, const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2337 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2338 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2339 | STRUCT_STAT st; |
| 2340 | int result; |
| 2341 | |
| 2342 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2343 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2344 | return NULL; |
| 2345 | #endif |
| 2346 | |
| 2347 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2348 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2349 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2350 | return NULL; |
| 2351 | |
| 2352 | Py_BEGIN_ALLOW_THREADS |
| 2353 | if (path->fd != -1) |
| 2354 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2355 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2356 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2357 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2358 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2359 | result = win32_lstat(path->wide, &st); |
| 2360 | #else |
| 2361 | else |
| 2362 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2363 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2364 | result = LSTAT(path->narrow, &st); |
| 2365 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2366 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2367 | #ifdef HAVE_FSTATAT |
| 2368 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2369 | result = fstatat(dir_fd, path->narrow, &st, |
| 2370 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2371 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2372 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2373 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2374 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2375 | Py_END_ALLOW_THREADS |
| 2376 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2377 | if (result != 0) { |
| 2378 | return path_error(path); |
| 2379 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2380 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2381 | return _pystat_fromstructstat(module, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2384 | /*[python input] |
| 2385 | |
| 2386 | for s in """ |
| 2387 | |
| 2388 | FACCESSAT |
| 2389 | FCHMODAT |
| 2390 | FCHOWNAT |
| 2391 | FSTATAT |
| 2392 | LINKAT |
| 2393 | MKDIRAT |
| 2394 | MKFIFOAT |
| 2395 | MKNODAT |
| 2396 | OPENAT |
| 2397 | READLINKAT |
| 2398 | SYMLINKAT |
| 2399 | UNLINKAT |
| 2400 | |
| 2401 | """.strip().split(): |
| 2402 | s = s.strip() |
| 2403 | print(""" |
| 2404 | #ifdef HAVE_{s} |
| 2405 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2406 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2407 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2408 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2409 | """.rstrip().format(s=s)) |
| 2410 | |
| 2411 | for s in """ |
| 2412 | |
| 2413 | FCHDIR |
| 2414 | FCHMOD |
| 2415 | FCHOWN |
| 2416 | FDOPENDIR |
| 2417 | FEXECVE |
| 2418 | FPATHCONF |
| 2419 | FSTATVFS |
| 2420 | FTRUNCATE |
| 2421 | |
| 2422 | """.strip().split(): |
| 2423 | s = s.strip() |
| 2424 | print(""" |
| 2425 | #ifdef HAVE_{s} |
| 2426 | #define PATH_HAVE_{s} 1 |
| 2427 | #else |
| 2428 | #define PATH_HAVE_{s} 0 |
| 2429 | #endif |
| 2430 | |
| 2431 | """.rstrip().format(s=s)) |
| 2432 | [python start generated code]*/ |
| 2433 | |
| 2434 | #ifdef HAVE_FACCESSAT |
| 2435 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2436 | #else |
| 2437 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2438 | #endif |
| 2439 | |
| 2440 | #ifdef HAVE_FCHMODAT |
| 2441 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2442 | #else |
| 2443 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2444 | #endif |
| 2445 | |
| 2446 | #ifdef HAVE_FCHOWNAT |
| 2447 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2448 | #else |
| 2449 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2450 | #endif |
| 2451 | |
| 2452 | #ifdef HAVE_FSTATAT |
| 2453 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2454 | #else |
| 2455 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2456 | #endif |
| 2457 | |
| 2458 | #ifdef HAVE_LINKAT |
| 2459 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2460 | #else |
| 2461 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2462 | #endif |
| 2463 | |
| 2464 | #ifdef HAVE_MKDIRAT |
| 2465 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2466 | #else |
| 2467 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2468 | #endif |
| 2469 | |
| 2470 | #ifdef HAVE_MKFIFOAT |
| 2471 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2472 | #else |
| 2473 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2474 | #endif |
| 2475 | |
| 2476 | #ifdef HAVE_MKNODAT |
| 2477 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2478 | #else |
| 2479 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2480 | #endif |
| 2481 | |
| 2482 | #ifdef HAVE_OPENAT |
| 2483 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2484 | #else |
| 2485 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2486 | #endif |
| 2487 | |
| 2488 | #ifdef HAVE_READLINKAT |
| 2489 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2490 | #else |
| 2491 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2492 | #endif |
| 2493 | |
| 2494 | #ifdef HAVE_SYMLINKAT |
| 2495 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2496 | #else |
| 2497 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2498 | #endif |
| 2499 | |
| 2500 | #ifdef HAVE_UNLINKAT |
| 2501 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2502 | #else |
| 2503 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2504 | #endif |
| 2505 | |
| 2506 | #ifdef HAVE_FCHDIR |
| 2507 | #define PATH_HAVE_FCHDIR 1 |
| 2508 | #else |
| 2509 | #define PATH_HAVE_FCHDIR 0 |
| 2510 | #endif |
| 2511 | |
| 2512 | #ifdef HAVE_FCHMOD |
| 2513 | #define PATH_HAVE_FCHMOD 1 |
| 2514 | #else |
| 2515 | #define PATH_HAVE_FCHMOD 0 |
| 2516 | #endif |
| 2517 | |
| 2518 | #ifdef HAVE_FCHOWN |
| 2519 | #define PATH_HAVE_FCHOWN 1 |
| 2520 | #else |
| 2521 | #define PATH_HAVE_FCHOWN 0 |
| 2522 | #endif |
| 2523 | |
| 2524 | #ifdef HAVE_FDOPENDIR |
| 2525 | #define PATH_HAVE_FDOPENDIR 1 |
| 2526 | #else |
| 2527 | #define PATH_HAVE_FDOPENDIR 0 |
| 2528 | #endif |
| 2529 | |
| 2530 | #ifdef HAVE_FEXECVE |
| 2531 | #define PATH_HAVE_FEXECVE 1 |
| 2532 | #else |
| 2533 | #define PATH_HAVE_FEXECVE 0 |
| 2534 | #endif |
| 2535 | |
| 2536 | #ifdef HAVE_FPATHCONF |
| 2537 | #define PATH_HAVE_FPATHCONF 1 |
| 2538 | #else |
| 2539 | #define PATH_HAVE_FPATHCONF 0 |
| 2540 | #endif |
| 2541 | |
| 2542 | #ifdef HAVE_FSTATVFS |
| 2543 | #define PATH_HAVE_FSTATVFS 1 |
| 2544 | #else |
| 2545 | #define PATH_HAVE_FSTATVFS 0 |
| 2546 | #endif |
| 2547 | |
| 2548 | #ifdef HAVE_FTRUNCATE |
| 2549 | #define PATH_HAVE_FTRUNCATE 1 |
| 2550 | #else |
| 2551 | #define PATH_HAVE_FTRUNCATE 0 |
| 2552 | #endif |
| 2553 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2554 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2555 | #ifdef MS_WINDOWS |
| 2556 | #undef PATH_HAVE_FTRUNCATE |
| 2557 | #define PATH_HAVE_FTRUNCATE 1 |
| 2558 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2559 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2560 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2561 | |
| 2562 | class path_t_converter(CConverter): |
| 2563 | |
| 2564 | type = "path_t" |
| 2565 | impl_by_reference = True |
| 2566 | parse_by_reference = True |
| 2567 | |
| 2568 | converter = 'path_converter' |
| 2569 | |
| 2570 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2571 | # right now path_t doesn't support default values. |
| 2572 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2573 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2574 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2575 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2576 | if self.c_default not in (None, 'Py_None'): |
| 2577 | 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] | 2578 | |
| 2579 | self.nullable = nullable |
| 2580 | self.allow_fd = allow_fd |
| 2581 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2582 | def pre_render(self): |
| 2583 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2584 | if isinstance(value, str): |
| 2585 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2586 | return str(int(bool(value))) |
| 2587 | |
| 2588 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2589 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2590 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2591 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2592 | strify(self.nullable), |
| 2593 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2594 | ) |
| 2595 | |
| 2596 | def cleanup(self): |
| 2597 | return "path_cleanup(&" + self.name + ");\n" |
| 2598 | |
| 2599 | |
| 2600 | class dir_fd_converter(CConverter): |
| 2601 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2603 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2604 | if self.default in (unspecified, None): |
| 2605 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2606 | if isinstance(requires, str): |
| 2607 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2608 | else: |
| 2609 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2610 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2611 | class fildes_converter(CConverter): |
| 2612 | type = 'int' |
| 2613 | converter = 'fildes_converter' |
| 2614 | |
| 2615 | class uid_t_converter(CConverter): |
| 2616 | type = "uid_t" |
| 2617 | converter = '_Py_Uid_Converter' |
| 2618 | |
| 2619 | class gid_t_converter(CConverter): |
| 2620 | type = "gid_t" |
| 2621 | converter = '_Py_Gid_Converter' |
| 2622 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2623 | class dev_t_converter(CConverter): |
| 2624 | type = 'dev_t' |
| 2625 | converter = '_Py_Dev_Converter' |
| 2626 | |
| 2627 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2628 | type = 'dev_t' |
| 2629 | conversion_fn = '_PyLong_FromDev' |
| 2630 | unsigned_cast = '(dev_t)' |
| 2631 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2632 | class FSConverter_converter(CConverter): |
| 2633 | type = 'PyObject *' |
| 2634 | converter = 'PyUnicode_FSConverter' |
| 2635 | def converter_init(self): |
| 2636 | if self.default is not unspecified: |
| 2637 | fail("FSConverter_converter does not support default values") |
| 2638 | self.c_default = 'NULL' |
| 2639 | |
| 2640 | def cleanup(self): |
| 2641 | return "Py_XDECREF(" + self.name + ");\n" |
| 2642 | |
| 2643 | class pid_t_converter(CConverter): |
| 2644 | type = 'pid_t' |
| 2645 | format_unit = '" _Py_PARSE_PID "' |
| 2646 | |
| 2647 | class idtype_t_converter(int_converter): |
| 2648 | type = 'idtype_t' |
| 2649 | |
| 2650 | class id_t_converter(CConverter): |
| 2651 | type = 'id_t' |
| 2652 | format_unit = '" _Py_PARSE_PID "' |
| 2653 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2654 | class intptr_t_converter(CConverter): |
| 2655 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2656 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2657 | |
| 2658 | class Py_off_t_converter(CConverter): |
| 2659 | type = 'Py_off_t' |
| 2660 | converter = 'Py_off_t_converter' |
| 2661 | |
| 2662 | class Py_off_t_return_converter(long_return_converter): |
| 2663 | type = 'Py_off_t' |
| 2664 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2665 | |
| 2666 | class path_confname_converter(CConverter): |
| 2667 | type="int" |
| 2668 | converter="conv_path_confname" |
| 2669 | |
| 2670 | class confstr_confname_converter(path_confname_converter): |
| 2671 | converter='conv_confstr_confname' |
| 2672 | |
| 2673 | class sysconf_confname_converter(path_confname_converter): |
| 2674 | converter="conv_sysconf_confname" |
| 2675 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2676 | [python start generated code]*/ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2677 | /*[python end generated code: output=da39a3ee5e6b4b0d input=f1c8ae8d744f6c8b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2678 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2679 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2680 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2681 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2682 | |
| 2683 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2684 | 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] | 2685 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2686 | |
| 2687 | * |
| 2688 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2689 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2690 | If not None, it should be a file descriptor open to a directory, |
| 2691 | and path should be a relative string; path will then be relative to |
| 2692 | that directory. |
| 2693 | |
| 2694 | follow_symlinks: bool = True |
| 2695 | If False, and the last element of the path is a symbolic link, |
| 2696 | stat will examine the symbolic link itself instead of the file |
| 2697 | the link points to. |
| 2698 | |
| 2699 | Perform a stat system call on the given path. |
| 2700 | |
| 2701 | dir_fd and follow_symlinks may not be implemented |
| 2702 | on your platform. If they are unavailable, using them will raise a |
| 2703 | NotImplementedError. |
| 2704 | |
| 2705 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2706 | an open file descriptor. |
| 2707 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2708 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2709 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2710 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2711 | 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] | 2712 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2713 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2714 | return posix_do_stat(module, "stat", path, dir_fd, follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2715 | } |
| 2716 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2717 | |
| 2718 | /*[clinic input] |
| 2719 | os.lstat |
| 2720 | |
| 2721 | path : path_t |
| 2722 | |
| 2723 | * |
| 2724 | |
| 2725 | dir_fd : dir_fd(requires='fstatat') = None |
| 2726 | |
| 2727 | Perform a stat system call on the given path, without following symbolic links. |
| 2728 | |
| 2729 | Like stat(), but do not follow symbolic links. |
| 2730 | Equivalent to stat(path, follow_symlinks=False). |
| 2731 | [clinic start generated code]*/ |
| 2732 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2733 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2734 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2735 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2736 | { |
| 2737 | int follow_symlinks = 0; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2738 | return posix_do_stat(module, "lstat", path, dir_fd, follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2739 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2740 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2741 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2742 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2743 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2744 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2745 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2746 | 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] | 2747 | |
| 2748 | mode: int |
| 2749 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2750 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2751 | |
| 2752 | * |
| 2753 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2754 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2755 | If not None, it should be a file descriptor open to a directory, |
| 2756 | and path should be relative; path will then be relative to that |
| 2757 | directory. |
| 2758 | |
| 2759 | effective_ids: bool = False |
| 2760 | If True, access will use the effective uid/gid instead of |
| 2761 | the real uid/gid. |
| 2762 | |
| 2763 | follow_symlinks: bool = True |
| 2764 | If False, and the last element of the path is a symbolic link, |
| 2765 | access will examine the symbolic link itself instead of the file |
| 2766 | the link points to. |
| 2767 | |
| 2768 | Use the real uid/gid to test for access to a path. |
| 2769 | |
| 2770 | {parameters} |
| 2771 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2772 | on your platform. If they are unavailable, using them will raise a |
| 2773 | NotImplementedError. |
| 2774 | |
| 2775 | Note that most operations will use the effective uid/gid, therefore this |
| 2776 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2777 | has the specified access to the path. |
| 2778 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2779 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2780 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2781 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2782 | 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] | 2783 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2784 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2785 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2786 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2787 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2788 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2789 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2790 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2791 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2792 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2793 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2794 | #ifndef HAVE_FACCESSAT |
| 2795 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2796 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2797 | |
| 2798 | if (effective_ids) { |
| 2799 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2800 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2801 | } |
| 2802 | #endif |
| 2803 | |
| 2804 | #ifdef MS_WINDOWS |
| 2805 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2806 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2807 | Py_END_ALLOW_THREADS |
| 2808 | |
| 2809 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2810 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2811 | * * we didn't get a -1, and |
| 2812 | * * write access wasn't requested, |
| 2813 | * * or the file isn't read-only, |
| 2814 | * * or it's a directory. |
| 2815 | * (Directories cannot be read-only on Windows.) |
| 2816 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2817 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2818 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2819 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2820 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2821 | #else |
| 2822 | |
| 2823 | Py_BEGIN_ALLOW_THREADS |
| 2824 | #ifdef HAVE_FACCESSAT |
| 2825 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2826 | effective_ids || |
| 2827 | !follow_symlinks) { |
| 2828 | int flags = 0; |
| 2829 | if (!follow_symlinks) |
| 2830 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2831 | if (effective_ids) |
| 2832 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2833 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2834 | } |
| 2835 | else |
| 2836 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2837 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2838 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2839 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2840 | #endif |
| 2841 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2842 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2845 | #ifndef F_OK |
| 2846 | #define F_OK 0 |
| 2847 | #endif |
| 2848 | #ifndef R_OK |
| 2849 | #define R_OK 4 |
| 2850 | #endif |
| 2851 | #ifndef W_OK |
| 2852 | #define W_OK 2 |
| 2853 | #endif |
| 2854 | #ifndef X_OK |
| 2855 | #define X_OK 1 |
| 2856 | #endif |
| 2857 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2858 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2859 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2860 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2861 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2862 | |
| 2863 | fd: int |
| 2864 | Integer file descriptor handle. |
| 2865 | |
| 2866 | / |
| 2867 | |
| 2868 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2869 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2870 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2871 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2872 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2873 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2874 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2875 | |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2876 | long size = sysconf(_SC_TTY_NAME_MAX); |
| 2877 | if (size == -1) { |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2878 | return posix_error(); |
| 2879 | } |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2880 | char *buffer = (char *)PyMem_RawMalloc(size); |
| 2881 | if (buffer == NULL) { |
| 2882 | return PyErr_NoMemory(); |
| 2883 | } |
| 2884 | int ret = ttyname_r(fd, buffer, size); |
| 2885 | if (ret != 0) { |
| 2886 | PyMem_RawFree(buffer); |
| 2887 | errno = ret; |
| 2888 | return posix_error(); |
| 2889 | } |
| 2890 | PyObject *res = PyUnicode_DecodeFSDefault(buffer); |
| 2891 | PyMem_RawFree(buffer); |
| 2892 | return res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2893 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2894 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2895 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2896 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2897 | /*[clinic input] |
| 2898 | os.ctermid |
| 2899 | |
| 2900 | Return the name of the controlling terminal for this process. |
| 2901 | [clinic start generated code]*/ |
| 2902 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2903 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2904 | os_ctermid_impl(PyObject *module) |
| 2905 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2906 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2907 | char *ret; |
| 2908 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2909 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2910 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2911 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2912 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2913 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2914 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2915 | if (ret == NULL) |
| 2916 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2917 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2918 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2919 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2920 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2921 | |
| 2922 | /*[clinic input] |
| 2923 | os.chdir |
| 2924 | |
| 2925 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2926 | |
| 2927 | Change the current working directory to the specified path. |
| 2928 | |
| 2929 | path may always be specified as a string. |
| 2930 | On some platforms, path may also be specified as an open file descriptor. |
| 2931 | If this functionality is unavailable, using it raises an exception. |
| 2932 | [clinic start generated code]*/ |
| 2933 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2934 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2935 | os_chdir_impl(PyObject *module, path_t *path) |
| 2936 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2937 | { |
| 2938 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2939 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 2940 | if (PySys_Audit("os.chdir", "(O)", path->object) < 0) { |
| 2941 | return NULL; |
| 2942 | } |
| 2943 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2944 | Py_BEGIN_ALLOW_THREADS |
| 2945 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2946 | /* on unix, success = 0, on windows, success = !0 */ |
| 2947 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2948 | #else |
| 2949 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2950 | if (path->fd != -1) |
| 2951 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2952 | else |
| 2953 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2954 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2955 | #endif |
| 2956 | Py_END_ALLOW_THREADS |
| 2957 | |
| 2958 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2959 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2960 | } |
| 2961 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2962 | Py_RETURN_NONE; |
| 2963 | } |
| 2964 | |
| 2965 | |
| 2966 | #ifdef HAVE_FCHDIR |
| 2967 | /*[clinic input] |
| 2968 | os.fchdir |
| 2969 | |
| 2970 | fd: fildes |
| 2971 | |
| 2972 | Change to the directory of the given file descriptor. |
| 2973 | |
| 2974 | fd must be opened on a directory, not a file. |
| 2975 | Equivalent to os.chdir(fd). |
| 2976 | |
| 2977 | [clinic start generated code]*/ |
| 2978 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2979 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2980 | os_fchdir_impl(PyObject *module, int fd) |
| 2981 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2982 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 2983 | if (PySys_Audit("os.chdir", "(i)", fd) < 0) { |
| 2984 | return NULL; |
| 2985 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2986 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2987 | } |
| 2988 | #endif /* HAVE_FCHDIR */ |
| 2989 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2990 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2991 | /*[clinic input] |
| 2992 | os.chmod |
| 2993 | |
| 2994 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2995 | 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] | 2996 | On some platforms, path may also be specified as an open file descriptor. |
| 2997 | If this functionality is unavailable, using it raises an exception. |
| 2998 | |
| 2999 | mode: int |
| 3000 | Operating-system mode bitfield. |
| 3001 | |
| 3002 | * |
| 3003 | |
| 3004 | dir_fd : dir_fd(requires='fchmodat') = None |
| 3005 | If not None, it should be a file descriptor open to a directory, |
| 3006 | and path should be relative; path will then be relative to that |
| 3007 | directory. |
| 3008 | |
| 3009 | follow_symlinks: bool = True |
| 3010 | If False, and the last element of the path is a symbolic link, |
| 3011 | chmod will modify the symbolic link itself instead of the file |
| 3012 | the link points to. |
| 3013 | |
| 3014 | Change the access permissions of a file. |
| 3015 | |
| 3016 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3017 | an open file descriptor. |
| 3018 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3019 | If they are unavailable, using them will raise a NotImplementedError. |
| 3020 | |
| 3021 | [clinic start generated code]*/ |
| 3022 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3023 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3024 | 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] | 3025 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3026 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3027 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3028 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3029 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3030 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3031 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3032 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3033 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3034 | #ifdef HAVE_FCHMODAT |
| 3035 | int fchmodat_nofollow_unsupported = 0; |
| 3036 | #endif |
| 3037 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3038 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 3039 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3040 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3041 | #endif |
| 3042 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3043 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, |
| 3044 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3045 | return NULL; |
| 3046 | } |
| 3047 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3048 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3049 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3050 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 3051 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3052 | result = 0; |
| 3053 | else { |
| 3054 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3055 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 3056 | else |
| 3057 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3058 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3059 | } |
| 3060 | Py_END_ALLOW_THREADS |
| 3061 | |
| 3062 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3063 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3064 | } |
| 3065 | #else /* MS_WINDOWS */ |
| 3066 | Py_BEGIN_ALLOW_THREADS |
| 3067 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3068 | if (path->fd != -1) |
| 3069 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3070 | else |
| 3071 | #endif |
| 3072 | #ifdef HAVE_LCHMOD |
| 3073 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3074 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3075 | else |
| 3076 | #endif |
| 3077 | #ifdef HAVE_FCHMODAT |
| 3078 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 3079 | /* |
| 3080 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 3081 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3082 | * and then says it isn't implemented yet. |
| 3083 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3084 | * |
| 3085 | * Once it is supported, os.chmod will automatically |
| 3086 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 3087 | * Until then, we need to be careful what exception we raise. |
| 3088 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3089 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3090 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3091 | /* |
| 3092 | * But wait! We can't throw the exception without allowing threads, |
| 3093 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 3094 | */ |
| 3095 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3096 | result && |
| 3097 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 3098 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3099 | } |
| 3100 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3101 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3102 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3103 | Py_END_ALLOW_THREADS |
| 3104 | |
| 3105 | if (result) { |
| 3106 | #ifdef HAVE_FCHMODAT |
| 3107 | if (fchmodat_nofollow_unsupported) { |
| 3108 | if (dir_fd != DEFAULT_DIR_FD) |
| 3109 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3110 | dir_fd, follow_symlinks); |
| 3111 | else |
| 3112 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 3113 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3114 | } |
| 3115 | else |
| 3116 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3117 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3118 | } |
| 3119 | #endif |
| 3120 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3121 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3124 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3125 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3126 | /*[clinic input] |
| 3127 | os.fchmod |
| 3128 | |
| 3129 | fd: int |
| 3130 | mode: int |
| 3131 | |
| 3132 | Change the access permissions of the file given by file descriptor fd. |
| 3133 | |
| 3134 | Equivalent to os.chmod(fd, mode). |
| 3135 | [clinic start generated code]*/ |
| 3136 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3137 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3138 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 3139 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3140 | { |
| 3141 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3142 | int async_err = 0; |
| 3143 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3144 | if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) { |
| 3145 | return NULL; |
| 3146 | } |
| 3147 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3148 | do { |
| 3149 | Py_BEGIN_ALLOW_THREADS |
| 3150 | res = fchmod(fd, mode); |
| 3151 | Py_END_ALLOW_THREADS |
| 3152 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3153 | if (res != 0) |
| 3154 | return (!async_err) ? posix_error() : NULL; |
| 3155 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3156 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3157 | } |
| 3158 | #endif /* HAVE_FCHMOD */ |
| 3159 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3160 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3161 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3162 | /*[clinic input] |
| 3163 | os.lchmod |
| 3164 | |
| 3165 | path: path_t |
| 3166 | mode: int |
| 3167 | |
| 3168 | Change the access permissions of a file, without following symbolic links. |
| 3169 | |
| 3170 | If path is a symlink, this affects the link itself rather than the target. |
| 3171 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 3172 | [clinic start generated code]*/ |
| 3173 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3174 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3175 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 3176 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3177 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3178 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3179 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) { |
| 3180 | return NULL; |
| 3181 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3182 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3183 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3184 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3185 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3186 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3187 | return NULL; |
| 3188 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3189 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3190 | } |
| 3191 | #endif /* HAVE_LCHMOD */ |
| 3192 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3193 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3194 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3195 | /*[clinic input] |
| 3196 | os.chflags |
| 3197 | |
| 3198 | path: path_t |
| 3199 | flags: unsigned_long(bitwise=True) |
| 3200 | follow_symlinks: bool=True |
| 3201 | |
| 3202 | Set file flags. |
| 3203 | |
| 3204 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3205 | link, chflags will change flags on the symbolic link itself instead of the |
| 3206 | file the link points to. |
| 3207 | follow_symlinks may not be implemented on your platform. If it is |
| 3208 | unavailable, using it will raise a NotImplementedError. |
| 3209 | |
| 3210 | [clinic start generated code]*/ |
| 3211 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3212 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3213 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3214 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3215 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3216 | { |
| 3217 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3218 | |
| 3219 | #ifndef HAVE_LCHFLAGS |
| 3220 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3221 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3222 | #endif |
| 3223 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3224 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3225 | return NULL; |
| 3226 | } |
| 3227 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3228 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3229 | #ifdef HAVE_LCHFLAGS |
| 3230 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3231 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3232 | else |
| 3233 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3234 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3235 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3236 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3237 | if (result) |
| 3238 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3239 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3240 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3241 | } |
| 3242 | #endif /* HAVE_CHFLAGS */ |
| 3243 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3244 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3245 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3246 | /*[clinic input] |
| 3247 | os.lchflags |
| 3248 | |
| 3249 | path: path_t |
| 3250 | flags: unsigned_long(bitwise=True) |
| 3251 | |
| 3252 | Set file flags. |
| 3253 | |
| 3254 | This function will not follow symbolic links. |
| 3255 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3256 | [clinic start generated code]*/ |
| 3257 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3258 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3259 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3260 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3261 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3262 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3263 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3264 | return NULL; |
| 3265 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3266 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3267 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3268 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3269 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3270 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3271 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3272 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3273 | } |
| 3274 | #endif /* HAVE_LCHFLAGS */ |
| 3275 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3276 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3277 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3278 | /*[clinic input] |
| 3279 | os.chroot |
| 3280 | path: path_t |
| 3281 | |
| 3282 | Change root directory to path. |
| 3283 | |
| 3284 | [clinic start generated code]*/ |
| 3285 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3286 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3287 | os_chroot_impl(PyObject *module, path_t *path) |
| 3288 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3289 | { |
| 3290 | int res; |
| 3291 | Py_BEGIN_ALLOW_THREADS |
| 3292 | res = chroot(path->narrow); |
| 3293 | Py_END_ALLOW_THREADS |
| 3294 | if (res < 0) |
| 3295 | return path_error(path); |
| 3296 | Py_RETURN_NONE; |
| 3297 | } |
| 3298 | #endif /* HAVE_CHROOT */ |
| 3299 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3300 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3301 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3302 | /*[clinic input] |
| 3303 | os.fsync |
| 3304 | |
| 3305 | fd: fildes |
| 3306 | |
| 3307 | Force write of fd to disk. |
| 3308 | [clinic start generated code]*/ |
| 3309 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3310 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3311 | os_fsync_impl(PyObject *module, int fd) |
| 3312 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3313 | { |
| 3314 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3315 | } |
| 3316 | #endif /* HAVE_FSYNC */ |
| 3317 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3318 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3319 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3320 | /*[clinic input] |
| 3321 | os.sync |
| 3322 | |
| 3323 | Force write of everything to disk. |
| 3324 | [clinic start generated code]*/ |
| 3325 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3326 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3327 | os_sync_impl(PyObject *module) |
| 3328 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3329 | { |
| 3330 | Py_BEGIN_ALLOW_THREADS |
| 3331 | sync(); |
| 3332 | Py_END_ALLOW_THREADS |
| 3333 | Py_RETURN_NONE; |
| 3334 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3335 | #endif /* HAVE_SYNC */ |
| 3336 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3337 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3338 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3339 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3340 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3341 | #endif |
| 3342 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3343 | /*[clinic input] |
| 3344 | os.fdatasync |
| 3345 | |
| 3346 | fd: fildes |
| 3347 | |
| 3348 | Force write of fd to disk without forcing update of metadata. |
| 3349 | [clinic start generated code]*/ |
| 3350 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3351 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3352 | os_fdatasync_impl(PyObject *module, int fd) |
| 3353 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3354 | { |
| 3355 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3356 | } |
| 3357 | #endif /* HAVE_FDATASYNC */ |
| 3358 | |
| 3359 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3360 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3361 | /*[clinic input] |
| 3362 | os.chown |
| 3363 | |
| 3364 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3365 | 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] | 3366 | |
| 3367 | uid: uid_t |
| 3368 | |
| 3369 | gid: gid_t |
| 3370 | |
| 3371 | * |
| 3372 | |
| 3373 | dir_fd : dir_fd(requires='fchownat') = None |
| 3374 | If not None, it should be a file descriptor open to a directory, |
| 3375 | and path should be relative; path will then be relative to that |
| 3376 | directory. |
| 3377 | |
| 3378 | follow_symlinks: bool = True |
| 3379 | If False, and the last element of the path is a symbolic link, |
| 3380 | stat will examine the symbolic link itself instead of the file |
| 3381 | the link points to. |
| 3382 | |
| 3383 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3384 | |
| 3385 | path may always be specified as a string. |
| 3386 | On some platforms, path may also be specified as an open file descriptor. |
| 3387 | If this functionality is unavailable, using it raises an exception. |
| 3388 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3389 | and path should be relative; path will then be relative to that directory. |
| 3390 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3391 | link, chown will modify the symbolic link itself instead of the file the |
| 3392 | link points to. |
| 3393 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3394 | an open file descriptor. |
| 3395 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3396 | If they are unavailable, using them will raise a NotImplementedError. |
| 3397 | |
| 3398 | [clinic start generated code]*/ |
| 3399 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3400 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3401 | 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] | 3402 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3403 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3404 | { |
| 3405 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3406 | |
| 3407 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3408 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3409 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3410 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3411 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3412 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3413 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3414 | |
| 3415 | #ifdef __APPLE__ |
| 3416 | /* |
| 3417 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3418 | * (But we still have an lchown symbol because of weak-linking.) |
| 3419 | * It doesn't have fchownat either. So there's no possibility |
| 3420 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3421 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3422 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3423 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3424 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3425 | } |
| 3426 | #endif |
| 3427 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3428 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, |
| 3429 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3430 | return NULL; |
| 3431 | } |
| 3432 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3433 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3434 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3435 | if (path->fd != -1) |
| 3436 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3437 | else |
| 3438 | #endif |
| 3439 | #ifdef HAVE_LCHOWN |
| 3440 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3441 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3442 | else |
| 3443 | #endif |
| 3444 | #ifdef HAVE_FCHOWNAT |
| 3445 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3446 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3447 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3448 | else |
| 3449 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3450 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3451 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3452 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3453 | if (result) |
| 3454 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3456 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3457 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3458 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3459 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3460 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3461 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3462 | /*[clinic input] |
| 3463 | os.fchown |
| 3464 | |
| 3465 | fd: int |
| 3466 | uid: uid_t |
| 3467 | gid: gid_t |
| 3468 | |
| 3469 | Change the owner and group id of the file specified by file descriptor. |
| 3470 | |
| 3471 | Equivalent to os.chown(fd, uid, gid). |
| 3472 | |
| 3473 | [clinic start generated code]*/ |
| 3474 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3475 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3476 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3477 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3478 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3479 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3480 | int async_err = 0; |
| 3481 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3482 | if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) { |
| 3483 | return NULL; |
| 3484 | } |
| 3485 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3486 | do { |
| 3487 | Py_BEGIN_ALLOW_THREADS |
| 3488 | res = fchown(fd, uid, gid); |
| 3489 | Py_END_ALLOW_THREADS |
| 3490 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3491 | if (res != 0) |
| 3492 | return (!async_err) ? posix_error() : NULL; |
| 3493 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3494 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3495 | } |
| 3496 | #endif /* HAVE_FCHOWN */ |
| 3497 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3498 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3499 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3500 | /*[clinic input] |
| 3501 | os.lchown |
| 3502 | |
| 3503 | path : path_t |
| 3504 | uid: uid_t |
| 3505 | gid: gid_t |
| 3506 | |
| 3507 | Change the owner and group id of path to the numeric uid and gid. |
| 3508 | |
| 3509 | This function will not follow symbolic links. |
| 3510 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3511 | [clinic start generated code]*/ |
| 3512 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3513 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3514 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3515 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3516 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3517 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3518 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) { |
| 3519 | return NULL; |
| 3520 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3521 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3522 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3523 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3524 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3525 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3526 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3527 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3528 | } |
| 3529 | #endif /* HAVE_LCHOWN */ |
| 3530 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3531 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3532 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3533 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3534 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3535 | #ifdef MS_WINDOWS |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3536 | wchar_t wbuf[MAXPATHLEN]; |
| 3537 | wchar_t *wbuf2 = wbuf; |
| 3538 | DWORD len; |
| 3539 | |
| 3540 | Py_BEGIN_ALLOW_THREADS |
| 3541 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
| 3542 | /* If the buffer is large enough, len does not include the |
| 3543 | terminating \0. If the buffer is too small, len includes |
| 3544 | the space needed for the terminator. */ |
| 3545 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | ec3e20a | 2019-06-28 18:01:59 +0200 | [diff] [blame] | 3546 | if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3547 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3548 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3549 | else { |
| 3550 | wbuf2 = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3551 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3552 | if (wbuf2) { |
| 3553 | len = GetCurrentDirectoryW(len, wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3554 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3555 | } |
| 3556 | Py_END_ALLOW_THREADS |
| 3557 | |
| 3558 | if (!wbuf2) { |
| 3559 | PyErr_NoMemory(); |
| 3560 | return NULL; |
| 3561 | } |
| 3562 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3563 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3564 | PyMem_RawFree(wbuf2); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3565 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3566 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3567 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3568 | PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3569 | if (wbuf2 != wbuf) { |
| 3570 | PyMem_RawFree(wbuf2); |
| 3571 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3572 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3573 | if (use_bytes) { |
| 3574 | if (resobj == NULL) { |
| 3575 | return NULL; |
| 3576 | } |
| 3577 | Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj)); |
| 3578 | } |
| 3579 | |
| 3580 | return resobj; |
| 3581 | #else |
| 3582 | const size_t chunk = 1024; |
| 3583 | |
| 3584 | char *buf = NULL; |
| 3585 | char *cwd = NULL; |
| 3586 | size_t buflen = 0; |
| 3587 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3588 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3589 | do { |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3590 | char *newbuf; |
| 3591 | if (buflen <= PY_SSIZE_T_MAX - chunk) { |
| 3592 | buflen += chunk; |
| 3593 | newbuf = PyMem_RawRealloc(buf, buflen); |
| 3594 | } |
| 3595 | else { |
| 3596 | newbuf = NULL; |
| 3597 | } |
| 3598 | if (newbuf == NULL) { |
| 3599 | PyMem_RawFree(buf); |
| 3600 | buf = NULL; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3601 | break; |
| 3602 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3603 | buf = newbuf; |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3604 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3605 | cwd = getcwd(buf, buflen); |
| 3606 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3607 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3608 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3609 | if (buf == NULL) { |
| 3610 | return PyErr_NoMemory(); |
| 3611 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3612 | if (cwd == NULL) { |
| 3613 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3614 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3615 | } |
| 3616 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3617 | PyObject *obj; |
| 3618 | if (use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3619 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3620 | } |
| 3621 | else { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3622 | obj = PyUnicode_DecodeFSDefault(buf); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3623 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3624 | PyMem_RawFree(buf); |
| 3625 | |
| 3626 | return obj; |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3627 | #endif /* !MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3628 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3629 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3630 | |
| 3631 | /*[clinic input] |
| 3632 | os.getcwd |
| 3633 | |
| 3634 | Return a unicode string representing the current working directory. |
| 3635 | [clinic start generated code]*/ |
| 3636 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3637 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3638 | os_getcwd_impl(PyObject *module) |
| 3639 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3640 | { |
| 3641 | return posix_getcwd(0); |
| 3642 | } |
| 3643 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3644 | |
| 3645 | /*[clinic input] |
| 3646 | os.getcwdb |
| 3647 | |
| 3648 | Return a bytes string representing the current working directory. |
| 3649 | [clinic start generated code]*/ |
| 3650 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3651 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3652 | os_getcwdb_impl(PyObject *module) |
| 3653 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3654 | { |
| 3655 | return posix_getcwd(1); |
| 3656 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3657 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3658 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3659 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3660 | #define HAVE_LINK 1 |
| 3661 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3662 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3663 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3664 | /*[clinic input] |
| 3665 | |
| 3666 | os.link |
| 3667 | |
| 3668 | src : path_t |
| 3669 | dst : path_t |
| 3670 | * |
| 3671 | src_dir_fd : dir_fd = None |
| 3672 | dst_dir_fd : dir_fd = None |
| 3673 | follow_symlinks: bool = True |
| 3674 | |
| 3675 | Create a hard link to a file. |
| 3676 | |
| 3677 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3678 | descriptor open to a directory, and the respective path string (src or dst) |
| 3679 | should be relative; the path will then be relative to that directory. |
| 3680 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3681 | link, link will create a link to the symbolic link itself instead of the |
| 3682 | file the link points to. |
| 3683 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3684 | platform. If they are unavailable, using them will raise a |
| 3685 | NotImplementedError. |
| 3686 | [clinic start generated code]*/ |
| 3687 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3688 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3689 | 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] | 3690 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3691 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3692 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3693 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3694 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3695 | #else |
| 3696 | int result; |
| 3697 | #endif |
| 3698 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3699 | #ifndef HAVE_LINKAT |
| 3700 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3701 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3702 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3703 | } |
| 3704 | #endif |
| 3705 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3706 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3707 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3708 | PyErr_SetString(PyExc_NotImplementedError, |
| 3709 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3710 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3711 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3712 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3713 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3714 | if (PySys_Audit("os.link", "OOii", src->object, dst->object, |
| 3715 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 3716 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 3717 | return NULL; |
| 3718 | } |
| 3719 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3720 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3721 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3722 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3723 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3724 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3725 | if (!result) |
| 3726 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3727 | #else |
| 3728 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3729 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3730 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3731 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3732 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3733 | result = linkat(src_dir_fd, src->narrow, |
| 3734 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3735 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3736 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3737 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3738 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3739 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3740 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3741 | if (result) |
| 3742 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3743 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3744 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3745 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3746 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3747 | #endif |
| 3748 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3749 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3750 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3751 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3752 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3753 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3754 | PyObject *v; |
| 3755 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3756 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3757 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3758 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3759 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3760 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3761 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3762 | WIN32_FIND_DATAW wFileData; |
| 3763 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3764 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3765 | if (!path->wide) { /* Default arg: "." */ |
| 3766 | po_wchars = L"."; |
| 3767 | len = 1; |
| 3768 | } else { |
| 3769 | po_wchars = path->wide; |
| 3770 | len = wcslen(path->wide); |
| 3771 | } |
| 3772 | /* The +5 is so we can append "\\*.*\0" */ |
| 3773 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3774 | if (!wnamebuf) { |
| 3775 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3776 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3777 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3778 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3779 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3780 | wchar_t wch = wnamebuf[len-1]; |
| 3781 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3782 | wnamebuf[len++] = SEP; |
| 3783 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3784 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3785 | if ((list = PyList_New(0)) == NULL) { |
| 3786 | goto exit; |
| 3787 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3788 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3789 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3790 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3791 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3792 | int error = GetLastError(); |
| 3793 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3794 | goto exit; |
| 3795 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3796 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3797 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3798 | } |
| 3799 | do { |
| 3800 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3801 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3802 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3803 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3804 | wcslen(wFileData.cFileName)); |
| 3805 | if (path->narrow && v) { |
| 3806 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3807 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3808 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3809 | Py_DECREF(list); |
| 3810 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3811 | break; |
| 3812 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3813 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3814 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3815 | Py_DECREF(list); |
| 3816 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3817 | break; |
| 3818 | } |
| 3819 | Py_DECREF(v); |
| 3820 | } |
| 3821 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3822 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3823 | Py_END_ALLOW_THREADS |
| 3824 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3825 | it got to the end of the directory. */ |
| 3826 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3827 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3828 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3829 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3830 | } |
| 3831 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3832 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3833 | exit: |
| 3834 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3835 | if (FindClose(hFindFile) == FALSE) { |
| 3836 | if (list != NULL) { |
| 3837 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3838 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3839 | } |
| 3840 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3841 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3842 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3843 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3844 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3845 | } /* end of _listdir_windows_no_opendir */ |
| 3846 | |
| 3847 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3848 | |
| 3849 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3850 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3851 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3852 | PyObject *v; |
| 3853 | DIR *dirp = NULL; |
| 3854 | struct dirent *ep; |
| 3855 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3856 | #ifdef HAVE_FDOPENDIR |
| 3857 | int fd = -1; |
| 3858 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3859 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3860 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3861 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3862 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3863 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3864 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3865 | if (fd == -1) |
| 3866 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3867 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3868 | return_str = 1; |
| 3869 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3870 | Py_BEGIN_ALLOW_THREADS |
| 3871 | dirp = fdopendir(fd); |
| 3872 | Py_END_ALLOW_THREADS |
| 3873 | } |
| 3874 | else |
| 3875 | #endif |
| 3876 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3877 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3878 | if (path->narrow) { |
| 3879 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3880 | /* only return bytes if they specified a bytes-like object */ |
| 3881 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3882 | } |
| 3883 | else { |
| 3884 | name = "."; |
| 3885 | return_str = 1; |
| 3886 | } |
| 3887 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3888 | Py_BEGIN_ALLOW_THREADS |
| 3889 | dirp = opendir(name); |
| 3890 | Py_END_ALLOW_THREADS |
| 3891 | } |
| 3892 | |
| 3893 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3894 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3895 | #ifdef HAVE_FDOPENDIR |
| 3896 | if (fd != -1) { |
| 3897 | Py_BEGIN_ALLOW_THREADS |
| 3898 | close(fd); |
| 3899 | Py_END_ALLOW_THREADS |
| 3900 | } |
| 3901 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3902 | goto exit; |
| 3903 | } |
| 3904 | if ((list = PyList_New(0)) == NULL) { |
| 3905 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3906 | } |
| 3907 | for (;;) { |
| 3908 | errno = 0; |
| 3909 | Py_BEGIN_ALLOW_THREADS |
| 3910 | ep = readdir(dirp); |
| 3911 | Py_END_ALLOW_THREADS |
| 3912 | if (ep == NULL) { |
| 3913 | if (errno == 0) { |
| 3914 | break; |
| 3915 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3916 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3917 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3918 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3919 | } |
| 3920 | } |
| 3921 | if (ep->d_name[0] == '.' && |
| 3922 | (NAMLEN(ep) == 1 || |
| 3923 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3924 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3925 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3926 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3927 | else |
| 3928 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3929 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3930 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3931 | break; |
| 3932 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3933 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3934 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3935 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3936 | break; |
| 3937 | } |
| 3938 | Py_DECREF(v); |
| 3939 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3940 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3941 | exit: |
| 3942 | if (dirp != NULL) { |
| 3943 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3944 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3945 | if (fd > -1) |
| 3946 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3947 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3948 | closedir(dirp); |
| 3949 | Py_END_ALLOW_THREADS |
| 3950 | } |
| 3951 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3952 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3953 | } /* end of _posix_listdir */ |
| 3954 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3955 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3956 | |
| 3957 | /*[clinic input] |
| 3958 | os.listdir |
| 3959 | |
| 3960 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3961 | |
| 3962 | Return a list containing the names of the files in the directory. |
| 3963 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3964 | 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] | 3965 | the filenames returned will also be bytes; in all other circumstances |
| 3966 | the filenames returned will be str. |
| 3967 | If path is None, uses the path='.'. |
| 3968 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3969 | the file descriptor must refer to a directory. |
| 3970 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3971 | |
| 3972 | The list is in arbitrary order. It does not include the special |
| 3973 | entries '.' and '..' even if they are present in the directory. |
| 3974 | |
| 3975 | |
| 3976 | [clinic start generated code]*/ |
| 3977 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3978 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3979 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3980 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3981 | { |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 3982 | if (PySys_Audit("os.listdir", "O", |
| 3983 | path->object ? path->object : Py_None) < 0) { |
| 3984 | return NULL; |
| 3985 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3986 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3987 | return _listdir_windows_no_opendir(path, NULL); |
| 3988 | #else |
| 3989 | return _posix_listdir(path, NULL); |
| 3990 | #endif |
| 3991 | } |
| 3992 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3993 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3994 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3995 | /*[clinic input] |
| 3996 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3997 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3998 | path: path_t |
| 3999 | / |
| 4000 | |
| 4001 | [clinic start generated code]*/ |
| 4002 | |
| 4003 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4004 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 4005 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 4006 | { |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4007 | wchar_t *abspath; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4008 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4009 | /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */ |
| 4010 | if (_Py_abspath(path->wide, &abspath) < 0) { |
| 4011 | return win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4012 | } |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4013 | if (abspath == NULL) { |
| 4014 | return PyErr_NoMemory(); |
| 4015 | } |
| 4016 | |
| 4017 | PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath)); |
| 4018 | PyMem_RawFree(abspath); |
| 4019 | if (str == NULL) { |
| 4020 | return NULL; |
| 4021 | } |
| 4022 | if (path->narrow) { |
| 4023 | Py_SETREF(str, PyUnicode_EncodeFSDefault(str)); |
| 4024 | } |
| 4025 | return str; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4026 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4027 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 4028 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4029 | /*[clinic input] |
| 4030 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 4031 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4032 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4033 | / |
| 4034 | |
| 4035 | A helper function for samepath on windows. |
| 4036 | [clinic start generated code]*/ |
| 4037 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4038 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4039 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 4040 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4041 | { |
| 4042 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4043 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 4044 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4045 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4046 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4047 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4048 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4049 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4050 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4051 | 0, /* desired access */ |
| 4052 | 0, /* share mode */ |
| 4053 | NULL, /* security attributes */ |
| 4054 | OPEN_EXISTING, |
| 4055 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 4056 | FILE_FLAG_BACKUP_SEMANTICS, |
| 4057 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4058 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4059 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4060 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4061 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4062 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4063 | |
| 4064 | /* We have a good handle to the target, use it to determine the |
| 4065 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4066 | while (1) { |
| 4067 | Py_BEGIN_ALLOW_THREADS |
| 4068 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 4069 | buf_size, VOLUME_NAME_DOS); |
| 4070 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4071 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4072 | if (!result_length) { |
| 4073 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 4074 | path->object); |
| 4075 | goto cleanup; |
| 4076 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4077 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4078 | if (result_length < buf_size) { |
| 4079 | break; |
| 4080 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4081 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4082 | wchar_t *tmp; |
| 4083 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 4084 | result_length * sizeof(*tmp)); |
| 4085 | if (!tmp) { |
| 4086 | result = PyErr_NoMemory(); |
| 4087 | goto cleanup; |
| 4088 | } |
| 4089 | |
| 4090 | buf_size = result_length; |
| 4091 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4092 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4093 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4094 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4095 | if (result && path->narrow) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4096 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4097 | } |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4098 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4099 | cleanup: |
| 4100 | if (target_path != buf) { |
| 4101 | PyMem_Free(target_path); |
| 4102 | } |
| 4103 | CloseHandle(hFile); |
| 4104 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4105 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 4106 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4107 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4108 | /*[clinic input] |
| 4109 | os._getvolumepathname |
| 4110 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4111 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4112 | |
| 4113 | A helper function for ismount on Win32. |
| 4114 | [clinic start generated code]*/ |
| 4115 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4116 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4117 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 4118 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4119 | { |
| 4120 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4121 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4122 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4123 | BOOL ret; |
| 4124 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4125 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4126 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4127 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 4128 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4129 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4130 | return NULL; |
| 4131 | } |
| 4132 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4133 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4134 | if (mountpath == NULL) |
| 4135 | return PyErr_NoMemory(); |
| 4136 | |
| 4137 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4138 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4139 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4140 | Py_END_ALLOW_THREADS |
| 4141 | |
| 4142 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4143 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4144 | goto exit; |
| 4145 | } |
| 4146 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4147 | if (path->narrow) |
| 4148 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4149 | |
| 4150 | exit: |
| 4151 | PyMem_Free(mountpath); |
| 4152 | return result; |
| 4153 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4154 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4155 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4156 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4157 | |
| 4158 | /*[clinic input] |
| 4159 | os.mkdir |
| 4160 | |
| 4161 | path : path_t |
| 4162 | |
| 4163 | mode: int = 0o777 |
| 4164 | |
| 4165 | * |
| 4166 | |
| 4167 | dir_fd : dir_fd(requires='mkdirat') = None |
| 4168 | |
| 4169 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4170 | |
| 4171 | Create a directory. |
| 4172 | |
| 4173 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4174 | and path should be relative; path will then be relative to that directory. |
| 4175 | dir_fd may not be implemented on your platform. |
| 4176 | If it is unavailable, using it will raise a NotImplementedError. |
| 4177 | |
| 4178 | The mode argument is ignored on Windows. |
| 4179 | [clinic start generated code]*/ |
| 4180 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4181 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4182 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 4183 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4184 | { |
| 4185 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4186 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4187 | if (PySys_Audit("os.mkdir", "Oii", path->object, mode, |
| 4188 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4189 | return NULL; |
| 4190 | } |
| 4191 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4192 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4193 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4194 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4195 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4196 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4197 | if (!result) |
| 4198 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4199 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4200 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4201 | #if HAVE_MKDIRAT |
| 4202 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4203 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4204 | else |
| 4205 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4206 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4207 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4208 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4209 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4210 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4211 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4212 | if (result < 0) |
| 4213 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4214 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4215 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4218 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4219 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4220 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4221 | #include <sys/resource.h> |
| 4222 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4223 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4224 | |
| 4225 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4226 | /*[clinic input] |
| 4227 | os.nice |
| 4228 | |
| 4229 | increment: int |
| 4230 | / |
| 4231 | |
| 4232 | Add increment to the priority of process and return the new priority. |
| 4233 | [clinic start generated code]*/ |
| 4234 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4235 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4236 | os_nice_impl(PyObject *module, int increment) |
| 4237 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4238 | { |
| 4239 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4240 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4241 | /* There are two flavours of 'nice': one that returns the new |
| 4242 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4243 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4244 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4245 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4246 | If we are of the nice family that returns the new priority, we |
| 4247 | need to clear errno before the call, and check if errno is filled |
| 4248 | before calling posix_error() on a returnvalue of -1, because the |
| 4249 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4250 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4251 | errno = 0; |
| 4252 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4253 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4254 | if (value == 0) |
| 4255 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4256 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4257 | if (value == -1 && errno != 0) |
| 4258 | /* either nice() or getpriority() returned an error */ |
| 4259 | return posix_error(); |
| 4260 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4261 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4262 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4263 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4264 | |
| 4265 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4266 | /*[clinic input] |
| 4267 | os.getpriority |
| 4268 | |
| 4269 | which: int |
| 4270 | who: int |
| 4271 | |
| 4272 | Return program scheduling priority. |
| 4273 | [clinic start generated code]*/ |
| 4274 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4275 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4276 | os_getpriority_impl(PyObject *module, int which, int who) |
| 4277 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4278 | { |
| 4279 | int retval; |
| 4280 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4281 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4282 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4283 | if (errno != 0) |
| 4284 | return posix_error(); |
| 4285 | return PyLong_FromLong((long)retval); |
| 4286 | } |
| 4287 | #endif /* HAVE_GETPRIORITY */ |
| 4288 | |
| 4289 | |
| 4290 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4291 | /*[clinic input] |
| 4292 | os.setpriority |
| 4293 | |
| 4294 | which: int |
| 4295 | who: int |
| 4296 | priority: int |
| 4297 | |
| 4298 | Set program scheduling priority. |
| 4299 | [clinic start generated code]*/ |
| 4300 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4301 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4302 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4303 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4304 | { |
| 4305 | int retval; |
| 4306 | |
| 4307 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4308 | if (retval == -1) |
| 4309 | return posix_error(); |
| 4310 | Py_RETURN_NONE; |
| 4311 | } |
| 4312 | #endif /* HAVE_SETPRIORITY */ |
| 4313 | |
| 4314 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4315 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4316 | 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] | 4317 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4318 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4319 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4320 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4321 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4322 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4323 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4324 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4325 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4326 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4327 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4328 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4329 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4330 | #ifndef HAVE_RENAMEAT |
| 4331 | if (dir_fd_specified) { |
| 4332 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4333 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4334 | } |
| 4335 | #endif |
| 4336 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4337 | if (PySys_Audit("os.rename", "OOii", src->object, dst->object, |
| 4338 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 4339 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 4340 | return NULL; |
| 4341 | } |
| 4342 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4343 | #ifdef MS_WINDOWS |
| 4344 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4345 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4346 | Py_END_ALLOW_THREADS |
| 4347 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4348 | if (!result) |
| 4349 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4350 | |
| 4351 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4352 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4353 | PyErr_Format(PyExc_ValueError, |
| 4354 | "%s: src and dst must be the same type", function_name); |
| 4355 | return NULL; |
| 4356 | } |
| 4357 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4358 | Py_BEGIN_ALLOW_THREADS |
| 4359 | #ifdef HAVE_RENAMEAT |
| 4360 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4361 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4362 | else |
| 4363 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4364 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4365 | Py_END_ALLOW_THREADS |
| 4366 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4367 | if (result) |
| 4368 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4369 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4370 | Py_RETURN_NONE; |
| 4371 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4372 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4373 | |
| 4374 | /*[clinic input] |
| 4375 | os.rename |
| 4376 | |
| 4377 | src : path_t |
| 4378 | dst : path_t |
| 4379 | * |
| 4380 | src_dir_fd : dir_fd = None |
| 4381 | dst_dir_fd : dir_fd = None |
| 4382 | |
| 4383 | Rename a file or directory. |
| 4384 | |
| 4385 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4386 | descriptor open to a directory, and the respective path string (src or dst) |
| 4387 | should be relative; the path will then be relative to that directory. |
| 4388 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4389 | If they are unavailable, using them will raise a NotImplementedError. |
| 4390 | [clinic start generated code]*/ |
| 4391 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4392 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4393 | 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] | 4394 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4395 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4396 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4397 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4398 | } |
| 4399 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4400 | |
| 4401 | /*[clinic input] |
| 4402 | os.replace = os.rename |
| 4403 | |
| 4404 | Rename a file or directory, overwriting the destination. |
| 4405 | |
| 4406 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4407 | descriptor open to a directory, and the respective path string (src or dst) |
| 4408 | should be relative; the path will then be relative to that directory. |
| 4409 | 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] | 4410 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4411 | [clinic start generated code]*/ |
| 4412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4413 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4414 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4415 | int dst_dir_fd) |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4416 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4417 | { |
| 4418 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4419 | } |
| 4420 | |
| 4421 | |
| 4422 | /*[clinic input] |
| 4423 | os.rmdir |
| 4424 | |
| 4425 | path: path_t |
| 4426 | * |
| 4427 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4428 | |
| 4429 | Remove a directory. |
| 4430 | |
| 4431 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4432 | and path should be relative; path will then be relative to that directory. |
| 4433 | dir_fd may not be implemented on your platform. |
| 4434 | If it is unavailable, using it will raise a NotImplementedError. |
| 4435 | [clinic start generated code]*/ |
| 4436 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4437 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4438 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4439 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4440 | { |
| 4441 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4442 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4443 | if (PySys_Audit("os.rmdir", "Oi", path->object, |
| 4444 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4445 | return NULL; |
| 4446 | } |
| 4447 | |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4448 | Py_BEGIN_ALLOW_THREADS |
| 4449 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4450 | /* Windows, success=1, UNIX, success=0 */ |
| 4451 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4452 | #else |
| 4453 | #ifdef HAVE_UNLINKAT |
| 4454 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4455 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4456 | else |
| 4457 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4458 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4459 | #endif |
| 4460 | Py_END_ALLOW_THREADS |
| 4461 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4462 | if (result) |
| 4463 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4464 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4465 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4466 | } |
| 4467 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4468 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4469 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4470 | #ifdef MS_WINDOWS |
| 4471 | /*[clinic input] |
| 4472 | os.system -> long |
| 4473 | |
| 4474 | command: Py_UNICODE |
| 4475 | |
| 4476 | Execute the command in a subshell. |
| 4477 | [clinic start generated code]*/ |
| 4478 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4479 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4480 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4481 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4482 | { |
| 4483 | long result; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4484 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4485 | if (PySys_Audit("os.system", "(u)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4486 | return -1; |
| 4487 | } |
| 4488 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4489 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4490 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4491 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4492 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4493 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4494 | return result; |
| 4495 | } |
| 4496 | #else /* MS_WINDOWS */ |
| 4497 | /*[clinic input] |
| 4498 | os.system -> long |
| 4499 | |
| 4500 | command: FSConverter |
| 4501 | |
| 4502 | Execute the command in a subshell. |
| 4503 | [clinic start generated code]*/ |
| 4504 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4505 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4506 | os_system_impl(PyObject *module, PyObject *command) |
| 4507 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4508 | { |
| 4509 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4510 | const char *bytes = PyBytes_AsString(command); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4511 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4512 | if (PySys_Audit("os.system", "(O)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4513 | return -1; |
| 4514 | } |
| 4515 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4516 | Py_BEGIN_ALLOW_THREADS |
| 4517 | result = system(bytes); |
| 4518 | Py_END_ALLOW_THREADS |
| 4519 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4520 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4521 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4522 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4523 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4524 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4525 | /*[clinic input] |
| 4526 | os.umask |
| 4527 | |
| 4528 | mask: int |
| 4529 | / |
| 4530 | |
| 4531 | Set the current numeric umask and return the previous umask. |
| 4532 | [clinic start generated code]*/ |
| 4533 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4534 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4535 | os_umask_impl(PyObject *module, int mask) |
| 4536 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4537 | { |
| 4538 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4539 | if (i < 0) |
| 4540 | return posix_error(); |
| 4541 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4542 | } |
| 4543 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4544 | #ifdef MS_WINDOWS |
| 4545 | |
| 4546 | /* override the default DeleteFileW behavior so that directory |
| 4547 | symlinks can be removed with this function, the same as with |
| 4548 | Unix symlinks */ |
| 4549 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4550 | { |
| 4551 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4552 | WIN32_FIND_DATAW find_data; |
| 4553 | HANDLE find_data_handle; |
| 4554 | int is_directory = 0; |
| 4555 | int is_link = 0; |
| 4556 | |
| 4557 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4558 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4559 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4560 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4561 | it is a symlink */ |
| 4562 | if(is_directory && |
| 4563 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4564 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4565 | |
| 4566 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4567 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4568 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4569 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4570 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4571 | FindClose(find_data_handle); |
| 4572 | } |
| 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | if (is_directory && is_link) |
| 4577 | return RemoveDirectoryW(lpFileName); |
| 4578 | |
| 4579 | return DeleteFileW(lpFileName); |
| 4580 | } |
| 4581 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4582 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4583 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4584 | /*[clinic input] |
| 4585 | os.unlink |
| 4586 | |
| 4587 | path: path_t |
| 4588 | * |
| 4589 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4590 | |
| 4591 | Remove a file (same as remove()). |
| 4592 | |
| 4593 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4594 | and path should be relative; path will then be relative to that directory. |
| 4595 | dir_fd may not be implemented on your platform. |
| 4596 | If it is unavailable, using it will raise a NotImplementedError. |
| 4597 | |
| 4598 | [clinic start generated code]*/ |
| 4599 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4600 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4601 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4602 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4603 | { |
| 4604 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4605 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4606 | if (PySys_Audit("os.remove", "Oi", path->object, |
| 4607 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4608 | return NULL; |
| 4609 | } |
| 4610 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4611 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4612 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4613 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4614 | /* Windows, success=1, UNIX, success=0 */ |
| 4615 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4616 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4617 | #ifdef HAVE_UNLINKAT |
| 4618 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4619 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4620 | else |
| 4621 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4622 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4623 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4624 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4625 | Py_END_ALLOW_THREADS |
| 4626 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4627 | if (result) |
| 4628 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4629 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4630 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4631 | } |
| 4632 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4633 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4634 | /*[clinic input] |
| 4635 | os.remove = os.unlink |
| 4636 | |
| 4637 | Remove a file (same as unlink()). |
| 4638 | |
| 4639 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4640 | and path should be relative; path will then be relative to that directory. |
| 4641 | dir_fd may not be implemented on your platform. |
| 4642 | If it is unavailable, using it will raise a NotImplementedError. |
| 4643 | [clinic start generated code]*/ |
| 4644 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4645 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4646 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4647 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4648 | { |
| 4649 | return os_unlink_impl(module, path, dir_fd); |
| 4650 | } |
| 4651 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4652 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4653 | static PyStructSequence_Field uname_result_fields[] = { |
| 4654 | {"sysname", "operating system name"}, |
| 4655 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4656 | {"release", "operating system release"}, |
| 4657 | {"version", "operating system version"}, |
| 4658 | {"machine", "hardware identifier"}, |
| 4659 | {NULL} |
| 4660 | }; |
| 4661 | |
| 4662 | PyDoc_STRVAR(uname_result__doc__, |
| 4663 | "uname_result: Result from os.uname().\n\n\ |
| 4664 | This object may be accessed either as a tuple of\n\ |
| 4665 | (sysname, nodename, release, version, machine),\n\ |
| 4666 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4667 | \n\ |
| 4668 | See os.uname for more information."); |
| 4669 | |
| 4670 | static PyStructSequence_Desc uname_result_desc = { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4671 | MODNAME ".uname_result", /* name */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4672 | uname_result__doc__, /* doc */ |
| 4673 | uname_result_fields, |
| 4674 | 5 |
| 4675 | }; |
| 4676 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4677 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4678 | /*[clinic input] |
| 4679 | os.uname |
| 4680 | |
| 4681 | Return an object identifying the current operating system. |
| 4682 | |
| 4683 | The object behaves like a named tuple with the following fields: |
| 4684 | (sysname, nodename, release, version, machine) |
| 4685 | |
| 4686 | [clinic start generated code]*/ |
| 4687 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4688 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4689 | os_uname_impl(PyObject *module) |
| 4690 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4691 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4692 | struct utsname u; |
| 4693 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4694 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4695 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4696 | Py_BEGIN_ALLOW_THREADS |
| 4697 | res = uname(&u); |
| 4698 | Py_END_ALLOW_THREADS |
| 4699 | if (res < 0) |
| 4700 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4701 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 4702 | PyObject *UnameResultType = get_posix_state(module)->UnameResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4703 | value = PyStructSequence_New((PyTypeObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4704 | if (value == NULL) |
| 4705 | return NULL; |
| 4706 | |
| 4707 | #define SET(i, field) \ |
| 4708 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4709 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4710 | if (!o) { \ |
| 4711 | Py_DECREF(value); \ |
| 4712 | return NULL; \ |
| 4713 | } \ |
| 4714 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4715 | } \ |
| 4716 | |
| 4717 | SET(0, u.sysname); |
| 4718 | SET(1, u.nodename); |
| 4719 | SET(2, u.release); |
| 4720 | SET(3, u.version); |
| 4721 | SET(4, u.machine); |
| 4722 | |
| 4723 | #undef SET |
| 4724 | |
| 4725 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4726 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4727 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4728 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4729 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4730 | |
| 4731 | typedef struct { |
| 4732 | int now; |
| 4733 | time_t atime_s; |
| 4734 | long atime_ns; |
| 4735 | time_t mtime_s; |
| 4736 | long mtime_ns; |
| 4737 | } utime_t; |
| 4738 | |
| 4739 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4740 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4741 | * they also intentionally leak the declaration of a pointer named "time" |
| 4742 | */ |
| 4743 | #define UTIME_TO_TIMESPEC \ |
| 4744 | struct timespec ts[2]; \ |
| 4745 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4746 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4747 | time = NULL; \ |
| 4748 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4749 | ts[0].tv_sec = ut->atime_s; \ |
| 4750 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4751 | ts[1].tv_sec = ut->mtime_s; \ |
| 4752 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4753 | time = ts; \ |
| 4754 | } \ |
| 4755 | |
| 4756 | #define UTIME_TO_TIMEVAL \ |
| 4757 | struct timeval tv[2]; \ |
| 4758 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4759 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4760 | time = NULL; \ |
| 4761 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4762 | tv[0].tv_sec = ut->atime_s; \ |
| 4763 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4764 | tv[1].tv_sec = ut->mtime_s; \ |
| 4765 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4766 | time = tv; \ |
| 4767 | } \ |
| 4768 | |
| 4769 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4770 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4771 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4772 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4773 | time = NULL; \ |
| 4774 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4775 | u.actime = ut->atime_s; \ |
| 4776 | u.modtime = ut->mtime_s; \ |
| 4777 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | #define UTIME_TO_TIME_T \ |
| 4781 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4782 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4783 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4784 | time = NULL; \ |
| 4785 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4786 | timet[0] = ut->atime_s; \ |
| 4787 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4788 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4789 | } \ |
| 4790 | |
| 4791 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4792 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4793 | |
| 4794 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4795 | 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] | 4796 | { |
| 4797 | #ifdef HAVE_UTIMENSAT |
| 4798 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4799 | UTIME_TO_TIMESPEC; |
| 4800 | return utimensat(dir_fd, path, time, flags); |
| 4801 | #elif defined(HAVE_FUTIMESAT) |
| 4802 | UTIME_TO_TIMEVAL; |
| 4803 | /* |
| 4804 | * follow_symlinks will never be false here; |
| 4805 | * we only allow !follow_symlinks and dir_fd together |
| 4806 | * if we have utimensat() |
| 4807 | */ |
| 4808 | assert(follow_symlinks); |
| 4809 | return futimesat(dir_fd, path, time); |
| 4810 | #endif |
| 4811 | } |
| 4812 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4813 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4814 | #else |
| 4815 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4816 | #endif |
| 4817 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4818 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4819 | |
| 4820 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4821 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4822 | { |
| 4823 | #ifdef HAVE_FUTIMENS |
| 4824 | UTIME_TO_TIMESPEC; |
| 4825 | return futimens(fd, time); |
| 4826 | #else |
| 4827 | UTIME_TO_TIMEVAL; |
| 4828 | return futimes(fd, time); |
| 4829 | #endif |
| 4830 | } |
| 4831 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4832 | #define PATH_UTIME_HAVE_FD 1 |
| 4833 | #else |
| 4834 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4835 | #endif |
| 4836 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4837 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4838 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4839 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4840 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4841 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4842 | |
| 4843 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4844 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4845 | { |
| 4846 | #ifdef HAVE_UTIMENSAT |
| 4847 | UTIME_TO_TIMESPEC; |
| 4848 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4849 | #else |
| 4850 | UTIME_TO_TIMEVAL; |
| 4851 | return lutimes(path, time); |
| 4852 | #endif |
| 4853 | } |
| 4854 | |
| 4855 | #endif |
| 4856 | |
| 4857 | #ifndef MS_WINDOWS |
| 4858 | |
| 4859 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4860 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4861 | { |
| 4862 | #ifdef HAVE_UTIMENSAT |
| 4863 | UTIME_TO_TIMESPEC; |
| 4864 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4865 | #elif defined(HAVE_UTIMES) |
| 4866 | UTIME_TO_TIMEVAL; |
| 4867 | return utimes(path, time); |
| 4868 | #elif defined(HAVE_UTIME_H) |
| 4869 | UTIME_TO_UTIMBUF; |
| 4870 | return utime(path, time); |
| 4871 | #else |
| 4872 | UTIME_TO_TIME_T; |
| 4873 | return utime(path, time); |
| 4874 | #endif |
| 4875 | } |
| 4876 | |
| 4877 | #endif |
| 4878 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4879 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4880 | 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] | 4881 | { |
| 4882 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4883 | PyObject *divmod; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4884 | divmod = PyNumber_Divmod(py_long, get_posix_state(module)->billion); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4885 | if (!divmod) |
| 4886 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4887 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4888 | PyErr_Format(PyExc_TypeError, |
| 4889 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4890 | _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod))); |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4891 | goto exit; |
| 4892 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4893 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4894 | if ((*s == -1) && PyErr_Occurred()) |
| 4895 | goto exit; |
| 4896 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4897 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4898 | goto exit; |
| 4899 | |
| 4900 | result = 1; |
| 4901 | exit: |
| 4902 | Py_XDECREF(divmod); |
| 4903 | return result; |
| 4904 | } |
| 4905 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4906 | |
| 4907 | /*[clinic input] |
| 4908 | os.utime |
| 4909 | |
| 4910 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4911 | times: object = None |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4912 | * |
| 4913 | ns: object = NULL |
| 4914 | dir_fd: dir_fd(requires='futimensat') = None |
| 4915 | follow_symlinks: bool=True |
| 4916 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4917 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4918 | |
| 4919 | Set the access and modified time of path. |
| 4920 | |
| 4921 | path may always be specified as a string. |
| 4922 | On some platforms, path may also be specified as an open file descriptor. |
| 4923 | If this functionality is unavailable, using it raises an exception. |
| 4924 | |
| 4925 | If times is not None, it must be a tuple (atime, mtime); |
| 4926 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4927 | 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] | 4928 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4929 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4930 | 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] | 4931 | Specifying tuples for both times and ns is an error. |
| 4932 | |
| 4933 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4934 | and path should be relative; path will then be relative to that directory. |
| 4935 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4936 | link, utime will modify the symbolic link itself instead of the file the |
| 4937 | link points to. |
| 4938 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4939 | as an open file descriptor. |
| 4940 | dir_fd and follow_symlinks may not be available on your platform. |
| 4941 | If they are unavailable, using them will raise a NotImplementedError. |
| 4942 | |
| 4943 | [clinic start generated code]*/ |
| 4944 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4945 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4946 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4947 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4948 | /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4949 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4950 | #ifdef MS_WINDOWS |
| 4951 | HANDLE hFile; |
| 4952 | FILETIME atime, mtime; |
| 4953 | #else |
| 4954 | int result; |
| 4955 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4956 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4957 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4958 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4959 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4960 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4961 | if (times != Py_None && ns) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4962 | PyErr_SetString(PyExc_ValueError, |
| 4963 | "utime: you may specify either 'times'" |
| 4964 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4965 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4966 | } |
| 4967 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4968 | if (times != Py_None) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4969 | time_t a_sec, m_sec; |
| 4970 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4971 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4972 | PyErr_SetString(PyExc_TypeError, |
| 4973 | "utime: 'times' must be either" |
| 4974 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4975 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4976 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4977 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4978 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4979 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4980 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4981 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4982 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4983 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4984 | utime.atime_s = a_sec; |
| 4985 | utime.atime_ns = a_nsec; |
| 4986 | utime.mtime_s = m_sec; |
| 4987 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4988 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4989 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4990 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4991 | PyErr_SetString(PyExc_TypeError, |
| 4992 | "utime: 'ns' must be a tuple of two ints"); |
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; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4996 | 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] | 4997 | &utime.atime_s, &utime.atime_ns) || |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4998 | !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] | 4999 | &utime.mtime_s, &utime.mtime_ns)) { |
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 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5002 | } |
| 5003 | else { |
| 5004 | /* times and ns are both None/unspecified. use "now". */ |
| 5005 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5006 | } |
| 5007 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 5008 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5009 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5010 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5011 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 5012 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5013 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 5014 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 5015 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5016 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5017 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5018 | #if !defined(HAVE_UTIMENSAT) |
| 5019 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 5020 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5021 | "utime: cannot use dir_fd and follow_symlinks " |
| 5022 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5023 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5024 | } |
| 5025 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5026 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5027 | if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None, |
| 5028 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 5029 | return NULL; |
| 5030 | } |
| 5031 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5032 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5033 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5034 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 5035 | NULL, OPEN_EXISTING, |
| 5036 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5037 | Py_END_ALLOW_THREADS |
| 5038 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5039 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5040 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 5041 | } |
| 5042 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5043 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 5044 | GetSystemTimeAsFileTime(&mtime); |
| 5045 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5046 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5047 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 5048 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 5049 | _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] | 5050 | } |
| 5051 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 5052 | /* Avoid putting the file name into the error here, |
| 5053 | as that may confuse the user into believing that |
| 5054 | something is wrong with the file, when it also |
| 5055 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 5056 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5057 | CloseHandle(hFile); |
| 5058 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5059 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5060 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5061 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5062 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 5063 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 5064 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5065 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5066 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5067 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 5068 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5069 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5070 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5071 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5072 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5073 | else |
| 5074 | #endif |
| 5075 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5076 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5077 | if (path->fd != -1) |
| 5078 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5079 | else |
| 5080 | #endif |
| 5081 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5082 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5083 | |
| 5084 | Py_END_ALLOW_THREADS |
| 5085 | |
| 5086 | if (result < 0) { |
| 5087 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5088 | posix_error(); |
| 5089 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5090 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5091 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5092 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5093 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5094 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5097 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5098 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5099 | |
| 5100 | /*[clinic input] |
| 5101 | os._exit |
| 5102 | |
| 5103 | status: int |
| 5104 | |
| 5105 | Exit to the system with specified status, without normal exit processing. |
| 5106 | [clinic start generated code]*/ |
| 5107 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5108 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5109 | os__exit_impl(PyObject *module, int status) |
| 5110 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5111 | { |
| 5112 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5113 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5116 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5117 | #define EXECV_CHAR wchar_t |
| 5118 | #else |
| 5119 | #define EXECV_CHAR char |
| 5120 | #endif |
| 5121 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5122 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5123 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5124 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5125 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5126 | Py_ssize_t i; |
| 5127 | for (i = 0; i < count; i++) |
| 5128 | PyMem_Free(array[i]); |
| 5129 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5130 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5131 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5132 | static int |
| 5133 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5134 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5135 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5136 | PyObject *ub; |
| 5137 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5138 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5139 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5140 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5141 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 5142 | if (*out) |
| 5143 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5144 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5145 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5146 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5147 | size = PyBytes_GET_SIZE(ub); |
| 5148 | *out = PyMem_Malloc(size + 1); |
| 5149 | if (*out) { |
| 5150 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 5151 | result = 1; |
| 5152 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5153 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5154 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5155 | Py_DECREF(ub); |
| 5156 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5157 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5158 | #endif |
| 5159 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5160 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5161 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5162 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5163 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5164 | Py_ssize_t i, pos, envc; |
| 5165 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5166 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5167 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5168 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5169 | i = PyMapping_Size(env); |
| 5170 | if (i < 0) |
| 5171 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5172 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5173 | if (envlist == NULL) { |
| 5174 | PyErr_NoMemory(); |
| 5175 | return NULL; |
| 5176 | } |
| 5177 | envc = 0; |
| 5178 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5179 | if (!keys) |
| 5180 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5181 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5182 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5183 | goto error; |
| 5184 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5185 | PyErr_Format(PyExc_TypeError, |
| 5186 | "env.keys() or env.values() is not a list"); |
| 5187 | goto error; |
| 5188 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5189 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5190 | for (pos = 0; pos < i; pos++) { |
| 5191 | key = PyList_GetItem(keys, pos); |
| 5192 | val = PyList_GetItem(vals, pos); |
| 5193 | if (!key || !val) |
| 5194 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5195 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5196 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5197 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 5198 | goto error; |
| 5199 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 5200 | Py_DECREF(key2); |
| 5201 | goto error; |
| 5202 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5203 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 5204 | defining hidden environment variables. */ |
| 5205 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 5206 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 5207 | { |
| 5208 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5209 | Py_DECREF(key2); |
| 5210 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5211 | goto error; |
| 5212 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5213 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 5214 | #else |
| 5215 | if (!PyUnicode_FSConverter(key, &key2)) |
| 5216 | goto error; |
| 5217 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 5218 | Py_DECREF(key2); |
| 5219 | goto error; |
| 5220 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5221 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 5222 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 5223 | { |
| 5224 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5225 | Py_DECREF(key2); |
| 5226 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5227 | goto error; |
| 5228 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5229 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 5230 | PyBytes_AS_STRING(val2)); |
| 5231 | #endif |
| 5232 | Py_DECREF(key2); |
| 5233 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5234 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5235 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5236 | |
| 5237 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 5238 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5239 | goto error; |
| 5240 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5241 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5242 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5243 | } |
| 5244 | Py_DECREF(vals); |
| 5245 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5246 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5247 | envlist[envc] = 0; |
| 5248 | *envc_ptr = envc; |
| 5249 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5250 | |
| 5251 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5252 | Py_XDECREF(keys); |
| 5253 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5254 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5255 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5256 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5257 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5258 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5259 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5260 | { |
| 5261 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5262 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5263 | if (argvlist == NULL) { |
| 5264 | PyErr_NoMemory(); |
| 5265 | return NULL; |
| 5266 | } |
| 5267 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5268 | PyObject* item = PySequence_ITEM(argv, i); |
| 5269 | if (item == NULL) |
| 5270 | goto fail; |
| 5271 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5272 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5273 | goto fail; |
| 5274 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5275 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5276 | } |
| 5277 | argvlist[*argc] = NULL; |
| 5278 | return argvlist; |
| 5279 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5280 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5281 | free_string_array(argvlist, *argc); |
| 5282 | return NULL; |
| 5283 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5284 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5285 | #endif |
| 5286 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5287 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5288 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5289 | /*[clinic input] |
| 5290 | os.execv |
| 5291 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5292 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5293 | Path of executable file. |
| 5294 | argv: object |
| 5295 | Tuple or list of strings. |
| 5296 | / |
| 5297 | |
| 5298 | Execute an executable path with arguments, replacing current process. |
| 5299 | [clinic start generated code]*/ |
| 5300 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5301 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5302 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5303 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5304 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5305 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5306 | Py_ssize_t argc; |
| 5307 | |
| 5308 | /* execv has two arguments: (path, argv), where |
| 5309 | argv is a list or tuple of strings. */ |
| 5310 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5311 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5312 | PyErr_SetString(PyExc_TypeError, |
| 5313 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5314 | return NULL; |
| 5315 | } |
| 5316 | argc = PySequence_Size(argv); |
| 5317 | if (argc < 1) { |
| 5318 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5319 | return NULL; |
| 5320 | } |
| 5321 | |
| 5322 | argvlist = parse_arglist(argv, &argc); |
| 5323 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5324 | return NULL; |
| 5325 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5326 | if (!argvlist[0][0]) { |
| 5327 | PyErr_SetString(PyExc_ValueError, |
| 5328 | "execv() arg 2 first element cannot be empty"); |
| 5329 | free_string_array(argvlist, argc); |
| 5330 | return NULL; |
| 5331 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5332 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5333 | if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5334 | free_string_array(argvlist, argc); |
| 5335 | return NULL; |
| 5336 | } |
| 5337 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5338 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5339 | #ifdef HAVE_WEXECV |
| 5340 | _wexecv(path->wide, argvlist); |
| 5341 | #else |
| 5342 | execv(path->narrow, argvlist); |
| 5343 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5344 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5345 | |
| 5346 | /* If we get here it's definitely an error */ |
| 5347 | |
| 5348 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5349 | return posix_error(); |
| 5350 | } |
| 5351 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5352 | |
| 5353 | /*[clinic input] |
| 5354 | os.execve |
| 5355 | |
| 5356 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5357 | Path of executable file. |
| 5358 | argv: object |
| 5359 | Tuple or list of strings. |
| 5360 | env: object |
| 5361 | Dictionary of strings mapping to strings. |
| 5362 | |
| 5363 | Execute an executable path with arguments, replacing current process. |
| 5364 | [clinic start generated code]*/ |
| 5365 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5366 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5367 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5368 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5369 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5370 | EXECV_CHAR **argvlist = NULL; |
| 5371 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5372 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5373 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5374 | /* execve has three arguments: (path, argv, env), where |
| 5375 | argv is a list or tuple of strings and env is a dictionary |
| 5376 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5377 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5378 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5379 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5380 | "execve: argv must be a tuple or list"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5381 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5382 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5383 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5384 | if (argc < 1) { |
| 5385 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5386 | return NULL; |
| 5387 | } |
| 5388 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5389 | if (!PyMapping_Check(env)) { |
| 5390 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5391 | "execve: environment must be a mapping object"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5392 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5393 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5394 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5395 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5396 | if (argvlist == NULL) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5397 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5398 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5399 | if (!argvlist[0][0]) { |
| 5400 | PyErr_SetString(PyExc_ValueError, |
| 5401 | "execve: argv first element cannot be empty"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5402 | goto fail_0; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5403 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5404 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5405 | envlist = parse_envlist(env, &envc); |
| 5406 | if (envlist == NULL) |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5407 | goto fail_0; |
| 5408 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5409 | if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5410 | goto fail_1; |
| 5411 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5412 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5413 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5414 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5415 | if (path->fd > -1) |
| 5416 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5417 | else |
| 5418 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5419 | #ifdef HAVE_WEXECV |
| 5420 | _wexecve(path->wide, argvlist, envlist); |
| 5421 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5422 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5423 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5424 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5425 | |
| 5426 | /* If we get here it's definitely an error */ |
| 5427 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5428 | posix_path_error(path); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5429 | fail_1: |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5430 | free_string_array(envlist, envc); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5431 | fail_0: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5432 | if (argvlist) |
| 5433 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5434 | return NULL; |
| 5435 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5436 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5437 | #endif /* HAVE_EXECV */ |
| 5438 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5439 | #ifdef HAVE_POSIX_SPAWN |
| 5440 | |
| 5441 | enum posix_spawn_file_actions_identifier { |
| 5442 | POSIX_SPAWN_OPEN, |
| 5443 | POSIX_SPAWN_CLOSE, |
| 5444 | POSIX_SPAWN_DUP2 |
| 5445 | }; |
| 5446 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5447 | #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] | 5448 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5449 | convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5450 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5451 | |
| 5452 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5453 | parse_posix_spawn_flags(PyObject *module, const char *func_name, PyObject *setpgroup, |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5454 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5455 | PyObject *setsigdef, PyObject *scheduler, |
| 5456 | posix_spawnattr_t *attrp) |
| 5457 | { |
| 5458 | long all_flags = 0; |
| 5459 | |
| 5460 | errno = posix_spawnattr_init(attrp); |
| 5461 | if (errno) { |
| 5462 | posix_error(); |
| 5463 | return -1; |
| 5464 | } |
| 5465 | |
| 5466 | if (setpgroup) { |
| 5467 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5468 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5469 | goto fail; |
| 5470 | } |
| 5471 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5472 | if (errno) { |
| 5473 | posix_error(); |
| 5474 | goto fail; |
| 5475 | } |
| 5476 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5477 | } |
| 5478 | |
| 5479 | if (resetids) { |
| 5480 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5481 | } |
| 5482 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5483 | if (setsid) { |
| 5484 | #ifdef POSIX_SPAWN_SETSID |
| 5485 | all_flags |= POSIX_SPAWN_SETSID; |
| 5486 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5487 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5488 | #else |
| 5489 | argument_unavailable_error(func_name, "setsid"); |
| 5490 | return -1; |
| 5491 | #endif |
| 5492 | } |
| 5493 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5494 | if (setsigmask) { |
| 5495 | sigset_t set; |
| 5496 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5497 | goto fail; |
| 5498 | } |
| 5499 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5500 | if (errno) { |
| 5501 | posix_error(); |
| 5502 | goto fail; |
| 5503 | } |
| 5504 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5505 | } |
| 5506 | |
| 5507 | if (setsigdef) { |
| 5508 | sigset_t set; |
| 5509 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5510 | goto fail; |
| 5511 | } |
| 5512 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5513 | if (errno) { |
| 5514 | posix_error(); |
| 5515 | goto fail; |
| 5516 | } |
| 5517 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5518 | } |
| 5519 | |
| 5520 | if (scheduler) { |
| 5521 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5522 | PyObject *py_schedpolicy; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5523 | PyObject *schedparam_obj; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5524 | struct sched_param schedparam; |
| 5525 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5526 | if (!PyArg_ParseTuple(scheduler, "OO" |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5527 | ";A scheduler tuple must have two elements", |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5528 | &py_schedpolicy, &schedparam_obj)) { |
| 5529 | goto fail; |
| 5530 | } |
| 5531 | if (!convert_sched_param(module, schedparam_obj, &schedparam)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5532 | goto fail; |
| 5533 | } |
| 5534 | if (py_schedpolicy != Py_None) { |
| 5535 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5536 | |
| 5537 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5538 | goto fail; |
| 5539 | } |
| 5540 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5541 | if (errno) { |
| 5542 | posix_error(); |
| 5543 | goto fail; |
| 5544 | } |
| 5545 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5546 | } |
| 5547 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5548 | if (errno) { |
| 5549 | posix_error(); |
| 5550 | goto fail; |
| 5551 | } |
| 5552 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5553 | #else |
| 5554 | PyErr_SetString(PyExc_NotImplementedError, |
| 5555 | "The scheduler option is not supported in this system."); |
| 5556 | goto fail; |
| 5557 | #endif |
| 5558 | } |
| 5559 | |
| 5560 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5561 | if (errno) { |
| 5562 | posix_error(); |
| 5563 | goto fail; |
| 5564 | } |
| 5565 | |
| 5566 | return 0; |
| 5567 | |
| 5568 | fail: |
| 5569 | (void)posix_spawnattr_destroy(attrp); |
| 5570 | return -1; |
| 5571 | } |
| 5572 | |
| 5573 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5574 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5575 | posix_spawn_file_actions_t *file_actionsp, |
| 5576 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5577 | { |
| 5578 | PyObject *seq; |
| 5579 | PyObject *file_action = NULL; |
| 5580 | PyObject *tag_obj; |
| 5581 | |
| 5582 | seq = PySequence_Fast(file_actions, |
| 5583 | "file_actions must be a sequence or None"); |
| 5584 | if (seq == NULL) { |
| 5585 | return -1; |
| 5586 | } |
| 5587 | |
| 5588 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5589 | if (errno) { |
| 5590 | posix_error(); |
| 5591 | Py_DECREF(seq); |
| 5592 | return -1; |
| 5593 | } |
| 5594 | |
Zackery Spytz | d52a83a | 2019-06-26 14:54:20 -0600 | [diff] [blame] | 5595 | 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] | 5596 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5597 | Py_INCREF(file_action); |
| 5598 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5599 | PyErr_SetString(PyExc_TypeError, |
| 5600 | "Each file_actions element must be a non-empty tuple"); |
| 5601 | goto fail; |
| 5602 | } |
| 5603 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5604 | if (tag == -1 && PyErr_Occurred()) { |
| 5605 | goto fail; |
| 5606 | } |
| 5607 | |
| 5608 | /* Populate the file_actions object */ |
| 5609 | switch (tag) { |
| 5610 | case POSIX_SPAWN_OPEN: { |
| 5611 | int fd, oflag; |
| 5612 | PyObject *path; |
| 5613 | unsigned long mode; |
| 5614 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5615 | ";A open file_action tuple must have 5 elements", |
| 5616 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5617 | &oflag, &mode)) |
| 5618 | { |
| 5619 | goto fail; |
| 5620 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5621 | if (PyList_Append(temp_buffer, path)) { |
| 5622 | Py_DECREF(path); |
| 5623 | goto fail; |
| 5624 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5625 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5626 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5627 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5628 | if (errno) { |
| 5629 | posix_error(); |
| 5630 | goto fail; |
| 5631 | } |
| 5632 | break; |
| 5633 | } |
| 5634 | case POSIX_SPAWN_CLOSE: { |
| 5635 | int fd; |
| 5636 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5637 | ";A close file_action tuple must have 2 elements", |
| 5638 | &tag_obj, &fd)) |
| 5639 | { |
| 5640 | goto fail; |
| 5641 | } |
| 5642 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5643 | if (errno) { |
| 5644 | posix_error(); |
| 5645 | goto fail; |
| 5646 | } |
| 5647 | break; |
| 5648 | } |
| 5649 | case POSIX_SPAWN_DUP2: { |
| 5650 | int fd1, fd2; |
| 5651 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5652 | ";A dup2 file_action tuple must have 3 elements", |
| 5653 | &tag_obj, &fd1, &fd2)) |
| 5654 | { |
| 5655 | goto fail; |
| 5656 | } |
| 5657 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5658 | fd1, fd2); |
| 5659 | if (errno) { |
| 5660 | posix_error(); |
| 5661 | goto fail; |
| 5662 | } |
| 5663 | break; |
| 5664 | } |
| 5665 | default: { |
| 5666 | PyErr_SetString(PyExc_TypeError, |
| 5667 | "Unknown file_actions identifier"); |
| 5668 | goto fail; |
| 5669 | } |
| 5670 | } |
| 5671 | Py_DECREF(file_action); |
| 5672 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5673 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5674 | Py_DECREF(seq); |
| 5675 | return 0; |
| 5676 | |
| 5677 | fail: |
| 5678 | Py_DECREF(seq); |
| 5679 | Py_DECREF(file_action); |
| 5680 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5681 | return -1; |
| 5682 | } |
| 5683 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5684 | |
| 5685 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5686 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 5687 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5688 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5689 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5690 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5691 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5692 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5693 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5694 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5695 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5696 | posix_spawnattr_t attr; |
| 5697 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5698 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5699 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5700 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5701 | pid_t pid; |
| 5702 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5703 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5704 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5705 | 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] | 5706 | like posix.environ. */ |
| 5707 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5708 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5709 | PyErr_Format(PyExc_TypeError, |
| 5710 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5711 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5712 | } |
| 5713 | argc = PySequence_Size(argv); |
| 5714 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5715 | PyErr_Format(PyExc_ValueError, |
| 5716 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5717 | return NULL; |
| 5718 | } |
| 5719 | |
| 5720 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5721 | PyErr_Format(PyExc_TypeError, |
| 5722 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5723 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5724 | } |
| 5725 | |
| 5726 | argvlist = parse_arglist(argv, &argc); |
| 5727 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5728 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5729 | } |
| 5730 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5731 | PyErr_Format(PyExc_ValueError, |
| 5732 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5733 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5734 | } |
| 5735 | |
| 5736 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5737 | if (envlist == NULL) { |
| 5738 | goto exit; |
| 5739 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5740 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 5741 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5742 | /* There is a bug in old versions of glibc that makes some of the |
| 5743 | * helper functions for manipulating file actions not copy the provided |
| 5744 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5745 | * copy the value of path for some old versions of glibc (<2.20). |
| 5746 | * The use of temp_buffer here is a workaround that keeps the |
| 5747 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5748 | * Check https://bugs.python.org/issue33630 and |
| 5749 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5750 | temp_buffer = PyList_New(0); |
| 5751 | if (!temp_buffer) { |
| 5752 | goto exit; |
| 5753 | } |
| 5754 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5755 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5756 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5757 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5758 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5759 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5760 | if (parse_posix_spawn_flags(module, func_name, setpgroup, resetids, setsid, |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5761 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5762 | goto exit; |
| 5763 | } |
| 5764 | attrp = &attr; |
| 5765 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5766 | if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5767 | goto exit; |
| 5768 | } |
| 5769 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5770 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5771 | #ifdef HAVE_POSIX_SPAWNP |
| 5772 | if (use_posix_spawnp) { |
| 5773 | err_code = posix_spawnp(&pid, path->narrow, |
| 5774 | file_actionsp, attrp, argvlist, envlist); |
| 5775 | } |
| 5776 | else |
| 5777 | #endif /* HAVE_POSIX_SPAWNP */ |
| 5778 | { |
| 5779 | err_code = posix_spawn(&pid, path->narrow, |
| 5780 | file_actionsp, attrp, argvlist, envlist); |
| 5781 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5782 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5783 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5784 | if (err_code) { |
| 5785 | errno = err_code; |
| 5786 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5787 | goto exit; |
| 5788 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5789 | #ifdef _Py_MEMORY_SANITIZER |
| 5790 | __msan_unpoison(&pid, sizeof(pid)); |
| 5791 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5792 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5793 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5794 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5795 | if (file_actionsp) { |
| 5796 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5797 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5798 | if (attrp) { |
| 5799 | (void)posix_spawnattr_destroy(attrp); |
| 5800 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5801 | if (envlist) { |
| 5802 | free_string_array(envlist, envc); |
| 5803 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5804 | if (argvlist) { |
| 5805 | free_string_array(argvlist, argc); |
| 5806 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5807 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5808 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5809 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5810 | |
| 5811 | |
| 5812 | /*[clinic input] |
| 5813 | |
| 5814 | os.posix_spawn |
| 5815 | path: path_t |
| 5816 | Path of executable file. |
| 5817 | argv: object |
| 5818 | Tuple or list of strings. |
| 5819 | env: object |
| 5820 | Dictionary of strings mapping to strings. |
| 5821 | / |
| 5822 | * |
| 5823 | file_actions: object(c_default='NULL') = () |
| 5824 | A sequence of file action tuples. |
| 5825 | setpgroup: object = NULL |
| 5826 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5827 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5828 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 5829 | setsid: bool(accept={int}) = False |
| 5830 | 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] | 5831 | setsigmask: object(c_default='NULL') = () |
| 5832 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5833 | setsigdef: object(c_default='NULL') = () |
| 5834 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5835 | scheduler: object = NULL |
| 5836 | A tuple with the scheduler policy (optional) and parameters. |
| 5837 | |
| 5838 | Execute the program specified by path in a new process. |
| 5839 | [clinic start generated code]*/ |
| 5840 | |
| 5841 | static PyObject * |
| 5842 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5843 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5844 | PyObject *setpgroup, int resetids, int setsid, |
| 5845 | PyObject *setsigmask, PyObject *setsigdef, |
| 5846 | PyObject *scheduler) |
| 5847 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5848 | { |
| 5849 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5850 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5851 | scheduler); |
| 5852 | } |
| 5853 | #endif /* HAVE_POSIX_SPAWN */ |
| 5854 | |
| 5855 | |
| 5856 | |
| 5857 | #ifdef HAVE_POSIX_SPAWNP |
| 5858 | /*[clinic input] |
| 5859 | |
| 5860 | os.posix_spawnp |
| 5861 | path: path_t |
| 5862 | Path of executable file. |
| 5863 | argv: object |
| 5864 | Tuple or list of strings. |
| 5865 | env: object |
| 5866 | Dictionary of strings mapping to strings. |
| 5867 | / |
| 5868 | * |
| 5869 | file_actions: object(c_default='NULL') = () |
| 5870 | A sequence of file action tuples. |
| 5871 | setpgroup: object = NULL |
| 5872 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5873 | resetids: bool(accept={int}) = False |
| 5874 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5875 | setsid: bool(accept={int}) = False |
| 5876 | 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] | 5877 | setsigmask: object(c_default='NULL') = () |
| 5878 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5879 | setsigdef: object(c_default='NULL') = () |
| 5880 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5881 | scheduler: object = NULL |
| 5882 | A tuple with the scheduler policy (optional) and parameters. |
| 5883 | |
| 5884 | Execute the program specified by path in a new process. |
| 5885 | [clinic start generated code]*/ |
| 5886 | |
| 5887 | static PyObject * |
| 5888 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5889 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5890 | PyObject *setpgroup, int resetids, int setsid, |
| 5891 | PyObject *setsigmask, PyObject *setsigdef, |
| 5892 | PyObject *scheduler) |
| 5893 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5894 | { |
| 5895 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5896 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5897 | scheduler); |
| 5898 | } |
| 5899 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5900 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5901 | #ifdef HAVE_RTPSPAWN |
| 5902 | static intptr_t |
| 5903 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], |
| 5904 | const char *envp[]) |
| 5905 | { |
| 5906 | RTP_ID rtpid; |
| 5907 | int status; |
| 5908 | pid_t res; |
| 5909 | int async_err = 0; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5910 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5911 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
| 5912 | uStackSize=0 cannot be used, the default stack size is too small for |
| 5913 | Python. */ |
| 5914 | if (envp) { |
| 5915 | rtpid = rtpSpawn(rtpFileName, argv, envp, |
| 5916 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5917 | } |
| 5918 | else { |
| 5919 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, |
| 5920 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5921 | } |
| 5922 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { |
| 5923 | do { |
| 5924 | res = waitpid((pid_t)rtpid, &status, 0); |
| 5925 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 5926 | |
| 5927 | if (res < 0) |
| 5928 | return RTP_ID_ERROR; |
| 5929 | return ((intptr_t)status); |
| 5930 | } |
| 5931 | return ((intptr_t)rtpid); |
| 5932 | } |
| 5933 | #endif |
| 5934 | |
| 5935 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5936 | /*[clinic input] |
| 5937 | os.spawnv |
| 5938 | |
| 5939 | mode: int |
| 5940 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5941 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5942 | Path of executable file. |
| 5943 | argv: object |
| 5944 | Tuple or list of strings. |
| 5945 | / |
| 5946 | |
| 5947 | Execute the program specified by path in a new process. |
| 5948 | [clinic start generated code]*/ |
| 5949 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5950 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5951 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5952 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5953 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5954 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5955 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5956 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5957 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5958 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5959 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5960 | /* spawnv has three arguments: (mode, path, argv), where |
| 5961 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5962 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5963 | if (PyList_Check(argv)) { |
| 5964 | argc = PyList_Size(argv); |
| 5965 | getitem = PyList_GetItem; |
| 5966 | } |
| 5967 | else if (PyTuple_Check(argv)) { |
| 5968 | argc = PyTuple_Size(argv); |
| 5969 | getitem = PyTuple_GetItem; |
| 5970 | } |
| 5971 | else { |
| 5972 | PyErr_SetString(PyExc_TypeError, |
| 5973 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5974 | return NULL; |
| 5975 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5976 | if (argc == 0) { |
| 5977 | PyErr_SetString(PyExc_ValueError, |
| 5978 | "spawnv() arg 2 cannot be empty"); |
| 5979 | return NULL; |
| 5980 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5981 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5982 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5983 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5984 | return PyErr_NoMemory(); |
| 5985 | } |
| 5986 | for (i = 0; i < argc; i++) { |
| 5987 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5988 | &argvlist[i])) { |
| 5989 | free_string_array(argvlist, i); |
| 5990 | PyErr_SetString( |
| 5991 | PyExc_TypeError, |
| 5992 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5993 | return NULL; |
| 5994 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5995 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5996 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5997 | PyErr_SetString( |
| 5998 | PyExc_ValueError, |
| 5999 | "spawnv() arg 2 first element cannot be empty"); |
| 6000 | return NULL; |
| 6001 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6002 | } |
| 6003 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6004 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6005 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6006 | if (mode == _OLD_P_OVERLAY) |
| 6007 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6008 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6009 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6010 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6011 | Py_None) < 0) { |
| 6012 | free_string_array(argvlist, argc); |
| 6013 | return NULL; |
| 6014 | } |
| 6015 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6016 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6017 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6018 | #ifdef HAVE_WSPAWNV |
| 6019 | spawnval = _wspawnv(mode, path->wide, argvlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6020 | #elif defined(HAVE_RTPSPAWN) |
| 6021 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6022 | #else |
| 6023 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 6024 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6025 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6026 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6027 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6028 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6029 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6030 | if (spawnval == -1) |
| 6031 | return posix_error(); |
| 6032 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6033 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6034 | } |
| 6035 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6036 | /*[clinic input] |
| 6037 | os.spawnve |
| 6038 | |
| 6039 | mode: int |
| 6040 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6041 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6042 | Path of executable file. |
| 6043 | argv: object |
| 6044 | Tuple or list of strings. |
| 6045 | env: object |
| 6046 | Dictionary of strings mapping to strings. |
| 6047 | / |
| 6048 | |
| 6049 | Execute the program specified by path in a new process. |
| 6050 | [clinic start generated code]*/ |
| 6051 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6052 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6053 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6054 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6055 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6056 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6057 | EXECV_CHAR **argvlist; |
| 6058 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6059 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 6060 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6061 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6062 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6063 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6064 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6065 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 6066 | argv is a list or tuple of strings and env is a dictionary |
| 6067 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6068 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6069 | if (PyList_Check(argv)) { |
| 6070 | argc = PyList_Size(argv); |
| 6071 | getitem = PyList_GetItem; |
| 6072 | } |
| 6073 | else if (PyTuple_Check(argv)) { |
| 6074 | argc = PyTuple_Size(argv); |
| 6075 | getitem = PyTuple_GetItem; |
| 6076 | } |
| 6077 | else { |
| 6078 | PyErr_SetString(PyExc_TypeError, |
| 6079 | "spawnve() arg 2 must be a tuple or list"); |
| 6080 | goto fail_0; |
| 6081 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 6082 | if (argc == 0) { |
| 6083 | PyErr_SetString(PyExc_ValueError, |
| 6084 | "spawnve() arg 2 cannot be empty"); |
| 6085 | goto fail_0; |
| 6086 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6087 | if (!PyMapping_Check(env)) { |
| 6088 | PyErr_SetString(PyExc_TypeError, |
| 6089 | "spawnve() arg 3 must be a mapping object"); |
| 6090 | goto fail_0; |
| 6091 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6092 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6093 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6094 | if (argvlist == NULL) { |
| 6095 | PyErr_NoMemory(); |
| 6096 | goto fail_0; |
| 6097 | } |
| 6098 | for (i = 0; i < argc; i++) { |
| 6099 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 6100 | &argvlist[i])) |
| 6101 | { |
| 6102 | lastarg = i; |
| 6103 | goto fail_1; |
| 6104 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6105 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6106 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6107 | PyErr_SetString( |
| 6108 | PyExc_ValueError, |
| 6109 | "spawnv() arg 2 first element cannot be empty"); |
| 6110 | goto fail_1; |
| 6111 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6112 | } |
| 6113 | lastarg = argc; |
| 6114 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6115 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6116 | envlist = parse_envlist(env, &envc); |
| 6117 | if (envlist == NULL) |
| 6118 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6119 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6120 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6121 | if (mode == _OLD_P_OVERLAY) |
| 6122 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6123 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6124 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6125 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6126 | goto fail_2; |
| 6127 | } |
| 6128 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6129 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6130 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6131 | #ifdef HAVE_WSPAWNV |
| 6132 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6133 | #elif defined(HAVE_RTPSPAWN) |
| 6134 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, |
| 6135 | (const char **)envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6136 | #else |
| 6137 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 6138 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6139 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6140 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6141 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6142 | if (spawnval == -1) |
| 6143 | (void) posix_error(); |
| 6144 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6145 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6146 | |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6147 | fail_2: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6148 | while (--envc >= 0) |
| 6149 | PyMem_DEL(envlist[envc]); |
| 6150 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 6151 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6152 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6153 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6154 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6155 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6156 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6157 | #endif /* HAVE_SPAWNV */ |
| 6158 | |
| 6159 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6160 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6161 | |
| 6162 | /* Helper function to validate arguments. |
| 6163 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 6164 | If obj is non-NULL it must be callable. */ |
| 6165 | static int |
| 6166 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 6167 | { |
| 6168 | if (obj && !PyCallable_Check(obj)) { |
| 6169 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6170 | obj_name, _PyType_Name(Py_TYPE(obj))); |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6171 | return -1; |
| 6172 | } |
| 6173 | return 0; |
| 6174 | } |
| 6175 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6176 | /*[clinic input] |
| 6177 | os.register_at_fork |
| 6178 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6179 | * |
| 6180 | before: object=NULL |
| 6181 | A callable to be called in the parent before the fork() syscall. |
| 6182 | after_in_child: object=NULL |
| 6183 | A callable to be called in the child after fork(). |
| 6184 | after_in_parent: object=NULL |
| 6185 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6186 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6187 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6188 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6189 | 'before' callbacks are called in reverse order. |
| 6190 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6191 | |
| 6192 | [clinic start generated code]*/ |
| 6193 | |
| 6194 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6195 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 6196 | PyObject *after_in_child, PyObject *after_in_parent) |
| 6197 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6198 | { |
| 6199 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6200 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6201 | if (!before && !after_in_child && !after_in_parent) { |
| 6202 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 6203 | return NULL; |
| 6204 | } |
| 6205 | if (check_null_or_callable(before, "before") || |
| 6206 | check_null_or_callable(after_in_child, "after_in_child") || |
| 6207 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6208 | return NULL; |
| 6209 | } |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6210 | interp = _PyInterpreterState_GET(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6211 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6212 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6213 | return NULL; |
| 6214 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6215 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6216 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6217 | } |
| 6218 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 6219 | return NULL; |
| 6220 | } |
| 6221 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6222 | } |
| 6223 | #endif /* HAVE_FORK */ |
| 6224 | |
| 6225 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6226 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6227 | /*[clinic input] |
| 6228 | os.fork1 |
| 6229 | |
| 6230 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 6231 | |
| 6232 | Return 0 to child process and PID of child to parent process. |
| 6233 | [clinic start generated code]*/ |
| 6234 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6235 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6236 | os_fork1_impl(PyObject *module) |
| 6237 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6238 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6239 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6240 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6241 | if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6242 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6243 | return NULL; |
| 6244 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6245 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6246 | pid = fork1(); |
| 6247 | if (pid == 0) { |
| 6248 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6249 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6250 | } else { |
| 6251 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6252 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6253 | } |
| 6254 | if (pid == -1) |
| 6255 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6256 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6257 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6258 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6259 | |
| 6260 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6261 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6262 | /*[clinic input] |
| 6263 | os.fork |
| 6264 | |
| 6265 | Fork a child process. |
| 6266 | |
| 6267 | Return 0 to child process and PID of child to parent process. |
| 6268 | [clinic start generated code]*/ |
| 6269 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6270 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6271 | os_fork_impl(PyObject *module) |
| 6272 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6273 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6274 | pid_t pid; |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 6275 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 6276 | if (interp->config._isolated_interpreter) { |
| 6277 | PyErr_SetString(PyExc_RuntimeError, |
| 6278 | "fork not supported for isolated subinterpreters"); |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6279 | return NULL; |
| 6280 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6281 | if (PySys_Audit("os.fork", NULL) < 0) { |
| 6282 | return NULL; |
| 6283 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6284 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6285 | pid = fork(); |
| 6286 | if (pid == 0) { |
| 6287 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6288 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6289 | } else { |
| 6290 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6291 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6292 | } |
| 6293 | if (pid == -1) |
| 6294 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6295 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6296 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6297 | #endif /* HAVE_FORK */ |
| 6298 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6299 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6300 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6301 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6302 | /*[clinic input] |
| 6303 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6304 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6305 | policy: int |
| 6306 | |
| 6307 | Get the maximum scheduling priority for policy. |
| 6308 | [clinic start generated code]*/ |
| 6309 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6310 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6311 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 6312 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6313 | { |
| 6314 | int max; |
| 6315 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6316 | max = sched_get_priority_max(policy); |
| 6317 | if (max < 0) |
| 6318 | return posix_error(); |
| 6319 | return PyLong_FromLong(max); |
| 6320 | } |
| 6321 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6322 | |
| 6323 | /*[clinic input] |
| 6324 | os.sched_get_priority_min |
| 6325 | |
| 6326 | policy: int |
| 6327 | |
| 6328 | Get the minimum scheduling priority for policy. |
| 6329 | [clinic start generated code]*/ |
| 6330 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6331 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6332 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 6333 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6334 | { |
| 6335 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6336 | if (min < 0) |
| 6337 | return posix_error(); |
| 6338 | return PyLong_FromLong(min); |
| 6339 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6340 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 6341 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6342 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6343 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 6344 | /*[clinic input] |
| 6345 | os.sched_getscheduler |
| 6346 | pid: pid_t |
| 6347 | / |
| 6348 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6349 | Get the scheduling policy for the process identified by pid. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6350 | |
| 6351 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6352 | [clinic start generated code]*/ |
| 6353 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6354 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6355 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6356 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6357 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6358 | int policy; |
| 6359 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6360 | policy = sched_getscheduler(pid); |
| 6361 | if (policy < 0) |
| 6362 | return posix_error(); |
| 6363 | return PyLong_FromLong(policy); |
| 6364 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6365 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6366 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6367 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6368 | #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] | 6369 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6370 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6371 | |
| 6372 | @classmethod |
| 6373 | os.sched_param.__new__ |
| 6374 | |
| 6375 | sched_priority: object |
| 6376 | A scheduling parameter. |
| 6377 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6378 | Currently has only one field: sched_priority |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6379 | [clinic start generated code]*/ |
| 6380 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6381 | static PyObject * |
| 6382 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6383 | /*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6384 | { |
| 6385 | PyObject *res; |
| 6386 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6387 | res = PyStructSequence_New(type); |
| 6388 | if (!res) |
| 6389 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6390 | Py_INCREF(sched_priority); |
| 6391 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6392 | return res; |
| 6393 | } |
| 6394 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6395 | PyDoc_VAR(os_sched_param__doc__); |
| 6396 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6397 | static PyStructSequence_Field sched_param_fields[] = { |
| 6398 | {"sched_priority", "the scheduling priority"}, |
| 6399 | {0} |
| 6400 | }; |
| 6401 | |
| 6402 | static PyStructSequence_Desc sched_param_desc = { |
| 6403 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6404 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6405 | sched_param_fields, |
| 6406 | 1 |
| 6407 | }; |
| 6408 | |
| 6409 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6410 | convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6411 | { |
| 6412 | long priority; |
| 6413 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6414 | if (!Py_IS_TYPE(param, (PyTypeObject *)get_posix_state(module)->SchedParamType)) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6415 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6416 | return 0; |
| 6417 | } |
| 6418 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6419 | if (priority == -1 && PyErr_Occurred()) |
| 6420 | return 0; |
| 6421 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6422 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6423 | return 0; |
| 6424 | } |
| 6425 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6426 | return 1; |
| 6427 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6428 | #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] | 6429 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6430 | |
| 6431 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6432 | /*[clinic input] |
| 6433 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6434 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6435 | pid: pid_t |
| 6436 | policy: int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6437 | param as param_obj: object |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6438 | / |
| 6439 | |
| 6440 | Set the scheduling policy for the process identified by pid. |
| 6441 | |
| 6442 | If pid is 0, the calling process is changed. |
| 6443 | param is an instance of sched_param. |
| 6444 | [clinic start generated code]*/ |
| 6445 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6446 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6447 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6448 | PyObject *param_obj) |
| 6449 | /*[clinic end generated code: output=cde27faa55dc993e input=73013d731bd8fbe9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6450 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6451 | struct sched_param param; |
| 6452 | if (!convert_sched_param(module, param_obj, ¶m)) { |
| 6453 | return NULL; |
| 6454 | } |
| 6455 | |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6456 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6457 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6458 | ** scheduling policy under Solaris/Illumos, and others. |
| 6459 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6460 | */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6461 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6462 | return posix_error(); |
| 6463 | Py_RETURN_NONE; |
| 6464 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6465 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6466 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6467 | |
| 6468 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6469 | /*[clinic input] |
| 6470 | os.sched_getparam |
| 6471 | pid: pid_t |
| 6472 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6473 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6474 | Returns scheduling parameters for the process identified by pid. |
| 6475 | |
| 6476 | If pid is 0, returns parameters for the calling process. |
| 6477 | Return value is an instance of sched_param. |
| 6478 | [clinic start generated code]*/ |
| 6479 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6480 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6481 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6482 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6483 | { |
| 6484 | struct sched_param param; |
| 6485 | PyObject *result; |
| 6486 | PyObject *priority; |
| 6487 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6488 | if (sched_getparam(pid, ¶m)) |
| 6489 | return posix_error(); |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6490 | PyObject *SchedParamType = get_posix_state(module)->SchedParamType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6491 | result = PyStructSequence_New((PyTypeObject *)SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6492 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6493 | return NULL; |
| 6494 | priority = PyLong_FromLong(param.sched_priority); |
| 6495 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6496 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6497 | return NULL; |
| 6498 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6499 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6500 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6501 | } |
| 6502 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6503 | |
| 6504 | /*[clinic input] |
| 6505 | os.sched_setparam |
| 6506 | pid: pid_t |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6507 | param as param_obj: object |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6508 | / |
| 6509 | |
| 6510 | Set scheduling parameters for the process identified by pid. |
| 6511 | |
| 6512 | If pid is 0, sets parameters for the calling process. |
| 6513 | param should be an instance of sched_param. |
| 6514 | [clinic start generated code]*/ |
| 6515 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6516 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6517 | os_sched_setparam_impl(PyObject *module, pid_t pid, PyObject *param_obj) |
| 6518 | /*[clinic end generated code: output=f19fe020a53741c1 input=27b98337c8b2dcc7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6519 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6520 | struct sched_param param; |
| 6521 | if (!convert_sched_param(module, param_obj, ¶m)) { |
| 6522 | return NULL; |
| 6523 | } |
| 6524 | |
| 6525 | if (sched_setparam(pid, ¶m)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6526 | return posix_error(); |
| 6527 | Py_RETURN_NONE; |
| 6528 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6529 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6530 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6531 | |
| 6532 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6533 | /*[clinic input] |
| 6534 | os.sched_rr_get_interval -> double |
| 6535 | pid: pid_t |
| 6536 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6537 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6538 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6539 | |
| 6540 | Value returned is a float. |
| 6541 | [clinic start generated code]*/ |
| 6542 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6543 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6544 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6545 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6546 | { |
| 6547 | struct timespec interval; |
| 6548 | if (sched_rr_get_interval(pid, &interval)) { |
| 6549 | posix_error(); |
| 6550 | return -1.0; |
| 6551 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6552 | #ifdef _Py_MEMORY_SANITIZER |
| 6553 | __msan_unpoison(&interval, sizeof(interval)); |
| 6554 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6555 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6556 | } |
| 6557 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6558 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6559 | |
| 6560 | /*[clinic input] |
| 6561 | os.sched_yield |
| 6562 | |
| 6563 | Voluntarily relinquish the CPU. |
| 6564 | [clinic start generated code]*/ |
| 6565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6566 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6567 | os_sched_yield_impl(PyObject *module) |
| 6568 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6569 | { |
| 6570 | if (sched_yield()) |
| 6571 | return posix_error(); |
| 6572 | Py_RETURN_NONE; |
| 6573 | } |
| 6574 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6575 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6576 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6577 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6578 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6579 | /*[clinic input] |
| 6580 | os.sched_setaffinity |
| 6581 | pid: pid_t |
| 6582 | mask : object |
| 6583 | / |
| 6584 | |
| 6585 | Set the CPU affinity of the process identified by pid to mask. |
| 6586 | |
| 6587 | mask should be an iterable of integers identifying CPUs. |
| 6588 | [clinic start generated code]*/ |
| 6589 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6590 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6591 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6592 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6593 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6594 | int ncpus; |
| 6595 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6596 | cpu_set_t *cpu_set = NULL; |
| 6597 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6599 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6600 | if (iterator == NULL) |
| 6601 | return NULL; |
| 6602 | |
| 6603 | ncpus = NCPUS_START; |
| 6604 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6605 | cpu_set = CPU_ALLOC(ncpus); |
| 6606 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6607 | PyErr_NoMemory(); |
| 6608 | goto error; |
| 6609 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6610 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6611 | |
| 6612 | while ((item = PyIter_Next(iterator))) { |
| 6613 | long cpu; |
| 6614 | if (!PyLong_Check(item)) { |
| 6615 | PyErr_Format(PyExc_TypeError, |
| 6616 | "expected an iterator of ints, " |
| 6617 | "but iterator yielded %R", |
| 6618 | Py_TYPE(item)); |
| 6619 | Py_DECREF(item); |
| 6620 | goto error; |
| 6621 | } |
| 6622 | cpu = PyLong_AsLong(item); |
| 6623 | Py_DECREF(item); |
| 6624 | if (cpu < 0) { |
| 6625 | if (!PyErr_Occurred()) |
| 6626 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6627 | goto error; |
| 6628 | } |
| 6629 | if (cpu > INT_MAX - 1) { |
| 6630 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6631 | goto error; |
| 6632 | } |
| 6633 | if (cpu >= ncpus) { |
| 6634 | /* Grow CPU mask to fit the CPU number */ |
| 6635 | int newncpus = ncpus; |
| 6636 | cpu_set_t *newmask; |
| 6637 | size_t newsetsize; |
| 6638 | while (newncpus <= cpu) { |
| 6639 | if (newncpus > INT_MAX / 2) |
| 6640 | newncpus = cpu + 1; |
| 6641 | else |
| 6642 | newncpus = newncpus * 2; |
| 6643 | } |
| 6644 | newmask = CPU_ALLOC(newncpus); |
| 6645 | if (newmask == NULL) { |
| 6646 | PyErr_NoMemory(); |
| 6647 | goto error; |
| 6648 | } |
| 6649 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6650 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6651 | memcpy(newmask, cpu_set, setsize); |
| 6652 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6653 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6654 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6655 | ncpus = newncpus; |
| 6656 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6657 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6658 | } |
Brandt Bucher | 45a30af | 2019-06-27 09:10:57 -0700 | [diff] [blame] | 6659 | if (PyErr_Occurred()) { |
| 6660 | goto error; |
| 6661 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6662 | Py_CLEAR(iterator); |
| 6663 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6664 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6665 | posix_error(); |
| 6666 | goto error; |
| 6667 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6668 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6669 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6670 | |
| 6671 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6672 | if (cpu_set) |
| 6673 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6674 | Py_XDECREF(iterator); |
| 6675 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6676 | } |
| 6677 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6678 | |
| 6679 | /*[clinic input] |
| 6680 | os.sched_getaffinity |
| 6681 | pid: pid_t |
| 6682 | / |
| 6683 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6684 | 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] | 6685 | |
| 6686 | The affinity is returned as a set of CPU identifiers. |
| 6687 | [clinic start generated code]*/ |
| 6688 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6689 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6690 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6691 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6692 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6693 | int cpu, ncpus, count; |
| 6694 | size_t setsize; |
| 6695 | cpu_set_t *mask = NULL; |
| 6696 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6697 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6698 | ncpus = NCPUS_START; |
| 6699 | while (1) { |
| 6700 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6701 | mask = CPU_ALLOC(ncpus); |
| 6702 | if (mask == NULL) |
| 6703 | return PyErr_NoMemory(); |
| 6704 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6705 | break; |
| 6706 | CPU_FREE(mask); |
| 6707 | if (errno != EINVAL) |
| 6708 | return posix_error(); |
| 6709 | if (ncpus > INT_MAX / 2) { |
| 6710 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6711 | "a large enough CPU set"); |
| 6712 | return NULL; |
| 6713 | } |
| 6714 | ncpus = ncpus * 2; |
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 | |
| 6717 | res = PySet_New(NULL); |
| 6718 | if (res == NULL) |
| 6719 | goto error; |
| 6720 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6721 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6722 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6723 | --count; |
| 6724 | if (cpu_num == NULL) |
| 6725 | goto error; |
| 6726 | if (PySet_Add(res, cpu_num)) { |
| 6727 | Py_DECREF(cpu_num); |
| 6728 | goto error; |
| 6729 | } |
| 6730 | Py_DECREF(cpu_num); |
| 6731 | } |
| 6732 | } |
| 6733 | CPU_FREE(mask); |
| 6734 | return res; |
| 6735 | |
| 6736 | error: |
| 6737 | if (mask) |
| 6738 | CPU_FREE(mask); |
| 6739 | Py_XDECREF(res); |
| 6740 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6741 | } |
| 6742 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6743 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6744 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6745 | #endif /* HAVE_SCHED_H */ |
| 6746 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6747 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6748 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6749 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6750 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6751 | #define DEV_PTY_FILE "/dev/ptc" |
| 6752 | #define HAVE_DEV_PTMX |
| 6753 | #else |
| 6754 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6755 | #endif |
| 6756 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6757 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6758 | #ifdef HAVE_PTY_H |
| 6759 | #include <pty.h> |
| 6760 | #else |
| 6761 | #ifdef HAVE_LIBUTIL_H |
| 6762 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6763 | #else |
| 6764 | #ifdef HAVE_UTIL_H |
| 6765 | #include <util.h> |
| 6766 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6767 | #endif /* HAVE_LIBUTIL_H */ |
| 6768 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6769 | #ifdef HAVE_STROPTS_H |
| 6770 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6771 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6772 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6773 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6774 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6775 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6776 | /*[clinic input] |
| 6777 | os.openpty |
| 6778 | |
| 6779 | Open a pseudo-terminal. |
| 6780 | |
| 6781 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6782 | for both the master and slave ends. |
| 6783 | [clinic start generated code]*/ |
| 6784 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6785 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6786 | os_openpty_impl(PyObject *module) |
| 6787 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6788 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6789 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6790 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6791 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6792 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6793 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6794 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6795 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6796 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6797 | #endif |
| 6798 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6799 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6800 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6801 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6802 | goto posix_error; |
| 6803 | |
| 6804 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6805 | goto error; |
| 6806 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6807 | goto error; |
| 6808 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6809 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6810 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6811 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6812 | goto posix_error; |
| 6813 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6814 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6815 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6816 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6817 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6818 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6819 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6820 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6821 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6822 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6823 | goto posix_error; |
| 6824 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6825 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6826 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6827 | /* change permission of slave */ |
| 6828 | if (grantpt(master_fd) < 0) { |
| 6829 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6830 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6831 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6832 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6833 | /* unlock slave */ |
| 6834 | if (unlockpt(master_fd) < 0) { |
| 6835 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6836 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6837 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6838 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6839 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6840 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6841 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6842 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6843 | goto posix_error; |
| 6844 | |
| 6845 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6846 | if (slave_fd == -1) |
| 6847 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6848 | |
| 6849 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6850 | goto posix_error; |
| 6851 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6852 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6853 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6854 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6855 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6856 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6857 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6858 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6859 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6860 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6861 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6862 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6863 | posix_error: |
| 6864 | posix_error(); |
| 6865 | error: |
| 6866 | if (master_fd != -1) |
| 6867 | close(master_fd); |
| 6868 | if (slave_fd != -1) |
| 6869 | close(slave_fd); |
| 6870 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6871 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6872 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6873 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6874 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6875 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6876 | /*[clinic input] |
| 6877 | os.forkpty |
| 6878 | |
| 6879 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6880 | |
| 6881 | Returns a tuple of (pid, master_fd). |
| 6882 | Like fork(), return pid of 0 to the child process, |
| 6883 | and pid of child to the parent process. |
| 6884 | To both, return fd of newly opened pseudo-terminal. |
| 6885 | [clinic start generated code]*/ |
| 6886 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6887 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6888 | os_forkpty_impl(PyObject *module) |
| 6889 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6890 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6891 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6892 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6893 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6894 | if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6895 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6896 | return NULL; |
| 6897 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6898 | if (PySys_Audit("os.forkpty", NULL) < 0) { |
| 6899 | return NULL; |
| 6900 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6901 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6902 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6903 | if (pid == 0) { |
| 6904 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6905 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6906 | } else { |
| 6907 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6908 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6909 | } |
| 6910 | if (pid == -1) |
| 6911 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6912 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6913 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6914 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6915 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6916 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6917 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6918 | /*[clinic input] |
| 6919 | os.getegid |
| 6920 | |
| 6921 | Return the current process's effective group id. |
| 6922 | [clinic start generated code]*/ |
| 6923 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6924 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6925 | os_getegid_impl(PyObject *module) |
| 6926 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6927 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6928 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6929 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6930 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6931 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6932 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6933 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6934 | /*[clinic input] |
| 6935 | os.geteuid |
| 6936 | |
| 6937 | Return the current process's effective user id. |
| 6938 | [clinic start generated code]*/ |
| 6939 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6940 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6941 | os_geteuid_impl(PyObject *module) |
| 6942 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6943 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6944 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6945 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6946 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6947 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6948 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6949 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6950 | /*[clinic input] |
| 6951 | os.getgid |
| 6952 | |
| 6953 | Return the current process's group id. |
| 6954 | [clinic start generated code]*/ |
| 6955 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6956 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6957 | os_getgid_impl(PyObject *module) |
| 6958 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6959 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6960 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6961 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6962 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6963 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6964 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6965 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6966 | /*[clinic input] |
| 6967 | os.getpid |
| 6968 | |
| 6969 | Return the current process id. |
| 6970 | [clinic start generated code]*/ |
| 6971 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6972 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6973 | os_getpid_impl(PyObject *module) |
| 6974 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6975 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6976 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6977 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6978 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6979 | |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6980 | #ifdef NGROUPS_MAX |
| 6981 | #define MAX_GROUPS NGROUPS_MAX |
| 6982 | #else |
| 6983 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6984 | #define MAX_GROUPS 64 |
| 6985 | #endif |
| 6986 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6987 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6988 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 6989 | #ifdef __APPLE__ |
| 6990 | /*[clinic input] |
| 6991 | os.getgrouplist |
| 6992 | |
| 6993 | user: str |
| 6994 | username to lookup |
| 6995 | group as basegid: int |
| 6996 | base group id of the user |
| 6997 | / |
| 6998 | |
| 6999 | Returns a list of groups to which a user belongs. |
| 7000 | [clinic start generated code]*/ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7001 | |
| 7002 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7003 | os_getgrouplist_impl(PyObject *module, const char *user, int basegid) |
| 7004 | /*[clinic end generated code: output=6e734697b8c26de0 input=f8d870374b09a490]*/ |
| 7005 | #else |
| 7006 | /*[clinic input] |
| 7007 | os.getgrouplist |
| 7008 | |
| 7009 | user: str |
| 7010 | username to lookup |
| 7011 | group as basegid: gid_t |
| 7012 | base group id of the user |
| 7013 | / |
| 7014 | |
| 7015 | Returns a list of groups to which a user belongs. |
| 7016 | [clinic start generated code]*/ |
| 7017 | |
| 7018 | static PyObject * |
| 7019 | os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid) |
| 7020 | /*[clinic end generated code: output=0ebd7fb70115575b input=cc61d5c20b08958d]*/ |
| 7021 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7022 | { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7023 | int i, ngroups; |
| 7024 | PyObject *list; |
| 7025 | #ifdef __APPLE__ |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7026 | int *groups; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7027 | #else |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7028 | gid_t *groups; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7029 | #endif |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 7030 | |
| 7031 | /* |
| 7032 | * NGROUPS_MAX is defined by POSIX.1 as the maximum |
| 7033 | * number of supplimental groups a users can belong to. |
| 7034 | * We have to increment it by one because |
| 7035 | * getgrouplist() returns both the supplemental groups |
| 7036 | * and the primary group, i.e. all of the groups the |
| 7037 | * user belongs to. |
| 7038 | */ |
| 7039 | ngroups = 1 + MAX_GROUPS; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7040 | |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7041 | while (1) { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7042 | #ifdef __APPLE__ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7043 | groups = PyMem_New(int, ngroups); |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7044 | #else |
| 7045 | groups = PyMem_New(gid_t, ngroups); |
| 7046 | #endif |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7047 | if (groups == NULL) { |
| 7048 | return PyErr_NoMemory(); |
| 7049 | } |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7050 | |
| 7051 | int old_ngroups = ngroups; |
| 7052 | if (getgrouplist(user, basegid, groups, &ngroups) != -1) { |
| 7053 | /* Success */ |
| 7054 | break; |
| 7055 | } |
| 7056 | |
| 7057 | /* getgrouplist() fails if the group list is too small */ |
| 7058 | PyMem_Free(groups); |
| 7059 | |
| 7060 | if (ngroups > old_ngroups) { |
| 7061 | /* If the group list is too small, the glibc implementation of |
| 7062 | getgrouplist() sets ngroups to the total number of groups and |
| 7063 | returns -1. */ |
| 7064 | } |
| 7065 | else { |
| 7066 | /* Double the group list size */ |
| 7067 | if (ngroups > INT_MAX / 2) { |
| 7068 | return PyErr_NoMemory(); |
| 7069 | } |
| 7070 | ngroups *= 2; |
| 7071 | } |
| 7072 | |
| 7073 | /* Retry getgrouplist() with a larger group list */ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7074 | } |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7075 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 7076 | #ifdef _Py_MEMORY_SANITIZER |
| 7077 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 7078 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 7079 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 7080 | #endif |
| 7081 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7082 | list = PyList_New(ngroups); |
| 7083 | if (list == NULL) { |
| 7084 | PyMem_Del(groups); |
| 7085 | return NULL; |
| 7086 | } |
| 7087 | |
| 7088 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7089 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7090 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7091 | #else |
| 7092 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 7093 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7094 | if (o == NULL) { |
| 7095 | Py_DECREF(list); |
| 7096 | PyMem_Del(groups); |
| 7097 | return NULL; |
| 7098 | } |
| 7099 | PyList_SET_ITEM(list, i, o); |
| 7100 | } |
| 7101 | |
| 7102 | PyMem_Del(groups); |
| 7103 | |
| 7104 | return list; |
| 7105 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7106 | #endif /* HAVE_GETGROUPLIST */ |
| 7107 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7108 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7109 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7110 | /*[clinic input] |
| 7111 | os.getgroups |
| 7112 | |
| 7113 | Return list of supplemental group IDs for the process. |
| 7114 | [clinic start generated code]*/ |
| 7115 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7116 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7117 | os_getgroups_impl(PyObject *module) |
| 7118 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7119 | { |
| 7120 | PyObject *result = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7121 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7122 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7123 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7124 | * This is a helper variable to store the intermediate result when |
| 7125 | * that happens. |
| 7126 | * |
| 7127 | * To keep the code readable the OSX behaviour is unconditional, |
| 7128 | * according to the POSIX spec this should be safe on all unix-y |
| 7129 | * systems. |
| 7130 | */ |
| 7131 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7132 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7133 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7134 | #ifdef __APPLE__ |
| 7135 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 7136 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 7137 | * always first call getgroups with length 0 to get the actual number |
| 7138 | * of groups. |
| 7139 | */ |
| 7140 | n = getgroups(0, NULL); |
| 7141 | if (n < 0) { |
| 7142 | return posix_error(); |
| 7143 | } else if (n <= MAX_GROUPS) { |
| 7144 | /* groups will fit in existing array */ |
| 7145 | alt_grouplist = grouplist; |
| 7146 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7147 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7148 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7149 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7150 | } |
| 7151 | } |
| 7152 | |
| 7153 | n = getgroups(n, alt_grouplist); |
| 7154 | if (n == -1) { |
| 7155 | if (alt_grouplist != grouplist) { |
| 7156 | PyMem_Free(alt_grouplist); |
| 7157 | } |
| 7158 | return posix_error(); |
| 7159 | } |
| 7160 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7161 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7162 | if (n < 0) { |
| 7163 | if (errno == EINVAL) { |
| 7164 | n = getgroups(0, NULL); |
| 7165 | if (n == -1) { |
| 7166 | return posix_error(); |
| 7167 | } |
| 7168 | if (n == 0) { |
| 7169 | /* Avoid malloc(0) */ |
| 7170 | alt_grouplist = grouplist; |
| 7171 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7172 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7173 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7174 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7175 | } |
| 7176 | n = getgroups(n, alt_grouplist); |
| 7177 | if (n == -1) { |
| 7178 | PyMem_Free(alt_grouplist); |
| 7179 | return posix_error(); |
| 7180 | } |
| 7181 | } |
| 7182 | } else { |
| 7183 | return posix_error(); |
| 7184 | } |
| 7185 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7186 | #endif |
| 7187 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7188 | result = PyList_New(n); |
| 7189 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7190 | int i; |
| 7191 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7192 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7193 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7194 | Py_DECREF(result); |
| 7195 | result = NULL; |
| 7196 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7197 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7198 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7199 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7200 | } |
| 7201 | |
| 7202 | if (alt_grouplist != grouplist) { |
| 7203 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7204 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7205 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7206 | return result; |
| 7207 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7208 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7209 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7210 | #ifdef HAVE_INITGROUPS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7211 | #ifdef __APPLE__ |
| 7212 | /*[clinic input] |
| 7213 | os.initgroups |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7214 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7215 | username as oname: FSConverter |
| 7216 | gid: int |
| 7217 | / |
| 7218 | |
| 7219 | Initialize the group access list. |
| 7220 | |
| 7221 | Call the system initgroups() to initialize the group access list with all of |
| 7222 | the groups of which the specified username is a member, plus the specified |
| 7223 | group id. |
| 7224 | [clinic start generated code]*/ |
| 7225 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7226 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7227 | os_initgroups_impl(PyObject *module, PyObject *oname, int gid) |
| 7228 | /*[clinic end generated code: output=7f074d30a425fd3a input=df3d54331b0af204]*/ |
| 7229 | #else |
| 7230 | /*[clinic input] |
| 7231 | os.initgroups |
| 7232 | |
| 7233 | username as oname: FSConverter |
| 7234 | gid: gid_t |
| 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 | |
| 7244 | static PyObject * |
| 7245 | os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid) |
| 7246 | /*[clinic end generated code: output=59341244521a9e3f input=0cb91bdc59a4c564]*/ |
| 7247 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7248 | { |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7249 | const char *username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7250 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7251 | if (initgroups(username, gid) == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7252 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7253 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7254 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7255 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7256 | #endif /* HAVE_INITGROUPS */ |
| 7257 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7258 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7259 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7260 | /*[clinic input] |
| 7261 | os.getpgid |
| 7262 | |
| 7263 | pid: pid_t |
| 7264 | |
| 7265 | Call the system call getpgid(), and return the result. |
| 7266 | [clinic start generated code]*/ |
| 7267 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7268 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7269 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 7270 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7271 | { |
| 7272 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7273 | if (pgid < 0) |
| 7274 | return posix_error(); |
| 7275 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7276 | } |
| 7277 | #endif /* HAVE_GETPGID */ |
| 7278 | |
| 7279 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7280 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7281 | /*[clinic input] |
| 7282 | os.getpgrp |
| 7283 | |
| 7284 | Return the current process group id. |
| 7285 | [clinic start generated code]*/ |
| 7286 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7287 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7288 | os_getpgrp_impl(PyObject *module) |
| 7289 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7290 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7291 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7292 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7293 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7294 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7295 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7296 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7297 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7298 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7299 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7300 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7301 | /*[clinic input] |
| 7302 | os.setpgrp |
| 7303 | |
| 7304 | Make the current process the leader of its process group. |
| 7305 | [clinic start generated code]*/ |
| 7306 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7307 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7308 | os_setpgrp_impl(PyObject *module) |
| 7309 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7310 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7311 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7312 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7313 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7314 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7315 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7316 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7317 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7318 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7319 | #endif /* HAVE_SETPGRP */ |
| 7320 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7321 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7322 | |
| 7323 | #ifdef MS_WINDOWS |
| 7324 | #include <tlhelp32.h> |
| 7325 | |
| 7326 | static PyObject* |
| 7327 | win32_getppid() |
| 7328 | { |
| 7329 | HANDLE snapshot; |
| 7330 | pid_t mypid; |
| 7331 | PyObject* result = NULL; |
| 7332 | BOOL have_record; |
| 7333 | PROCESSENTRY32 pe; |
| 7334 | |
| 7335 | mypid = getpid(); /* This function never fails */ |
| 7336 | |
| 7337 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 7338 | if (snapshot == INVALID_HANDLE_VALUE) |
| 7339 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 7340 | |
| 7341 | pe.dwSize = sizeof(pe); |
| 7342 | have_record = Process32First(snapshot, &pe); |
| 7343 | while (have_record) { |
| 7344 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 7345 | /* We could cache the ulong value in a static variable. */ |
| 7346 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 7347 | break; |
| 7348 | } |
| 7349 | |
| 7350 | have_record = Process32Next(snapshot, &pe); |
| 7351 | } |
| 7352 | |
| 7353 | /* If our loop exits and our pid was not found (result will be NULL) |
| 7354 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 7355 | * error anyway, so let's raise it. */ |
| 7356 | if (!result) |
| 7357 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7358 | |
| 7359 | CloseHandle(snapshot); |
| 7360 | |
| 7361 | return result; |
| 7362 | } |
| 7363 | #endif /*MS_WINDOWS*/ |
| 7364 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7365 | |
| 7366 | /*[clinic input] |
| 7367 | os.getppid |
| 7368 | |
| 7369 | Return the parent's process id. |
| 7370 | |
| 7371 | If the parent process has already exited, Windows machines will still |
| 7372 | return its id; others systems will return the id of the 'init' process (1). |
| 7373 | [clinic start generated code]*/ |
| 7374 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7375 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7376 | os_getppid_impl(PyObject *module) |
| 7377 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7378 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7379 | #ifdef MS_WINDOWS |
| 7380 | return win32_getppid(); |
| 7381 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7382 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7383 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7384 | } |
| 7385 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7386 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7387 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7388 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7389 | /*[clinic input] |
| 7390 | os.getlogin |
| 7391 | |
| 7392 | Return the actual login name. |
| 7393 | [clinic start generated code]*/ |
| 7394 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7395 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7396 | os_getlogin_impl(PyObject *module) |
| 7397 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7398 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7399 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7400 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7401 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7402 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7403 | |
| 7404 | if (GetUserNameW(user_name, &num_chars)) { |
| 7405 | /* num_chars is the number of unicode chars plus null terminator */ |
| 7406 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7407 | } |
| 7408 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7409 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7410 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7411 | char *name; |
| 7412 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7413 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7414 | errno = 0; |
| 7415 | name = getlogin(); |
| 7416 | if (name == NULL) { |
| 7417 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7418 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7419 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7420 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7421 | } |
| 7422 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7423 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7424 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7425 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7426 | return result; |
| 7427 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7428 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7429 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7430 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7431 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7432 | /*[clinic input] |
| 7433 | os.getuid |
| 7434 | |
| 7435 | Return the current process's user id. |
| 7436 | [clinic start generated code]*/ |
| 7437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7438 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7439 | os_getuid_impl(PyObject *module) |
| 7440 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7441 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7442 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7443 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7444 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7445 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7446 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7447 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7448 | #define HAVE_KILL |
| 7449 | #endif /* MS_WINDOWS */ |
| 7450 | |
| 7451 | #ifdef HAVE_KILL |
| 7452 | /*[clinic input] |
| 7453 | os.kill |
| 7454 | |
| 7455 | pid: pid_t |
| 7456 | signal: Py_ssize_t |
| 7457 | / |
| 7458 | |
| 7459 | Kill a process with a signal. |
| 7460 | [clinic start generated code]*/ |
| 7461 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7462 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7463 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7464 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7465 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7466 | if (PySys_Audit("os.kill", "in", pid, signal) < 0) { |
| 7467 | return NULL; |
| 7468 | } |
| 7469 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7470 | if (kill(pid, (int)signal) == -1) |
| 7471 | return posix_error(); |
| 7472 | Py_RETURN_NONE; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7473 | #else /* !MS_WINDOWS */ |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7474 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7475 | DWORD sig = (DWORD)signal; |
| 7476 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7477 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7478 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7479 | /* Console processes which share a common console can be sent CTRL+C or |
| 7480 | CTRL+BREAK events, provided they handle said events. */ |
| 7481 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7482 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7483 | err = GetLastError(); |
| 7484 | PyErr_SetFromWindowsErr(err); |
| 7485 | } |
| 7486 | else |
| 7487 | Py_RETURN_NONE; |
| 7488 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7489 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7490 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7491 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7492 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7493 | if (handle == NULL) { |
| 7494 | err = GetLastError(); |
| 7495 | return PyErr_SetFromWindowsErr(err); |
| 7496 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7497 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7498 | if (TerminateProcess(handle, sig) == 0) { |
| 7499 | err = GetLastError(); |
| 7500 | result = PyErr_SetFromWindowsErr(err); |
| 7501 | } else { |
| 7502 | Py_INCREF(Py_None); |
| 7503 | result = Py_None; |
| 7504 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7505 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7506 | CloseHandle(handle); |
| 7507 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7508 | #endif /* !MS_WINDOWS */ |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7509 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7510 | #endif /* HAVE_KILL */ |
| 7511 | |
| 7512 | |
| 7513 | #ifdef HAVE_KILLPG |
| 7514 | /*[clinic input] |
| 7515 | os.killpg |
| 7516 | |
| 7517 | pgid: pid_t |
| 7518 | signal: int |
| 7519 | / |
| 7520 | |
| 7521 | Kill a process group with a signal. |
| 7522 | [clinic start generated code]*/ |
| 7523 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7524 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7525 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7526 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7527 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7528 | if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) { |
| 7529 | return NULL; |
| 7530 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7531 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7532 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7533 | take the same type. Moreover, pid_t is always at least as wide as |
| 7534 | int (else compilation of this module fails), which is safe. */ |
| 7535 | if (killpg(pgid, signal) == -1) |
| 7536 | return posix_error(); |
| 7537 | Py_RETURN_NONE; |
| 7538 | } |
| 7539 | #endif /* HAVE_KILLPG */ |
| 7540 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7541 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7542 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7543 | #ifdef HAVE_SYS_LOCK_H |
| 7544 | #include <sys/lock.h> |
| 7545 | #endif |
| 7546 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7547 | /*[clinic input] |
| 7548 | os.plock |
| 7549 | op: int |
| 7550 | / |
| 7551 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7552 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7553 | [clinic start generated code]*/ |
| 7554 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7555 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7556 | os_plock_impl(PyObject *module, int op) |
| 7557 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7558 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7559 | if (plock(op) == -1) |
| 7560 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7561 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7562 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7563 | #endif /* HAVE_PLOCK */ |
| 7564 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7565 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7566 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7567 | /*[clinic input] |
| 7568 | os.setuid |
| 7569 | |
| 7570 | uid: uid_t |
| 7571 | / |
| 7572 | |
| 7573 | Set the current process's user id. |
| 7574 | [clinic start generated code]*/ |
| 7575 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7576 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7577 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7578 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7579 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7580 | if (setuid(uid) < 0) |
| 7581 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7582 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7583 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7584 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7585 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7586 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7587 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7588 | /*[clinic input] |
| 7589 | os.seteuid |
| 7590 | |
| 7591 | euid: uid_t |
| 7592 | / |
| 7593 | |
| 7594 | Set the current process's effective user id. |
| 7595 | [clinic start generated code]*/ |
| 7596 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7597 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7598 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7599 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7600 | { |
| 7601 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7602 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7603 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7604 | } |
| 7605 | #endif /* HAVE_SETEUID */ |
| 7606 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7607 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7608 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7609 | /*[clinic input] |
| 7610 | os.setegid |
| 7611 | |
| 7612 | egid: gid_t |
| 7613 | / |
| 7614 | |
| 7615 | Set the current process's effective group id. |
| 7616 | [clinic start generated code]*/ |
| 7617 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7618 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7619 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7620 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7621 | { |
| 7622 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7623 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7624 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7625 | } |
| 7626 | #endif /* HAVE_SETEGID */ |
| 7627 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7628 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7629 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7630 | /*[clinic input] |
| 7631 | os.setreuid |
| 7632 | |
| 7633 | ruid: uid_t |
| 7634 | euid: uid_t |
| 7635 | / |
| 7636 | |
| 7637 | Set the current process's real and effective user ids. |
| 7638 | [clinic start generated code]*/ |
| 7639 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7640 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7641 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7642 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7643 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7644 | if (setreuid(ruid, euid) < 0) { |
| 7645 | return posix_error(); |
| 7646 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7647 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7648 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7649 | } |
| 7650 | #endif /* HAVE_SETREUID */ |
| 7651 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7652 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7653 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7654 | /*[clinic input] |
| 7655 | os.setregid |
| 7656 | |
| 7657 | rgid: gid_t |
| 7658 | egid: gid_t |
| 7659 | / |
| 7660 | |
| 7661 | Set the current process's real and effective group ids. |
| 7662 | [clinic start generated code]*/ |
| 7663 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7664 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7665 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7666 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7667 | { |
| 7668 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7669 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7670 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7671 | } |
| 7672 | #endif /* HAVE_SETREGID */ |
| 7673 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7674 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7675 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7676 | /*[clinic input] |
| 7677 | os.setgid |
| 7678 | gid: gid_t |
| 7679 | / |
| 7680 | |
| 7681 | Set the current process's group id. |
| 7682 | [clinic start generated code]*/ |
| 7683 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7684 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7685 | os_setgid_impl(PyObject *module, gid_t gid) |
| 7686 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7687 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7688 | if (setgid(gid) < 0) |
| 7689 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7690 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7691 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7692 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7693 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7694 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7695 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7696 | /*[clinic input] |
| 7697 | os.setgroups |
| 7698 | |
| 7699 | groups: object |
| 7700 | / |
| 7701 | |
| 7702 | Set the groups of the current process to list. |
| 7703 | [clinic start generated code]*/ |
| 7704 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7705 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7706 | os_setgroups(PyObject *module, PyObject *groups) |
| 7707 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7708 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7709 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7710 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7711 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7712 | if (!PySequence_Check(groups)) { |
| 7713 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7714 | return NULL; |
| 7715 | } |
| 7716 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7717 | if (len < 0) { |
| 7718 | return NULL; |
| 7719 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7720 | if (len > MAX_GROUPS) { |
| 7721 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7722 | return NULL; |
| 7723 | } |
| 7724 | for(i = 0; i < len; i++) { |
| 7725 | PyObject *elem; |
| 7726 | elem = PySequence_GetItem(groups, i); |
| 7727 | if (!elem) |
| 7728 | return NULL; |
| 7729 | if (!PyLong_Check(elem)) { |
| 7730 | PyErr_SetString(PyExc_TypeError, |
| 7731 | "groups must be integers"); |
| 7732 | Py_DECREF(elem); |
| 7733 | return NULL; |
| 7734 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7735 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7736 | Py_DECREF(elem); |
| 7737 | return NULL; |
| 7738 | } |
| 7739 | } |
| 7740 | Py_DECREF(elem); |
| 7741 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7742 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7743 | if (setgroups(len, grouplist) < 0) |
| 7744 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7745 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7746 | } |
| 7747 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7748 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7749 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7750 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7751 | wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7752 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7753 | PyObject *result; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7754 | PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7755 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7756 | if (pid == -1) |
| 7757 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7758 | |
Zackery Spytz | 682107c | 2019-09-09 09:48:32 -0600 | [diff] [blame] | 7759 | // If wait succeeded but no child was ready to report status, ru will not |
| 7760 | // have been populated. |
| 7761 | if (pid == 0) { |
| 7762 | memset(ru, 0, sizeof(*ru)); |
| 7763 | } |
| 7764 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7765 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7766 | if (m == NULL) |
| 7767 | return NULL; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7768 | struct_rusage = PyObject_GetAttr(m, get_posix_state(module)->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7769 | Py_DECREF(m); |
| 7770 | if (struct_rusage == NULL) |
| 7771 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7772 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7773 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7774 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
Eddie Elizondo | e4db1f0 | 2019-11-25 19:07:37 -0800 | [diff] [blame] | 7775 | Py_DECREF(struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7776 | if (!result) |
| 7777 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7778 | |
| 7779 | #ifndef doubletime |
| 7780 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7781 | #endif |
| 7782 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7783 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7784 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7785 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7786 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7787 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7788 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7789 | SET_INT(result, 2, ru->ru_maxrss); |
| 7790 | SET_INT(result, 3, ru->ru_ixrss); |
| 7791 | SET_INT(result, 4, ru->ru_idrss); |
| 7792 | SET_INT(result, 5, ru->ru_isrss); |
| 7793 | SET_INT(result, 6, ru->ru_minflt); |
| 7794 | SET_INT(result, 7, ru->ru_majflt); |
| 7795 | SET_INT(result, 8, ru->ru_nswap); |
| 7796 | SET_INT(result, 9, ru->ru_inblock); |
| 7797 | SET_INT(result, 10, ru->ru_oublock); |
| 7798 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7799 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7800 | SET_INT(result, 13, ru->ru_nsignals); |
| 7801 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7802 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7803 | #undef SET_INT |
| 7804 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7805 | if (PyErr_Occurred()) { |
| 7806 | Py_DECREF(result); |
| 7807 | return NULL; |
| 7808 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7809 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7810 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7811 | } |
| 7812 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7813 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7814 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7815 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7816 | /*[clinic input] |
| 7817 | os.wait3 |
| 7818 | |
| 7819 | options: int |
| 7820 | Wait for completion of a child process. |
| 7821 | |
| 7822 | Returns a tuple of information about the child process: |
| 7823 | (pid, status, rusage) |
| 7824 | [clinic start generated code]*/ |
| 7825 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7826 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7827 | os_wait3_impl(PyObject *module, int options) |
| 7828 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7829 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7830 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7831 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7832 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7833 | WAIT_TYPE status; |
| 7834 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7835 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7836 | do { |
| 7837 | Py_BEGIN_ALLOW_THREADS |
| 7838 | pid = wait3(&status, options, &ru); |
| 7839 | Py_END_ALLOW_THREADS |
| 7840 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7841 | if (pid < 0) |
| 7842 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7843 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7844 | return wait_helper(module, pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7845 | } |
| 7846 | #endif /* HAVE_WAIT3 */ |
| 7847 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7848 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7849 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7850 | /*[clinic input] |
| 7851 | |
| 7852 | os.wait4 |
| 7853 | |
| 7854 | pid: pid_t |
| 7855 | options: int |
| 7856 | |
| 7857 | Wait for completion of a specific child process. |
| 7858 | |
| 7859 | Returns a tuple of information about the child process: |
| 7860 | (pid, status, rusage) |
| 7861 | [clinic start generated code]*/ |
| 7862 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7863 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7864 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7865 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7866 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7867 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7868 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7869 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7870 | WAIT_TYPE status; |
| 7871 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7872 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7873 | do { |
| 7874 | Py_BEGIN_ALLOW_THREADS |
| 7875 | res = wait4(pid, &status, options, &ru); |
| 7876 | Py_END_ALLOW_THREADS |
| 7877 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7878 | if (res < 0) |
| 7879 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7880 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7881 | return wait_helper(module, res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7882 | } |
| 7883 | #endif /* HAVE_WAIT4 */ |
| 7884 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7885 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7886 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7887 | /*[clinic input] |
| 7888 | os.waitid |
| 7889 | |
| 7890 | idtype: idtype_t |
| 7891 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7892 | id: id_t |
| 7893 | The id to wait on. |
| 7894 | options: int |
| 7895 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7896 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7897 | / |
| 7898 | |
| 7899 | Returns the result of waiting for a process or processes. |
| 7900 | |
| 7901 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7902 | no children in a waitable state. |
| 7903 | [clinic start generated code]*/ |
| 7904 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7905 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7906 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7907 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7908 | { |
| 7909 | PyObject *result; |
| 7910 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7911 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7912 | siginfo_t si; |
| 7913 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7914 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7915 | do { |
| 7916 | Py_BEGIN_ALLOW_THREADS |
| 7917 | res = waitid(idtype, id, &si, options); |
| 7918 | Py_END_ALLOW_THREADS |
| 7919 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7920 | if (res < 0) |
| 7921 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7922 | |
| 7923 | if (si.si_pid == 0) |
| 7924 | Py_RETURN_NONE; |
| 7925 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 7926 | PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7927 | result = PyStructSequence_New((PyTypeObject *)WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7928 | if (!result) |
| 7929 | return NULL; |
| 7930 | |
| 7931 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7932 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7933 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7934 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7935 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7936 | if (PyErr_Occurred()) { |
| 7937 | Py_DECREF(result); |
| 7938 | return NULL; |
| 7939 | } |
| 7940 | |
| 7941 | return result; |
| 7942 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7943 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7944 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7945 | |
| 7946 | #if defined(HAVE_WAITPID) |
| 7947 | /*[clinic input] |
| 7948 | os.waitpid |
| 7949 | pid: pid_t |
| 7950 | options: int |
| 7951 | / |
| 7952 | |
| 7953 | Wait for completion of a given child process. |
| 7954 | |
| 7955 | Returns a tuple of information regarding the child process: |
| 7956 | (pid, status) |
| 7957 | |
| 7958 | The options argument is ignored on Windows. |
| 7959 | [clinic start generated code]*/ |
| 7960 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7961 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7962 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7963 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7964 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7965 | pid_t res; |
| 7966 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7967 | WAIT_TYPE status; |
| 7968 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7969 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7970 | do { |
| 7971 | Py_BEGIN_ALLOW_THREADS |
| 7972 | res = waitpid(pid, &status, options); |
| 7973 | Py_END_ALLOW_THREADS |
| 7974 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7975 | if (res < 0) |
| 7976 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7977 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7978 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7979 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7980 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7981 | /* 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] | 7982 | /*[clinic input] |
| 7983 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7984 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7985 | options: int |
| 7986 | / |
| 7987 | |
| 7988 | Wait for completion of a given process. |
| 7989 | |
| 7990 | Returns a tuple of information regarding the process: |
| 7991 | (pid, status << 8) |
| 7992 | |
| 7993 | The options argument is ignored on Windows. |
| 7994 | [clinic start generated code]*/ |
| 7995 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7996 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7997 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7998 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7999 | { |
| 8000 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8001 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8002 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8003 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8004 | do { |
| 8005 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 8006 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8007 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 8008 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8009 | Py_END_ALLOW_THREADS |
| 8010 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8011 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8012 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8013 | |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 8014 | unsigned long long ustatus = (unsigned int)status; |
| 8015 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8016 | /* 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] | 8017 | return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8018 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8019 | #endif |
| 8020 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8021 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8022 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8023 | /*[clinic input] |
| 8024 | os.wait |
| 8025 | |
| 8026 | Wait for completion of a child process. |
| 8027 | |
| 8028 | Returns a tuple of information about the child process: |
| 8029 | (pid, status) |
| 8030 | [clinic start generated code]*/ |
| 8031 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8032 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8033 | os_wait_impl(PyObject *module) |
| 8034 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 8035 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8036 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8037 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8038 | WAIT_TYPE status; |
| 8039 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8040 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8041 | do { |
| 8042 | Py_BEGIN_ALLOW_THREADS |
| 8043 | pid = wait(&status); |
| 8044 | Py_END_ALLOW_THREADS |
| 8045 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8046 | if (pid < 0) |
| 8047 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8048 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8049 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8050 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8051 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8052 | |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 8053 | #if defined(__linux__) && defined(__NR_pidfd_open) |
| 8054 | /*[clinic input] |
| 8055 | os.pidfd_open |
| 8056 | pid: pid_t |
| 8057 | flags: unsigned_int = 0 |
| 8058 | |
| 8059 | Return a file descriptor referring to the process *pid*. |
| 8060 | |
| 8061 | The descriptor can be used to perform process management without races and |
| 8062 | signals. |
| 8063 | [clinic start generated code]*/ |
| 8064 | |
| 8065 | static PyObject * |
| 8066 | os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags) |
| 8067 | /*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/ |
| 8068 | { |
| 8069 | int fd = syscall(__NR_pidfd_open, pid, flags); |
| 8070 | if (fd < 0) { |
| 8071 | return posix_error(); |
| 8072 | } |
| 8073 | return PyLong_FromLong(fd); |
| 8074 | } |
| 8075 | #endif |
| 8076 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8077 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8078 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8079 | /*[clinic input] |
| 8080 | os.readlink |
| 8081 | |
| 8082 | path: path_t |
| 8083 | * |
| 8084 | dir_fd: dir_fd(requires='readlinkat') = None |
| 8085 | |
| 8086 | Return a string representing the path to which the symbolic link points. |
| 8087 | |
| 8088 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8089 | and path should be relative; path will then be relative to that directory. |
| 8090 | |
| 8091 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 8092 | using it will raise a NotImplementedError. |
| 8093 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8094 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8095 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8096 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 8097 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8098 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8099 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 8100 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8101 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8102 | |
| 8103 | Py_BEGIN_ALLOW_THREADS |
| 8104 | #ifdef HAVE_READLINKAT |
| 8105 | if (dir_fd != DEFAULT_DIR_FD) |
| 8106 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 8107 | else |
| 8108 | #endif |
| 8109 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 8110 | Py_END_ALLOW_THREADS |
| 8111 | |
| 8112 | if (length < 0) { |
| 8113 | return path_error(path); |
| 8114 | } |
| 8115 | buffer[length] = '\0'; |
| 8116 | |
| 8117 | if (PyUnicode_Check(path->object)) |
| 8118 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 8119 | else |
| 8120 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8121 | #elif defined(MS_WINDOWS) |
| 8122 | DWORD n_bytes_returned; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8123 | DWORD io_result = 0; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8124 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8125 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 8126 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8127 | PyObject *result = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8128 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8129 | /* First get a handle to the reparse point */ |
| 8130 | Py_BEGIN_ALLOW_THREADS |
| 8131 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8132 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8133 | 0, |
| 8134 | 0, |
| 8135 | 0, |
| 8136 | OPEN_EXISTING, |
| 8137 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 8138 | 0); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8139 | if (reparse_point_handle != INVALID_HANDLE_VALUE) { |
| 8140 | /* New call DeviceIoControl to read the reparse point */ |
| 8141 | io_result = DeviceIoControl( |
| 8142 | reparse_point_handle, |
| 8143 | FSCTL_GET_REPARSE_POINT, |
| 8144 | 0, 0, /* in buffer */ |
| 8145 | target_buffer, sizeof(target_buffer), |
| 8146 | &n_bytes_returned, |
| 8147 | 0 /* we're not using OVERLAPPED_IO */ |
| 8148 | ); |
| 8149 | CloseHandle(reparse_point_handle); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8150 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8151 | Py_END_ALLOW_THREADS |
| 8152 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8153 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8154 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8155 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8156 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8157 | wchar_t *name = NULL; |
| 8158 | Py_ssize_t nameLen = 0; |
| 8159 | if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8160 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8161 | name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 8162 | rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 8163 | nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8164 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8165 | else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) |
| 8166 | { |
| 8167 | name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer + |
| 8168 | rdb->MountPointReparseBuffer.SubstituteNameOffset); |
| 8169 | nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
| 8170 | } |
| 8171 | else |
| 8172 | { |
| 8173 | PyErr_SetString(PyExc_ValueError, "not a symbolic link"); |
| 8174 | } |
| 8175 | if (name) { |
| 8176 | if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) { |
| 8177 | /* Our buffer is mutable, so this is okay */ |
| 8178 | name[1] = L'\\'; |
| 8179 | } |
| 8180 | result = PyUnicode_FromWideChar(name, nameLen); |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8181 | if (result && path->narrow) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8182 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
| 8183 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8184 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8185 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8186 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8187 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8188 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8189 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8190 | #if defined(MS_WINDOWS) |
| 8191 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8192 | /* Remove the last portion of the path - return 0 on success */ |
| 8193 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8194 | _dirnameW(WCHAR *path) |
| 8195 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8196 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8197 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 8198 | if (length == MAX_PATH) { |
| 8199 | return -1; |
| 8200 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8201 | |
| 8202 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8203 | for(ptr = path + length; ptr != path; ptr--) { |
| 8204 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8205 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8206 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8207 | } |
| 8208 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8209 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8210 | } |
| 8211 | |
Minmin Gong | 7f21c9a | 2020-05-18 09:17:19 -0700 | [diff] [blame] | 8212 | #endif |
| 8213 | |
| 8214 | #ifdef HAVE_SYMLINK |
| 8215 | |
| 8216 | #if defined(MS_WINDOWS) |
| 8217 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8218 | /* Is this path absolute? */ |
| 8219 | static int |
| 8220 | _is_absW(const WCHAR *path) |
| 8221 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8222 | return path[0] == L'\\' || path[0] == L'/' || |
| 8223 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8224 | } |
| 8225 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8226 | /* join root and rest with a backslash - return 0 on success */ |
| 8227 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8228 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 8229 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8230 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8231 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8232 | } |
| 8233 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8234 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 8235 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8236 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8237 | |
| 8238 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 8239 | return -1; |
| 8240 | } |
| 8241 | |
| 8242 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8243 | } |
| 8244 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8245 | /* Return True if the path at src relative to dest is a directory */ |
| 8246 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8247 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8248 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8249 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 8250 | WCHAR dest_parent[MAX_PATH]; |
| 8251 | WCHAR src_resolved[MAX_PATH] = L""; |
| 8252 | |
| 8253 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8254 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 8255 | _dirnameW(dest_parent)) { |
| 8256 | return 0; |
| 8257 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8258 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8259 | if (_joinW(src_resolved, dest_parent, src)) { |
| 8260 | return 0; |
| 8261 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8262 | return ( |
| 8263 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 8264 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 8265 | ); |
| 8266 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8267 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8268 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8269 | |
| 8270 | /*[clinic input] |
| 8271 | os.symlink |
| 8272 | src: path_t |
| 8273 | dst: path_t |
| 8274 | target_is_directory: bool = False |
| 8275 | * |
| 8276 | dir_fd: dir_fd(requires='symlinkat')=None |
| 8277 | |
| 8278 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 8279 | |
| 8280 | Create a symbolic link pointing to src named dst. |
| 8281 | |
| 8282 | target_is_directory is required on Windows if the target is to be |
| 8283 | interpreted as a directory. (On Windows, symlink requires |
| 8284 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 8285 | target_is_directory is ignored on non-Windows platforms. |
| 8286 | |
| 8287 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8288 | and path should be relative; path will then be relative to that directory. |
| 8289 | dir_fd may not be implemented on your platform. |
| 8290 | If it is unavailable, using it will raise a NotImplementedError. |
| 8291 | |
| 8292 | [clinic start generated code]*/ |
| 8293 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8294 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8295 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8296 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8297 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8298 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8299 | #ifdef MS_WINDOWS |
| 8300 | DWORD result; |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8301 | DWORD flags = 0; |
| 8302 | |
| 8303 | /* Assumed true, set to false if detected to not be available. */ |
| 8304 | static int windows_has_symlink_unprivileged_flag = TRUE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8305 | #else |
| 8306 | int result; |
| 8307 | #endif |
| 8308 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 8309 | if (PySys_Audit("os.symlink", "OOi", src->object, dst->object, |
| 8310 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 8311 | return NULL; |
| 8312 | } |
| 8313 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8314 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8315 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8316 | if (windows_has_symlink_unprivileged_flag) { |
| 8317 | /* Allow non-admin symlinks if system allows it. */ |
| 8318 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 8319 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8320 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8321 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8322 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8323 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
| 8324 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { |
| 8325 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 8326 | } |
| 8327 | |
| 8328 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8329 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8330 | Py_END_ALLOW_THREADS |
| 8331 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8332 | if (windows_has_symlink_unprivileged_flag && !result && |
| 8333 | ERROR_INVALID_PARAMETER == GetLastError()) { |
| 8334 | |
| 8335 | Py_BEGIN_ALLOW_THREADS |
| 8336 | _Py_BEGIN_SUPPRESS_IPH |
| 8337 | /* This error might be caused by |
| 8338 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. |
| 8339 | Try again, and update windows_has_symlink_unprivileged_flag if we |
| 8340 | are successful this time. |
| 8341 | |
| 8342 | NOTE: There is a risk of a race condition here if there are other |
| 8343 | conditions than the flag causing ERROR_INVALID_PARAMETER, and |
| 8344 | another process (or thread) changes that condition in between our |
| 8345 | calls to CreateSymbolicLink. |
| 8346 | */ |
| 8347 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 8348 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
| 8349 | _Py_END_SUPPRESS_IPH |
| 8350 | Py_END_ALLOW_THREADS |
| 8351 | |
| 8352 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { |
| 8353 | windows_has_symlink_unprivileged_flag = FALSE; |
| 8354 | } |
| 8355 | } |
| 8356 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8357 | if (!result) |
| 8358 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8359 | |
| 8360 | #else |
| 8361 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8362 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 8363 | PyErr_SetString(PyExc_ValueError, |
| 8364 | "symlink: src and dst must be the same type"); |
| 8365 | return NULL; |
| 8366 | } |
| 8367 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8368 | Py_BEGIN_ALLOW_THREADS |
| 8369 | #if HAVE_SYMLINKAT |
| 8370 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8371 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8372 | else |
| 8373 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8374 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8375 | Py_END_ALLOW_THREADS |
| 8376 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8377 | if (result) |
| 8378 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8379 | #endif |
| 8380 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8381 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8382 | } |
| 8383 | #endif /* HAVE_SYMLINK */ |
| 8384 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8385 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8386 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8387 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8388 | static PyStructSequence_Field times_result_fields[] = { |
| 8389 | {"user", "user time"}, |
| 8390 | {"system", "system time"}, |
| 8391 | {"children_user", "user time of children"}, |
| 8392 | {"children_system", "system time of children"}, |
| 8393 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 8394 | {NULL} |
| 8395 | }; |
| 8396 | |
| 8397 | PyDoc_STRVAR(times_result__doc__, |
| 8398 | "times_result: Result from os.times().\n\n\ |
| 8399 | This object may be accessed either as a tuple of\n\ |
| 8400 | (user, system, children_user, children_system, elapsed),\n\ |
| 8401 | or via the attributes user, system, children_user, children_system,\n\ |
| 8402 | and elapsed.\n\ |
| 8403 | \n\ |
| 8404 | See os.times for more information."); |
| 8405 | |
| 8406 | static PyStructSequence_Desc times_result_desc = { |
| 8407 | "times_result", /* name */ |
| 8408 | times_result__doc__, /* doc */ |
| 8409 | times_result_fields, |
| 8410 | 5 |
| 8411 | }; |
| 8412 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8413 | #ifdef MS_WINDOWS |
| 8414 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 8415 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8416 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8417 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8418 | |
| 8419 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8420 | build_times_result(PyObject *module, double user, double system, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8421 | double children_user, double children_system, |
| 8422 | double elapsed) |
| 8423 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8424 | PyObject *TimesResultType = get_posix_state(module)->TimesResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8425 | PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8426 | if (value == NULL) |
| 8427 | return NULL; |
| 8428 | |
| 8429 | #define SET(i, field) \ |
| 8430 | { \ |
| 8431 | PyObject *o = PyFloat_FromDouble(field); \ |
| 8432 | if (!o) { \ |
| 8433 | Py_DECREF(value); \ |
| 8434 | return NULL; \ |
| 8435 | } \ |
| 8436 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 8437 | } \ |
| 8438 | |
| 8439 | SET(0, user); |
| 8440 | SET(1, system); |
| 8441 | SET(2, children_user); |
| 8442 | SET(3, children_system); |
| 8443 | SET(4, elapsed); |
| 8444 | |
| 8445 | #undef SET |
| 8446 | |
| 8447 | return value; |
| 8448 | } |
| 8449 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8450 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8451 | #ifndef MS_WINDOWS |
| 8452 | #define NEED_TICKS_PER_SECOND |
| 8453 | static long ticks_per_second = -1; |
| 8454 | #endif /* MS_WINDOWS */ |
| 8455 | |
| 8456 | /*[clinic input] |
| 8457 | os.times |
| 8458 | |
| 8459 | Return a collection containing process timing information. |
| 8460 | |
| 8461 | The object returned behaves like a named tuple with these fields: |
| 8462 | (utime, stime, cutime, cstime, elapsed_time) |
| 8463 | All fields are floating point numbers. |
| 8464 | [clinic start generated code]*/ |
| 8465 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8466 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8467 | os_times_impl(PyObject *module) |
| 8468 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8469 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8470 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8471 | FILETIME create, exit, kernel, user; |
| 8472 | HANDLE hProc; |
| 8473 | hProc = GetCurrentProcess(); |
| 8474 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8475 | /* The fields of a FILETIME structure are the hi and lo part |
| 8476 | of a 64-bit value expressed in 100 nanosecond units. |
| 8477 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8478 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8479 | */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8480 | return build_times_result(module, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8481 | (double)(user.dwHighDateTime*429.4967296 + |
| 8482 | user.dwLowDateTime*1e-7), |
| 8483 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8484 | kernel.dwLowDateTime*1e-7), |
| 8485 | (double)0, |
| 8486 | (double)0, |
| 8487 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8488 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8489 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8490 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8491 | |
| 8492 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8493 | struct tms t; |
| 8494 | clock_t c; |
| 8495 | errno = 0; |
| 8496 | c = times(&t); |
| 8497 | if (c == (clock_t) -1) |
| 8498 | return posix_error(); |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8499 | return build_times_result(module, |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8500 | (double)t.tms_utime / ticks_per_second, |
| 8501 | (double)t.tms_stime / ticks_per_second, |
| 8502 | (double)t.tms_cutime / ticks_per_second, |
| 8503 | (double)t.tms_cstime / ticks_per_second, |
| 8504 | (double)c / ticks_per_second); |
| 8505 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8506 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8507 | #endif /* HAVE_TIMES */ |
| 8508 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8509 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8510 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8511 | /*[clinic input] |
| 8512 | os.getsid |
| 8513 | |
| 8514 | pid: pid_t |
| 8515 | / |
| 8516 | |
| 8517 | Call the system call getsid(pid) and return the result. |
| 8518 | [clinic start generated code]*/ |
| 8519 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8520 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8521 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8522 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8523 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8524 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8525 | sid = getsid(pid); |
| 8526 | if (sid < 0) |
| 8527 | return posix_error(); |
| 8528 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8529 | } |
| 8530 | #endif /* HAVE_GETSID */ |
| 8531 | |
| 8532 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8533 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8534 | /*[clinic input] |
| 8535 | os.setsid |
| 8536 | |
| 8537 | Call the system call setsid(). |
| 8538 | [clinic start generated code]*/ |
| 8539 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8540 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8541 | os_setsid_impl(PyObject *module) |
| 8542 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8543 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8544 | if (setsid() < 0) |
| 8545 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8546 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8547 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8548 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8549 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8550 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8551 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8552 | /*[clinic input] |
| 8553 | os.setpgid |
| 8554 | |
| 8555 | pid: pid_t |
| 8556 | pgrp: pid_t |
| 8557 | / |
| 8558 | |
| 8559 | Call the system call setpgid(pid, pgrp). |
| 8560 | [clinic start generated code]*/ |
| 8561 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8562 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8563 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8564 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8565 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8566 | if (setpgid(pid, pgrp) < 0) |
| 8567 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8568 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8569 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8570 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8571 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8572 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8573 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8574 | /*[clinic input] |
| 8575 | os.tcgetpgrp |
| 8576 | |
| 8577 | fd: int |
| 8578 | / |
| 8579 | |
| 8580 | Return the process group associated with the terminal specified by fd. |
| 8581 | [clinic start generated code]*/ |
| 8582 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8583 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8584 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8585 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8586 | { |
| 8587 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8588 | if (pgid < 0) |
| 8589 | return posix_error(); |
| 8590 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8591 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8592 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8593 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8594 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8595 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8596 | /*[clinic input] |
| 8597 | os.tcsetpgrp |
| 8598 | |
| 8599 | fd: int |
| 8600 | pgid: pid_t |
| 8601 | / |
| 8602 | |
| 8603 | Set the process group associated with the terminal specified by fd. |
| 8604 | [clinic start generated code]*/ |
| 8605 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8606 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8607 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8608 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8609 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8610 | if (tcsetpgrp(fd, pgid) < 0) |
| 8611 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8612 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8613 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8614 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8615 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8616 | /* Functions acting on file descriptors */ |
| 8617 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8618 | #ifdef O_CLOEXEC |
| 8619 | extern int _Py_open_cloexec_works; |
| 8620 | #endif |
| 8621 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8622 | |
| 8623 | /*[clinic input] |
| 8624 | os.open -> int |
| 8625 | path: path_t |
| 8626 | flags: int |
| 8627 | mode: int = 0o777 |
| 8628 | * |
| 8629 | dir_fd: dir_fd(requires='openat') = None |
| 8630 | |
| 8631 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8632 | |
| 8633 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8634 | |
| 8635 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8636 | and path should be relative; path will then be relative to that directory. |
| 8637 | dir_fd may not be implemented on your platform. |
| 8638 | If it is unavailable, using it will raise a NotImplementedError. |
| 8639 | [clinic start generated code]*/ |
| 8640 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8641 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8642 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8643 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8644 | { |
| 8645 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8646 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8647 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8648 | #ifdef O_CLOEXEC |
| 8649 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8650 | #elif !defined(MS_WINDOWS) |
| 8651 | int *atomic_flag_works = NULL; |
| 8652 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8653 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8654 | #ifdef MS_WINDOWS |
| 8655 | flags |= O_NOINHERIT; |
| 8656 | #elif defined(O_CLOEXEC) |
| 8657 | flags |= O_CLOEXEC; |
| 8658 | #endif |
| 8659 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 8660 | if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) { |
| 8661 | return -1; |
| 8662 | } |
| 8663 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8664 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8665 | do { |
| 8666 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8667 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8668 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8669 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8670 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8671 | if (dir_fd != DEFAULT_DIR_FD) |
| 8672 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8673 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8674 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8675 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8676 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8677 | Py_END_ALLOW_THREADS |
| 8678 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8679 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8680 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8681 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8682 | if (!async_err) |
| 8683 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8684 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8685 | } |
| 8686 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8687 | #ifndef MS_WINDOWS |
| 8688 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8689 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8690 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8691 | } |
| 8692 | #endif |
| 8693 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8694 | return fd; |
| 8695 | } |
| 8696 | |
| 8697 | |
| 8698 | /*[clinic input] |
| 8699 | os.close |
| 8700 | |
| 8701 | fd: int |
| 8702 | |
| 8703 | Close a file descriptor. |
| 8704 | [clinic start generated code]*/ |
| 8705 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8706 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8707 | os_close_impl(PyObject *module, int fd) |
| 8708 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8709 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8710 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8711 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8712 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8713 | * for more details. |
| 8714 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8715 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8716 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8717 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8718 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8719 | Py_END_ALLOW_THREADS |
| 8720 | if (res < 0) |
| 8721 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8722 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8723 | } |
| 8724 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8725 | |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8726 | #ifdef HAVE_FDWALK |
| 8727 | static int |
| 8728 | _fdwalk_close_func(void *lohi, int fd) |
| 8729 | { |
| 8730 | int lo = ((int *)lohi)[0]; |
| 8731 | int hi = ((int *)lohi)[1]; |
| 8732 | |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8733 | if (fd >= hi) { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8734 | return 1; |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8735 | } |
| 8736 | else if (fd >= lo) { |
| 8737 | /* Ignore errors */ |
| 8738 | (void)close(fd); |
| 8739 | } |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8740 | return 0; |
| 8741 | } |
| 8742 | #endif /* HAVE_FDWALK */ |
| 8743 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8744 | /*[clinic input] |
| 8745 | os.closerange |
| 8746 | |
| 8747 | fd_low: int |
| 8748 | fd_high: int |
| 8749 | / |
| 8750 | |
| 8751 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8752 | [clinic start generated code]*/ |
| 8753 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8754 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8755 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8756 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8757 | { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8758 | #ifdef HAVE_FDWALK |
| 8759 | int lohi[2]; |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8760 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8761 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8762 | _Py_BEGIN_SUPPRESS_IPH |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8763 | #ifdef HAVE_FDWALK |
| 8764 | lohi[0] = Py_MAX(fd_low, 0); |
| 8765 | lohi[1] = fd_high; |
| 8766 | fdwalk(_fdwalk_close_func, lohi); |
| 8767 | #else |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8768 | fd_low = Py_MAX(fd_low, 0); |
| 8769 | #ifdef __FreeBSD__ |
| 8770 | if (fd_high >= sysconf(_SC_OPEN_MAX)) { |
| 8771 | /* Any errors encountered while closing file descriptors are ignored */ |
| 8772 | closefrom(fd_low); |
| 8773 | } |
| 8774 | else |
| 8775 | #endif |
| 8776 | { |
| 8777 | for (int i = fd_low; i < fd_high; i++) { |
| 8778 | /* Ignore errors */ |
| 8779 | (void)close(i); |
| 8780 | } |
| 8781 | } |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8782 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8783 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8784 | Py_END_ALLOW_THREADS |
| 8785 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8786 | } |
| 8787 | |
| 8788 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8789 | /*[clinic input] |
| 8790 | os.dup -> int |
| 8791 | |
| 8792 | fd: int |
| 8793 | / |
| 8794 | |
| 8795 | Return a duplicate of a file descriptor. |
| 8796 | [clinic start generated code]*/ |
| 8797 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8798 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8799 | os_dup_impl(PyObject *module, int fd) |
| 8800 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8801 | { |
| 8802 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8803 | } |
| 8804 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8805 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8806 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8807 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8808 | fd: int |
| 8809 | fd2: int |
| 8810 | inheritable: bool=True |
| 8811 | |
| 8812 | Duplicate file descriptor. |
| 8813 | [clinic start generated code]*/ |
| 8814 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8815 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8816 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8817 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8818 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8819 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8820 | #if defined(HAVE_DUP3) && \ |
| 8821 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8822 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8823 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8824 | #endif |
| 8825 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8826 | if (fd < 0 || fd2 < 0) { |
| 8827 | posix_error(); |
| 8828 | return -1; |
| 8829 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8830 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8831 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8832 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8833 | * upon close(), and therefore below. |
| 8834 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8835 | #ifdef MS_WINDOWS |
| 8836 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8837 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8838 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8839 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8840 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8841 | if (res < 0) { |
| 8842 | posix_error(); |
| 8843 | return -1; |
| 8844 | } |
| 8845 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8846 | |
| 8847 | /* Character files like console cannot be make non-inheritable */ |
| 8848 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8849 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8850 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8851 | } |
| 8852 | |
| 8853 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8854 | Py_BEGIN_ALLOW_THREADS |
| 8855 | if (!inheritable) |
| 8856 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8857 | else |
| 8858 | res = dup2(fd, fd2); |
| 8859 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8860 | if (res < 0) { |
| 8861 | posix_error(); |
| 8862 | return -1; |
| 8863 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8864 | |
| 8865 | #else |
| 8866 | |
| 8867 | #ifdef HAVE_DUP3 |
| 8868 | if (!inheritable && dup3_works != 0) { |
| 8869 | Py_BEGIN_ALLOW_THREADS |
| 8870 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8871 | Py_END_ALLOW_THREADS |
| 8872 | if (res < 0) { |
| 8873 | if (dup3_works == -1) |
| 8874 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8875 | if (dup3_works) { |
| 8876 | posix_error(); |
| 8877 | return -1; |
| 8878 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8879 | } |
| 8880 | } |
| 8881 | |
| 8882 | if (inheritable || dup3_works == 0) |
| 8883 | { |
| 8884 | #endif |
| 8885 | Py_BEGIN_ALLOW_THREADS |
| 8886 | res = dup2(fd, fd2); |
| 8887 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8888 | if (res < 0) { |
| 8889 | posix_error(); |
| 8890 | return -1; |
| 8891 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8892 | |
| 8893 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8894 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8895 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8896 | } |
| 8897 | #ifdef HAVE_DUP3 |
| 8898 | } |
| 8899 | #endif |
| 8900 | |
| 8901 | #endif |
| 8902 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8903 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8904 | } |
| 8905 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8906 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8907 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8908 | /*[clinic input] |
| 8909 | os.lockf |
| 8910 | |
| 8911 | fd: int |
| 8912 | An open file descriptor. |
| 8913 | command: int |
| 8914 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8915 | length: Py_off_t |
| 8916 | The number of bytes to lock, starting at the current position. |
| 8917 | / |
| 8918 | |
| 8919 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8920 | |
| 8921 | [clinic start generated code]*/ |
| 8922 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8923 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8924 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8925 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8926 | { |
| 8927 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8928 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 8929 | if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) { |
| 8930 | return NULL; |
| 8931 | } |
| 8932 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8933 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8934 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8935 | Py_END_ALLOW_THREADS |
| 8936 | |
| 8937 | if (res < 0) |
| 8938 | return posix_error(); |
| 8939 | |
| 8940 | Py_RETURN_NONE; |
| 8941 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8942 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8943 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8944 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8945 | /*[clinic input] |
| 8946 | os.lseek -> Py_off_t |
| 8947 | |
| 8948 | fd: int |
| 8949 | position: Py_off_t |
| 8950 | how: int |
| 8951 | / |
| 8952 | |
| 8953 | Set the position of a file descriptor. Return the new position. |
| 8954 | |
| 8955 | Return the new cursor position in number of bytes |
| 8956 | relative to the beginning of the file. |
| 8957 | [clinic start generated code]*/ |
| 8958 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8959 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8960 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8961 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8962 | { |
| 8963 | Py_off_t result; |
| 8964 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8965 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8966 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8967 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8968 | case 0: how = SEEK_SET; break; |
| 8969 | case 1: how = SEEK_CUR; break; |
| 8970 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8971 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8972 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8973 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8974 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8975 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8976 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8977 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8978 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8979 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8980 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8981 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8982 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8983 | if (result < 0) |
| 8984 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8986 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8987 | } |
| 8988 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8990 | /*[clinic input] |
| 8991 | os.read |
| 8992 | fd: int |
| 8993 | length: Py_ssize_t |
| 8994 | / |
| 8995 | |
| 8996 | Read from a file descriptor. Returns a bytes object. |
| 8997 | [clinic start generated code]*/ |
| 8998 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8999 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9000 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 9001 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9002 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9003 | Py_ssize_t n; |
| 9004 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9005 | |
| 9006 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9007 | errno = EINVAL; |
| 9008 | return posix_error(); |
| 9009 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9010 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 9011 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9012 | |
| 9013 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9014 | if (buffer == NULL) |
| 9015 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9016 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9017 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 9018 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9019 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9020 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9021 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9022 | |
| 9023 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9024 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9025 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9026 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9027 | } |
| 9028 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9029 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9030 | || defined(__APPLE__))) \ |
| 9031 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 9032 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9033 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9034 | 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] | 9035 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9036 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9037 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9038 | *iov = PyMem_New(struct iovec, cnt); |
| 9039 | if (*iov == NULL) { |
| 9040 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9041 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9042 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9043 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9044 | *buf = PyMem_New(Py_buffer, cnt); |
| 9045 | if (*buf == NULL) { |
| 9046 | PyMem_Del(*iov); |
| 9047 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9048 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9049 | } |
| 9050 | |
| 9051 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9052 | PyObject *item = PySequence_GetItem(seq, i); |
| 9053 | if (item == NULL) |
| 9054 | goto fail; |
| 9055 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 9056 | Py_DECREF(item); |
| 9057 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9058 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9059 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9060 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9061 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9062 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9063 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9064 | |
| 9065 | fail: |
| 9066 | PyMem_Del(*iov); |
| 9067 | for (j = 0; j < i; j++) { |
| 9068 | PyBuffer_Release(&(*buf)[j]); |
| 9069 | } |
| 9070 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9071 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9072 | } |
| 9073 | |
| 9074 | static void |
| 9075 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 9076 | { |
| 9077 | int i; |
| 9078 | PyMem_Del(iov); |
| 9079 | for (i = 0; i < cnt; i++) { |
| 9080 | PyBuffer_Release(&buf[i]); |
| 9081 | } |
| 9082 | PyMem_Del(buf); |
| 9083 | } |
| 9084 | #endif |
| 9085 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9086 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9087 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9088 | /*[clinic input] |
| 9089 | os.readv -> Py_ssize_t |
| 9090 | |
| 9091 | fd: int |
| 9092 | buffers: object |
| 9093 | / |
| 9094 | |
| 9095 | Read from a file descriptor fd into an iterable of buffers. |
| 9096 | |
| 9097 | The buffers should be mutable buffers accepting bytes. |
| 9098 | readv will transfer data into each buffer until it is full |
| 9099 | and then move on to the next buffer in the sequence to hold |
| 9100 | the rest of the data. |
| 9101 | |
| 9102 | readv returns the total number of bytes read, |
| 9103 | which may be less than the total capacity of all the buffers. |
| 9104 | [clinic start generated code]*/ |
| 9105 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9106 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9107 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 9108 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9109 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9110 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9111 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9112 | struct iovec *iov; |
| 9113 | Py_buffer *buf; |
| 9114 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9115 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9116 | PyErr_SetString(PyExc_TypeError, |
| 9117 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9118 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9119 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9120 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9121 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9122 | if (cnt < 0) |
| 9123 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9124 | |
| 9125 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 9126 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9127 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9128 | do { |
| 9129 | Py_BEGIN_ALLOW_THREADS |
| 9130 | n = readv(fd, iov, cnt); |
| 9131 | Py_END_ALLOW_THREADS |
| 9132 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9133 | |
| 9134 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9135 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9136 | if (!async_err) |
| 9137 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9138 | return -1; |
| 9139 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9140 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9141 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9142 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9143 | #endif /* HAVE_READV */ |
| 9144 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9145 | |
| 9146 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9147 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9148 | os.pread |
| 9149 | |
| 9150 | fd: int |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9151 | length: Py_ssize_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9152 | offset: Py_off_t |
| 9153 | / |
| 9154 | |
| 9155 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 9156 | |
| 9157 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 9158 | the beginning of the file. The file offset remains unchanged. |
| 9159 | [clinic start generated code]*/ |
| 9160 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9161 | static PyObject * |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9162 | os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset) |
| 9163 | /*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9164 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9165 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9166 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9167 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9168 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9169 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9170 | errno = EINVAL; |
| 9171 | return posix_error(); |
| 9172 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9173 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9174 | if (buffer == NULL) |
| 9175 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9176 | |
| 9177 | do { |
| 9178 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9179 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9180 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9181 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9182 | Py_END_ALLOW_THREADS |
| 9183 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9184 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9185 | if (n < 0) { |
| 9186 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9187 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9188 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9189 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9190 | _PyBytes_Resize(&buffer, n); |
| 9191 | return buffer; |
| 9192 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9193 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9194 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9195 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 9196 | /*[clinic input] |
| 9197 | os.preadv -> Py_ssize_t |
| 9198 | |
| 9199 | fd: int |
| 9200 | buffers: object |
| 9201 | offset: Py_off_t |
| 9202 | flags: int = 0 |
| 9203 | / |
| 9204 | |
| 9205 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 9206 | |
| 9207 | Combines the functionality of readv() and pread(). As readv(), it will |
| 9208 | transfer data into each buffer until it is full and then move on to the next |
| 9209 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 9210 | specifies the file offset at which the input operation is to be performed. It |
| 9211 | will return the total number of bytes read (which can be less than the total |
| 9212 | capacity of all the objects). |
| 9213 | |
| 9214 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9215 | |
| 9216 | - RWF_HIPRI |
| 9217 | - RWF_NOWAIT |
| 9218 | |
| 9219 | Using non-zero flags requires Linux 4.6 or newer. |
| 9220 | [clinic start generated code]*/ |
| 9221 | |
| 9222 | static Py_ssize_t |
| 9223 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9224 | int flags) |
| 9225 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 9226 | { |
| 9227 | Py_ssize_t cnt, n; |
| 9228 | int async_err = 0; |
| 9229 | struct iovec *iov; |
| 9230 | Py_buffer *buf; |
| 9231 | |
| 9232 | if (!PySequence_Check(buffers)) { |
| 9233 | PyErr_SetString(PyExc_TypeError, |
| 9234 | "preadv2() arg 2 must be a sequence"); |
| 9235 | return -1; |
| 9236 | } |
| 9237 | |
| 9238 | cnt = PySequence_Size(buffers); |
| 9239 | if (cnt < 0) { |
| 9240 | return -1; |
| 9241 | } |
| 9242 | |
| 9243 | #ifndef HAVE_PREADV2 |
| 9244 | if(flags != 0) { |
| 9245 | argument_unavailable_error("preadv2", "flags"); |
| 9246 | return -1; |
| 9247 | } |
| 9248 | #endif |
| 9249 | |
| 9250 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 9251 | return -1; |
| 9252 | } |
| 9253 | #ifdef HAVE_PREADV2 |
| 9254 | do { |
| 9255 | Py_BEGIN_ALLOW_THREADS |
| 9256 | _Py_BEGIN_SUPPRESS_IPH |
| 9257 | n = preadv2(fd, iov, cnt, offset, flags); |
| 9258 | _Py_END_SUPPRESS_IPH |
| 9259 | Py_END_ALLOW_THREADS |
| 9260 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9261 | #else |
| 9262 | do { |
| 9263 | Py_BEGIN_ALLOW_THREADS |
| 9264 | _Py_BEGIN_SUPPRESS_IPH |
| 9265 | n = preadv(fd, iov, cnt, offset); |
| 9266 | _Py_END_SUPPRESS_IPH |
| 9267 | Py_END_ALLOW_THREADS |
| 9268 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9269 | #endif |
| 9270 | |
| 9271 | iov_cleanup(iov, buf, cnt); |
| 9272 | if (n < 0) { |
| 9273 | if (!async_err) { |
| 9274 | posix_error(); |
| 9275 | } |
| 9276 | return -1; |
| 9277 | } |
| 9278 | |
| 9279 | return n; |
| 9280 | } |
| 9281 | #endif /* HAVE_PREADV */ |
| 9282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9283 | |
| 9284 | /*[clinic input] |
| 9285 | os.write -> Py_ssize_t |
| 9286 | |
| 9287 | fd: int |
| 9288 | data: Py_buffer |
| 9289 | / |
| 9290 | |
| 9291 | Write a bytes object to a file descriptor. |
| 9292 | [clinic start generated code]*/ |
| 9293 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9294 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9295 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 9296 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9297 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9298 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9299 | } |
| 9300 | |
| 9301 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9302 | #ifdef __APPLE__ |
| 9303 | /*[clinic input] |
| 9304 | os.sendfile |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9305 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9306 | out_fd: int |
| 9307 | in_fd: int |
| 9308 | offset: Py_off_t |
| 9309 | count as sbytes: Py_off_t |
| 9310 | headers: object(c_default="NULL") = () |
| 9311 | trailers: object(c_default="NULL") = () |
| 9312 | flags: int = 0 |
| 9313 | |
| 9314 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9315 | [clinic start generated code]*/ |
| 9316 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9317 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9318 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, |
| 9319 | Py_off_t sbytes, PyObject *headers, PyObject *trailers, |
| 9320 | int flags) |
| 9321 | /*[clinic end generated code: output=81c4bcd143f5c82b input=b0d72579d4c69afa]*/ |
| 9322 | #elif defined(__FreeBSD__) || defined(__DragonFly__) |
| 9323 | /*[clinic input] |
| 9324 | os.sendfile |
| 9325 | |
| 9326 | out_fd: int |
| 9327 | in_fd: int |
| 9328 | offset: Py_off_t |
| 9329 | count: Py_ssize_t |
| 9330 | headers: object(c_default="NULL") = () |
| 9331 | trailers: object(c_default="NULL") = () |
| 9332 | flags: int = 0 |
| 9333 | |
| 9334 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9335 | [clinic start generated code]*/ |
| 9336 | |
| 9337 | static PyObject * |
| 9338 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, |
| 9339 | Py_ssize_t count, PyObject *headers, PyObject *trailers, |
| 9340 | int flags) |
| 9341 | /*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/ |
| 9342 | #else |
| 9343 | /*[clinic input] |
| 9344 | os.sendfile |
| 9345 | |
| 9346 | out_fd: int |
| 9347 | in_fd: int |
| 9348 | offset as offobj: object |
| 9349 | count: Py_ssize_t |
| 9350 | |
| 9351 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9352 | [clinic start generated code]*/ |
| 9353 | |
| 9354 | static PyObject * |
| 9355 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj, |
| 9356 | Py_ssize_t count) |
| 9357 | /*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/ |
| 9358 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9359 | { |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9360 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9361 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9362 | |
| 9363 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 9364 | #ifndef __APPLE__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9365 | off_t sbytes; |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9366 | #endif |
| 9367 | Py_buffer *hbuf, *tbuf; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9368 | struct sf_hdtr sf; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9369 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 9370 | sf.headers = NULL; |
| 9371 | sf.trailers = NULL; |
| 9372 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9373 | if (headers != NULL) { |
| 9374 | if (!PySequence_Check(headers)) { |
| 9375 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9376 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9377 | return NULL; |
| 9378 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9379 | Py_ssize_t i = PySequence_Size(headers); |
| 9380 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9381 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9382 | if (i > INT_MAX) { |
| 9383 | PyErr_SetString(PyExc_OverflowError, |
| 9384 | "sendfile() header is too large"); |
| 9385 | return NULL; |
| 9386 | } |
| 9387 | if (i > 0) { |
| 9388 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9389 | if (iov_setup(&(sf.headers), &hbuf, |
| 9390 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9391 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9392 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9393 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 9394 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 9395 | # define OFF_T_MAX 0x7fffffffffffffff |
| 9396 | if (sbytes >= OFF_T_MAX - blen) { |
| 9397 | PyErr_SetString(PyExc_OverflowError, |
| 9398 | "sendfile() header is too large"); |
| 9399 | return NULL; |
| 9400 | } |
| 9401 | sbytes += blen; |
| 9402 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9403 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9404 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9405 | } |
| 9406 | } |
| 9407 | if (trailers != NULL) { |
| 9408 | if (!PySequence_Check(trailers)) { |
| 9409 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9410 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9411 | return NULL; |
| 9412 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9413 | Py_ssize_t i = PySequence_Size(trailers); |
| 9414 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9415 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9416 | if (i > INT_MAX) { |
| 9417 | PyErr_SetString(PyExc_OverflowError, |
| 9418 | "sendfile() trailer is too large"); |
| 9419 | return NULL; |
| 9420 | } |
| 9421 | if (i > 0) { |
| 9422 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9423 | if (iov_setup(&(sf.trailers), &tbuf, |
| 9424 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9425 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9426 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9427 | } |
| 9428 | } |
| 9429 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9430 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9431 | do { |
| 9432 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9433 | #ifdef __APPLE__ |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9434 | ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9435 | #else |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9436 | ret = sendfile(in_fd, out_fd, offset, count, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9437 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9438 | Py_END_ALLOW_THREADS |
| 9439 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9440 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9441 | |
| 9442 | if (sf.headers != NULL) |
| 9443 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 9444 | if (sf.trailers != NULL) |
| 9445 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 9446 | |
| 9447 | if (ret < 0) { |
| 9448 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 9449 | if (sbytes != 0) { |
| 9450 | // some data has been sent |
| 9451 | goto done; |
| 9452 | } |
| 9453 | else { |
| 9454 | // no data has been sent; upper application is supposed |
| 9455 | // to retry on EAGAIN or EBUSY |
| 9456 | return posix_error(); |
| 9457 | } |
| 9458 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9459 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9460 | } |
| 9461 | goto done; |
| 9462 | |
| 9463 | done: |
| 9464 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 9465 | return Py_BuildValue("l", sbytes); |
| 9466 | #else |
| 9467 | return Py_BuildValue("L", sbytes); |
| 9468 | #endif |
| 9469 | |
| 9470 | #else |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9471 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9472 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9473 | do { |
| 9474 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9475 | ret = sendfile(out_fd, in_fd, NULL, count); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9476 | Py_END_ALLOW_THREADS |
| 9477 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9478 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9479 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9480 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9481 | } |
| 9482 | #endif |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9483 | off_t offset; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9484 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9485 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9486 | |
| 9487 | do { |
| 9488 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9489 | ret = sendfile(out_fd, in_fd, &offset, count); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9490 | Py_END_ALLOW_THREADS |
| 9491 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9492 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9493 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9494 | return Py_BuildValue("n", ret); |
| 9495 | #endif |
| 9496 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9497 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9498 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9499 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9500 | #if defined(__APPLE__) |
| 9501 | /*[clinic input] |
| 9502 | os._fcopyfile |
| 9503 | |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9504 | in_fd: int |
| 9505 | out_fd: int |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9506 | flags: int |
| 9507 | / |
| 9508 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9509 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9510 | [clinic start generated code]*/ |
| 9511 | |
| 9512 | static PyObject * |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9513 | os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags) |
| 9514 | /*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9515 | { |
| 9516 | int ret; |
| 9517 | |
| 9518 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9519 | ret = fcopyfile(in_fd, out_fd, NULL, flags); |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9520 | Py_END_ALLOW_THREADS |
| 9521 | if (ret < 0) |
| 9522 | return posix_error(); |
| 9523 | Py_RETURN_NONE; |
| 9524 | } |
| 9525 | #endif |
| 9526 | |
| 9527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9528 | /*[clinic input] |
| 9529 | os.fstat |
| 9530 | |
| 9531 | fd : int |
| 9532 | |
| 9533 | Perform a stat system call on the given file descriptor. |
| 9534 | |
| 9535 | Like stat(), but for an open file descriptor. |
| 9536 | Equivalent to os.stat(fd). |
| 9537 | [clinic start generated code]*/ |
| 9538 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9539 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9540 | os_fstat_impl(PyObject *module, int fd) |
| 9541 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9542 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9543 | STRUCT_STAT st; |
| 9544 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9545 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9546 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9547 | do { |
| 9548 | Py_BEGIN_ALLOW_THREADS |
| 9549 | res = FSTAT(fd, &st); |
| 9550 | Py_END_ALLOW_THREADS |
| 9551 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9552 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9553 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9554 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9555 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9556 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9557 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9558 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9559 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 9560 | return _pystat_fromstructstat(module, &st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9561 | } |
| 9562 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9563 | |
| 9564 | /*[clinic input] |
| 9565 | os.isatty -> bool |
| 9566 | fd: int |
| 9567 | / |
| 9568 | |
| 9569 | Return True if the fd is connected to a terminal. |
| 9570 | |
| 9571 | Return True if the file descriptor is an open file descriptor |
| 9572 | connected to the slave end of a terminal. |
| 9573 | [clinic start generated code]*/ |
| 9574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9575 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9576 | os_isatty_impl(PyObject *module, int fd) |
| 9577 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9578 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9579 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9580 | _Py_BEGIN_SUPPRESS_IPH |
| 9581 | return_value = isatty(fd); |
| 9582 | _Py_END_SUPPRESS_IPH |
| 9583 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9584 | } |
| 9585 | |
| 9586 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9587 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9588 | /*[clinic input] |
| 9589 | os.pipe |
| 9590 | |
| 9591 | Create a pipe. |
| 9592 | |
| 9593 | Returns a tuple of two file descriptors: |
| 9594 | (read_fd, write_fd) |
| 9595 | [clinic start generated code]*/ |
| 9596 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9597 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9598 | os_pipe_impl(PyObject *module) |
| 9599 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9600 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9601 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9602 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9603 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9604 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9605 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9606 | #else |
| 9607 | int res; |
| 9608 | #endif |
| 9609 | |
| 9610 | #ifdef MS_WINDOWS |
| 9611 | attr.nLength = sizeof(attr); |
| 9612 | attr.lpSecurityDescriptor = NULL; |
| 9613 | attr.bInheritHandle = FALSE; |
| 9614 | |
| 9615 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9616 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9617 | ok = CreatePipe(&read, &write, &attr, 0); |
| 9618 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9619 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 9620 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9621 | if (fds[0] == -1 || fds[1] == -1) { |
| 9622 | CloseHandle(read); |
| 9623 | CloseHandle(write); |
| 9624 | ok = 0; |
| 9625 | } |
| 9626 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9627 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9628 | Py_END_ALLOW_THREADS |
| 9629 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9630 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9631 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9632 | #else |
| 9633 | |
| 9634 | #ifdef HAVE_PIPE2 |
| 9635 | Py_BEGIN_ALLOW_THREADS |
| 9636 | res = pipe2(fds, O_CLOEXEC); |
| 9637 | Py_END_ALLOW_THREADS |
| 9638 | |
| 9639 | if (res != 0 && errno == ENOSYS) |
| 9640 | { |
| 9641 | #endif |
| 9642 | Py_BEGIN_ALLOW_THREADS |
| 9643 | res = pipe(fds); |
| 9644 | Py_END_ALLOW_THREADS |
| 9645 | |
| 9646 | if (res == 0) { |
| 9647 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9648 | close(fds[0]); |
| 9649 | close(fds[1]); |
| 9650 | return NULL; |
| 9651 | } |
| 9652 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9653 | close(fds[0]); |
| 9654 | close(fds[1]); |
| 9655 | return NULL; |
| 9656 | } |
| 9657 | } |
| 9658 | #ifdef HAVE_PIPE2 |
| 9659 | } |
| 9660 | #endif |
| 9661 | |
| 9662 | if (res != 0) |
| 9663 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9664 | #endif /* !MS_WINDOWS */ |
| 9665 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9666 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9667 | #endif /* HAVE_PIPE */ |
| 9668 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9669 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9670 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9671 | /*[clinic input] |
| 9672 | os.pipe2 |
| 9673 | |
| 9674 | flags: int |
| 9675 | / |
| 9676 | |
| 9677 | Create a pipe with flags set atomically. |
| 9678 | |
| 9679 | Returns a tuple of two file descriptors: |
| 9680 | (read_fd, write_fd) |
| 9681 | |
| 9682 | flags can be constructed by ORing together one or more of these values: |
| 9683 | O_NONBLOCK, O_CLOEXEC. |
| 9684 | [clinic start generated code]*/ |
| 9685 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9686 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9687 | os_pipe2_impl(PyObject *module, int flags) |
| 9688 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9689 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9690 | int fds[2]; |
| 9691 | int res; |
| 9692 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9693 | res = pipe2(fds, flags); |
| 9694 | if (res != 0) |
| 9695 | return posix_error(); |
| 9696 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9697 | } |
| 9698 | #endif /* HAVE_PIPE2 */ |
| 9699 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9700 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9701 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9702 | /*[clinic input] |
| 9703 | os.writev -> Py_ssize_t |
| 9704 | fd: int |
| 9705 | buffers: object |
| 9706 | / |
| 9707 | |
| 9708 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9709 | |
| 9710 | Returns the total number of bytes written. |
| 9711 | buffers must be a sequence of bytes-like objects. |
| 9712 | [clinic start generated code]*/ |
| 9713 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9714 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9715 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9716 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9717 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9718 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9719 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9720 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9721 | struct iovec *iov; |
| 9722 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9723 | |
| 9724 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9725 | PyErr_SetString(PyExc_TypeError, |
| 9726 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9727 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9728 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9729 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9730 | if (cnt < 0) |
| 9731 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9732 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9733 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9734 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9735 | } |
| 9736 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9737 | do { |
| 9738 | Py_BEGIN_ALLOW_THREADS |
| 9739 | result = writev(fd, iov, cnt); |
| 9740 | Py_END_ALLOW_THREADS |
| 9741 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9742 | |
| 9743 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9744 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9745 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9746 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9747 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9748 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9749 | #endif /* HAVE_WRITEV */ |
| 9750 | |
| 9751 | |
| 9752 | #ifdef HAVE_PWRITE |
| 9753 | /*[clinic input] |
| 9754 | os.pwrite -> Py_ssize_t |
| 9755 | |
| 9756 | fd: int |
| 9757 | buffer: Py_buffer |
| 9758 | offset: Py_off_t |
| 9759 | / |
| 9760 | |
| 9761 | Write bytes to a file descriptor starting at a particular offset. |
| 9762 | |
| 9763 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9764 | the file. Returns the number of bytes writte. Does not change the |
| 9765 | current file offset. |
| 9766 | [clinic start generated code]*/ |
| 9767 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9768 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9769 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9770 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9771 | { |
| 9772 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9773 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9774 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9775 | do { |
| 9776 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9777 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9778 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9779 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9780 | Py_END_ALLOW_THREADS |
| 9781 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9782 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9783 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9784 | posix_error(); |
| 9785 | return size; |
| 9786 | } |
| 9787 | #endif /* HAVE_PWRITE */ |
| 9788 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9789 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9790 | /*[clinic input] |
| 9791 | os.pwritev -> Py_ssize_t |
| 9792 | |
| 9793 | fd: int |
| 9794 | buffers: object |
| 9795 | offset: Py_off_t |
| 9796 | flags: int = 0 |
| 9797 | / |
| 9798 | |
| 9799 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9800 | |
| 9801 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9802 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9803 | buffer is written before proceeding to second, and so on. The operating system may |
| 9804 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9805 | This function writes the contents of each object to the file descriptor and returns |
| 9806 | the total number of bytes written. |
| 9807 | |
| 9808 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9809 | |
| 9810 | - RWF_DSYNC |
| 9811 | - RWF_SYNC |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 9812 | - RWF_APPEND |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9813 | |
| 9814 | Using non-zero flags requires Linux 4.7 or newer. |
| 9815 | [clinic start generated code]*/ |
| 9816 | |
| 9817 | static Py_ssize_t |
| 9818 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9819 | int flags) |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 9820 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=35358c327e1a2a8e]*/ |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9821 | { |
| 9822 | Py_ssize_t cnt; |
| 9823 | Py_ssize_t result; |
| 9824 | int async_err = 0; |
| 9825 | struct iovec *iov; |
| 9826 | Py_buffer *buf; |
| 9827 | |
| 9828 | if (!PySequence_Check(buffers)) { |
| 9829 | PyErr_SetString(PyExc_TypeError, |
| 9830 | "pwritev() arg 2 must be a sequence"); |
| 9831 | return -1; |
| 9832 | } |
| 9833 | |
| 9834 | cnt = PySequence_Size(buffers); |
| 9835 | if (cnt < 0) { |
| 9836 | return -1; |
| 9837 | } |
| 9838 | |
| 9839 | #ifndef HAVE_PWRITEV2 |
| 9840 | if(flags != 0) { |
| 9841 | argument_unavailable_error("pwritev2", "flags"); |
| 9842 | return -1; |
| 9843 | } |
| 9844 | #endif |
| 9845 | |
| 9846 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9847 | return -1; |
| 9848 | } |
| 9849 | #ifdef HAVE_PWRITEV2 |
| 9850 | do { |
| 9851 | Py_BEGIN_ALLOW_THREADS |
| 9852 | _Py_BEGIN_SUPPRESS_IPH |
| 9853 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9854 | _Py_END_SUPPRESS_IPH |
| 9855 | Py_END_ALLOW_THREADS |
| 9856 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9857 | #else |
| 9858 | do { |
| 9859 | Py_BEGIN_ALLOW_THREADS |
| 9860 | _Py_BEGIN_SUPPRESS_IPH |
| 9861 | result = pwritev(fd, iov, cnt, offset); |
| 9862 | _Py_END_SUPPRESS_IPH |
| 9863 | Py_END_ALLOW_THREADS |
| 9864 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9865 | #endif |
| 9866 | |
| 9867 | iov_cleanup(iov, buf, cnt); |
| 9868 | if (result < 0) { |
| 9869 | if (!async_err) { |
| 9870 | posix_error(); |
| 9871 | } |
| 9872 | return -1; |
| 9873 | } |
| 9874 | |
| 9875 | return result; |
| 9876 | } |
| 9877 | #endif /* HAVE_PWRITEV */ |
| 9878 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9879 | #ifdef HAVE_COPY_FILE_RANGE |
| 9880 | /*[clinic input] |
| 9881 | |
| 9882 | os.copy_file_range |
| 9883 | src: int |
| 9884 | Source file descriptor. |
| 9885 | dst: int |
| 9886 | Destination file descriptor. |
| 9887 | count: Py_ssize_t |
| 9888 | Number of bytes to copy. |
| 9889 | offset_src: object = None |
| 9890 | Starting offset in src. |
| 9891 | offset_dst: object = None |
| 9892 | Starting offset in dst. |
| 9893 | |
| 9894 | Copy count bytes from one file descriptor to another. |
| 9895 | |
| 9896 | If offset_src is None, then src is read from the current position; |
| 9897 | respectively for offset_dst. |
| 9898 | [clinic start generated code]*/ |
| 9899 | |
| 9900 | static PyObject * |
| 9901 | os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, |
| 9902 | PyObject *offset_src, PyObject *offset_dst) |
| 9903 | /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ |
| 9904 | { |
| 9905 | off_t offset_src_val, offset_dst_val; |
| 9906 | off_t *p_offset_src = NULL; |
| 9907 | off_t *p_offset_dst = NULL; |
| 9908 | Py_ssize_t ret; |
| 9909 | int async_err = 0; |
| 9910 | /* The flags argument is provided to allow |
| 9911 | * for future extensions and currently must be to 0. */ |
| 9912 | int flags = 0; |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9913 | |
| 9914 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9915 | if (count < 0) { |
| 9916 | PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); |
| 9917 | return NULL; |
| 9918 | } |
| 9919 | |
| 9920 | if (offset_src != Py_None) { |
| 9921 | if (!Py_off_t_converter(offset_src, &offset_src_val)) { |
| 9922 | return NULL; |
| 9923 | } |
| 9924 | p_offset_src = &offset_src_val; |
| 9925 | } |
| 9926 | |
| 9927 | if (offset_dst != Py_None) { |
| 9928 | if (!Py_off_t_converter(offset_dst, &offset_dst_val)) { |
| 9929 | return NULL; |
| 9930 | } |
| 9931 | p_offset_dst = &offset_dst_val; |
| 9932 | } |
| 9933 | |
| 9934 | do { |
| 9935 | Py_BEGIN_ALLOW_THREADS |
| 9936 | ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags); |
| 9937 | Py_END_ALLOW_THREADS |
| 9938 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9939 | |
| 9940 | if (ret < 0) { |
| 9941 | return (!async_err) ? posix_error() : NULL; |
| 9942 | } |
| 9943 | |
| 9944 | return PyLong_FromSsize_t(ret); |
| 9945 | } |
| 9946 | #endif /* HAVE_COPY_FILE_RANGE*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9947 | |
| 9948 | #ifdef HAVE_MKFIFO |
| 9949 | /*[clinic input] |
| 9950 | os.mkfifo |
| 9951 | |
| 9952 | path: path_t |
| 9953 | mode: int=0o666 |
| 9954 | * |
| 9955 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9956 | |
| 9957 | Create a "fifo" (a POSIX named pipe). |
| 9958 | |
| 9959 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9960 | and path should be relative; path will then be relative to that directory. |
| 9961 | dir_fd may not be implemented on your platform. |
| 9962 | If it is unavailable, using it will raise a NotImplementedError. |
| 9963 | [clinic start generated code]*/ |
| 9964 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9965 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9966 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9967 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9968 | { |
| 9969 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9970 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9971 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9972 | do { |
| 9973 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9974 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9975 | if (dir_fd != DEFAULT_DIR_FD) |
| 9976 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9977 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9978 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9979 | result = mkfifo(path->narrow, mode); |
| 9980 | Py_END_ALLOW_THREADS |
| 9981 | } while (result != 0 && errno == EINTR && |
| 9982 | !(async_err = PyErr_CheckSignals())); |
| 9983 | if (result != 0) |
| 9984 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9985 | |
| 9986 | Py_RETURN_NONE; |
| 9987 | } |
| 9988 | #endif /* HAVE_MKFIFO */ |
| 9989 | |
| 9990 | |
| 9991 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9992 | /*[clinic input] |
| 9993 | os.mknod |
| 9994 | |
| 9995 | path: path_t |
| 9996 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9997 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9998 | * |
| 9999 | dir_fd: dir_fd(requires='mknodat')=None |
| 10000 | |
| 10001 | Create a node in the file system. |
| 10002 | |
| 10003 | Create a node in the file system (file, device special file or named pipe) |
| 10004 | at path. mode specifies both the permissions to use and the |
| 10005 | type of node to be created, being combined (bitwise OR) with one of |
| 10006 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 10007 | device defines the newly created device special file (probably using |
| 10008 | os.makedev()). Otherwise device is ignored. |
| 10009 | |
| 10010 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 10011 | and path should be relative; path will then be relative to that directory. |
| 10012 | dir_fd may not be implemented on your platform. |
| 10013 | If it is unavailable, using it will raise a NotImplementedError. |
| 10014 | [clinic start generated code]*/ |
| 10015 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10016 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10017 | 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] | 10018 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10019 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10020 | { |
| 10021 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10022 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10023 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10024 | do { |
| 10025 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10026 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10027 | if (dir_fd != DEFAULT_DIR_FD) |
| 10028 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 10029 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10030 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10031 | result = mknod(path->narrow, mode, device); |
| 10032 | Py_END_ALLOW_THREADS |
| 10033 | } while (result != 0 && errno == EINTR && |
| 10034 | !(async_err = PyErr_CheckSignals())); |
| 10035 | if (result != 0) |
| 10036 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10037 | |
| 10038 | Py_RETURN_NONE; |
| 10039 | } |
| 10040 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 10041 | |
| 10042 | |
| 10043 | #ifdef HAVE_DEVICE_MACROS |
| 10044 | /*[clinic input] |
| 10045 | os.major -> unsigned_int |
| 10046 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10047 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10048 | / |
| 10049 | |
| 10050 | Extracts a device major number from a raw device number. |
| 10051 | [clinic start generated code]*/ |
| 10052 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10053 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10054 | os_major_impl(PyObject *module, dev_t device) |
| 10055 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10056 | { |
| 10057 | return major(device); |
| 10058 | } |
| 10059 | |
| 10060 | |
| 10061 | /*[clinic input] |
| 10062 | os.minor -> unsigned_int |
| 10063 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10064 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10065 | / |
| 10066 | |
| 10067 | Extracts a device minor number from a raw device number. |
| 10068 | [clinic start generated code]*/ |
| 10069 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10070 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10071 | os_minor_impl(PyObject *module, dev_t device) |
| 10072 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10073 | { |
| 10074 | return minor(device); |
| 10075 | } |
| 10076 | |
| 10077 | |
| 10078 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10079 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10080 | |
| 10081 | major: int |
| 10082 | minor: int |
| 10083 | / |
| 10084 | |
| 10085 | Composes a raw device number from the major and minor device numbers. |
| 10086 | [clinic start generated code]*/ |
| 10087 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10088 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10089 | os_makedev_impl(PyObject *module, int major, int minor) |
| 10090 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10091 | { |
| 10092 | return makedev(major, minor); |
| 10093 | } |
| 10094 | #endif /* HAVE_DEVICE_MACROS */ |
| 10095 | |
| 10096 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10097 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10098 | /*[clinic input] |
| 10099 | os.ftruncate |
| 10100 | |
| 10101 | fd: int |
| 10102 | length: Py_off_t |
| 10103 | / |
| 10104 | |
| 10105 | Truncate a file, specified by file descriptor, to a specific length. |
| 10106 | [clinic start generated code]*/ |
| 10107 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10108 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10109 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 10110 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10111 | { |
| 10112 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10113 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10114 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10115 | if (PySys_Audit("os.truncate", "in", fd, length) < 0) { |
| 10116 | return NULL; |
| 10117 | } |
| 10118 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10119 | do { |
| 10120 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10121 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10122 | #ifdef MS_WINDOWS |
| 10123 | result = _chsize_s(fd, length); |
| 10124 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10125 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10126 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10127 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10128 | Py_END_ALLOW_THREADS |
| 10129 | } while (result != 0 && errno == EINTR && |
| 10130 | !(async_err = PyErr_CheckSignals())); |
| 10131 | if (result != 0) |
| 10132 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10133 | Py_RETURN_NONE; |
| 10134 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10135 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10136 | |
| 10137 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10138 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10139 | /*[clinic input] |
| 10140 | os.truncate |
| 10141 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 10142 | length: Py_off_t |
| 10143 | |
| 10144 | Truncate a file, specified by path, to a specific length. |
| 10145 | |
| 10146 | On some platforms, path may also be specified as an open file descriptor. |
| 10147 | If this functionality is unavailable, using it raises an exception. |
| 10148 | [clinic start generated code]*/ |
| 10149 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10150 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10151 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 10152 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10153 | { |
| 10154 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10155 | #ifdef MS_WINDOWS |
| 10156 | int fd; |
| 10157 | #endif |
| 10158 | |
| 10159 | if (path->fd != -1) |
| 10160 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10161 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10162 | if (PySys_Audit("os.truncate", "On", path->object, length) < 0) { |
| 10163 | return NULL; |
| 10164 | } |
| 10165 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10166 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10167 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10168 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10169 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 10170 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10171 | result = -1; |
| 10172 | else { |
| 10173 | result = _chsize_s(fd, length); |
| 10174 | close(fd); |
| 10175 | if (result < 0) |
| 10176 | errno = result; |
| 10177 | } |
| 10178 | #else |
| 10179 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10180 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10181 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10182 | Py_END_ALLOW_THREADS |
| 10183 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 10184 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10185 | |
| 10186 | Py_RETURN_NONE; |
| 10187 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10188 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10189 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10190 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10191 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 10192 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 10193 | defined, which is the case in Python on AIX. AIX bug report: |
| 10194 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 10195 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 10196 | # define POSIX_FADVISE_AIX_BUG |
| 10197 | #endif |
| 10198 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10199 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10200 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10201 | /*[clinic input] |
| 10202 | os.posix_fallocate |
| 10203 | |
| 10204 | fd: int |
| 10205 | offset: Py_off_t |
| 10206 | length: Py_off_t |
| 10207 | / |
| 10208 | |
| 10209 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 10210 | |
| 10211 | Ensure that the file specified by fd encompasses a range of bytes |
| 10212 | starting at offset bytes from the beginning and continuing for length bytes. |
| 10213 | [clinic start generated code]*/ |
| 10214 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10215 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10216 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10217 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10218 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10219 | { |
| 10220 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10221 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10222 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10223 | do { |
| 10224 | Py_BEGIN_ALLOW_THREADS |
| 10225 | result = posix_fallocate(fd, offset, length); |
| 10226 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10227 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10228 | |
| 10229 | if (result == 0) |
| 10230 | Py_RETURN_NONE; |
| 10231 | |
| 10232 | if (async_err) |
| 10233 | return NULL; |
| 10234 | |
| 10235 | errno = result; |
| 10236 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10237 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10238 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10239 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10240 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10241 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10242 | /*[clinic input] |
| 10243 | os.posix_fadvise |
| 10244 | |
| 10245 | fd: int |
| 10246 | offset: Py_off_t |
| 10247 | length: Py_off_t |
| 10248 | advice: int |
| 10249 | / |
| 10250 | |
| 10251 | Announce an intention to access data in a specific pattern. |
| 10252 | |
| 10253 | Announce an intention to access data in a specific pattern, thus allowing |
| 10254 | the kernel to make optimizations. |
| 10255 | The advice applies to the region of the file specified by fd starting at |
| 10256 | offset and continuing for length bytes. |
| 10257 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 10258 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 10259 | POSIX_FADV_DONTNEED. |
| 10260 | [clinic start generated code]*/ |
| 10261 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10262 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10263 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10264 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10265 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10266 | { |
| 10267 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10268 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10269 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10270 | do { |
| 10271 | Py_BEGIN_ALLOW_THREADS |
| 10272 | result = posix_fadvise(fd, offset, length, advice); |
| 10273 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10274 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10275 | |
| 10276 | if (result == 0) |
| 10277 | Py_RETURN_NONE; |
| 10278 | |
| 10279 | if (async_err) |
| 10280 | return NULL; |
| 10281 | |
| 10282 | errno = result; |
| 10283 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10284 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10285 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10286 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10287 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 10288 | #ifdef MS_WINDOWS |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10289 | static PyObject* |
| 10290 | win32_putenv(PyObject *name, PyObject *value) |
| 10291 | { |
| 10292 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 10293 | defining hidden environment variables. */ |
| 10294 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 10295 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 10296 | { |
| 10297 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10298 | return NULL; |
| 10299 | } |
| 10300 | PyObject *unicode; |
| 10301 | if (value != NULL) { |
| 10302 | unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 10303 | } |
| 10304 | else { |
| 10305 | unicode = PyUnicode_FromFormat("%U=", name); |
| 10306 | } |
| 10307 | if (unicode == NULL) { |
| 10308 | return NULL; |
| 10309 | } |
| 10310 | |
| 10311 | Py_ssize_t size; |
| 10312 | /* PyUnicode_AsWideCharString() rejects embedded null characters */ |
| 10313 | wchar_t *env = PyUnicode_AsWideCharString(unicode, &size); |
| 10314 | Py_DECREF(unicode); |
| 10315 | |
| 10316 | if (env == NULL) { |
| 10317 | return NULL; |
| 10318 | } |
| 10319 | if (size > _MAX_ENV) { |
| 10320 | PyErr_Format(PyExc_ValueError, |
| 10321 | "the environment variable is longer than %u characters", |
| 10322 | _MAX_ENV); |
| 10323 | PyMem_Free(env); |
| 10324 | return NULL; |
| 10325 | } |
| 10326 | |
| 10327 | /* _wputenv() and SetEnvironmentVariableW() update the environment in the |
| 10328 | Process Environment Block (PEB). _wputenv() also updates CRT 'environ' |
| 10329 | and '_wenviron' variables, whereas SetEnvironmentVariableW() does not. |
| 10330 | |
| 10331 | Prefer _wputenv() to be compatible with C libraries using CRT |
| 10332 | variables and CRT functions using these variables (ex: getenv()). */ |
| 10333 | int err = _wputenv(env); |
| 10334 | PyMem_Free(env); |
| 10335 | |
| 10336 | if (err) { |
| 10337 | posix_error(); |
| 10338 | return NULL; |
| 10339 | } |
| 10340 | |
| 10341 | Py_RETURN_NONE; |
| 10342 | } |
| 10343 | #endif |
| 10344 | |
| 10345 | |
| 10346 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10347 | /*[clinic input] |
| 10348 | os.putenv |
| 10349 | |
| 10350 | name: unicode |
| 10351 | value: unicode |
| 10352 | / |
| 10353 | |
| 10354 | Change or add an environment variable. |
| 10355 | [clinic start generated code]*/ |
| 10356 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10357 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10358 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10359 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10360 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10361 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10362 | return NULL; |
| 10363 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10364 | return win32_putenv(name, value); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10365 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10366 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10367 | /*[clinic input] |
| 10368 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10370 | name: FSConverter |
| 10371 | value: FSConverter |
| 10372 | / |
| 10373 | |
| 10374 | Change or add an environment variable. |
| 10375 | [clinic start generated code]*/ |
| 10376 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10377 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10378 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10379 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10380 | { |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10381 | const char *name_string = PyBytes_AS_STRING(name); |
| 10382 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10383 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10384 | if (strchr(name_string, '=') != NULL) { |
| 10385 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10386 | return NULL; |
| 10387 | } |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10388 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10389 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10390 | return NULL; |
| 10391 | } |
| 10392 | |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10393 | if (setenv(name_string, value_string, 1)) { |
| 10394 | return posix_error(); |
| 10395 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10396 | Py_RETURN_NONE; |
| 10397 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10398 | #endif /* !defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10399 | |
| 10400 | |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10401 | #ifdef MS_WINDOWS |
| 10402 | /*[clinic input] |
| 10403 | os.unsetenv |
| 10404 | name: unicode |
| 10405 | / |
| 10406 | |
| 10407 | Delete an environment variable. |
| 10408 | [clinic start generated code]*/ |
| 10409 | |
| 10410 | static PyObject * |
| 10411 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10412 | /*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/ |
| 10413 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10414 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10415 | return NULL; |
| 10416 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10417 | return win32_putenv(name, NULL); |
| 10418 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10419 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10420 | /*[clinic input] |
| 10421 | os.unsetenv |
| 10422 | name: FSConverter |
| 10423 | / |
| 10424 | |
| 10425 | Delete an environment variable. |
| 10426 | [clinic start generated code]*/ |
| 10427 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10428 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10429 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10430 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10431 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10432 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10433 | return NULL; |
| 10434 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10435 | #ifdef HAVE_BROKEN_UNSETENV |
| 10436 | unsetenv(PyBytes_AS_STRING(name)); |
| 10437 | #else |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10438 | int err = unsetenv(PyBytes_AS_STRING(name)); |
| 10439 | if (err) { |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10440 | return posix_error(); |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10441 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10442 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10443 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10444 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10445 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10446 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10447 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10448 | |
| 10449 | /*[clinic input] |
| 10450 | os.strerror |
| 10451 | |
| 10452 | code: int |
| 10453 | / |
| 10454 | |
| 10455 | Translate an error code to a message string. |
| 10456 | [clinic start generated code]*/ |
| 10457 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10458 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10459 | os_strerror_impl(PyObject *module, int code) |
| 10460 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10461 | { |
| 10462 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10463 | if (message == NULL) { |
| 10464 | PyErr_SetString(PyExc_ValueError, |
| 10465 | "strerror() argument out of range"); |
| 10466 | return NULL; |
| 10467 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 10468 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10469 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10470 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10471 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10472 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10473 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10474 | /*[clinic input] |
| 10475 | os.WCOREDUMP -> bool |
| 10476 | |
| 10477 | status: int |
| 10478 | / |
| 10479 | |
| 10480 | Return True if the process returning status was dumped to a core file. |
| 10481 | [clinic start generated code]*/ |
| 10482 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10483 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10484 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 10485 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10486 | { |
| 10487 | WAIT_TYPE wait_status; |
| 10488 | WAIT_STATUS_INT(wait_status) = status; |
| 10489 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10490 | } |
| 10491 | #endif /* WCOREDUMP */ |
| 10492 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10493 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10494 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10495 | /*[clinic input] |
| 10496 | os.WIFCONTINUED -> bool |
| 10497 | |
| 10498 | status: int |
| 10499 | |
| 10500 | Return True if a particular process was continued from a job control stop. |
| 10501 | |
| 10502 | Return True if the process returning status was continued from a |
| 10503 | job control stop. |
| 10504 | [clinic start generated code]*/ |
| 10505 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10506 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10507 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 10508 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10509 | { |
| 10510 | WAIT_TYPE wait_status; |
| 10511 | WAIT_STATUS_INT(wait_status) = status; |
| 10512 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10513 | } |
| 10514 | #endif /* WIFCONTINUED */ |
| 10515 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10516 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10517 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10518 | /*[clinic input] |
| 10519 | os.WIFSTOPPED -> bool |
| 10520 | |
| 10521 | status: int |
| 10522 | |
| 10523 | Return True if the process returning status was stopped. |
| 10524 | [clinic start generated code]*/ |
| 10525 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10526 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10527 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 10528 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10529 | { |
| 10530 | WAIT_TYPE wait_status; |
| 10531 | WAIT_STATUS_INT(wait_status) = status; |
| 10532 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10533 | } |
| 10534 | #endif /* WIFSTOPPED */ |
| 10535 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10536 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10537 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10538 | /*[clinic input] |
| 10539 | os.WIFSIGNALED -> bool |
| 10540 | |
| 10541 | status: int |
| 10542 | |
| 10543 | Return True if the process returning status was terminated by a signal. |
| 10544 | [clinic start generated code]*/ |
| 10545 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10546 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10547 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 10548 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10549 | { |
| 10550 | WAIT_TYPE wait_status; |
| 10551 | WAIT_STATUS_INT(wait_status) = status; |
| 10552 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10553 | } |
| 10554 | #endif /* WIFSIGNALED */ |
| 10555 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10556 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10557 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10558 | /*[clinic input] |
| 10559 | os.WIFEXITED -> bool |
| 10560 | |
| 10561 | status: int |
| 10562 | |
| 10563 | Return True if the process returning status exited via the exit() system call. |
| 10564 | [clinic start generated code]*/ |
| 10565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10566 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10567 | os_WIFEXITED_impl(PyObject *module, int status) |
| 10568 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10569 | { |
| 10570 | WAIT_TYPE wait_status; |
| 10571 | WAIT_STATUS_INT(wait_status) = status; |
| 10572 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10573 | } |
| 10574 | #endif /* WIFEXITED */ |
| 10575 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10576 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10577 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10578 | /*[clinic input] |
| 10579 | os.WEXITSTATUS -> int |
| 10580 | |
| 10581 | status: int |
| 10582 | |
| 10583 | Return the process return code from status. |
| 10584 | [clinic start generated code]*/ |
| 10585 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10586 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10587 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 10588 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10589 | { |
| 10590 | WAIT_TYPE wait_status; |
| 10591 | WAIT_STATUS_INT(wait_status) = status; |
| 10592 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10593 | } |
| 10594 | #endif /* WEXITSTATUS */ |
| 10595 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10596 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10597 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10598 | /*[clinic input] |
| 10599 | os.WTERMSIG -> int |
| 10600 | |
| 10601 | status: int |
| 10602 | |
| 10603 | Return the signal that terminated the process that provided the status value. |
| 10604 | [clinic start generated code]*/ |
| 10605 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10606 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10607 | os_WTERMSIG_impl(PyObject *module, int status) |
| 10608 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10609 | { |
| 10610 | WAIT_TYPE wait_status; |
| 10611 | WAIT_STATUS_INT(wait_status) = status; |
| 10612 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10613 | } |
| 10614 | #endif /* WTERMSIG */ |
| 10615 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10616 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10617 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10618 | /*[clinic input] |
| 10619 | os.WSTOPSIG -> int |
| 10620 | |
| 10621 | status: int |
| 10622 | |
| 10623 | Return the signal that stopped the process that provided the status value. |
| 10624 | [clinic start generated code]*/ |
| 10625 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10626 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10627 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 10628 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10629 | { |
| 10630 | WAIT_TYPE wait_status; |
| 10631 | WAIT_STATUS_INT(wait_status) = status; |
| 10632 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10633 | } |
| 10634 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10635 | #endif /* HAVE_SYS_WAIT_H */ |
| 10636 | |
| 10637 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10638 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10639 | #ifdef _SCO_DS |
| 10640 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 10641 | needed definitions in sys/statvfs.h */ |
| 10642 | #define _SVID3 |
| 10643 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10644 | #include <sys/statvfs.h> |
| 10645 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10646 | static PyObject* |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10647 | _pystatvfs_fromstructstatvfs(PyObject *module, struct statvfs st) { |
| 10648 | PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 10649 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10650 | if (v == NULL) |
| 10651 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10652 | |
| 10653 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10654 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10655 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10656 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 10657 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 10658 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 10659 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 10660 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 10661 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 10662 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10663 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10664 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10665 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10666 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10667 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10668 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10669 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10670 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10671 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10672 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10673 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10674 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10675 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10676 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10677 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10678 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10679 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10680 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10681 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10682 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 10683 | * (issue #32390). */ |
| 10684 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 10685 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 10686 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10687 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10688 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10689 | if (PyErr_Occurred()) { |
| 10690 | Py_DECREF(v); |
| 10691 | return NULL; |
| 10692 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10693 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10694 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10695 | } |
| 10696 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10697 | |
| 10698 | /*[clinic input] |
| 10699 | os.fstatvfs |
| 10700 | fd: int |
| 10701 | / |
| 10702 | |
| 10703 | Perform an fstatvfs system call on the given fd. |
| 10704 | |
| 10705 | Equivalent to statvfs(fd). |
| 10706 | [clinic start generated code]*/ |
| 10707 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10708 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10709 | os_fstatvfs_impl(PyObject *module, int fd) |
| 10710 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10711 | { |
| 10712 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10713 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10714 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10715 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10716 | do { |
| 10717 | Py_BEGIN_ALLOW_THREADS |
| 10718 | result = fstatvfs(fd, &st); |
| 10719 | Py_END_ALLOW_THREADS |
| 10720 | } while (result != 0 && errno == EINTR && |
| 10721 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10722 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10723 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10724 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10725 | return _pystatvfs_fromstructstatvfs(module, st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10726 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10727 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10728 | |
| 10729 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10730 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10731 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10732 | /*[clinic input] |
| 10733 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10734 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10735 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10736 | |
| 10737 | Perform a statvfs system call on the given path. |
| 10738 | |
| 10739 | path may always be specified as a string. |
| 10740 | On some platforms, path may also be specified as an open file descriptor. |
| 10741 | If this functionality is unavailable, using it raises an exception. |
| 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_statvfs_impl(PyObject *module, path_t *path) |
| 10746 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10747 | { |
| 10748 | int result; |
| 10749 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10750 | |
| 10751 | Py_BEGIN_ALLOW_THREADS |
| 10752 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10753 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10754 | #ifdef __APPLE__ |
| 10755 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10756 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10757 | fd_specified("statvfs", path->fd); |
| 10758 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10759 | } |
| 10760 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10761 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10762 | } |
| 10763 | else |
| 10764 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10765 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10766 | Py_END_ALLOW_THREADS |
| 10767 | |
| 10768 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10769 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10770 | } |
| 10771 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10772 | return _pystatvfs_fromstructstatvfs(module, st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10773 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10774 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10775 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10776 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10777 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10778 | /*[clinic input] |
| 10779 | os._getdiskusage |
| 10780 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10781 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10782 | |
| 10783 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10784 | [clinic start generated code]*/ |
| 10785 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10786 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10787 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10788 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10789 | { |
| 10790 | BOOL retval; |
| 10791 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10792 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10793 | |
| 10794 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10795 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10796 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10797 | if (retval == 0) { |
| 10798 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10799 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10800 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10801 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10802 | if (dir_path == NULL) { |
| 10803 | return PyErr_NoMemory(); |
| 10804 | } |
| 10805 | |
| 10806 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10807 | |
| 10808 | if (_dirnameW(dir_path) != -1) { |
| 10809 | Py_BEGIN_ALLOW_THREADS |
| 10810 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10811 | Py_END_ALLOW_THREADS |
| 10812 | } |
| 10813 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10814 | err = GetLastError(); |
| 10815 | PyMem_Free(dir_path); |
| 10816 | if (retval) { |
| 10817 | goto success; |
| 10818 | } |
| 10819 | } |
| 10820 | return PyErr_SetFromWindowsErr(err); |
| 10821 | } |
| 10822 | |
| 10823 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10824 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10825 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10826 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10827 | |
| 10828 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10829 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10830 | * It maps strings representing configuration variable names to |
| 10831 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10832 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10833 | * rarely-used constants. There are three separate tables that use |
| 10834 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10835 | * |
| 10836 | * This code is always included, even if none of the interfaces that |
| 10837 | * need it are included. The #if hackery needed to avoid it would be |
| 10838 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10839 | */ |
| 10840 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10841 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10842 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10843 | }; |
| 10844 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10845 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10846 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10847 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10848 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10849 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10850 | int value = _PyLong_AsInt(arg); |
| 10851 | if (value == -1 && PyErr_Occurred()) |
| 10852 | return 0; |
| 10853 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10854 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10855 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10856 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10857 | /* look up the value in the table using a binary search */ |
| 10858 | size_t lo = 0; |
| 10859 | size_t mid; |
| 10860 | size_t hi = tablesize; |
| 10861 | int cmp; |
| 10862 | const char *confname; |
| 10863 | if (!PyUnicode_Check(arg)) { |
| 10864 | PyErr_SetString(PyExc_TypeError, |
| 10865 | "configuration names must be strings or integers"); |
| 10866 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10867 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10868 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10869 | if (confname == NULL) |
| 10870 | return 0; |
| 10871 | while (lo < hi) { |
| 10872 | mid = (lo + hi) / 2; |
| 10873 | cmp = strcmp(confname, table[mid].name); |
| 10874 | if (cmp < 0) |
| 10875 | hi = mid; |
| 10876 | else if (cmp > 0) |
| 10877 | lo = mid + 1; |
| 10878 | else { |
| 10879 | *valuep = table[mid].value; |
| 10880 | return 1; |
| 10881 | } |
| 10882 | } |
| 10883 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10884 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10885 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10886 | } |
| 10887 | |
| 10888 | |
| 10889 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10890 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10891 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10892 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10893 | #endif |
| 10894 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10895 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10896 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10897 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10898 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10899 | #endif |
| 10900 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10901 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10902 | #endif |
| 10903 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10904 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10905 | #endif |
| 10906 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10907 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10908 | #endif |
| 10909 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10910 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10911 | #endif |
| 10912 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10913 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10914 | #endif |
| 10915 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10916 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10917 | #endif |
| 10918 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10919 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10920 | #endif |
| 10921 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10922 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10923 | #endif |
| 10924 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10925 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10926 | #endif |
| 10927 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10928 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10929 | #endif |
| 10930 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10931 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10932 | #endif |
| 10933 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10934 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10935 | #endif |
| 10936 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10937 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10938 | #endif |
| 10939 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10940 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10941 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10942 | #ifdef _PC_ACL_ENABLED |
| 10943 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10944 | #endif |
| 10945 | #ifdef _PC_MIN_HOLE_SIZE |
| 10946 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10947 | #endif |
| 10948 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10949 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10950 | #endif |
| 10951 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10952 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10953 | #endif |
| 10954 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10955 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10956 | #endif |
| 10957 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10958 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10959 | #endif |
| 10960 | #ifdef _PC_REC_XFER_ALIGN |
| 10961 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10962 | #endif |
| 10963 | #ifdef _PC_SYMLINK_MAX |
| 10964 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10965 | #endif |
| 10966 | #ifdef _PC_XATTR_ENABLED |
| 10967 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10968 | #endif |
| 10969 | #ifdef _PC_XATTR_EXISTS |
| 10970 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10971 | #endif |
| 10972 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10973 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10974 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10975 | }; |
| 10976 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10977 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10978 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10979 | { |
| 10980 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10981 | sizeof(posix_constants_pathconf) |
| 10982 | / sizeof(struct constdef)); |
| 10983 | } |
| 10984 | #endif |
| 10985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10986 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10987 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10988 | /*[clinic input] |
| 10989 | os.fpathconf -> long |
| 10990 | |
| 10991 | fd: int |
| 10992 | name: path_confname |
| 10993 | / |
| 10994 | |
| 10995 | Return the configuration limit name for the file descriptor fd. |
| 10996 | |
| 10997 | If there is no limit, return -1. |
| 10998 | [clinic start generated code]*/ |
| 10999 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11000 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11001 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 11002 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11003 | { |
| 11004 | long limit; |
| 11005 | |
| 11006 | errno = 0; |
| 11007 | limit = fpathconf(fd, name); |
| 11008 | if (limit == -1 && errno != 0) |
| 11009 | posix_error(); |
| 11010 | |
| 11011 | return limit; |
| 11012 | } |
| 11013 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11014 | |
| 11015 | |
| 11016 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11017 | /*[clinic input] |
| 11018 | os.pathconf -> long |
| 11019 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 11020 | name: path_confname |
| 11021 | |
| 11022 | Return the configuration limit name for the file or directory path. |
| 11023 | |
| 11024 | If there is no limit, return -1. |
| 11025 | On some platforms, path may also be specified as an open file descriptor. |
| 11026 | If this functionality is unavailable, using it raises an exception. |
| 11027 | [clinic start generated code]*/ |
| 11028 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11029 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11030 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 11031 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11032 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11033 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11034 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11035 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11036 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11037 | if (path->fd != -1) |
| 11038 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11039 | else |
| 11040 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11041 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11042 | if (limit == -1 && errno != 0) { |
| 11043 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 11044 | /* could be a path or name problem */ |
| 11045 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11046 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11047 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11048 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11049 | |
| 11050 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11051 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11052 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11053 | |
| 11054 | #ifdef HAVE_CONFSTR |
| 11055 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11056 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11057 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11058 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11059 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11060 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11061 | #endif |
| 11062 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11063 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11064 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11065 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11066 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11067 | #endif |
| 11068 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11069 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11070 | #endif |
| 11071 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11072 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11073 | #endif |
| 11074 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11075 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11076 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11077 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11078 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11079 | #endif |
| 11080 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11081 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11082 | #endif |
| 11083 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11084 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11085 | #endif |
| 11086 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11087 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11088 | #endif |
| 11089 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11090 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11091 | #endif |
| 11092 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11093 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11094 | #endif |
| 11095 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11096 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11097 | #endif |
| 11098 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11099 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11100 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11101 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11102 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11103 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11104 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11105 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11106 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11107 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11108 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11109 | #endif |
| 11110 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11111 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11112 | #endif |
| 11113 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11114 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11115 | #endif |
| 11116 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11117 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11118 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11119 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11120 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11121 | #endif |
| 11122 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11123 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11124 | #endif |
| 11125 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11126 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11127 | #endif |
| 11128 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11129 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11130 | #endif |
| 11131 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11132 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11133 | #endif |
| 11134 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11135 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11136 | #endif |
| 11137 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11138 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11139 | #endif |
| 11140 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11141 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11142 | #endif |
| 11143 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11144 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11145 | #endif |
| 11146 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11147 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11148 | #endif |
| 11149 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11150 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11151 | #endif |
| 11152 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11153 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11154 | #endif |
| 11155 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11156 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11157 | #endif |
| 11158 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11159 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11160 | #endif |
| 11161 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11162 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11163 | #endif |
| 11164 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11165 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11166 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11167 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11168 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11169 | #endif |
| 11170 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11171 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11172 | #endif |
| 11173 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11174 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11175 | #endif |
| 11176 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11177 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11178 | #endif |
| 11179 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11180 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11181 | #endif |
| 11182 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11183 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11184 | #endif |
| 11185 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11186 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11187 | #endif |
| 11188 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11189 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11190 | #endif |
| 11191 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11192 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11193 | #endif |
| 11194 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11195 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11196 | #endif |
| 11197 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11198 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11199 | #endif |
| 11200 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11201 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11202 | #endif |
| 11203 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11204 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11205 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11206 | }; |
| 11207 | |
| 11208 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11209 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11210 | { |
| 11211 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 11212 | sizeof(posix_constants_confstr) |
| 11213 | / sizeof(struct constdef)); |
| 11214 | } |
| 11215 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11216 | |
| 11217 | /*[clinic input] |
| 11218 | os.confstr |
| 11219 | |
| 11220 | name: confstr_confname |
| 11221 | / |
| 11222 | |
| 11223 | Return a string-valued system configuration variable. |
| 11224 | [clinic start generated code]*/ |
| 11225 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11226 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11227 | os_confstr_impl(PyObject *module, int name) |
| 11228 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11229 | { |
| 11230 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11231 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11232 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11233 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11234 | errno = 0; |
| 11235 | len = confstr(name, buffer, sizeof(buffer)); |
| 11236 | if (len == 0) { |
| 11237 | if (errno) { |
| 11238 | posix_error(); |
| 11239 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11240 | } |
| 11241 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11242 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11243 | } |
| 11244 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11245 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11246 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11247 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11248 | char *buf = PyMem_Malloc(len); |
| 11249 | if (buf == NULL) |
| 11250 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11251 | len2 = confstr(name, buf, len); |
| 11252 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 11253 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11254 | PyMem_Free(buf); |
| 11255 | } |
| 11256 | else |
| 11257 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11258 | return result; |
| 11259 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11260 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11261 | |
| 11262 | |
| 11263 | #ifdef HAVE_SYSCONF |
| 11264 | static struct constdef posix_constants_sysconf[] = { |
| 11265 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11266 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11267 | #endif |
| 11268 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11269 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11270 | #endif |
| 11271 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11272 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11273 | #endif |
| 11274 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11275 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11276 | #endif |
| 11277 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11278 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11279 | #endif |
| 11280 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11281 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11282 | #endif |
| 11283 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11284 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11285 | #endif |
| 11286 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11287 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11288 | #endif |
| 11289 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11290 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11291 | #endif |
| 11292 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11293 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11294 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11295 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11296 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11297 | #endif |
| 11298 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11299 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11300 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11301 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11302 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11303 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11304 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11305 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11306 | #endif |
| 11307 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11308 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11309 | #endif |
| 11310 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11311 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11312 | #endif |
| 11313 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11314 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11315 | #endif |
| 11316 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11317 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11318 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11319 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11320 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11321 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11322 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11323 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11324 | #endif |
| 11325 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11326 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11327 | #endif |
| 11328 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11329 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11330 | #endif |
| 11331 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11332 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11333 | #endif |
| 11334 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11335 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11336 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11337 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11338 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11339 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11340 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11341 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11342 | #endif |
| 11343 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11344 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11345 | #endif |
| 11346 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11347 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11348 | #endif |
| 11349 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11350 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11351 | #endif |
| 11352 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11353 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11354 | #endif |
| 11355 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11356 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11357 | #endif |
| 11358 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11359 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11360 | #endif |
| 11361 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11362 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11363 | #endif |
| 11364 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11365 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11366 | #endif |
| 11367 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11368 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11369 | #endif |
| 11370 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11371 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11372 | #endif |
| 11373 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11374 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11375 | #endif |
| 11376 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11377 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11378 | #endif |
| 11379 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11380 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11381 | #endif |
| 11382 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11383 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11384 | #endif |
| 11385 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11386 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11387 | #endif |
| 11388 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11389 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11390 | #endif |
| 11391 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11392 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11393 | #endif |
| 11394 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11395 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11396 | #endif |
| 11397 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11398 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11399 | #endif |
| 11400 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11401 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11402 | #endif |
| 11403 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11404 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11405 | #endif |
| 11406 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11407 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11408 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11409 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11410 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11411 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11412 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11413 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11414 | #endif |
| 11415 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11416 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11417 | #endif |
| 11418 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11419 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11420 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11421 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11422 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11423 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11424 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11425 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11426 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11427 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11428 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11429 | #endif |
| 11430 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11431 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11432 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11433 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11434 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11435 | #endif |
| 11436 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11437 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11438 | #endif |
| 11439 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11440 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11441 | #endif |
| 11442 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11443 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
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_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11446 | {"SC_MAC", _SC_MAC}, |
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_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11449 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11450 | #endif |
| 11451 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11452 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11453 | #endif |
| 11454 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11455 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11456 | #endif |
| 11457 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11458 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11459 | #endif |
| 11460 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11461 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11462 | #endif |
| 11463 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11464 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11465 | #endif |
| 11466 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11467 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11468 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11469 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11470 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11471 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11472 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11473 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11474 | #endif |
| 11475 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11476 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11477 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11478 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11479 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11480 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11481 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11482 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11483 | #endif |
| 11484 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11485 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11486 | #endif |
| 11487 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11488 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11489 | #endif |
| 11490 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11491 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11492 | #endif |
| 11493 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11494 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11495 | #endif |
| 11496 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11497 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11498 | #endif |
| 11499 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11500 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11501 | #endif |
| 11502 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11503 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11504 | #endif |
| 11505 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11506 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11507 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11508 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11509 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11510 | #endif |
| 11511 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11512 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11513 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11514 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11515 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11516 | #endif |
| 11517 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11518 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11519 | #endif |
| 11520 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11521 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11522 | #endif |
| 11523 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11524 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11525 | #endif |
Batuhan Taşkaya | 909f4a3 | 2020-04-05 03:40:49 +0300 | [diff] [blame] | 11526 | #ifdef _SC_AIX_REALMEM |
| 11527 | {"SC_AIX_REALMEM", _SC_AIX_REALMEM}, |
| 11528 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11529 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11530 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11531 | #endif |
| 11532 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11533 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11534 | #endif |
| 11535 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11536 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11537 | #endif |
| 11538 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11539 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11540 | #endif |
| 11541 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11542 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11543 | #endif |
| 11544 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11545 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11546 | #endif |
| 11547 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11548 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11549 | #endif |
| 11550 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11551 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11552 | #endif |
| 11553 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11554 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11555 | #endif |
| 11556 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11557 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11558 | #endif |
| 11559 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11560 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11561 | #endif |
| 11562 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11563 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11564 | #endif |
| 11565 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11566 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11567 | #endif |
| 11568 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11569 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11570 | #endif |
| 11571 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11572 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11573 | #endif |
| 11574 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11575 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11576 | #endif |
| 11577 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11578 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11579 | #endif |
| 11580 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11581 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11582 | #endif |
| 11583 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11584 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11585 | #endif |
| 11586 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11587 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11588 | #endif |
| 11589 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11590 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11591 | #endif |
| 11592 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11593 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11594 | #endif |
| 11595 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11596 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11597 | #endif |
| 11598 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11599 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11600 | #endif |
| 11601 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11602 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11603 | #endif |
| 11604 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11605 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11606 | #endif |
| 11607 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11608 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11609 | #endif |
| 11610 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11611 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11612 | #endif |
| 11613 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11614 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11615 | #endif |
| 11616 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11617 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11618 | #endif |
| 11619 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11620 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11621 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11622 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11623 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11624 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11625 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11626 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11627 | #endif |
| 11628 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11629 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11630 | #endif |
| 11631 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11632 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11633 | #endif |
| 11634 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11635 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11636 | #endif |
| 11637 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11638 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11639 | #endif |
| 11640 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11641 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11642 | #endif |
| 11643 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11644 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11645 | #endif |
| 11646 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11647 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11648 | #endif |
| 11649 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11650 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11651 | #endif |
| 11652 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11653 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11654 | #endif |
| 11655 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11656 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11657 | #endif |
| 11658 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11659 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11660 | #endif |
| 11661 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11662 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11663 | #endif |
| 11664 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11665 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11666 | #endif |
| 11667 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11668 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11669 | #endif |
| 11670 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11671 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11672 | #endif |
| 11673 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11674 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11675 | #endif |
| 11676 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11677 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11678 | #endif |
| 11679 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11680 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11681 | #endif |
| 11682 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11683 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11684 | #endif |
| 11685 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11686 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11687 | #endif |
| 11688 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11689 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11690 | #endif |
| 11691 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11692 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11693 | #endif |
| 11694 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11695 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11696 | #endif |
| 11697 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11698 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11699 | #endif |
| 11700 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11701 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11702 | #endif |
| 11703 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11704 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11705 | #endif |
| 11706 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11707 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11708 | #endif |
| 11709 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11710 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11711 | #endif |
| 11712 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11713 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11714 | #endif |
| 11715 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11716 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11717 | #endif |
| 11718 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11719 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11720 | #endif |
| 11721 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11722 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11723 | #endif |
| 11724 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11725 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11726 | #endif |
| 11727 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11728 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11729 | #endif |
| 11730 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11731 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11732 | #endif |
| 11733 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11734 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11735 | #endif |
| 11736 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11737 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11738 | #endif |
| 11739 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11740 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11741 | #endif |
| 11742 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11743 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11744 | #endif |
| 11745 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11746 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11747 | #endif |
| 11748 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11749 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11750 | #endif |
| 11751 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11752 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11753 | #endif |
| 11754 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11755 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11756 | #endif |
| 11757 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11758 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11759 | #endif |
| 11760 | }; |
| 11761 | |
| 11762 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11763 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11764 | { |
| 11765 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11766 | sizeof(posix_constants_sysconf) |
| 11767 | / sizeof(struct constdef)); |
| 11768 | } |
| 11769 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11770 | |
| 11771 | /*[clinic input] |
| 11772 | os.sysconf -> long |
| 11773 | name: sysconf_confname |
| 11774 | / |
| 11775 | |
| 11776 | Return an integer-valued system configuration variable. |
| 11777 | [clinic start generated code]*/ |
| 11778 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11779 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11780 | os_sysconf_impl(PyObject *module, int name) |
| 11781 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11782 | { |
| 11783 | long value; |
| 11784 | |
| 11785 | errno = 0; |
| 11786 | value = sysconf(name); |
| 11787 | if (value == -1 && errno != 0) |
| 11788 | posix_error(); |
| 11789 | return value; |
| 11790 | } |
| 11791 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11792 | |
| 11793 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11794 | /* 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] | 11795 | * 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] | 11796 | * the exported dictionaries that are used to publish information about the |
| 11797 | * names available on the host platform. |
| 11798 | * |
| 11799 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11800 | * when used, even for platforms we're not able to test on. It also makes |
| 11801 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11802 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11803 | |
| 11804 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11805 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11806 | { |
| 11807 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11808 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11809 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11810 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11811 | |
| 11812 | return strcmp(c1->name, c2->name); |
| 11813 | } |
| 11814 | |
| 11815 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11816 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11817 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11818 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11819 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11820 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11821 | |
| 11822 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11823 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11824 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11825 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11826 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11827 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11828 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11829 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11830 | Py_XDECREF(o); |
| 11831 | Py_DECREF(d); |
| 11832 | return -1; |
| 11833 | } |
| 11834 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11835 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11836 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11837 | } |
| 11838 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11839 | /* Return -1 on failure, 0 on success. */ |
| 11840 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11841 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11842 | { |
| 11843 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11844 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11845 | sizeof(posix_constants_pathconf) |
| 11846 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11847 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11848 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11849 | #endif |
| 11850 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11851 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11852 | sizeof(posix_constants_confstr) |
| 11853 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11854 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11855 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11856 | #endif |
| 11857 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11858 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11859 | sizeof(posix_constants_sysconf) |
| 11860 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11861 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11862 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11863 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11864 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11865 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11866 | |
| 11867 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11868 | /*[clinic input] |
| 11869 | os.abort |
| 11870 | |
| 11871 | Abort the interpreter immediately. |
| 11872 | |
| 11873 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11874 | on the hosting operating system. This function never returns. |
| 11875 | [clinic start generated code]*/ |
| 11876 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11877 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11878 | os_abort_impl(PyObject *module) |
| 11879 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11880 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11881 | abort(); |
| 11882 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11883 | #ifndef __clang__ |
| 11884 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11885 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11886 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11887 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11888 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11889 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11890 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11891 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11892 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11893 | /* Grab ShellExecute dynamically from shell32 */ |
| 11894 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11895 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11896 | LPCWSTR, INT); |
| 11897 | static int |
| 11898 | check_ShellExecute() |
| 11899 | { |
| 11900 | HINSTANCE hShell32; |
| 11901 | |
| 11902 | /* only recheck */ |
| 11903 | if (-1 == has_ShellExecute) { |
| 11904 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11905 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11906 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11907 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11908 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11909 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11910 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11911 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11912 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11913 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11914 | } else { |
| 11915 | has_ShellExecute = 0; |
| 11916 | } |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 11917 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11918 | } |
| 11919 | return has_ShellExecute; |
| 11920 | } |
| 11921 | |
| 11922 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11923 | /*[clinic input] |
| 11924 | os.startfile |
| 11925 | filepath: path_t |
| 11926 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11927 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11928 | Start a file with its associated application. |
| 11929 | |
| 11930 | When "operation" is not specified or "open", this acts like |
| 11931 | double-clicking the file in Explorer, or giving the file name as an |
| 11932 | argument to the DOS "start" command: the file is opened with whatever |
| 11933 | application (if any) its extension is associated. |
| 11934 | When another "operation" is given, it specifies what should be done with |
| 11935 | the file. A typical operation is "print". |
| 11936 | |
| 11937 | startfile returns as soon as the associated application is launched. |
| 11938 | There is no option to wait for the application to close, and no way |
| 11939 | to retrieve the application's exit status. |
| 11940 | |
| 11941 | The filepath is relative to the current directory. If you want to use |
| 11942 | an absolute path, make sure the first character is not a slash ("/"); |
| 11943 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11944 | [clinic start generated code]*/ |
| 11945 | |
| 11946 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11947 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11948 | const Py_UNICODE *operation) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 11949 | /*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11950 | { |
| 11951 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11952 | |
| 11953 | if(!check_ShellExecute()) { |
| 11954 | /* If the OS doesn't have ShellExecute, return a |
| 11955 | NotImplementedError. */ |
| 11956 | return PyErr_Format(PyExc_NotImplementedError, |
| 11957 | "startfile not available on this platform"); |
| 11958 | } |
| 11959 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 11960 | if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 11961 | return NULL; |
| 11962 | } |
| 11963 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11964 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11965 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11966 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11967 | Py_END_ALLOW_THREADS |
| 11968 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11969 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11970 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11971 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11972 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11973 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11974 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11975 | #endif /* MS_WINDOWS */ |
| 11976 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11977 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11978 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11979 | /*[clinic input] |
| 11980 | os.getloadavg |
| 11981 | |
| 11982 | Return average recent system load information. |
| 11983 | |
| 11984 | Return the number of processes in the system run queue averaged over |
| 11985 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11986 | Raises OSError if the load average was unobtainable. |
| 11987 | [clinic start generated code]*/ |
| 11988 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11989 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11990 | os_getloadavg_impl(PyObject *module) |
| 11991 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11992 | { |
| 11993 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11994 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11995 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11996 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11997 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11998 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11999 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12000 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12001 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12002 | |
| 12003 | /*[clinic input] |
| 12004 | os.device_encoding |
| 12005 | fd: int |
| 12006 | |
| 12007 | Return a string describing the encoding of a terminal's file descriptor. |
| 12008 | |
| 12009 | The file descriptor must be attached to a terminal. |
| 12010 | If the device is not a terminal, return None. |
| 12011 | [clinic start generated code]*/ |
| 12012 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12013 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12014 | os_device_encoding_impl(PyObject *module, int fd) |
| 12015 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12016 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 12017 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 12018 | } |
| 12019 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12020 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12021 | #ifdef HAVE_SETRESUID |
| 12022 | /*[clinic input] |
| 12023 | os.setresuid |
| 12024 | |
| 12025 | ruid: uid_t |
| 12026 | euid: uid_t |
| 12027 | suid: uid_t |
| 12028 | / |
| 12029 | |
| 12030 | Set the current process's real, effective, and saved user ids. |
| 12031 | [clinic start generated code]*/ |
| 12032 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12033 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12034 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 12035 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12036 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12037 | if (setresuid(ruid, euid, suid) < 0) |
| 12038 | return posix_error(); |
| 12039 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12040 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12041 | #endif /* HAVE_SETRESUID */ |
| 12042 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12043 | |
| 12044 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12045 | /*[clinic input] |
| 12046 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12047 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12048 | rgid: gid_t |
| 12049 | egid: gid_t |
| 12050 | sgid: gid_t |
| 12051 | / |
| 12052 | |
| 12053 | Set the current process's real, effective, and saved group ids. |
| 12054 | [clinic start generated code]*/ |
| 12055 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12056 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12057 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 12058 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12059 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12060 | if (setresgid(rgid, egid, sgid) < 0) |
| 12061 | return posix_error(); |
| 12062 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12063 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12064 | #endif /* HAVE_SETRESGID */ |
| 12065 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12066 | |
| 12067 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12068 | /*[clinic input] |
| 12069 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12070 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12071 | Return a tuple of the current process's real, effective, and saved user ids. |
| 12072 | [clinic start generated code]*/ |
| 12073 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12074 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12075 | os_getresuid_impl(PyObject *module) |
| 12076 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12077 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12078 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12079 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 12080 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 12081 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 12082 | _PyLong_FromUid(euid), |
| 12083 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12084 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12085 | #endif /* HAVE_GETRESUID */ |
| 12086 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12087 | |
| 12088 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12089 | /*[clinic input] |
| 12090 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12092 | Return a tuple of the current process's real, effective, and saved group ids. |
| 12093 | [clinic start generated code]*/ |
| 12094 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12095 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12096 | os_getresgid_impl(PyObject *module) |
| 12097 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12098 | { |
| 12099 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12100 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 12101 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 12102 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 12103 | _PyLong_FromGid(egid), |
| 12104 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12105 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12106 | #endif /* HAVE_GETRESGID */ |
| 12107 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12108 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12109 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12110 | /*[clinic input] |
| 12111 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12112 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12113 | path: path_t(allow_fd=True) |
| 12114 | attribute: path_t |
| 12115 | * |
| 12116 | follow_symlinks: bool = True |
| 12117 | |
| 12118 | Return the value of extended attribute attribute on path. |
| 12119 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12120 | 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] | 12121 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12122 | link, getxattr will examine the symbolic link itself instead of the file |
| 12123 | the link points to. |
| 12124 | |
| 12125 | [clinic start generated code]*/ |
| 12126 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12127 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12128 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12129 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12130 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12131 | { |
| 12132 | Py_ssize_t i; |
| 12133 | PyObject *buffer = NULL; |
| 12134 | |
| 12135 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 12136 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12137 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12138 | if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) { |
| 12139 | return NULL; |
| 12140 | } |
| 12141 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12142 | for (i = 0; ; i++) { |
| 12143 | void *ptr; |
| 12144 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12145 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12146 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12147 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12148 | path_error(path); |
| 12149 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12150 | } |
| 12151 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 12152 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12153 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12154 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12155 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12156 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12157 | if (path->fd >= 0) |
| 12158 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12159 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12160 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12161 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12162 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12163 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12164 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12165 | if (result < 0) { |
| 12166 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12167 | if (errno == ERANGE) |
| 12168 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12169 | path_error(path); |
| 12170 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12171 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12172 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12173 | if (result != buffer_size) { |
| 12174 | /* Can only shrink. */ |
| 12175 | _PyBytes_Resize(&buffer, result); |
| 12176 | } |
| 12177 | break; |
| 12178 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12179 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12180 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12181 | } |
| 12182 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12183 | |
| 12184 | /*[clinic input] |
| 12185 | os.setxattr |
| 12186 | |
| 12187 | path: path_t(allow_fd=True) |
| 12188 | attribute: path_t |
| 12189 | value: Py_buffer |
| 12190 | flags: int = 0 |
| 12191 | * |
| 12192 | follow_symlinks: bool = True |
| 12193 | |
| 12194 | Set extended attribute attribute on path to value. |
| 12195 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12196 | 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] | 12197 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12198 | link, setxattr will modify the symbolic link itself instead of the file |
| 12199 | the link points to. |
| 12200 | |
| 12201 | [clinic start generated code]*/ |
| 12202 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12203 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12204 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12205 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12206 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12207 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12208 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12209 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12210 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12211 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12212 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12213 | if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object, |
| 12214 | value->buf, value->len, flags) < 0) { |
| 12215 | return NULL; |
| 12216 | } |
| 12217 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12218 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12219 | if (path->fd > -1) |
| 12220 | result = fsetxattr(path->fd, attribute->narrow, |
| 12221 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12222 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12223 | result = setxattr(path->narrow, attribute->narrow, |
| 12224 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12225 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12226 | result = lsetxattr(path->narrow, attribute->narrow, |
| 12227 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12228 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12229 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12230 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12231 | path_error(path); |
| 12232 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12233 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12234 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12235 | Py_RETURN_NONE; |
| 12236 | } |
| 12237 | |
| 12238 | |
| 12239 | /*[clinic input] |
| 12240 | os.removexattr |
| 12241 | |
| 12242 | path: path_t(allow_fd=True) |
| 12243 | attribute: path_t |
| 12244 | * |
| 12245 | follow_symlinks: bool = True |
| 12246 | |
| 12247 | Remove extended attribute attribute on path. |
| 12248 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12249 | 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] | 12250 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12251 | link, removexattr will modify the symbolic link itself instead of the file |
| 12252 | the link points to. |
| 12253 | |
| 12254 | [clinic start generated code]*/ |
| 12255 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12256 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12257 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12258 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12259 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12260 | { |
| 12261 | ssize_t result; |
| 12262 | |
| 12263 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 12264 | return NULL; |
| 12265 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12266 | if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) { |
| 12267 | return NULL; |
| 12268 | } |
| 12269 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12270 | Py_BEGIN_ALLOW_THREADS; |
| 12271 | if (path->fd > -1) |
| 12272 | result = fremovexattr(path->fd, attribute->narrow); |
| 12273 | else if (follow_symlinks) |
| 12274 | result = removexattr(path->narrow, attribute->narrow); |
| 12275 | else |
| 12276 | result = lremovexattr(path->narrow, attribute->narrow); |
| 12277 | Py_END_ALLOW_THREADS; |
| 12278 | |
| 12279 | if (result) { |
| 12280 | return path_error(path); |
| 12281 | } |
| 12282 | |
| 12283 | Py_RETURN_NONE; |
| 12284 | } |
| 12285 | |
| 12286 | |
| 12287 | /*[clinic input] |
| 12288 | os.listxattr |
| 12289 | |
| 12290 | path: path_t(allow_fd=True, nullable=True) = None |
| 12291 | * |
| 12292 | follow_symlinks: bool = True |
| 12293 | |
| 12294 | Return a list of extended attributes on path. |
| 12295 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12296 | 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] | 12297 | if path is None, listxattr will examine the current directory. |
| 12298 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12299 | link, listxattr will examine the symbolic link itself instead of the file |
| 12300 | the link points to. |
| 12301 | [clinic start generated code]*/ |
| 12302 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12303 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12304 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12305 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12306 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12307 | Py_ssize_t i; |
| 12308 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12309 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12310 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12311 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12312 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12313 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12314 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12315 | if (PySys_Audit("os.listxattr", "(O)", |
| 12316 | path->object ? path->object : Py_None) < 0) { |
| 12317 | return NULL; |
| 12318 | } |
| 12319 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12320 | name = path->narrow ? path->narrow : "."; |
| 12321 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12322 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12323 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12324 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12325 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12326 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12327 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 12328 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12329 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12330 | break; |
| 12331 | } |
| 12332 | buffer = PyMem_MALLOC(buffer_size); |
| 12333 | if (!buffer) { |
| 12334 | PyErr_NoMemory(); |
| 12335 | break; |
| 12336 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12337 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12338 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12339 | if (path->fd > -1) |
| 12340 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12341 | else if (follow_symlinks) |
| 12342 | length = listxattr(name, buffer, buffer_size); |
| 12343 | else |
| 12344 | length = llistxattr(name, buffer, buffer_size); |
| 12345 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12346 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12347 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12348 | if (errno == ERANGE) { |
| 12349 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 12350 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12351 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12352 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12353 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12354 | break; |
| 12355 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12356 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12357 | result = PyList_New(0); |
| 12358 | if (!result) { |
| 12359 | goto exit; |
| 12360 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12361 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12362 | end = buffer + length; |
| 12363 | for (trace = start = buffer; trace != end; trace++) { |
| 12364 | if (!*trace) { |
| 12365 | int error; |
| 12366 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 12367 | trace - start); |
| 12368 | if (!attribute) { |
| 12369 | Py_DECREF(result); |
| 12370 | result = NULL; |
| 12371 | goto exit; |
| 12372 | } |
| 12373 | error = PyList_Append(result, attribute); |
| 12374 | Py_DECREF(attribute); |
| 12375 | if (error) { |
| 12376 | Py_DECREF(result); |
| 12377 | result = NULL; |
| 12378 | goto exit; |
| 12379 | } |
| 12380 | start = trace + 1; |
| 12381 | } |
| 12382 | } |
| 12383 | break; |
| 12384 | } |
| 12385 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12386 | if (buffer) |
| 12387 | PyMem_FREE(buffer); |
| 12388 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12389 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12390 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12391 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12392 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12393 | /*[clinic input] |
| 12394 | os.urandom |
| 12395 | |
| 12396 | size: Py_ssize_t |
| 12397 | / |
| 12398 | |
| 12399 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 12400 | [clinic start generated code]*/ |
| 12401 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12402 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12403 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 12404 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12405 | { |
| 12406 | PyObject *bytes; |
| 12407 | int result; |
| 12408 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12409 | if (size < 0) |
| 12410 | return PyErr_Format(PyExc_ValueError, |
| 12411 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12412 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 12413 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12414 | return NULL; |
| 12415 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 12416 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12417 | if (result == -1) { |
| 12418 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12419 | return NULL; |
| 12420 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12421 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12422 | } |
| 12423 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 12424 | #ifdef HAVE_MEMFD_CREATE |
| 12425 | /*[clinic input] |
| 12426 | os.memfd_create |
| 12427 | |
| 12428 | name: FSConverter |
| 12429 | flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC |
| 12430 | |
| 12431 | [clinic start generated code]*/ |
| 12432 | |
| 12433 | static PyObject * |
| 12434 | os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags) |
| 12435 | /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/ |
| 12436 | { |
| 12437 | int fd; |
| 12438 | const char *bytes = PyBytes_AS_STRING(name); |
| 12439 | Py_BEGIN_ALLOW_THREADS |
| 12440 | fd = memfd_create(bytes, flags); |
| 12441 | Py_END_ALLOW_THREADS |
| 12442 | if (fd == -1) { |
| 12443 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12444 | } |
| 12445 | return PyLong_FromLong(fd); |
| 12446 | } |
| 12447 | #endif |
| 12448 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12449 | /* Terminal size querying */ |
| 12450 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12451 | PyDoc_STRVAR(TerminalSize_docstring, |
| 12452 | "A tuple of (columns, lines) for holding terminal window size"); |
| 12453 | |
| 12454 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 12455 | {"columns", "width of the terminal window in characters"}, |
| 12456 | {"lines", "height of the terminal window in characters"}, |
| 12457 | {NULL, NULL} |
| 12458 | }; |
| 12459 | |
| 12460 | static PyStructSequence_Desc TerminalSize_desc = { |
| 12461 | "os.terminal_size", |
| 12462 | TerminalSize_docstring, |
| 12463 | TerminalSize_fields, |
| 12464 | 2, |
| 12465 | }; |
| 12466 | |
| 12467 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12468 | /*[clinic input] |
| 12469 | os.get_terminal_size |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12470 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12471 | fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1 |
| 12472 | / |
| 12473 | |
| 12474 | Return the size of the terminal window as (columns, lines). |
| 12475 | |
| 12476 | The optional argument fd (default standard output) specifies |
| 12477 | which file descriptor should be queried. |
| 12478 | |
| 12479 | If the file descriptor is not connected to a terminal, an OSError |
| 12480 | is thrown. |
| 12481 | |
| 12482 | This function will only be defined if an implementation is |
| 12483 | available for this system. |
| 12484 | |
| 12485 | shutil.get_terminal_size is the high-level function which should |
| 12486 | normally be used, os.get_terminal_size is the low-level implementation. |
| 12487 | [clinic start generated code]*/ |
| 12488 | |
| 12489 | static PyObject * |
| 12490 | os_get_terminal_size_impl(PyObject *module, int fd) |
| 12491 | /*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12492 | { |
| 12493 | int columns, lines; |
| 12494 | PyObject *termsize; |
| 12495 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12496 | /* Under some conditions stdout may not be connected and |
| 12497 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 12498 | * GUI apps don't have valid standard streams by default. |
| 12499 | * |
| 12500 | * If this happens, and the optional fd argument is not present, |
| 12501 | * the ioctl below will fail returning EBADF. This is what we want. |
| 12502 | */ |
| 12503 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12504 | #ifdef TERMSIZE_USE_IOCTL |
| 12505 | { |
| 12506 | struct winsize w; |
| 12507 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 12508 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12509 | columns = w.ws_col; |
| 12510 | lines = w.ws_row; |
| 12511 | } |
| 12512 | #endif /* TERMSIZE_USE_IOCTL */ |
| 12513 | |
| 12514 | #ifdef TERMSIZE_USE_CONIO |
| 12515 | { |
| 12516 | DWORD nhandle; |
| 12517 | HANDLE handle; |
| 12518 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 12519 | switch (fd) { |
| 12520 | case 0: nhandle = STD_INPUT_HANDLE; |
| 12521 | break; |
| 12522 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 12523 | break; |
| 12524 | case 2: nhandle = STD_ERROR_HANDLE; |
| 12525 | break; |
| 12526 | default: |
| 12527 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 12528 | } |
| 12529 | handle = GetStdHandle(nhandle); |
| 12530 | if (handle == NULL) |
| 12531 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 12532 | if (handle == INVALID_HANDLE_VALUE) |
| 12533 | return PyErr_SetFromWindowsErr(0); |
| 12534 | |
| 12535 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 12536 | return PyErr_SetFromWindowsErr(0); |
| 12537 | |
| 12538 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 12539 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 12540 | } |
| 12541 | #endif /* TERMSIZE_USE_CONIO */ |
| 12542 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12543 | PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12544 | termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12545 | if (termsize == NULL) |
| 12546 | return NULL; |
| 12547 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 12548 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 12549 | if (PyErr_Occurred()) { |
| 12550 | Py_DECREF(termsize); |
| 12551 | return NULL; |
| 12552 | } |
| 12553 | return termsize; |
| 12554 | } |
| 12555 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 12556 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12557 | |
| 12558 | /*[clinic input] |
| 12559 | os.cpu_count |
| 12560 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 12561 | Return the number of CPUs in the system; return None if indeterminable. |
| 12562 | |
| 12563 | This number is not equivalent to the number of CPUs the current process can |
| 12564 | use. The number of usable CPUs can be obtained with |
| 12565 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12566 | [clinic start generated code]*/ |
| 12567 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12568 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12569 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12570 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12571 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12572 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12573 | #ifdef MS_WINDOWS |
Steve Dower | aa92927 | 2019-09-11 16:15:39 +0100 | [diff] [blame] | 12574 | ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12575 | #elif defined(__hpux) |
| 12576 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 12577 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 12578 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12579 | #elif defined(__DragonFly__) || \ |
| 12580 | defined(__OpenBSD__) || \ |
| 12581 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12582 | defined(__NetBSD__) || \ |
| 12583 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12584 | int mib[2]; |
| 12585 | size_t len = sizeof(ncpu); |
| 12586 | mib[0] = CTL_HW; |
| 12587 | mib[1] = HW_NCPU; |
| 12588 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 12589 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12590 | #endif |
| 12591 | if (ncpu >= 1) |
| 12592 | return PyLong_FromLong(ncpu); |
| 12593 | else |
| 12594 | Py_RETURN_NONE; |
| 12595 | } |
| 12596 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12597 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12598 | /*[clinic input] |
| 12599 | os.get_inheritable -> bool |
| 12600 | |
| 12601 | fd: int |
| 12602 | / |
| 12603 | |
| 12604 | Get the close-on-exe flag of the specified file descriptor. |
| 12605 | [clinic start generated code]*/ |
| 12606 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12607 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12608 | os_get_inheritable_impl(PyObject *module, int fd) |
| 12609 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12610 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12611 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12612 | _Py_BEGIN_SUPPRESS_IPH |
| 12613 | return_value = _Py_get_inheritable(fd); |
| 12614 | _Py_END_SUPPRESS_IPH |
| 12615 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12616 | } |
| 12617 | |
| 12618 | |
| 12619 | /*[clinic input] |
| 12620 | os.set_inheritable |
| 12621 | fd: int |
| 12622 | inheritable: int |
| 12623 | / |
| 12624 | |
| 12625 | Set the inheritable flag of the specified file descriptor. |
| 12626 | [clinic start generated code]*/ |
| 12627 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12628 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12629 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 12630 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12631 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12632 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12633 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12634 | _Py_BEGIN_SUPPRESS_IPH |
| 12635 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 12636 | _Py_END_SUPPRESS_IPH |
| 12637 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12638 | return NULL; |
| 12639 | Py_RETURN_NONE; |
| 12640 | } |
| 12641 | |
| 12642 | |
| 12643 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12644 | /*[clinic input] |
| 12645 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12646 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12647 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12648 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12649 | Get the close-on-exe flag of the specified file descriptor. |
| 12650 | [clinic start generated code]*/ |
| 12651 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12652 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12653 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12654 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12655 | { |
| 12656 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12657 | |
| 12658 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 12659 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12660 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12661 | } |
| 12662 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12663 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12664 | } |
| 12665 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12666 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12667 | /*[clinic input] |
| 12668 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12669 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12670 | inheritable: bool |
| 12671 | / |
| 12672 | |
| 12673 | Set the inheritable flag of the specified handle. |
| 12674 | [clinic start generated code]*/ |
| 12675 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12676 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12677 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12678 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12679 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12680 | { |
| 12681 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12682 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 12683 | PyErr_SetFromWindowsErr(0); |
| 12684 | return NULL; |
| 12685 | } |
| 12686 | Py_RETURN_NONE; |
| 12687 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12688 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12689 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12690 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12691 | /*[clinic input] |
| 12692 | os.get_blocking -> bool |
| 12693 | fd: int |
| 12694 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12695 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12696 | Get the blocking mode of the file descriptor. |
| 12697 | |
| 12698 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 12699 | [clinic start generated code]*/ |
| 12700 | |
| 12701 | static int |
| 12702 | os_get_blocking_impl(PyObject *module, int fd) |
| 12703 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12704 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12705 | int blocking; |
| 12706 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12707 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12708 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12709 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12710 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12711 | } |
| 12712 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12713 | /*[clinic input] |
| 12714 | os.set_blocking |
| 12715 | fd: int |
| 12716 | blocking: bool(accept={int}) |
| 12717 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12718 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12719 | Set the blocking mode of the specified file descriptor. |
| 12720 | |
| 12721 | Set the O_NONBLOCK flag if blocking is False, |
| 12722 | clear the O_NONBLOCK flag otherwise. |
| 12723 | [clinic start generated code]*/ |
| 12724 | |
| 12725 | static PyObject * |
| 12726 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 12727 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12728 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12729 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12730 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12731 | _Py_BEGIN_SUPPRESS_IPH |
| 12732 | result = _Py_set_blocking(fd, blocking); |
| 12733 | _Py_END_SUPPRESS_IPH |
| 12734 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12735 | return NULL; |
| 12736 | Py_RETURN_NONE; |
| 12737 | } |
| 12738 | #endif /* !MS_WINDOWS */ |
| 12739 | |
| 12740 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12741 | /*[clinic input] |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12742 | class os.DirEntry "DirEntry *" "DirEntryType" |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12743 | [clinic start generated code]*/ |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12744 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12745 | |
| 12746 | typedef struct { |
| 12747 | PyObject_HEAD |
| 12748 | PyObject *name; |
| 12749 | PyObject *path; |
| 12750 | PyObject *stat; |
| 12751 | PyObject *lstat; |
| 12752 | #ifdef MS_WINDOWS |
| 12753 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12754 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12755 | int got_file_index; |
| 12756 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12757 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12758 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12759 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12760 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12761 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12762 | #endif |
| 12763 | } DirEntry; |
| 12764 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12765 | static PyObject * |
| 12766 | _disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 12767 | { |
| 12768 | PyErr_Format(PyExc_TypeError, |
| 12769 | "cannot create '%.100s' instances", _PyType_Name(type)); |
| 12770 | return NULL; |
| 12771 | } |
| 12772 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12773 | static void |
| 12774 | DirEntry_dealloc(DirEntry *entry) |
| 12775 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12776 | PyTypeObject *tp = Py_TYPE(entry); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12777 | Py_XDECREF(entry->name); |
| 12778 | Py_XDECREF(entry->path); |
| 12779 | Py_XDECREF(entry->stat); |
| 12780 | Py_XDECREF(entry->lstat); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12781 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 12782 | free_func(entry); |
| 12783 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12784 | } |
| 12785 | |
| 12786 | /* Forward reference */ |
| 12787 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12788 | DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self, |
| 12789 | int follow_symlinks, unsigned short mode_bits); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12790 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12791 | /*[clinic input] |
| 12792 | os.DirEntry.is_symlink -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12793 | defining_class: defining_class |
| 12794 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12795 | |
| 12796 | Return True if the entry is a symbolic link; cached per entry. |
| 12797 | [clinic start generated code]*/ |
| 12798 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12799 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12800 | os_DirEntry_is_symlink_impl(DirEntry *self, PyTypeObject *defining_class) |
| 12801 | /*[clinic end generated code: output=293096d589b6d47c input=e9acc5ee4d511113]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12802 | { |
| 12803 | #ifdef MS_WINDOWS |
| 12804 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12805 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12806 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12807 | if (self->d_type != DT_UNKNOWN) |
| 12808 | return self->d_type == DT_LNK; |
| 12809 | else |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12810 | return DirEntry_test_mode(defining_class, self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12811 | #else |
| 12812 | /* POSIX without d_type */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12813 | return DirEntry_test_mode(defining_class, self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12814 | #endif |
| 12815 | } |
| 12816 | |
| 12817 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12818 | DirEntry_fetch_stat(PyObject *module, DirEntry *self, int follow_symlinks) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12819 | { |
| 12820 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12821 | STRUCT_STAT st; |
| 12822 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12823 | |
| 12824 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12825 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12826 | return NULL; |
| 12827 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12828 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12829 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12830 | return NULL; |
| 12831 | const char *path = PyBytes_AS_STRING(ub); |
| 12832 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12833 | #ifdef HAVE_FSTATAT |
| 12834 | result = fstatat(self->dir_fd, path, &st, |
| 12835 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12836 | #else |
| 12837 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12838 | return NULL; |
| 12839 | #endif /* HAVE_FSTATAT */ |
| 12840 | } |
| 12841 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12842 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12843 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12844 | if (follow_symlinks) |
| 12845 | result = STAT(path, &st); |
| 12846 | else |
| 12847 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12848 | } |
| 12849 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12850 | |
| 12851 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12852 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12853 | |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12854 | return _pystat_fromstructstat(module, &st); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12855 | } |
| 12856 | |
| 12857 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12858 | DirEntry_get_lstat(PyTypeObject *defining_class, DirEntry *self) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12859 | { |
| 12860 | if (!self->lstat) { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12861 | PyObject *module = PyType_GetModule(defining_class); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12862 | #ifdef MS_WINDOWS |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12863 | self->lstat = _pystat_fromstructstat(module, &self->win32_lstat); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12864 | #else /* POSIX */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12865 | self->lstat = DirEntry_fetch_stat(module, self, 0); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12866 | #endif |
| 12867 | } |
| 12868 | Py_XINCREF(self->lstat); |
| 12869 | return self->lstat; |
| 12870 | } |
| 12871 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12872 | /*[clinic input] |
| 12873 | os.DirEntry.stat |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12874 | defining_class: defining_class |
| 12875 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12876 | * |
| 12877 | follow_symlinks: bool = True |
| 12878 | |
| 12879 | Return stat_result object for the entry; cached per entry. |
| 12880 | [clinic start generated code]*/ |
| 12881 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12882 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12883 | os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class, |
| 12884 | int follow_symlinks) |
| 12885 | /*[clinic end generated code: output=23f803e19c3e780e input=e816273c4e67ee98]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12886 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12887 | if (!follow_symlinks) { |
| 12888 | return DirEntry_get_lstat(defining_class, self); |
| 12889 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12890 | |
| 12891 | if (!self->stat) { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12892 | int result = os_DirEntry_is_symlink_impl(self, defining_class); |
| 12893 | if (result == -1) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12894 | return NULL; |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12895 | } |
| 12896 | if (result) { |
| 12897 | PyObject *module = PyType_GetModule(defining_class); |
| 12898 | self->stat = DirEntry_fetch_stat(module, self, 1); |
| 12899 | } |
| 12900 | else { |
| 12901 | self->stat = DirEntry_get_lstat(defining_class, self); |
| 12902 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12903 | } |
| 12904 | |
| 12905 | Py_XINCREF(self->stat); |
| 12906 | return self->stat; |
| 12907 | } |
| 12908 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12909 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12910 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12911 | DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self, |
| 12912 | int follow_symlinks, unsigned short mode_bits) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12913 | { |
| 12914 | PyObject *stat = NULL; |
| 12915 | PyObject *st_mode = NULL; |
| 12916 | long mode; |
| 12917 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12918 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12919 | int is_symlink; |
| 12920 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12921 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12922 | #ifdef MS_WINDOWS |
| 12923 | unsigned long dir_bits; |
| 12924 | #endif |
| 12925 | |
| 12926 | #ifdef MS_WINDOWS |
| 12927 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12928 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12929 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12930 | is_symlink = self->d_type == DT_LNK; |
| 12931 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12932 | #endif |
| 12933 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12934 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12935 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12936 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12937 | stat = os_DirEntry_stat_impl(self, defining_class, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12938 | if (!stat) { |
| 12939 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12940 | /* If file doesn't exist (anymore), then return False |
| 12941 | (i.e., say it's not a file/directory) */ |
| 12942 | PyErr_Clear(); |
| 12943 | return 0; |
| 12944 | } |
| 12945 | goto error; |
| 12946 | } |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12947 | _posixstate* state = get_posix_state(PyType_GetModule(defining_class)); |
| 12948 | st_mode = PyObject_GetAttr(stat, state->st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12949 | if (!st_mode) |
| 12950 | goto error; |
| 12951 | |
| 12952 | mode = PyLong_AsLong(st_mode); |
| 12953 | if (mode == -1 && PyErr_Occurred()) |
| 12954 | goto error; |
| 12955 | Py_CLEAR(st_mode); |
| 12956 | Py_CLEAR(stat); |
| 12957 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12958 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12959 | } |
| 12960 | else if (is_symlink) { |
| 12961 | assert(mode_bits != S_IFLNK); |
| 12962 | result = 0; |
| 12963 | } |
| 12964 | else { |
| 12965 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 12966 | #ifdef MS_WINDOWS |
| 12967 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 12968 | if (mode_bits == S_IFDIR) |
| 12969 | result = dir_bits != 0; |
| 12970 | else |
| 12971 | result = dir_bits == 0; |
| 12972 | #else /* POSIX */ |
| 12973 | if (mode_bits == S_IFDIR) |
| 12974 | result = self->d_type == DT_DIR; |
| 12975 | else |
| 12976 | result = self->d_type == DT_REG; |
| 12977 | #endif |
| 12978 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12979 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12980 | |
| 12981 | return result; |
| 12982 | |
| 12983 | error: |
| 12984 | Py_XDECREF(st_mode); |
| 12985 | Py_XDECREF(stat); |
| 12986 | return -1; |
| 12987 | } |
| 12988 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12989 | /*[clinic input] |
| 12990 | os.DirEntry.is_dir -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12991 | defining_class: defining_class |
| 12992 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12993 | * |
| 12994 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12995 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12996 | Return True if the entry is a directory; cached per entry. |
| 12997 | [clinic start generated code]*/ |
| 12998 | |
| 12999 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13000 | os_DirEntry_is_dir_impl(DirEntry *self, PyTypeObject *defining_class, |
| 13001 | int follow_symlinks) |
| 13002 | /*[clinic end generated code: output=0cd453b9c0987fdf input=1a4ffd6dec9920cb]*/ |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13003 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13004 | return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13005 | } |
| 13006 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13007 | /*[clinic input] |
| 13008 | os.DirEntry.is_file -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13009 | defining_class: defining_class |
| 13010 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13011 | * |
| 13012 | follow_symlinks: bool = True |
| 13013 | |
| 13014 | Return True if the entry is a file; cached per entry. |
| 13015 | [clinic start generated code]*/ |
| 13016 | |
| 13017 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13018 | os_DirEntry_is_file_impl(DirEntry *self, PyTypeObject *defining_class, |
| 13019 | int follow_symlinks) |
| 13020 | /*[clinic end generated code: output=f7c277ab5ba80908 input=0a64c5a12e802e3b]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13021 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13022 | return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13023 | } |
| 13024 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13025 | /*[clinic input] |
| 13026 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13027 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13028 | Return inode of the entry; cached per entry. |
| 13029 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13030 | |
| 13031 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13032 | os_DirEntry_inode_impl(DirEntry *self) |
| 13033 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13034 | { |
| 13035 | #ifdef MS_WINDOWS |
| 13036 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13037 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 13038 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13039 | STRUCT_STAT stat; |
| 13040 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13041 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13042 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13043 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13044 | path = PyUnicode_AsUnicode(unicode); |
| 13045 | result = LSTAT(path, &stat); |
| 13046 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13047 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13048 | if (result != 0) |
| 13049 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13050 | |
| 13051 | self->win32_file_index = stat.st_ino; |
| 13052 | self->got_file_index = 1; |
| 13053 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 13054 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 13055 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13056 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 13057 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 13058 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13059 | #endif |
| 13060 | } |
| 13061 | |
| 13062 | static PyObject * |
| 13063 | DirEntry_repr(DirEntry *self) |
| 13064 | { |
| 13065 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 13066 | } |
| 13067 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13068 | /*[clinic input] |
| 13069 | os.DirEntry.__fspath__ |
| 13070 | |
| 13071 | Returns the path for the entry. |
| 13072 | [clinic start generated code]*/ |
| 13073 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 13074 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13075 | os_DirEntry___fspath___impl(DirEntry *self) |
| 13076 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 13077 | { |
| 13078 | Py_INCREF(self->path); |
| 13079 | return self->path; |
| 13080 | } |
| 13081 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13082 | static PyMemberDef DirEntry_members[] = { |
| 13083 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 13084 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 13085 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 13086 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 13087 | {NULL} |
| 13088 | }; |
| 13089 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13090 | #include "clinic/posixmodule.c.h" |
| 13091 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13092 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13093 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 13094 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 13095 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 13096 | OS_DIRENTRY_STAT_METHODDEF |
| 13097 | OS_DIRENTRY_INODE_METHODDEF |
| 13098 | OS_DIRENTRY___FSPATH___METHODDEF |
Batuhan Taşkaya | f9dd51e | 2020-04-08 00:37:19 +0300 | [diff] [blame] | 13099 | {"__class_getitem__", (PyCFunction)Py_GenericAlias, |
| 13100 | METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13101 | {NULL} |
| 13102 | }; |
| 13103 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13104 | static PyType_Slot DirEntryType_slots[] = { |
| 13105 | {Py_tp_new, _disabled_new}, |
| 13106 | {Py_tp_dealloc, DirEntry_dealloc}, |
| 13107 | {Py_tp_repr, DirEntry_repr}, |
| 13108 | {Py_tp_methods, DirEntry_methods}, |
| 13109 | {Py_tp_members, DirEntry_members}, |
| 13110 | {0, 0}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13111 | }; |
| 13112 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13113 | static PyType_Spec DirEntryType_spec = { |
| 13114 | MODNAME ".DirEntry", |
| 13115 | sizeof(DirEntry), |
| 13116 | 0, |
| 13117 | Py_TPFLAGS_DEFAULT, |
| 13118 | DirEntryType_slots |
| 13119 | }; |
| 13120 | |
| 13121 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13122 | #ifdef MS_WINDOWS |
| 13123 | |
| 13124 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 13125 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13126 | { |
| 13127 | Py_ssize_t path_len; |
| 13128 | Py_ssize_t size; |
| 13129 | wchar_t *result; |
| 13130 | wchar_t ch; |
| 13131 | |
| 13132 | if (!path_wide) { /* Default arg: "." */ |
| 13133 | path_wide = L"."; |
| 13134 | path_len = 1; |
| 13135 | } |
| 13136 | else { |
| 13137 | path_len = wcslen(path_wide); |
| 13138 | } |
| 13139 | |
| 13140 | /* The +1's are for the path separator and the NUL */ |
| 13141 | size = path_len + 1 + wcslen(filename) + 1; |
| 13142 | result = PyMem_New(wchar_t, size); |
| 13143 | if (!result) { |
| 13144 | PyErr_NoMemory(); |
| 13145 | return NULL; |
| 13146 | } |
| 13147 | wcscpy(result, path_wide); |
| 13148 | if (path_len > 0) { |
| 13149 | ch = result[path_len - 1]; |
| 13150 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 13151 | result[path_len++] = SEP; |
| 13152 | wcscpy(result + path_len, filename); |
| 13153 | } |
| 13154 | return result; |
| 13155 | } |
| 13156 | |
| 13157 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13158 | 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] | 13159 | { |
| 13160 | DirEntry *entry; |
| 13161 | BY_HANDLE_FILE_INFORMATION file_info; |
| 13162 | ULONG reparse_tag; |
| 13163 | wchar_t *joined_path; |
| 13164 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13165 | PyObject *DirEntryType = get_posix_state(module)->DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13166 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13167 | if (!entry) |
| 13168 | return NULL; |
| 13169 | entry->name = NULL; |
| 13170 | entry->path = NULL; |
| 13171 | entry->stat = NULL; |
| 13172 | entry->lstat = NULL; |
| 13173 | entry->got_file_index = 0; |
| 13174 | |
| 13175 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 13176 | if (!entry->name) |
| 13177 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13178 | if (path->narrow) { |
| 13179 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 13180 | if (!entry->name) |
| 13181 | goto error; |
| 13182 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13183 | |
| 13184 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 13185 | if (!joined_path) |
| 13186 | goto error; |
| 13187 | |
| 13188 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 13189 | PyMem_Free(joined_path); |
| 13190 | if (!entry->path) |
| 13191 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13192 | if (path->narrow) { |
| 13193 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 13194 | if (!entry->path) |
| 13195 | goto error; |
| 13196 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13197 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13198 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13199 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 13200 | |
| 13201 | return (PyObject *)entry; |
| 13202 | |
| 13203 | error: |
| 13204 | Py_DECREF(entry); |
| 13205 | return NULL; |
| 13206 | } |
| 13207 | |
| 13208 | #else /* POSIX */ |
| 13209 | |
| 13210 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 13211 | 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] | 13212 | { |
| 13213 | Py_ssize_t path_len; |
| 13214 | Py_ssize_t size; |
| 13215 | char *result; |
| 13216 | |
| 13217 | if (!path_narrow) { /* Default arg: "." */ |
| 13218 | path_narrow = "."; |
| 13219 | path_len = 1; |
| 13220 | } |
| 13221 | else { |
| 13222 | path_len = strlen(path_narrow); |
| 13223 | } |
| 13224 | |
| 13225 | if (filename_len == -1) |
| 13226 | filename_len = strlen(filename); |
| 13227 | |
| 13228 | /* The +1's are for the path separator and the NUL */ |
| 13229 | size = path_len + 1 + filename_len + 1; |
| 13230 | result = PyMem_New(char, size); |
| 13231 | if (!result) { |
| 13232 | PyErr_NoMemory(); |
| 13233 | return NULL; |
| 13234 | } |
| 13235 | strcpy(result, path_narrow); |
| 13236 | if (path_len > 0 && result[path_len - 1] != '/') |
| 13237 | result[path_len++] = '/'; |
| 13238 | strcpy(result + path_len, filename); |
| 13239 | return result; |
| 13240 | } |
| 13241 | |
| 13242 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13243 | DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name, |
| 13244 | Py_ssize_t name_len, ino_t d_ino |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13245 | #ifdef HAVE_DIRENT_D_TYPE |
| 13246 | , unsigned char d_type |
| 13247 | #endif |
| 13248 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13249 | { |
| 13250 | DirEntry *entry; |
| 13251 | char *joined_path; |
| 13252 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13253 | PyObject *DirEntryType = get_posix_state(module)->DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13254 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13255 | if (!entry) |
| 13256 | return NULL; |
| 13257 | entry->name = NULL; |
| 13258 | entry->path = NULL; |
| 13259 | entry->stat = NULL; |
| 13260 | entry->lstat = NULL; |
| 13261 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13262 | if (path->fd != -1) { |
| 13263 | entry->dir_fd = path->fd; |
| 13264 | joined_path = NULL; |
| 13265 | } |
| 13266 | else { |
| 13267 | entry->dir_fd = DEFAULT_DIR_FD; |
| 13268 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 13269 | if (!joined_path) |
| 13270 | goto error; |
| 13271 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13272 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 13273 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13274 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13275 | if (joined_path) |
| 13276 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13277 | } |
| 13278 | else { |
| 13279 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13280 | if (joined_path) |
| 13281 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13282 | } |
| 13283 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13284 | if (!entry->name) |
| 13285 | goto error; |
| 13286 | |
| 13287 | if (path->fd != -1) { |
| 13288 | entry->path = entry->name; |
| 13289 | Py_INCREF(entry->path); |
| 13290 | } |
| 13291 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13292 | goto error; |
| 13293 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13294 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13295 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13296 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13297 | entry->d_ino = d_ino; |
| 13298 | |
| 13299 | return (PyObject *)entry; |
| 13300 | |
| 13301 | error: |
| 13302 | Py_XDECREF(entry); |
| 13303 | return NULL; |
| 13304 | } |
| 13305 | |
| 13306 | #endif |
| 13307 | |
| 13308 | |
| 13309 | typedef struct { |
| 13310 | PyObject_HEAD |
| 13311 | path_t path; |
| 13312 | #ifdef MS_WINDOWS |
| 13313 | HANDLE handle; |
| 13314 | WIN32_FIND_DATAW file_data; |
| 13315 | int first_time; |
| 13316 | #else /* POSIX */ |
| 13317 | DIR *dirp; |
| 13318 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13319 | #ifdef HAVE_FDOPENDIR |
| 13320 | int fd; |
| 13321 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13322 | } ScandirIterator; |
| 13323 | |
| 13324 | #ifdef MS_WINDOWS |
| 13325 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13326 | static int |
| 13327 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13328 | { |
| 13329 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 13330 | } |
| 13331 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13332 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13333 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13334 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13335 | HANDLE handle = iterator->handle; |
| 13336 | |
| 13337 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13338 | return; |
| 13339 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13340 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13341 | Py_BEGIN_ALLOW_THREADS |
| 13342 | FindClose(handle); |
| 13343 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13344 | } |
| 13345 | |
| 13346 | static PyObject * |
| 13347 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13348 | { |
| 13349 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 13350 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13351 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13352 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13353 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13354 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13355 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13356 | |
| 13357 | while (1) { |
| 13358 | if (!iterator->first_time) { |
| 13359 | Py_BEGIN_ALLOW_THREADS |
| 13360 | success = FindNextFileW(iterator->handle, file_data); |
| 13361 | Py_END_ALLOW_THREADS |
| 13362 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13363 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13364 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13365 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13366 | break; |
| 13367 | } |
| 13368 | } |
| 13369 | iterator->first_time = 0; |
| 13370 | |
| 13371 | /* Skip over . and .. */ |
| 13372 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13373 | wcscmp(file_data->cFileName, L"..") != 0) |
| 13374 | { |
| 13375 | PyObject *module = PyType_GetModule(Py_TYPE(iterator)); |
| 13376 | entry = DirEntry_from_find_data(module, &iterator->path, file_data); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13377 | if (!entry) |
| 13378 | break; |
| 13379 | return entry; |
| 13380 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13381 | |
| 13382 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13383 | } |
| 13384 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13385 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13386 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13387 | return NULL; |
| 13388 | } |
| 13389 | |
| 13390 | #else /* POSIX */ |
| 13391 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13392 | static int |
| 13393 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13394 | { |
| 13395 | return !iterator->dirp; |
| 13396 | } |
| 13397 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13398 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13399 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13400 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13401 | DIR *dirp = iterator->dirp; |
| 13402 | |
| 13403 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13404 | return; |
| 13405 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13406 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13407 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13408 | #ifdef HAVE_FDOPENDIR |
| 13409 | if (iterator->path.fd != -1) |
| 13410 | rewinddir(dirp); |
| 13411 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13412 | closedir(dirp); |
| 13413 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13414 | return; |
| 13415 | } |
| 13416 | |
| 13417 | static PyObject * |
| 13418 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13419 | { |
| 13420 | struct dirent *direntp; |
| 13421 | Py_ssize_t name_len; |
| 13422 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13423 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13424 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13425 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13426 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13427 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13428 | |
| 13429 | while (1) { |
| 13430 | errno = 0; |
| 13431 | Py_BEGIN_ALLOW_THREADS |
| 13432 | direntp = readdir(iterator->dirp); |
| 13433 | Py_END_ALLOW_THREADS |
| 13434 | |
| 13435 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13436 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13437 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13438 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13439 | break; |
| 13440 | } |
| 13441 | |
| 13442 | /* Skip over . and .. */ |
| 13443 | name_len = NAMLEN(direntp); |
| 13444 | is_dot = direntp->d_name[0] == '.' && |
| 13445 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 13446 | if (!is_dot) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13447 | PyObject *module = PyType_GetModule(Py_TYPE(iterator)); |
| 13448 | entry = DirEntry_from_posix_info(module, |
| 13449 | &iterator->path, direntp->d_name, |
| 13450 | name_len, direntp->d_ino |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13451 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13452 | , direntp->d_type |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13453 | #endif |
| 13454 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13455 | if (!entry) |
| 13456 | break; |
| 13457 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13458 | } |
| 13459 | |
| 13460 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13461 | } |
| 13462 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13463 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13464 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13465 | return NULL; |
| 13466 | } |
| 13467 | |
| 13468 | #endif |
| 13469 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13470 | static PyObject * |
| 13471 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 13472 | { |
| 13473 | ScandirIterator_closedir(self); |
| 13474 | Py_RETURN_NONE; |
| 13475 | } |
| 13476 | |
| 13477 | static PyObject * |
| 13478 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 13479 | { |
| 13480 | Py_INCREF(self); |
| 13481 | return self; |
| 13482 | } |
| 13483 | |
| 13484 | static PyObject * |
| 13485 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 13486 | { |
| 13487 | ScandirIterator_closedir(self); |
| 13488 | Py_RETURN_NONE; |
| 13489 | } |
| 13490 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13491 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13492 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 13493 | { |
| 13494 | PyObject *error_type, *error_value, *error_traceback; |
| 13495 | |
| 13496 | /* Save the current exception, if any. */ |
| 13497 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 13498 | |
| 13499 | if (!ScandirIterator_is_closed(iterator)) { |
| 13500 | ScandirIterator_closedir(iterator); |
| 13501 | |
| 13502 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 13503 | "unclosed scandir iterator %R", iterator)) { |
| 13504 | /* Spurious errors can appear at shutdown */ |
| 13505 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 13506 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 13507 | } |
| 13508 | } |
| 13509 | } |
| 13510 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13511 | path_cleanup(&iterator->path); |
| 13512 | |
| 13513 | /* Restore the saved exception. */ |
| 13514 | PyErr_Restore(error_type, error_value, error_traceback); |
| 13515 | } |
| 13516 | |
| 13517 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13518 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 13519 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13520 | PyTypeObject *tp = Py_TYPE(iterator); |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13521 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 13522 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13523 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13524 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 13525 | free_func(iterator); |
| 13526 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13527 | } |
| 13528 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13529 | static PyMethodDef ScandirIterator_methods[] = { |
| 13530 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 13531 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 13532 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 13533 | {NULL} |
| 13534 | }; |
| 13535 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13536 | static PyType_Slot ScandirIteratorType_slots[] = { |
| 13537 | {Py_tp_new, _disabled_new}, |
| 13538 | {Py_tp_dealloc, ScandirIterator_dealloc}, |
| 13539 | {Py_tp_finalize, ScandirIterator_finalize}, |
| 13540 | {Py_tp_iter, PyObject_SelfIter}, |
| 13541 | {Py_tp_iternext, ScandirIterator_iternext}, |
| 13542 | {Py_tp_methods, ScandirIterator_methods}, |
| 13543 | {0, 0}, |
| 13544 | }; |
| 13545 | |
| 13546 | static PyType_Spec ScandirIteratorType_spec = { |
| 13547 | MODNAME ".ScandirIterator", |
| 13548 | sizeof(ScandirIterator), |
| 13549 | 0, |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13550 | // bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since |
| 13551 | // PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance. |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13552 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 13553 | ScandirIteratorType_slots |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13554 | }; |
| 13555 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13556 | /*[clinic input] |
| 13557 | os.scandir |
| 13558 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13559 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13560 | |
| 13561 | Return an iterator of DirEntry objects for given path. |
| 13562 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13563 | 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] | 13564 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 13565 | all other circumstances they will be str. |
| 13566 | |
| 13567 | If path is None, uses the path='.'. |
| 13568 | [clinic start generated code]*/ |
| 13569 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13570 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13571 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13572 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13573 | { |
| 13574 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13575 | #ifdef MS_WINDOWS |
| 13576 | wchar_t *path_strW; |
| 13577 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13578 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13579 | #ifdef HAVE_FDOPENDIR |
| 13580 | int fd = -1; |
| 13581 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13582 | #endif |
| 13583 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 13584 | if (PySys_Audit("os.scandir", "O", |
| 13585 | path->object ? path->object : Py_None) < 0) { |
| 13586 | return NULL; |
| 13587 | } |
| 13588 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 13589 | PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13590 | iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13591 | if (!iterator) |
| 13592 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13593 | |
| 13594 | #ifdef MS_WINDOWS |
| 13595 | iterator->handle = INVALID_HANDLE_VALUE; |
| 13596 | #else |
| 13597 | iterator->dirp = NULL; |
| 13598 | #endif |
| 13599 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13600 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 13601 | /* Move the ownership to iterator->path */ |
| 13602 | path->object = NULL; |
| 13603 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13604 | |
| 13605 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13606 | iterator->first_time = 1; |
| 13607 | |
| 13608 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 13609 | if (!path_strW) |
| 13610 | goto error; |
| 13611 | |
| 13612 | Py_BEGIN_ALLOW_THREADS |
| 13613 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 13614 | Py_END_ALLOW_THREADS |
| 13615 | |
| 13616 | PyMem_Free(path_strW); |
| 13617 | |
| 13618 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 13619 | path_error(&iterator->path); |
| 13620 | goto error; |
| 13621 | } |
| 13622 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13623 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13624 | #ifdef HAVE_FDOPENDIR |
| 13625 | if (path->fd != -1) { |
| 13626 | /* closedir() closes the FD, so we duplicate it */ |
| 13627 | fd = _Py_dup(path->fd); |
| 13628 | if (fd == -1) |
| 13629 | goto error; |
| 13630 | |
| 13631 | Py_BEGIN_ALLOW_THREADS |
| 13632 | iterator->dirp = fdopendir(fd); |
| 13633 | Py_END_ALLOW_THREADS |
| 13634 | } |
| 13635 | else |
| 13636 | #endif |
| 13637 | { |
| 13638 | if (iterator->path.narrow) |
| 13639 | path_str = iterator->path.narrow; |
| 13640 | else |
| 13641 | path_str = "."; |
| 13642 | |
| 13643 | Py_BEGIN_ALLOW_THREADS |
| 13644 | iterator->dirp = opendir(path_str); |
| 13645 | Py_END_ALLOW_THREADS |
| 13646 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13647 | |
| 13648 | if (!iterator->dirp) { |
| 13649 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13650 | #ifdef HAVE_FDOPENDIR |
| 13651 | if (fd != -1) { |
| 13652 | Py_BEGIN_ALLOW_THREADS |
| 13653 | close(fd); |
| 13654 | Py_END_ALLOW_THREADS |
| 13655 | } |
| 13656 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13657 | goto error; |
| 13658 | } |
| 13659 | #endif |
| 13660 | |
| 13661 | return (PyObject *)iterator; |
| 13662 | |
| 13663 | error: |
| 13664 | Py_DECREF(iterator); |
| 13665 | return NULL; |
| 13666 | } |
| 13667 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13668 | /* |
| 13669 | Return the file system path representation of the object. |
| 13670 | |
| 13671 | If the object is str or bytes, then allow it to pass through with |
| 13672 | an incremented refcount. If the object defines __fspath__(), then |
| 13673 | return the result of that method. All other types raise a TypeError. |
| 13674 | */ |
| 13675 | PyObject * |
| 13676 | PyOS_FSPath(PyObject *path) |
| 13677 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13678 | /* For error message reasons, this function is manually inlined in |
| 13679 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13680 | PyObject *func = NULL; |
| 13681 | PyObject *path_repr = NULL; |
| 13682 | |
| 13683 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 13684 | Py_INCREF(path); |
| 13685 | return path; |
| 13686 | } |
| 13687 | |
| 13688 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 13689 | if (NULL == func) { |
| 13690 | return PyErr_Format(PyExc_TypeError, |
| 13691 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13692 | "not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13693 | _PyType_Name(Py_TYPE(path))); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13694 | } |
| 13695 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13696 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13697 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13698 | if (NULL == path_repr) { |
| 13699 | return NULL; |
| 13700 | } |
| 13701 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13702 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 13703 | PyErr_Format(PyExc_TypeError, |
| 13704 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13705 | "not %.200s", _PyType_Name(Py_TYPE(path)), |
| 13706 | _PyType_Name(Py_TYPE(path_repr))); |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13707 | Py_DECREF(path_repr); |
| 13708 | return NULL; |
| 13709 | } |
| 13710 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13711 | return path_repr; |
| 13712 | } |
| 13713 | |
| 13714 | /*[clinic input] |
| 13715 | os.fspath |
| 13716 | |
| 13717 | path: object |
| 13718 | |
| 13719 | Return the file system path representation of the object. |
| 13720 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13721 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 13722 | object defines __fspath__(), then return the result of that method. All other |
| 13723 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13724 | [clinic start generated code]*/ |
| 13725 | |
| 13726 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13727 | os_fspath_impl(PyObject *module, PyObject *path) |
| 13728 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13729 | { |
| 13730 | return PyOS_FSPath(path); |
| 13731 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13732 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13733 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13734 | /*[clinic input] |
| 13735 | os.getrandom |
| 13736 | |
| 13737 | size: Py_ssize_t |
| 13738 | flags: int=0 |
| 13739 | |
| 13740 | Obtain a series of random bytes. |
| 13741 | [clinic start generated code]*/ |
| 13742 | |
| 13743 | static PyObject * |
| 13744 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 13745 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 13746 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13747 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13748 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13749 | |
| 13750 | if (size < 0) { |
| 13751 | errno = EINVAL; |
| 13752 | return posix_error(); |
| 13753 | } |
| 13754 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13755 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13756 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13757 | PyErr_NoMemory(); |
| 13758 | return NULL; |
| 13759 | } |
| 13760 | |
| 13761 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13762 | n = syscall(SYS_getrandom, |
| 13763 | PyBytes_AS_STRING(bytes), |
| 13764 | PyBytes_GET_SIZE(bytes), |
| 13765 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13766 | if (n < 0 && errno == EINTR) { |
| 13767 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13768 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13769 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13770 | |
| 13771 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13772 | continue; |
| 13773 | } |
| 13774 | break; |
| 13775 | } |
| 13776 | |
| 13777 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13778 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13779 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13780 | } |
| 13781 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13782 | if (n != size) { |
| 13783 | _PyBytes_Resize(&bytes, n); |
| 13784 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13785 | |
| 13786 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13787 | |
| 13788 | error: |
| 13789 | Py_DECREF(bytes); |
| 13790 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13791 | } |
| 13792 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13793 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13794 | #ifdef MS_WINDOWS |
| 13795 | /* bpo-36085: Helper functions for managing DLL search directories |
| 13796 | * on win32 |
| 13797 | */ |
| 13798 | |
| 13799 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); |
| 13800 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); |
| 13801 | |
| 13802 | /*[clinic input] |
| 13803 | os._add_dll_directory |
| 13804 | |
| 13805 | path: path_t |
| 13806 | |
| 13807 | Add a path to the DLL search path. |
| 13808 | |
| 13809 | This search path is used when resolving dependencies for imported |
| 13810 | extension modules (the module itself is resolved through sys.path), |
| 13811 | and also by ctypes. |
| 13812 | |
| 13813 | Returns an opaque value that may be passed to os.remove_dll_directory |
| 13814 | to remove this directory from the search path. |
| 13815 | [clinic start generated code]*/ |
| 13816 | |
| 13817 | static PyObject * |
| 13818 | os__add_dll_directory_impl(PyObject *module, path_t *path) |
| 13819 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ |
| 13820 | { |
| 13821 | HMODULE hKernel32; |
| 13822 | PAddDllDirectory AddDllDirectory; |
| 13823 | DLL_DIRECTORY_COOKIE cookie = 0; |
| 13824 | DWORD err = 0; |
| 13825 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 13826 | if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) { |
| 13827 | return NULL; |
| 13828 | } |
| 13829 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13830 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13831 | infrequent operation, just do it each time. Kernel32 is always |
| 13832 | loaded. */ |
| 13833 | Py_BEGIN_ALLOW_THREADS |
| 13834 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13835 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( |
| 13836 | hKernel32, "AddDllDirectory")) || |
| 13837 | !(cookie = (*AddDllDirectory)(path->wide))) { |
| 13838 | err = GetLastError(); |
| 13839 | } |
| 13840 | Py_END_ALLOW_THREADS |
| 13841 | |
| 13842 | if (err) { |
| 13843 | return win32_error_object_err("add_dll_directory", |
| 13844 | path->object, err); |
| 13845 | } |
| 13846 | |
| 13847 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); |
| 13848 | } |
| 13849 | |
| 13850 | /*[clinic input] |
| 13851 | os._remove_dll_directory |
| 13852 | |
| 13853 | cookie: object |
| 13854 | |
| 13855 | Removes a path from the DLL search path. |
| 13856 | |
| 13857 | The parameter is an opaque value that was returned from |
| 13858 | os.add_dll_directory. You can only remove directories that you added |
| 13859 | yourself. |
| 13860 | [clinic start generated code]*/ |
| 13861 | |
| 13862 | static PyObject * |
| 13863 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) |
| 13864 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ |
| 13865 | { |
| 13866 | HMODULE hKernel32; |
| 13867 | PRemoveDllDirectory RemoveDllDirectory; |
| 13868 | DLL_DIRECTORY_COOKIE cookieValue; |
| 13869 | DWORD err = 0; |
| 13870 | |
| 13871 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { |
| 13872 | PyErr_SetString(PyExc_TypeError, |
| 13873 | "Provided cookie was not returned from os.add_dll_directory"); |
| 13874 | return NULL; |
| 13875 | } |
| 13876 | |
| 13877 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( |
| 13878 | cookie, "DLL directory cookie"); |
| 13879 | |
| 13880 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13881 | infrequent operation, just do it each time. Kernel32 is always |
| 13882 | loaded. */ |
| 13883 | Py_BEGIN_ALLOW_THREADS |
| 13884 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13885 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( |
| 13886 | hKernel32, "RemoveDllDirectory")) || |
| 13887 | !(*RemoveDllDirectory)(cookieValue)) { |
| 13888 | err = GetLastError(); |
| 13889 | } |
| 13890 | Py_END_ALLOW_THREADS |
| 13891 | |
| 13892 | if (err) { |
| 13893 | return win32_error_object_err("remove_dll_directory", |
| 13894 | NULL, err); |
| 13895 | } |
| 13896 | |
| 13897 | if (PyCapsule_SetName(cookie, NULL)) { |
| 13898 | return NULL; |
| 13899 | } |
| 13900 | |
| 13901 | Py_RETURN_NONE; |
| 13902 | } |
| 13903 | |
| 13904 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13905 | |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13906 | |
| 13907 | /* Only check if WIFEXITED is available: expect that it comes |
| 13908 | with WEXITSTATUS, WIFSIGNALED, etc. |
| 13909 | |
| 13910 | os.waitstatus_to_exitcode() is implemented in C and not in Python, so |
| 13911 | subprocess can safely call it during late Python finalization without |
| 13912 | risking that used os attributes were set to None by _PyImport_Cleanup(). */ |
| 13913 | #if defined(WIFEXITED) || defined(MS_WINDOWS) |
| 13914 | /*[clinic input] |
| 13915 | os.waitstatus_to_exitcode |
| 13916 | |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13917 | status as status_obj: object |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13918 | |
| 13919 | Convert a wait status to an exit code. |
| 13920 | |
| 13921 | On Unix: |
| 13922 | |
| 13923 | * If WIFEXITED(status) is true, return WEXITSTATUS(status). |
| 13924 | * If WIFSIGNALED(status) is true, return -WTERMSIG(status). |
| 13925 | * Otherwise, raise a ValueError. |
| 13926 | |
| 13927 | On Windows, return status shifted right by 8 bits. |
| 13928 | |
| 13929 | On Unix, if the process is being traced or if waitpid() was called with |
| 13930 | WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true. |
| 13931 | This function must not be called if WIFSTOPPED(status) is true. |
| 13932 | [clinic start generated code]*/ |
| 13933 | |
| 13934 | static PyObject * |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13935 | os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj) |
| 13936 | /*[clinic end generated code: output=db50b1b0ba3c7153 input=7fe2d7fdaea3db42]*/ |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13937 | { |
| 13938 | #ifndef MS_WINDOWS |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13939 | int status = _PyLong_AsInt(status_obj); |
| 13940 | if (status == -1 && PyErr_Occurred()) { |
| 13941 | return NULL; |
| 13942 | } |
| 13943 | |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13944 | WAIT_TYPE wait_status; |
| 13945 | WAIT_STATUS_INT(wait_status) = status; |
| 13946 | int exitcode; |
| 13947 | if (WIFEXITED(wait_status)) { |
| 13948 | exitcode = WEXITSTATUS(wait_status); |
| 13949 | /* Sanity check to provide warranty on the function behavior. |
| 13950 | It should not occur in practice */ |
| 13951 | if (exitcode < 0) { |
| 13952 | PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode); |
| 13953 | return NULL; |
| 13954 | } |
| 13955 | } |
| 13956 | else if (WIFSIGNALED(wait_status)) { |
| 13957 | int signum = WTERMSIG(wait_status); |
| 13958 | /* Sanity check to provide warranty on the function behavior. |
| 13959 | It should not occurs in practice */ |
| 13960 | if (signum <= 0) { |
| 13961 | PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum); |
| 13962 | return NULL; |
| 13963 | } |
| 13964 | exitcode = -signum; |
| 13965 | } else if (WIFSTOPPED(wait_status)) { |
| 13966 | /* Status only received if the process is being traced |
| 13967 | or if waitpid() was called with WUNTRACED option. */ |
| 13968 | int signum = WSTOPSIG(wait_status); |
| 13969 | PyErr_Format(PyExc_ValueError, |
| 13970 | "process stopped by delivery of signal %i", |
| 13971 | signum); |
| 13972 | return NULL; |
| 13973 | } |
| 13974 | else { |
| 13975 | PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status); |
| 13976 | return NULL; |
| 13977 | } |
| 13978 | return PyLong_FromLong(exitcode); |
| 13979 | #else |
| 13980 | /* Windows implementation: see os.waitpid() implementation |
| 13981 | which uses _cwait(). */ |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13982 | unsigned long long status = PyLong_AsUnsignedLongLong(status_obj); |
| 13983 | if (status == (unsigned long long)-1 && PyErr_Occurred()) { |
| 13984 | return NULL; |
| 13985 | } |
| 13986 | |
| 13987 | unsigned long long exitcode = (status >> 8); |
| 13988 | /* ExitProcess() accepts an UINT type: |
| 13989 | reject exit code which doesn't fit in an UINT */ |
| 13990 | if (exitcode > UINT_MAX) { |
| 13991 | PyErr_Format(PyExc_ValueError, "invalid exit code: %llu", exitcode); |
| 13992 | return NULL; |
| 13993 | } |
| 13994 | return PyLong_FromUnsignedLong((unsigned long)exitcode); |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13995 | #endif |
| 13996 | } |
| 13997 | #endif |
| 13998 | |
| 13999 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14000 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 14001 | |
| 14002 | OS_STAT_METHODDEF |
| 14003 | OS_ACCESS_METHODDEF |
| 14004 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14005 | OS_CHDIR_METHODDEF |
| 14006 | OS_CHFLAGS_METHODDEF |
| 14007 | OS_CHMOD_METHODDEF |
| 14008 | OS_FCHMOD_METHODDEF |
| 14009 | OS_LCHMOD_METHODDEF |
| 14010 | OS_CHOWN_METHODDEF |
| 14011 | OS_FCHOWN_METHODDEF |
| 14012 | OS_LCHOWN_METHODDEF |
| 14013 | OS_LCHFLAGS_METHODDEF |
| 14014 | OS_CHROOT_METHODDEF |
| 14015 | OS_CTERMID_METHODDEF |
| 14016 | OS_GETCWD_METHODDEF |
| 14017 | OS_GETCWDB_METHODDEF |
| 14018 | OS_LINK_METHODDEF |
| 14019 | OS_LISTDIR_METHODDEF |
| 14020 | OS_LSTAT_METHODDEF |
| 14021 | OS_MKDIR_METHODDEF |
| 14022 | OS_NICE_METHODDEF |
| 14023 | OS_GETPRIORITY_METHODDEF |
| 14024 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14025 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 14026 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 14027 | OS_READLINK_METHODDEF |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 14028 | OS_COPY_FILE_RANGE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14029 | OS_RENAME_METHODDEF |
| 14030 | OS_REPLACE_METHODDEF |
| 14031 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14032 | OS_SYMLINK_METHODDEF |
| 14033 | OS_SYSTEM_METHODDEF |
| 14034 | OS_UMASK_METHODDEF |
| 14035 | OS_UNAME_METHODDEF |
| 14036 | OS_UNLINK_METHODDEF |
| 14037 | OS_REMOVE_METHODDEF |
| 14038 | OS_UTIME_METHODDEF |
| 14039 | OS_TIMES_METHODDEF |
| 14040 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14041 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14042 | OS_EXECV_METHODDEF |
| 14043 | OS_EXECVE_METHODDEF |
| 14044 | OS_SPAWNV_METHODDEF |
| 14045 | OS_SPAWNVE_METHODDEF |
| 14046 | OS_FORK1_METHODDEF |
| 14047 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 14048 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14049 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 14050 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 14051 | OS_SCHED_GETPARAM_METHODDEF |
| 14052 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 14053 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 14054 | OS_SCHED_SETPARAM_METHODDEF |
| 14055 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 14056 | OS_SCHED_YIELD_METHODDEF |
| 14057 | OS_SCHED_SETAFFINITY_METHODDEF |
| 14058 | OS_SCHED_GETAFFINITY_METHODDEF |
| 14059 | OS_OPENPTY_METHODDEF |
| 14060 | OS_FORKPTY_METHODDEF |
| 14061 | OS_GETEGID_METHODDEF |
| 14062 | OS_GETEUID_METHODDEF |
| 14063 | OS_GETGID_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14064 | OS_GETGROUPLIST_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14065 | OS_GETGROUPS_METHODDEF |
| 14066 | OS_GETPID_METHODDEF |
| 14067 | OS_GETPGRP_METHODDEF |
| 14068 | OS_GETPPID_METHODDEF |
| 14069 | OS_GETUID_METHODDEF |
| 14070 | OS_GETLOGIN_METHODDEF |
| 14071 | OS_KILL_METHODDEF |
| 14072 | OS_KILLPG_METHODDEF |
| 14073 | OS_PLOCK_METHODDEF |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 14074 | OS_STARTFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14075 | OS_SETUID_METHODDEF |
| 14076 | OS_SETEUID_METHODDEF |
| 14077 | OS_SETREUID_METHODDEF |
| 14078 | OS_SETGID_METHODDEF |
| 14079 | OS_SETEGID_METHODDEF |
| 14080 | OS_SETREGID_METHODDEF |
| 14081 | OS_SETGROUPS_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14082 | OS_INITGROUPS_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14083 | OS_GETPGID_METHODDEF |
| 14084 | OS_SETPGRP_METHODDEF |
| 14085 | OS_WAIT_METHODDEF |
| 14086 | OS_WAIT3_METHODDEF |
| 14087 | OS_WAIT4_METHODDEF |
| 14088 | OS_WAITID_METHODDEF |
| 14089 | OS_WAITPID_METHODDEF |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 14090 | OS_PIDFD_OPEN_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14091 | OS_GETSID_METHODDEF |
| 14092 | OS_SETSID_METHODDEF |
| 14093 | OS_SETPGID_METHODDEF |
| 14094 | OS_TCGETPGRP_METHODDEF |
| 14095 | OS_TCSETPGRP_METHODDEF |
| 14096 | OS_OPEN_METHODDEF |
| 14097 | OS_CLOSE_METHODDEF |
| 14098 | OS_CLOSERANGE_METHODDEF |
| 14099 | OS_DEVICE_ENCODING_METHODDEF |
| 14100 | OS_DUP_METHODDEF |
| 14101 | OS_DUP2_METHODDEF |
| 14102 | OS_LOCKF_METHODDEF |
| 14103 | OS_LSEEK_METHODDEF |
| 14104 | OS_READ_METHODDEF |
| 14105 | OS_READV_METHODDEF |
| 14106 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14107 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14108 | OS_WRITE_METHODDEF |
| 14109 | OS_WRITEV_METHODDEF |
| 14110 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14111 | OS_PWRITEV_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14112 | OS_SENDFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14113 | OS_FSTAT_METHODDEF |
| 14114 | OS_ISATTY_METHODDEF |
| 14115 | OS_PIPE_METHODDEF |
| 14116 | OS_PIPE2_METHODDEF |
| 14117 | OS_MKFIFO_METHODDEF |
| 14118 | OS_MKNOD_METHODDEF |
| 14119 | OS_MAJOR_METHODDEF |
| 14120 | OS_MINOR_METHODDEF |
| 14121 | OS_MAKEDEV_METHODDEF |
| 14122 | OS_FTRUNCATE_METHODDEF |
| 14123 | OS_TRUNCATE_METHODDEF |
| 14124 | OS_POSIX_FALLOCATE_METHODDEF |
| 14125 | OS_POSIX_FADVISE_METHODDEF |
| 14126 | OS_PUTENV_METHODDEF |
| 14127 | OS_UNSETENV_METHODDEF |
| 14128 | OS_STRERROR_METHODDEF |
| 14129 | OS_FCHDIR_METHODDEF |
| 14130 | OS_FSYNC_METHODDEF |
| 14131 | OS_SYNC_METHODDEF |
| 14132 | OS_FDATASYNC_METHODDEF |
| 14133 | OS_WCOREDUMP_METHODDEF |
| 14134 | OS_WIFCONTINUED_METHODDEF |
| 14135 | OS_WIFSTOPPED_METHODDEF |
| 14136 | OS_WIFSIGNALED_METHODDEF |
| 14137 | OS_WIFEXITED_METHODDEF |
| 14138 | OS_WEXITSTATUS_METHODDEF |
| 14139 | OS_WTERMSIG_METHODDEF |
| 14140 | OS_WSTOPSIG_METHODDEF |
| 14141 | OS_FSTATVFS_METHODDEF |
| 14142 | OS_STATVFS_METHODDEF |
| 14143 | OS_CONFSTR_METHODDEF |
| 14144 | OS_SYSCONF_METHODDEF |
| 14145 | OS_FPATHCONF_METHODDEF |
| 14146 | OS_PATHCONF_METHODDEF |
| 14147 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 14148 | OS__GETFULLPATHNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14149 | OS__GETDISKUSAGE_METHODDEF |
| 14150 | OS__GETFINALPATHNAME_METHODDEF |
| 14151 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 14152 | OS_GETLOADAVG_METHODDEF |
| 14153 | OS_URANDOM_METHODDEF |
| 14154 | OS_SETRESUID_METHODDEF |
| 14155 | OS_SETRESGID_METHODDEF |
| 14156 | OS_GETRESUID_METHODDEF |
| 14157 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 14158 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14159 | OS_GETXATTR_METHODDEF |
| 14160 | OS_SETXATTR_METHODDEF |
| 14161 | OS_REMOVEXATTR_METHODDEF |
| 14162 | OS_LISTXATTR_METHODDEF |
| 14163 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14164 | OS_GET_TERMINAL_SIZE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14165 | OS_CPU_COUNT_METHODDEF |
| 14166 | OS_GET_INHERITABLE_METHODDEF |
| 14167 | OS_SET_INHERITABLE_METHODDEF |
| 14168 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 14169 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 14170 | OS_GET_BLOCKING_METHODDEF |
| 14171 | OS_SET_BLOCKING_METHODDEF |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 14172 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14173 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14174 | OS_GETRANDOM_METHODDEF |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14175 | OS_MEMFD_CREATE_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14176 | OS__ADD_DLL_DIRECTORY_METHODDEF |
| 14177 | OS__REMOVE_DLL_DIRECTORY_METHODDEF |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14178 | OS_WAITSTATUS_TO_EXITCODE_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14179 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14180 | }; |
| 14181 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14182 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14183 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14184 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14185 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14186 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14187 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14188 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14189 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14190 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14191 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14192 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14193 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14194 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14195 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14196 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14197 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14198 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14199 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14200 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14201 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14202 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14203 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14204 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14205 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14206 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14207 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14208 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14209 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14210 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14211 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14212 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14213 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14214 | #endif |
| 14215 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14216 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14217 | #endif |
| 14218 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14219 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14220 | #endif |
| 14221 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14222 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14223 | #endif |
| 14224 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14225 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14226 | #endif |
| 14227 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14228 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14229 | #endif |
| 14230 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14231 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14232 | #endif |
| 14233 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14234 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14235 | #endif |
| 14236 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14237 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14238 | #endif |
| 14239 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14240 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14241 | #endif |
| 14242 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14243 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14244 | #endif |
| 14245 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14246 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14247 | #endif |
| 14248 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14249 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14250 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14251 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14252 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14253 | #endif |
| 14254 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14255 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14256 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14257 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14258 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14259 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14260 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14261 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14262 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14263 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14264 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14265 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14266 | #endif |
| 14267 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14268 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14269 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14270 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14271 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14272 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14273 | #endif |
| 14274 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14275 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14276 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14277 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14278 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14279 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14280 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14281 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14282 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 14283 | #ifdef O_TMPFILE |
| 14284 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 14285 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14286 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14287 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14288 | #endif |
| 14289 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14290 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14291 | #endif |
| 14292 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14293 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14294 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14295 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14296 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14297 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14298 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14299 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14300 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14301 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14302 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14303 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14304 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14305 | #endif |
| 14306 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14307 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14308 | #endif |
| 14309 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14310 | /* MS Windows */ |
| 14311 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14312 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14313 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14314 | #endif |
| 14315 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14316 | /* Optimize for short life (keep in memory). */ |
| 14317 | /* 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] | 14318 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14319 | #endif |
| 14320 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14321 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14322 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14323 | #endif |
| 14324 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14325 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14326 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14327 | #endif |
| 14328 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14329 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14330 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14331 | #endif |
| 14332 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14333 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14334 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14335 | /* Send a SIGIO signal whenever input or output |
| 14336 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14337 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14338 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14339 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14340 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14341 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14342 | #endif |
| 14343 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14344 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14345 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14346 | #endif |
| 14347 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14348 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14349 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14350 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14351 | #ifdef O_NOLINKS |
| 14352 | /* 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] | 14353 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14354 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14355 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14356 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14357 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14358 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 14359 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14360 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14361 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14362 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14363 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14364 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14365 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14366 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14367 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14368 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14369 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14370 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14371 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14372 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14373 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14374 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14375 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14376 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14377 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14378 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14379 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14380 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14381 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14382 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14383 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14384 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14385 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14386 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14387 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14388 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14389 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14390 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14391 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14392 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14393 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14394 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14395 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14396 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14397 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14398 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14399 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14400 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14401 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14402 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14403 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14404 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14405 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14406 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14407 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14408 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14409 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14410 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14411 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14412 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 14413 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14414 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14415 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14416 | #endif /* ST_RDONLY */ |
| 14417 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14418 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14419 | #endif /* ST_NOSUID */ |
| 14420 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 14421 | /* GNU extensions */ |
| 14422 | #ifdef ST_NODEV |
| 14423 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 14424 | #endif /* ST_NODEV */ |
| 14425 | #ifdef ST_NOEXEC |
| 14426 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 14427 | #endif /* ST_NOEXEC */ |
| 14428 | #ifdef ST_SYNCHRONOUS |
| 14429 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 14430 | #endif /* ST_SYNCHRONOUS */ |
| 14431 | #ifdef ST_MANDLOCK |
| 14432 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 14433 | #endif /* ST_MANDLOCK */ |
| 14434 | #ifdef ST_WRITE |
| 14435 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 14436 | #endif /* ST_WRITE */ |
| 14437 | #ifdef ST_APPEND |
| 14438 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 14439 | #endif /* ST_APPEND */ |
| 14440 | #ifdef ST_NOATIME |
| 14441 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 14442 | #endif /* ST_NOATIME */ |
| 14443 | #ifdef ST_NODIRATIME |
| 14444 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 14445 | #endif /* ST_NODIRATIME */ |
| 14446 | #ifdef ST_RELATIME |
| 14447 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 14448 | #endif /* ST_RELATIME */ |
| 14449 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14450 | /* FreeBSD sendfile() constants */ |
| 14451 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14452 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14453 | #endif |
| 14454 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14455 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14456 | #endif |
| 14457 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14458 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14459 | #endif |
| 14460 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14461 | /* constants for posix_fadvise */ |
| 14462 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14463 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14464 | #endif |
| 14465 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14466 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14467 | #endif |
| 14468 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14469 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14470 | #endif |
| 14471 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14472 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14473 | #endif |
| 14474 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14475 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14476 | #endif |
| 14477 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14478 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14479 | #endif |
| 14480 | |
| 14481 | /* constants for waitid */ |
| 14482 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14483 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 14484 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 14485 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 14486 | #ifdef P_PIDFD |
| 14487 | if (PyModule_AddIntMacro(m, P_PIDFD)) return -1; |
| 14488 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14489 | #endif |
| 14490 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14491 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14492 | #endif |
| 14493 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14494 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14495 | #endif |
| 14496 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14497 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14498 | #endif |
| 14499 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14500 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14501 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14502 | #ifdef CLD_KILLED |
| 14503 | if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1; |
| 14504 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14505 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14506 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14507 | #endif |
| 14508 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14509 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14510 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14511 | #ifdef CLD_STOPPED |
| 14512 | if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1; |
| 14513 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14514 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14515 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14516 | #endif |
| 14517 | |
| 14518 | /* constants for lockf */ |
| 14519 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14520 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14521 | #endif |
| 14522 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14523 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14524 | #endif |
| 14525 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14526 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14527 | #endif |
| 14528 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14529 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14530 | #endif |
| 14531 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14532 | #ifdef RWF_DSYNC |
| 14533 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 14534 | #endif |
| 14535 | #ifdef RWF_HIPRI |
| 14536 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 14537 | #endif |
| 14538 | #ifdef RWF_SYNC |
| 14539 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 14540 | #endif |
| 14541 | #ifdef RWF_NOWAIT |
| 14542 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 14543 | #endif |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 14544 | #ifdef RWF_APPEND |
| 14545 | if (PyModule_AddIntConstant(m, "RWF_APPEND", RWF_APPEND)) return -1; |
| 14546 | #endif |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14547 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14548 | /* constants for posix_spawn */ |
| 14549 | #ifdef HAVE_POSIX_SPAWN |
| 14550 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 14551 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 14552 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 14553 | #endif |
| 14554 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14555 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14556 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 14557 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14558 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14559 | #endif |
| 14560 | #ifdef HAVE_SPAWNV |
| 14561 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14562 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 14563 | #endif |
| 14564 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14565 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14566 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14567 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14568 | #endif |
| 14569 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14570 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14571 | #endif |
| 14572 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14573 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14574 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14575 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 14576 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14577 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14578 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14579 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14580 | #endif |
| 14581 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14582 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14583 | #endif |
| 14584 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14585 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14586 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14587 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14588 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14589 | #endif |
| 14590 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14591 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14592 | #endif |
| 14593 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14594 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14595 | #endif |
| 14596 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14597 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14598 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14599 | #endif |
| 14600 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 14601 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14602 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 14603 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 14604 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 14605 | #endif |
| 14606 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14607 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14608 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14609 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14610 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14611 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14612 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14613 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14614 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14615 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14616 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14617 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14618 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14619 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14620 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14621 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14622 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14623 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14624 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14625 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14626 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14627 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 14628 | #if HAVE_DECL_RTLD_MEMBER |
| 14629 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 14630 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14631 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14632 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 14633 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 14634 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 14635 | #endif |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14636 | #ifdef HAVE_MEMFD_CREATE |
| 14637 | if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1; |
| 14638 | if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1; |
| 14639 | #ifdef MFD_HUGETLB |
| 14640 | if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14641 | #endif |
| 14642 | #ifdef MFD_HUGE_SHIFT |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14643 | if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14644 | #endif |
| 14645 | #ifdef MFD_HUGE_MASK |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14646 | if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14647 | #endif |
| 14648 | #ifdef MFD_HUGE_64KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14649 | if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14650 | #endif |
| 14651 | #ifdef MFD_HUGE_512KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14652 | if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14653 | #endif |
| 14654 | #ifdef MFD_HUGE_1MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14655 | if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14656 | #endif |
| 14657 | #ifdef MFD_HUGE_2MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14658 | if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14659 | #endif |
| 14660 | #ifdef MFD_HUGE_8MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14661 | if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14662 | #endif |
| 14663 | #ifdef MFD_HUGE_16MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14664 | if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14665 | #endif |
| 14666 | #ifdef MFD_HUGE_32MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14667 | if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14668 | #endif |
| 14669 | #ifdef MFD_HUGE_256MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14670 | if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14671 | #endif |
| 14672 | #ifdef MFD_HUGE_512MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14673 | if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14674 | #endif |
| 14675 | #ifdef MFD_HUGE_1GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14676 | if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14677 | #endif |
| 14678 | #ifdef MFD_HUGE_2GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14679 | if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14680 | #endif |
| 14681 | #ifdef MFD_HUGE_16GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14682 | if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1; |
| 14683 | #endif |
| 14684 | #endif |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14685 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14686 | #if defined(__APPLE__) |
| 14687 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 14688 | #endif |
| 14689 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14690 | #ifdef MS_WINDOWS |
| 14691 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; |
| 14692 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; |
| 14693 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; |
| 14694 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; |
| 14695 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; |
| 14696 | #endif |
| 14697 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14698 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14699 | } |
| 14700 | |
| 14701 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14702 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14703 | |
| 14704 | #ifdef HAVE_FACCESSAT |
| 14705 | "HAVE_FACCESSAT", |
| 14706 | #endif |
| 14707 | |
| 14708 | #ifdef HAVE_FCHDIR |
| 14709 | "HAVE_FCHDIR", |
| 14710 | #endif |
| 14711 | |
| 14712 | #ifdef HAVE_FCHMOD |
| 14713 | "HAVE_FCHMOD", |
| 14714 | #endif |
| 14715 | |
| 14716 | #ifdef HAVE_FCHMODAT |
| 14717 | "HAVE_FCHMODAT", |
| 14718 | #endif |
| 14719 | |
| 14720 | #ifdef HAVE_FCHOWN |
| 14721 | "HAVE_FCHOWN", |
| 14722 | #endif |
| 14723 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 14724 | #ifdef HAVE_FCHOWNAT |
| 14725 | "HAVE_FCHOWNAT", |
| 14726 | #endif |
| 14727 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14728 | #ifdef HAVE_FEXECVE |
| 14729 | "HAVE_FEXECVE", |
| 14730 | #endif |
| 14731 | |
| 14732 | #ifdef HAVE_FDOPENDIR |
| 14733 | "HAVE_FDOPENDIR", |
| 14734 | #endif |
| 14735 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14736 | #ifdef HAVE_FPATHCONF |
| 14737 | "HAVE_FPATHCONF", |
| 14738 | #endif |
| 14739 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14740 | #ifdef HAVE_FSTATAT |
| 14741 | "HAVE_FSTATAT", |
| 14742 | #endif |
| 14743 | |
| 14744 | #ifdef HAVE_FSTATVFS |
| 14745 | "HAVE_FSTATVFS", |
| 14746 | #endif |
| 14747 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 14748 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14749 | "HAVE_FTRUNCATE", |
| 14750 | #endif |
| 14751 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14752 | #ifdef HAVE_FUTIMENS |
| 14753 | "HAVE_FUTIMENS", |
| 14754 | #endif |
| 14755 | |
| 14756 | #ifdef HAVE_FUTIMES |
| 14757 | "HAVE_FUTIMES", |
| 14758 | #endif |
| 14759 | |
| 14760 | #ifdef HAVE_FUTIMESAT |
| 14761 | "HAVE_FUTIMESAT", |
| 14762 | #endif |
| 14763 | |
| 14764 | #ifdef HAVE_LINKAT |
| 14765 | "HAVE_LINKAT", |
| 14766 | #endif |
| 14767 | |
| 14768 | #ifdef HAVE_LCHFLAGS |
| 14769 | "HAVE_LCHFLAGS", |
| 14770 | #endif |
| 14771 | |
| 14772 | #ifdef HAVE_LCHMOD |
| 14773 | "HAVE_LCHMOD", |
| 14774 | #endif |
| 14775 | |
| 14776 | #ifdef HAVE_LCHOWN |
| 14777 | "HAVE_LCHOWN", |
| 14778 | #endif |
| 14779 | |
| 14780 | #ifdef HAVE_LSTAT |
| 14781 | "HAVE_LSTAT", |
| 14782 | #endif |
| 14783 | |
| 14784 | #ifdef HAVE_LUTIMES |
| 14785 | "HAVE_LUTIMES", |
| 14786 | #endif |
| 14787 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14788 | #ifdef HAVE_MEMFD_CREATE |
| 14789 | "HAVE_MEMFD_CREATE", |
| 14790 | #endif |
| 14791 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14792 | #ifdef HAVE_MKDIRAT |
| 14793 | "HAVE_MKDIRAT", |
| 14794 | #endif |
| 14795 | |
| 14796 | #ifdef HAVE_MKFIFOAT |
| 14797 | "HAVE_MKFIFOAT", |
| 14798 | #endif |
| 14799 | |
| 14800 | #ifdef HAVE_MKNODAT |
| 14801 | "HAVE_MKNODAT", |
| 14802 | #endif |
| 14803 | |
| 14804 | #ifdef HAVE_OPENAT |
| 14805 | "HAVE_OPENAT", |
| 14806 | #endif |
| 14807 | |
| 14808 | #ifdef HAVE_READLINKAT |
| 14809 | "HAVE_READLINKAT", |
| 14810 | #endif |
| 14811 | |
| 14812 | #ifdef HAVE_RENAMEAT |
| 14813 | "HAVE_RENAMEAT", |
| 14814 | #endif |
| 14815 | |
| 14816 | #ifdef HAVE_SYMLINKAT |
| 14817 | "HAVE_SYMLINKAT", |
| 14818 | #endif |
| 14819 | |
| 14820 | #ifdef HAVE_UNLINKAT |
| 14821 | "HAVE_UNLINKAT", |
| 14822 | #endif |
| 14823 | |
| 14824 | #ifdef HAVE_UTIMENSAT |
| 14825 | "HAVE_UTIMENSAT", |
| 14826 | #endif |
| 14827 | |
| 14828 | #ifdef MS_WINDOWS |
| 14829 | "MS_WINDOWS", |
| 14830 | #endif |
| 14831 | |
| 14832 | NULL |
| 14833 | }; |
| 14834 | |
| 14835 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14836 | static int |
| 14837 | posixmodule_exec(PyObject *m) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14838 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14839 | _posixstate *state = get_posix_state(m); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14840 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14841 | /* Initialize environ dictionary */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14842 | PyObject *v = convertenviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14843 | Py_XINCREF(v); |
| 14844 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14845 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14846 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14847 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14848 | if (all_ins(m)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14849 | return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14850 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14851 | if (setup_confname_tables(m)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14852 | return -1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14853 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14854 | Py_INCREF(PyExc_OSError); |
| 14855 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 14856 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14857 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14858 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 14859 | PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc); |
| 14860 | if (WaitidResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14861 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14862 | } |
| 14863 | Py_INCREF(WaitidResultType); |
| 14864 | PyModule_AddObject(m, "waitid_result", WaitidResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14865 | state->WaitidResultType = WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14866 | #endif |
| 14867 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14868 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
| 14869 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 14870 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 14871 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 14872 | PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc); |
| 14873 | if (StatResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14874 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14875 | } |
| 14876 | Py_INCREF(StatResultType); |
| 14877 | PyModule_AddObject(m, "stat_result", StatResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14878 | state->StatResultType = StatResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14879 | structseq_new = ((PyTypeObject *)StatResultType)->tp_new; |
| 14880 | ((PyTypeObject *)StatResultType)->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14881 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14882 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
| 14883 | PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc); |
| 14884 | if (StatVFSResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14885 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14886 | } |
| 14887 | Py_INCREF(StatVFSResultType); |
| 14888 | PyModule_AddObject(m, "statvfs_result", StatVFSResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14889 | state->StatVFSResultType = StatVFSResultType; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14890 | #ifdef NEED_TICKS_PER_SECOND |
| 14891 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14892 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14893 | # elif defined(HZ) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14894 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14895 | # else |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14896 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14897 | # endif |
| 14898 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14899 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14900 | #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] | 14901 | sched_param_desc.name = MODNAME ".sched_param"; |
| 14902 | PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc); |
| 14903 | if (SchedParamType == NULL) { |
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 | } |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14906 | Py_INCREF(SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14907 | PyModule_AddObject(m, "sched_param", SchedParamType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14908 | state->SchedParamType = SchedParamType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14909 | ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param; |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14910 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14911 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14912 | /* initialize TerminalSize_info */ |
| 14913 | PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc); |
| 14914 | if (TerminalSizeType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14915 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14916 | } |
| 14917 | Py_INCREF(TerminalSizeType); |
| 14918 | PyModule_AddObject(m, "terminal_size", TerminalSizeType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14919 | state->TerminalSizeType = TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14920 | |
| 14921 | /* initialize scandir types */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14922 | PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14923 | if (ScandirIteratorType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14924 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14925 | } |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14926 | state->ScandirIteratorType = ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14927 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14928 | PyObject *DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14929 | if (DirEntryType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14930 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14931 | } |
| 14932 | Py_INCREF(DirEntryType); |
| 14933 | PyModule_AddObject(m, "DirEntry", DirEntryType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14934 | state->DirEntryType = DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14935 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14936 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14937 | PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(×_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14938 | if (TimesResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14939 | return -1; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14940 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14941 | Py_INCREF(TimesResultType); |
| 14942 | PyModule_AddObject(m, "times_result", TimesResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14943 | state->TimesResultType = TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14944 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14945 | PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14946 | if (UnameResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14947 | return -1; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14948 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14949 | Py_INCREF(UnameResultType); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14950 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14951 | state->UnameResultType = (PyObject *)UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14952 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14953 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14954 | /* |
| 14955 | * Step 2 of weak-linking support on Mac OS X. |
| 14956 | * |
| 14957 | * The code below removes functions that are not available on the |
| 14958 | * currently active platform. |
| 14959 | * |
| 14960 | * 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] | 14961 | * 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] | 14962 | * OSX 10.4. |
| 14963 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14964 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14965 | if (fstatvfs == NULL) { |
| 14966 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14967 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14968 | } |
| 14969 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14970 | #endif /* HAVE_FSTATVFS */ |
| 14971 | |
| 14972 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14973 | if (statvfs == NULL) { |
| 14974 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14975 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14976 | } |
| 14977 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14978 | #endif /* HAVE_STATVFS */ |
| 14979 | |
| 14980 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14981 | if (lchown == NULL) { |
| 14982 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14983 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14984 | } |
| 14985 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14986 | #endif /* HAVE_LCHOWN */ |
| 14987 | |
| 14988 | |
| 14989 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14990 | |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14991 | if ((state->billion = PyLong_FromLong(1000000000)) == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14992 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14993 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14994 | state->struct_rusage = PyUnicode_InternFromString("struct_rusage"); |
| 14995 | if (state->struct_rusage == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14996 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14997 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14998 | state->st_mode = PyUnicode_InternFromString("st_mode"); |
| 14999 | if (state->st_mode == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15000 | return -1; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 15001 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15002 | /* suppress "function not used" warnings */ |
| 15003 | { |
| 15004 | int ignored; |
| 15005 | fd_specified("", -1); |
| 15006 | follow_symlinks_specified("", 1); |
| 15007 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 15008 | dir_fd_converter(Py_None, &ignored); |
| 15009 | dir_fd_unavailable(Py_None, &ignored); |
| 15010 | } |
| 15011 | |
| 15012 | /* |
| 15013 | * provide list of locally available functions |
| 15014 | * so os.py can populate support_* lists |
| 15015 | */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15016 | PyObject *list = PyList_New(0); |
| 15017 | if (!list) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15018 | return -1; |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15019 | } |
| 15020 | for (const char * const *trace = have_functions; *trace; trace++) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15021 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 15022 | if (!unicode) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15023 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15024 | if (PyList_Append(list, unicode)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15025 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15026 | Py_DECREF(unicode); |
| 15027 | } |
| 15028 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 15029 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15030 | return 0; |
| 15031 | } |
| 15032 | |
| 15033 | |
| 15034 | static PyModuleDef_Slot posixmodile_slots[] = { |
| 15035 | {Py_mod_exec, posixmodule_exec}, |
| 15036 | {0, NULL} |
| 15037 | }; |
| 15038 | |
| 15039 | static struct PyModuleDef posixmodule = { |
| 15040 | PyModuleDef_HEAD_INIT, |
| 15041 | .m_name = MODNAME, |
| 15042 | .m_doc = posix__doc__, |
| 15043 | .m_size = sizeof(_posixstate), |
| 15044 | .m_methods = posix_methods, |
| 15045 | .m_slots = posixmodile_slots, |
| 15046 | .m_traverse = _posix_traverse, |
| 15047 | .m_clear = _posix_clear, |
| 15048 | .m_free = _posix_free, |
| 15049 | }; |
| 15050 | |
| 15051 | PyMODINIT_FUNC |
| 15052 | INITFUNC(void) |
| 15053 | { |
| 15054 | return PyModuleDef_Init(&posixmodule); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 15055 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15056 | |
| 15057 | #ifdef __cplusplus |
| 15058 | } |
| 15059 | #endif |