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 | |
| 473 | status = _PyEval_ReInitThreads(runtime); |
| 474 | if (_PyStatus_EXCEPTION(status)) { |
| 475 | goto fatal_error; |
| 476 | } |
| 477 | |
| 478 | status = _PyImport_ReInitLock(); |
| 479 | if (_PyStatus_EXCEPTION(status)) { |
| 480 | goto fatal_error; |
| 481 | } |
| 482 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 483 | _PySignal_AfterFork(); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 484 | |
| 485 | status = _PyRuntimeState_ReInitThreads(runtime); |
| 486 | if (_PyStatus_EXCEPTION(status)) { |
| 487 | goto fatal_error; |
| 488 | } |
| 489 | |
| 490 | status = _PyInterpreterState_DeleteExceptMain(runtime); |
| 491 | if (_PyStatus_EXCEPTION(status)) { |
| 492 | goto fatal_error; |
| 493 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 494 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 495 | run_at_forkers(_PyInterpreterState_GET()->after_forkers_child, 0); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 496 | return; |
| 497 | |
| 498 | fatal_error: |
| 499 | Py_ExitStatusException(status); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | static int |
| 503 | register_at_forker(PyObject **lst, PyObject *func) |
| 504 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 505 | if (func == NULL) /* nothing to register? do nothing. */ |
| 506 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 507 | if (*lst == NULL) { |
| 508 | *lst = PyList_New(0); |
| 509 | if (*lst == NULL) |
| 510 | return -1; |
| 511 | } |
| 512 | return PyList_Append(*lst, func); |
| 513 | } |
Victor Stinner | 87255be | 2020-04-07 23:11:49 +0200 | [diff] [blame] | 514 | #endif /* HAVE_FORK */ |
| 515 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 516 | |
| 517 | /* Legacy wrapper */ |
| 518 | void |
| 519 | PyOS_AfterFork(void) |
| 520 | { |
| 521 | #ifdef HAVE_FORK |
| 522 | PyOS_AfterFork_Child(); |
| 523 | #endif |
| 524 | } |
| 525 | |
| 526 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 527 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 528 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 529 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 530 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 531 | ULONG, struct _Py_stat_struct *); |
| 532 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 533 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 534 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 535 | #ifndef MS_WINDOWS |
| 536 | PyObject * |
| 537 | _PyLong_FromUid(uid_t uid) |
| 538 | { |
| 539 | if (uid == (uid_t)-1) |
| 540 | return PyLong_FromLong(-1); |
| 541 | return PyLong_FromUnsignedLong(uid); |
| 542 | } |
| 543 | |
| 544 | PyObject * |
| 545 | _PyLong_FromGid(gid_t gid) |
| 546 | { |
| 547 | if (gid == (gid_t)-1) |
| 548 | return PyLong_FromLong(-1); |
| 549 | return PyLong_FromUnsignedLong(gid); |
| 550 | } |
| 551 | |
| 552 | int |
| 553 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 554 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 555 | uid_t uid; |
| 556 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 557 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 558 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 559 | unsigned long uresult; |
| 560 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 561 | index = _PyNumber_Index(obj); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 562 | if (index == NULL) { |
| 563 | PyErr_Format(PyExc_TypeError, |
| 564 | "uid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 565 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 566 | return 0; |
| 567 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 568 | |
| 569 | /* |
| 570 | * Handling uid_t is complicated for two reasons: |
| 571 | * * Although uid_t is (always?) unsigned, it still |
| 572 | * accepts -1. |
| 573 | * * We don't know its size in advance--it may be |
| 574 | * bigger than an int, or it may be smaller than |
| 575 | * a long. |
| 576 | * |
| 577 | * So a bit of defensive programming is in order. |
| 578 | * Start with interpreting the value passed |
| 579 | * in as a signed long and see if it works. |
| 580 | */ |
| 581 | |
| 582 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 583 | |
| 584 | if (!overflow) { |
| 585 | uid = (uid_t)result; |
| 586 | |
| 587 | if (result == -1) { |
| 588 | if (PyErr_Occurred()) |
| 589 | goto fail; |
| 590 | /* It's a legitimate -1, we're done. */ |
| 591 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 592 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 593 | |
| 594 | /* Any other negative number is disallowed. */ |
| 595 | if (result < 0) |
| 596 | goto underflow; |
| 597 | |
| 598 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 599 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 600 | (long)uid != result) |
| 601 | goto underflow; |
| 602 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 603 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 604 | |
| 605 | if (overflow < 0) |
| 606 | goto underflow; |
| 607 | |
| 608 | /* |
| 609 | * Okay, the value overflowed a signed long. If it |
| 610 | * fits in an *unsigned* long, it may still be okay, |
| 611 | * as uid_t may be unsigned long on this platform. |
| 612 | */ |
| 613 | uresult = PyLong_AsUnsignedLong(index); |
| 614 | if (PyErr_Occurred()) { |
| 615 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 616 | goto overflow; |
| 617 | goto fail; |
| 618 | } |
| 619 | |
| 620 | uid = (uid_t)uresult; |
| 621 | |
| 622 | /* |
| 623 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 624 | * but this value would get interpreted as (uid_t)-1 by chown |
| 625 | * and its siblings. That's not what the user meant! So we |
| 626 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 627 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 628 | */ |
| 629 | if (uid == (uid_t)-1) |
| 630 | goto overflow; |
| 631 | |
| 632 | /* Ensure the value wasn't truncated. */ |
| 633 | if (sizeof(uid_t) < sizeof(long) && |
| 634 | (unsigned long)uid != uresult) |
| 635 | goto overflow; |
| 636 | /* fallthrough */ |
| 637 | |
| 638 | success: |
| 639 | Py_DECREF(index); |
| 640 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 641 | return 1; |
| 642 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 643 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 644 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 645 | "uid is less than minimum"); |
| 646 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 647 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 648 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 649 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 650 | "uid is greater than maximum"); |
| 651 | /* fallthrough */ |
| 652 | |
| 653 | fail: |
| 654 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | int |
| 659 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 660 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 661 | gid_t gid; |
| 662 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 663 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 664 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 665 | unsigned long uresult; |
| 666 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 667 | index = _PyNumber_Index(obj); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 668 | if (index == NULL) { |
| 669 | PyErr_Format(PyExc_TypeError, |
| 670 | "gid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 671 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 672 | return 0; |
| 673 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 674 | |
| 675 | /* |
| 676 | * Handling gid_t is complicated for two reasons: |
| 677 | * * Although gid_t is (always?) unsigned, it still |
| 678 | * accepts -1. |
| 679 | * * We don't know its size in advance--it may be |
| 680 | * bigger than an int, or it may be smaller than |
| 681 | * a long. |
| 682 | * |
| 683 | * So a bit of defensive programming is in order. |
| 684 | * Start with interpreting the value passed |
| 685 | * in as a signed long and see if it works. |
| 686 | */ |
| 687 | |
| 688 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 689 | |
| 690 | if (!overflow) { |
| 691 | gid = (gid_t)result; |
| 692 | |
| 693 | if (result == -1) { |
| 694 | if (PyErr_Occurred()) |
| 695 | goto fail; |
| 696 | /* It's a legitimate -1, we're done. */ |
| 697 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 698 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 699 | |
| 700 | /* Any other negative number is disallowed. */ |
| 701 | if (result < 0) { |
| 702 | goto underflow; |
| 703 | } |
| 704 | |
| 705 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 706 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 707 | (long)gid != result) |
| 708 | goto underflow; |
| 709 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 710 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 711 | |
| 712 | if (overflow < 0) |
| 713 | goto underflow; |
| 714 | |
| 715 | /* |
| 716 | * Okay, the value overflowed a signed long. If it |
| 717 | * fits in an *unsigned* long, it may still be okay, |
| 718 | * as gid_t may be unsigned long on this platform. |
| 719 | */ |
| 720 | uresult = PyLong_AsUnsignedLong(index); |
| 721 | if (PyErr_Occurred()) { |
| 722 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 723 | goto overflow; |
| 724 | goto fail; |
| 725 | } |
| 726 | |
| 727 | gid = (gid_t)uresult; |
| 728 | |
| 729 | /* |
| 730 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 731 | * but this value would get interpreted as (gid_t)-1 by chown |
| 732 | * and its siblings. That's not what the user meant! So we |
| 733 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 734 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 735 | */ |
| 736 | if (gid == (gid_t)-1) |
| 737 | goto overflow; |
| 738 | |
| 739 | /* Ensure the value wasn't truncated. */ |
| 740 | if (sizeof(gid_t) < sizeof(long) && |
| 741 | (unsigned long)gid != uresult) |
| 742 | goto overflow; |
| 743 | /* fallthrough */ |
| 744 | |
| 745 | success: |
| 746 | Py_DECREF(index); |
| 747 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 748 | return 1; |
| 749 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 750 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 751 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 752 | "gid is less than minimum"); |
| 753 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 754 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 755 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 756 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 757 | "gid is greater than maximum"); |
| 758 | /* fallthrough */ |
| 759 | |
| 760 | fail: |
| 761 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 762 | return 0; |
| 763 | } |
| 764 | #endif /* MS_WINDOWS */ |
| 765 | |
| 766 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 767 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 768 | |
| 769 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 770 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 771 | static int |
| 772 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 773 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 774 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 775 | if (PyErr_Occurred()) |
| 776 | return 0; |
| 777 | return 1; |
| 778 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 779 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 780 | |
| 781 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 782 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 783 | /* |
| 784 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 785 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 786 | * which doesn't play nicely with all the initializer lines in this file that |
| 787 | * look like this: |
| 788 | * int dir_fd = DEFAULT_DIR_FD; |
| 789 | */ |
| 790 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 791 | #else |
| 792 | #define DEFAULT_DIR_FD (-100) |
| 793 | #endif |
| 794 | |
| 795 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 796 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 797 | { |
| 798 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 799 | long long_value; |
| 800 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 801 | PyObject *index = _PyNumber_Index(o); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 802 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 803 | return 0; |
| 804 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 805 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 806 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 807 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 808 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 809 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 810 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 811 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 812 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 813 | return 0; |
| 814 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 815 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 816 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 817 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 818 | return 0; |
| 819 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 820 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 821 | *p = (int)long_value; |
| 822 | return 1; |
| 823 | } |
| 824 | |
| 825 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 826 | dir_fd_converter(PyObject *o, void *p) |
| 827 | { |
| 828 | if (o == Py_None) { |
| 829 | *(int *)p = DEFAULT_DIR_FD; |
| 830 | return 1; |
| 831 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 832 | else if (PyIndex_Check(o)) { |
| 833 | return _fd_converter(o, (int *)p); |
| 834 | } |
| 835 | else { |
| 836 | PyErr_Format(PyExc_TypeError, |
| 837 | "argument should be integer or None, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 838 | _PyType_Name(Py_TYPE(o))); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 839 | return 0; |
| 840 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 843 | typedef struct { |
| 844 | PyObject *billion; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 845 | PyObject *DirEntryType; |
| 846 | PyObject *ScandirIteratorType; |
| 847 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 848 | PyObject *SchedParamType; |
| 849 | #endif |
| 850 | PyObject *StatResultType; |
| 851 | PyObject *StatVFSResultType; |
| 852 | PyObject *TerminalSizeType; |
| 853 | PyObject *TimesResultType; |
| 854 | PyObject *UnameResultType; |
| 855 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 856 | PyObject *WaitidResultType; |
| 857 | #endif |
| 858 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 859 | PyObject *struct_rusage; |
| 860 | #endif |
| 861 | PyObject *st_mode; |
| 862 | } _posixstate; |
| 863 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 864 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 865 | static inline _posixstate* |
| 866 | get_posix_state(PyObject *module) |
| 867 | { |
| 868 | void *state = PyModule_GetState(module); |
| 869 | assert(state != NULL); |
| 870 | return (_posixstate *)state; |
| 871 | } |
| 872 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 873 | /* |
| 874 | * A PyArg_ParseTuple "converter" function |
| 875 | * that handles filesystem paths in the manner |
| 876 | * preferred by the os module. |
| 877 | * |
| 878 | * path_converter accepts (Unicode) strings and their |
| 879 | * subclasses, and bytes and their subclasses. What |
| 880 | * it does with the argument depends on the platform: |
| 881 | * |
| 882 | * * On Windows, if we get a (Unicode) string we |
| 883 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 884 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 885 | * |
| 886 | * * On all other platforms, strings are encoded |
| 887 | * to bytes using PyUnicode_FSConverter, then we |
| 888 | * extract the char * from the bytes object and |
| 889 | * return that. |
| 890 | * |
| 891 | * path_converter also optionally accepts signed |
| 892 | * integers (representing open file descriptors) instead |
| 893 | * of path strings. |
| 894 | * |
| 895 | * Input fields: |
| 896 | * path.nullable |
| 897 | * If nonzero, the path is permitted to be None. |
| 898 | * path.allow_fd |
| 899 | * If nonzero, the path is permitted to be a file handle |
| 900 | * (a signed int) instead of a string. |
| 901 | * path.function_name |
| 902 | * If non-NULL, path_converter will use that as the name |
| 903 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 904 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 905 | * path.argument_name |
| 906 | * If non-NULL, path_converter will use that as the name |
| 907 | * of the parameter in error messages. |
| 908 | * (If path.argument_name is NULL it uses "path".) |
| 909 | * |
| 910 | * Output fields: |
| 911 | * path.wide |
| 912 | * Points to the path if it was expressed as Unicode |
| 913 | * and was not encoded. (Only used on Windows.) |
| 914 | * path.narrow |
| 915 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 916 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 917 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 918 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 919 | * path.fd |
| 920 | * Contains a file descriptor if path.accept_fd was true |
| 921 | * and the caller provided a signed integer instead of any |
| 922 | * sort of string. |
| 923 | * |
| 924 | * WARNING: if your "path" parameter is optional, and is |
| 925 | * unspecified, path_converter will never get called. |
| 926 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 927 | * yourself! |
| 928 | * path.length |
| 929 | * The length of the path in characters, if specified as |
| 930 | * a string. |
| 931 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 932 | * The original object passed in (if get a PathLike object, |
| 933 | * the result of PyOS_FSPath() is treated as the original object). |
| 934 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 935 | * path.cleanup |
| 936 | * For internal use only. May point to a temporary object. |
| 937 | * (Pay no attention to the man behind the curtain.) |
| 938 | * |
| 939 | * At most one of path.wide or path.narrow will be non-NULL. |
| 940 | * If path was None and path.nullable was set, |
| 941 | * or if path was an integer and path.allow_fd was set, |
| 942 | * both path.wide and path.narrow will be NULL |
| 943 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 944 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 945 | * path_converter takes care to not write to the path_t |
| 946 | * unless it's successful. However it must reset the |
| 947 | * "cleanup" field each time it's called. |
| 948 | * |
| 949 | * Use as follows: |
| 950 | * path_t path; |
| 951 | * memset(&path, 0, sizeof(path)); |
| 952 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 953 | * // ... use values from path ... |
| 954 | * path_cleanup(&path); |
| 955 | * |
| 956 | * (Note that if PyArg_Parse fails you don't need to call |
| 957 | * path_cleanup(). However it is safe to do so.) |
| 958 | */ |
| 959 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 960 | const char *function_name; |
| 961 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 962 | int nullable; |
| 963 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 964 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 965 | #ifdef MS_WINDOWS |
| 966 | BOOL narrow; |
| 967 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 968 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 969 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 970 | int fd; |
| 971 | Py_ssize_t length; |
| 972 | PyObject *object; |
| 973 | PyObject *cleanup; |
| 974 | } path_t; |
| 975 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 976 | #ifdef MS_WINDOWS |
| 977 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 978 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 979 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 980 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 981 | {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] | 982 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 983 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 984 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 985 | path_cleanup(path_t *path) |
| 986 | { |
| 987 | Py_CLEAR(path->object); |
| 988 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 992 | path_converter(PyObject *o, void *p) |
| 993 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 994 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 995 | PyObject *bytes = NULL; |
| 996 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 997 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 998 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 999 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1000 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1001 | const wchar_t *wide; |
| 1002 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1003 | |
| 1004 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 1005 | PyErr_Format(exc, "%s%s" fmt, \ |
| 1006 | path->function_name ? path->function_name : "", \ |
| 1007 | path->function_name ? ": " : "", \ |
| 1008 | path->argument_name ? path->argument_name : "path") |
| 1009 | |
| 1010 | /* Py_CLEANUP_SUPPORTED support */ |
| 1011 | if (o == NULL) { |
| 1012 | path_cleanup(path); |
| 1013 | return 1; |
| 1014 | } |
| 1015 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1016 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1017 | path->object = path->cleanup = NULL; |
| 1018 | /* path->object owns a reference to the original object */ |
| 1019 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1020 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1021 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1022 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1023 | #ifdef MS_WINDOWS |
| 1024 | path->narrow = FALSE; |
| 1025 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1026 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1027 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1028 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1029 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1032 | /* Only call this here so that we don't treat the return value of |
| 1033 | os.fspath() as an fd or buffer. */ |
| 1034 | is_index = path->allow_fd && PyIndex_Check(o); |
| 1035 | is_buffer = PyObject_CheckBuffer(o); |
| 1036 | is_bytes = PyBytes_Check(o); |
| 1037 | is_unicode = PyUnicode_Check(o); |
| 1038 | |
| 1039 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 1040 | /* Inline PyOS_FSPath() for better error messages. */ |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1041 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1042 | |
| 1043 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 1044 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1045 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1046 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1047 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1048 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1049 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1050 | goto error_exit; |
| 1051 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1052 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1053 | is_unicode = 1; |
| 1054 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1055 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1056 | is_bytes = 1; |
| 1057 | } |
| 1058 | else { |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1059 | PyErr_Format(PyExc_TypeError, |
| 1060 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1061 | "not %.200s", _PyType_Name(Py_TYPE(o)), |
| 1062 | _PyType_Name(Py_TYPE(res))); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1063 | Py_DECREF(res); |
| 1064 | goto error_exit; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1065 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1066 | |
| 1067 | /* still owns a reference to the original object */ |
| 1068 | Py_DECREF(o); |
| 1069 | o = res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1073 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1074 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1075 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1076 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1077 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1078 | if (length > 32767) { |
| 1079 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
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 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1082 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1083 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1084 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1085 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1086 | |
| 1087 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1088 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1089 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1090 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1091 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1092 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1093 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1094 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1095 | #endif |
| 1096 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1097 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1098 | bytes = o; |
| 1099 | Py_INCREF(bytes); |
| 1100 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1101 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1102 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1103 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1104 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1105 | "%s%s%s should be %s, not %.200s", |
| 1106 | path->function_name ? path->function_name : "", |
| 1107 | path->function_name ? ": " : "", |
| 1108 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1109 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1110 | "integer or None" : |
| 1111 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1112 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1113 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1114 | _PyType_Name(Py_TYPE(o)))) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1115 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1116 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1117 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1118 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1119 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1120 | } |
| 1121 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1122 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1123 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1124 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1125 | } |
| 1126 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1127 | #ifdef MS_WINDOWS |
| 1128 | path->narrow = FALSE; |
| 1129 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1130 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1131 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1132 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1133 | } |
| 1134 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1135 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1136 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1137 | path->function_name ? path->function_name : "", |
| 1138 | path->function_name ? ": " : "", |
| 1139 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1140 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1141 | "integer or None" : |
| 1142 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1143 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1144 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1145 | _PyType_Name(Py_TYPE(o))); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1146 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1149 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1150 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1151 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1152 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1153 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1156 | #ifdef MS_WINDOWS |
| 1157 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1158 | narrow, |
| 1159 | length |
| 1160 | ); |
| 1161 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1162 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1165 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1166 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1167 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1168 | } |
| 1169 | if (length > 32767) { |
| 1170 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
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 (wcslen(wide) != length) { |
| 1174 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1175 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1176 | } |
| 1177 | path->wide = wide; |
| 1178 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1179 | path->cleanup = wo; |
| 1180 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1181 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1182 | path->wide = NULL; |
| 1183 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1184 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1185 | /* Still a reference owned by path->object, don't have to |
| 1186 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1187 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1188 | } |
| 1189 | else { |
| 1190 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1191 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1192 | #endif |
| 1193 | path->fd = -1; |
| 1194 | |
| 1195 | success_exit: |
| 1196 | path->length = length; |
| 1197 | path->object = o; |
| 1198 | return Py_CLEANUP_SUPPORTED; |
| 1199 | |
| 1200 | error_exit: |
| 1201 | Py_XDECREF(o); |
| 1202 | Py_XDECREF(bytes); |
| 1203 | #ifdef MS_WINDOWS |
| 1204 | Py_XDECREF(wo); |
| 1205 | #endif |
| 1206 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1210 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1211 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1212 | PyErr_Format(PyExc_NotImplementedError, |
| 1213 | "%s%s%s unavailable on this platform", |
| 1214 | (function_name != NULL) ? function_name : "", |
| 1215 | (function_name != NULL) ? ": ": "", |
| 1216 | argument_name); |
| 1217 | } |
| 1218 | |
| 1219 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1220 | dir_fd_unavailable(PyObject *o, void *p) |
| 1221 | { |
| 1222 | int dir_fd; |
| 1223 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1224 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1225 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1226 | argument_unavailable_error(NULL, "dir_fd"); |
| 1227 | return 0; |
| 1228 | } |
| 1229 | *(int *)p = dir_fd; |
| 1230 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1234 | fd_specified(const char *function_name, int fd) |
| 1235 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1236 | if (fd == -1) |
| 1237 | return 0; |
| 1238 | |
| 1239 | argument_unavailable_error(function_name, "fd"); |
| 1240 | return 1; |
| 1241 | } |
| 1242 | |
| 1243 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1244 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1245 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1246 | if (follow_symlinks) |
| 1247 | return 0; |
| 1248 | |
| 1249 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1250 | return 1; |
| 1251 | } |
| 1252 | |
| 1253 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1254 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1255 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1256 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1257 | #ifndef MS_WINDOWS |
| 1258 | && !path->narrow |
| 1259 | #endif |
| 1260 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1261 | PyErr_Format(PyExc_ValueError, |
| 1262 | "%s: can't specify dir_fd without matching path", |
| 1263 | function_name); |
| 1264 | return 1; |
| 1265 | } |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1270 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1271 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1272 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1273 | PyErr_Format(PyExc_ValueError, |
| 1274 | "%s: can't specify both dir_fd and fd", |
| 1275 | function_name); |
| 1276 | return 1; |
| 1277 | } |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1282 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1283 | int follow_symlinks) |
| 1284 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1285 | if ((fd > 0) && (!follow_symlinks)) { |
| 1286 | PyErr_Format(PyExc_ValueError, |
| 1287 | "%s: cannot use fd and follow_symlinks together", |
| 1288 | function_name); |
| 1289 | return 1; |
| 1290 | } |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1295 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1296 | int follow_symlinks) |
| 1297 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1298 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1299 | PyErr_Format(PyExc_ValueError, |
| 1300 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1301 | function_name); |
| 1302 | return 1; |
| 1303 | } |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1307 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1308 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1309 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1310 | typedef off_t Py_off_t; |
| 1311 | #endif |
| 1312 | |
| 1313 | static int |
| 1314 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1315 | { |
| 1316 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1317 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1318 | #else |
| 1319 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1320 | #endif |
| 1321 | if (PyErr_Occurred()) |
| 1322 | return 0; |
| 1323 | return 1; |
| 1324 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1325 | |
| 1326 | static PyObject * |
| 1327 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1328 | { |
| 1329 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1330 | return PyLong_FromLongLong(offset); |
| 1331 | #else |
| 1332 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1333 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1334 | } |
| 1335 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1336 | #ifdef HAVE_SIGSET_T |
| 1337 | /* Convert an iterable of integers to a sigset. |
| 1338 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1339 | int |
| 1340 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1341 | { |
| 1342 | sigset_t *mask = (sigset_t *)addr; |
| 1343 | PyObject *iterator, *item; |
| 1344 | long signum; |
| 1345 | int overflow; |
| 1346 | |
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1347 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
| 1348 | if (sigemptyset(mask) < (0)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1349 | /* Probably only if mask == NULL. */ |
| 1350 | PyErr_SetFromErrno(PyExc_OSError); |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | iterator = PyObject_GetIter(obj); |
| 1355 | if (iterator == NULL) { |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1360 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1361 | Py_DECREF(item); |
| 1362 | if (signum <= 0 || signum >= NSIG) { |
| 1363 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1364 | PyErr_Format(PyExc_ValueError, |
| 1365 | "signal number %ld out of range", signum); |
| 1366 | } |
| 1367 | goto error; |
| 1368 | } |
| 1369 | if (sigaddset(mask, (int)signum)) { |
| 1370 | if (errno != EINVAL) { |
| 1371 | /* Probably impossible */ |
| 1372 | PyErr_SetFromErrno(PyExc_OSError); |
| 1373 | goto error; |
| 1374 | } |
| 1375 | /* For backwards compatibility, allow idioms such as |
| 1376 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1377 | */ |
| 1378 | const char msg[] = |
| 1379 | "invalid signal number %ld, please use valid_signals()"; |
| 1380 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1381 | goto error; |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | if (!PyErr_Occurred()) { |
| 1386 | Py_DECREF(iterator); |
| 1387 | return 1; |
| 1388 | } |
| 1389 | |
| 1390 | error: |
| 1391 | Py_DECREF(iterator); |
| 1392 | return 0; |
| 1393 | } |
| 1394 | #endif /* HAVE_SIGSET_T */ |
| 1395 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1396 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1397 | |
| 1398 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1399 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1400 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1401 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1402 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1403 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1404 | |
| 1405 | if (0 == DeviceIoControl( |
| 1406 | reparse_point_handle, |
| 1407 | FSCTL_GET_REPARSE_POINT, |
| 1408 | NULL, 0, /* in buffer */ |
| 1409 | target_buffer, sizeof(target_buffer), |
| 1410 | &n_bytes_returned, |
| 1411 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1412 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (reparse_tag) |
| 1415 | *reparse_tag = rdb->ReparseTag; |
| 1416 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1417 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1418 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1419 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1420 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1421 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1422 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1423 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1424 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1425 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1426 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1427 | */ |
| 1428 | #include <crt_externs.h> |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1429 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1430 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1431 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1432 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1433 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1434 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1435 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1436 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1437 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1438 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1439 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1440 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1441 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1442 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | d = PyDict_New(); |
| 1444 | if (d == NULL) |
| 1445 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1446 | #ifdef MS_WINDOWS |
| 1447 | /* _wenviron must be initialized in this way if the program is started |
| 1448 | through main() instead of wmain(). */ |
| 1449 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1450 | e = _wenviron; |
Benoit Hudson | 723f71a | 2019-12-06 14:15:03 -0500 | [diff] [blame] | 1451 | #elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
| 1452 | /* environ is not accessible as an extern in a shared object on OSX; use |
| 1453 | _NSGetEnviron to resolve it. The value changes if you add environment |
| 1454 | variables between calls to Py_Initialize, so don't cache the value. */ |
| 1455 | e = *_NSGetEnviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1456 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1457 | e = environ; |
| 1458 | #endif |
| 1459 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1460 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1461 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1462 | PyObject *k; |
| 1463 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1464 | #ifdef MS_WINDOWS |
| 1465 | const wchar_t *p = wcschr(*e, L'='); |
| 1466 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1467 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1468 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1469 | if (p == NULL) |
| 1470 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1471 | #ifdef MS_WINDOWS |
| 1472 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1473 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1474 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1475 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1476 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1477 | Py_DECREF(d); |
| 1478 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1479 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1480 | #ifdef MS_WINDOWS |
| 1481 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1482 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1483 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1484 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1485 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1486 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1487 | Py_DECREF(d); |
| 1488 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1489 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1490 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1491 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1492 | Py_DECREF(v); |
| 1493 | Py_DECREF(k); |
| 1494 | Py_DECREF(d); |
| 1495 | return NULL; |
| 1496 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1497 | } |
| 1498 | Py_DECREF(k); |
| 1499 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1500 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1501 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1504 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1505 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1506 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1507 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1508 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1509 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1510 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1511 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1512 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1513 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1514 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1515 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1516 | /* XXX We should pass the function name along in the future. |
| 1517 | (winreg.c also wants to pass the function name.) |
| 1518 | This would however require an additional param to the |
| 1519 | Windows error object, which is non-trivial. |
| 1520 | */ |
| 1521 | errno = GetLastError(); |
| 1522 | if (filename) |
| 1523 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1524 | else |
| 1525 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1526 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1527 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1528 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1529 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1530 | { |
| 1531 | /* XXX - see win32_error for comments on 'function' */ |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1532 | if (filename) |
| 1533 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1534 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1535 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1536 | filename); |
| 1537 | else |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1538 | return PyErr_SetFromWindowsErr(err); |
| 1539 | } |
| 1540 | |
| 1541 | static PyObject * |
| 1542 | win32_error_object(const char* function, PyObject* filename) |
| 1543 | { |
| 1544 | errno = GetLastError(); |
| 1545 | return win32_error_object_err(function, filename, errno); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1546 | } |
| 1547 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1548 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1549 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1550 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1551 | posix_path_object_error(PyObject *path) |
| 1552 | { |
| 1553 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1554 | } |
| 1555 | |
| 1556 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1557 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1558 | { |
| 1559 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1560 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1561 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1562 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1563 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1564 | #endif |
| 1565 | } |
| 1566 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1567 | static PyObject * |
| 1568 | path_object_error2(PyObject *path, PyObject *path2) |
| 1569 | { |
| 1570 | #ifdef MS_WINDOWS |
| 1571 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1572 | PyExc_OSError, 0, path, path2); |
| 1573 | #else |
| 1574 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1575 | #endif |
| 1576 | } |
| 1577 | |
| 1578 | static PyObject * |
| 1579 | path_error(path_t *path) |
| 1580 | { |
| 1581 | return path_object_error(path->object); |
| 1582 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1583 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1584 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1585 | posix_path_error(path_t *path) |
| 1586 | { |
| 1587 | return posix_path_object_error(path->object); |
| 1588 | } |
| 1589 | |
| 1590 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1591 | path_error2(path_t *path, path_t *path2) |
| 1592 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1593 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1597 | /* POSIX generic methods */ |
| 1598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1599 | static int |
| 1600 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1601 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1602 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1603 | int *pointer = (int *)p; |
| 1604 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1605 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1606 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1607 | *pointer = fd; |
| 1608 | return 1; |
| 1609 | } |
| 1610 | |
| 1611 | static PyObject * |
| 1612 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1613 | { |
| 1614 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1615 | int async_err = 0; |
| 1616 | |
| 1617 | do { |
| 1618 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1619 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1620 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1621 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1622 | Py_END_ALLOW_THREADS |
| 1623 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1624 | if (res != 0) |
| 1625 | return (!async_err) ? posix_error() : NULL; |
| 1626 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1627 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1628 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1629 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1630 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1631 | /* This is a reimplementation of the C library's chdir function, |
| 1632 | but one that produces Win32 errors instead of DOS error codes. |
| 1633 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1634 | it also needs to set "magic" environment variables indicating |
| 1635 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1636 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1637 | win32_wchdir(LPCWSTR path) |
| 1638 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1639 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1640 | int result; |
| 1641 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1642 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1643 | if(!SetCurrentDirectoryW(path)) |
| 1644 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1645 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1646 | if (!result) |
| 1647 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1648 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1649 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1650 | if (!new_path) { |
| 1651 | SetLastError(ERROR_OUTOFMEMORY); |
| 1652 | return FALSE; |
| 1653 | } |
| 1654 | result = GetCurrentDirectoryW(result, new_path); |
| 1655 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1656 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1657 | return FALSE; |
| 1658 | } |
| 1659 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1660 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1661 | wcsncmp(new_path, L"//", 2) == 0); |
| 1662 | if (!is_unc_like_path) { |
| 1663 | env[1] = new_path[0]; |
| 1664 | result = SetEnvironmentVariableW(env, new_path); |
| 1665 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1666 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1667 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1668 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1669 | } |
| 1670 | #endif |
| 1671 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1672 | #ifdef MS_WINDOWS |
| 1673 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1674 | - time stamps are restricted to second resolution |
| 1675 | - file modification times suffer from forth-and-back conversions between |
| 1676 | UTC and local time |
| 1677 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1678 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1679 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1680 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1681 | #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1682 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1683 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1684 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1685 | BY_HANDLE_FILE_INFORMATION *info, |
| 1686 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1687 | { |
| 1688 | memset(info, 0, sizeof(*info)); |
| 1689 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1690 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1691 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1692 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1693 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1694 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1695 | /* info->nNumberOfLinks = 1; */ |
| 1696 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1697 | *reparse_tag = pFileData->dwReserved0; |
| 1698 | else |
| 1699 | *reparse_tag = 0; |
| 1700 | } |
| 1701 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1702 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1703 | 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] | 1704 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1705 | HANDLE hFindFile; |
| 1706 | WIN32_FIND_DATAW FileData; |
| 1707 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1708 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1709 | return FALSE; |
| 1710 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1711 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1712 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1715 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1716 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1717 | BOOL traverse) |
| 1718 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1719 | HANDLE hFile; |
| 1720 | BY_HANDLE_FILE_INFORMATION fileInfo; |
| 1721 | FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 }; |
| 1722 | DWORD fileType, error; |
| 1723 | BOOL isUnhandledTag = FALSE; |
| 1724 | int retval = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1725 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1726 | DWORD access = FILE_READ_ATTRIBUTES; |
| 1727 | DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */ |
| 1728 | if (!traverse) { |
| 1729 | flags |= FILE_FLAG_OPEN_REPARSE_POINT; |
| 1730 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1731 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1732 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1733 | if (hFile == INVALID_HANDLE_VALUE) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1734 | /* Either the path doesn't exist, or the caller lacks access. */ |
| 1735 | error = GetLastError(); |
| 1736 | switch (error) { |
| 1737 | case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */ |
| 1738 | case ERROR_SHARING_VIOLATION: /* It's a paging file. */ |
| 1739 | /* Try reading the parent directory. */ |
| 1740 | if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) { |
| 1741 | /* Cannot read the parent directory. */ |
| 1742 | SetLastError(error); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1743 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1744 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1745 | if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1746 | if (traverse || |
| 1747 | !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1748 | /* The stat call has to traverse but cannot, so fail. */ |
| 1749 | SetLastError(error); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1750 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1751 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1752 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1753 | break; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1754 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1755 | case ERROR_INVALID_PARAMETER: |
| 1756 | /* \\.\con requires read or write access. */ |
| 1757 | hFile = CreateFileW(path, access | GENERIC_READ, |
| 1758 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1759 | OPEN_EXISTING, flags, NULL); |
| 1760 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1761 | SetLastError(error); |
| 1762 | return -1; |
| 1763 | } |
| 1764 | break; |
| 1765 | |
| 1766 | case ERROR_CANT_ACCESS_FILE: |
| 1767 | /* bpo37834: open unhandled reparse points if traverse fails. */ |
| 1768 | if (traverse) { |
| 1769 | traverse = FALSE; |
| 1770 | isUnhandledTag = TRUE; |
| 1771 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, |
| 1772 | flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 1773 | } |
| 1774 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1775 | SetLastError(error); |
| 1776 | return -1; |
| 1777 | } |
| 1778 | break; |
| 1779 | |
| 1780 | default: |
| 1781 | return -1; |
| 1782 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1783 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1784 | |
| 1785 | if (hFile != INVALID_HANDLE_VALUE) { |
| 1786 | /* Handle types other than files on disk. */ |
| 1787 | fileType = GetFileType(hFile); |
| 1788 | if (fileType != FILE_TYPE_DISK) { |
| 1789 | if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) { |
| 1790 | retval = -1; |
| 1791 | goto cleanup; |
| 1792 | } |
| 1793 | DWORD fileAttributes = GetFileAttributesW(path); |
| 1794 | memset(result, 0, sizeof(*result)); |
| 1795 | if (fileAttributes != INVALID_FILE_ATTRIBUTES && |
| 1796 | fileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 1797 | /* \\.\pipe\ or \\.\mailslot\ */ |
| 1798 | result->st_mode = _S_IFDIR; |
| 1799 | } else if (fileType == FILE_TYPE_CHAR) { |
| 1800 | /* \\.\nul */ |
| 1801 | result->st_mode = _S_IFCHR; |
| 1802 | } else if (fileType == FILE_TYPE_PIPE) { |
| 1803 | /* \\.\pipe\spam */ |
| 1804 | result->st_mode = _S_IFIFO; |
| 1805 | } |
| 1806 | /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */ |
| 1807 | goto cleanup; |
| 1808 | } |
| 1809 | |
| 1810 | /* Query the reparse tag, and traverse a non-link. */ |
| 1811 | if (!traverse) { |
| 1812 | if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, |
| 1813 | &tagInfo, sizeof(tagInfo))) { |
| 1814 | /* Allow devices that do not support FileAttributeTagInfo. */ |
| 1815 | switch (GetLastError()) { |
| 1816 | case ERROR_INVALID_PARAMETER: |
| 1817 | case ERROR_INVALID_FUNCTION: |
| 1818 | case ERROR_NOT_SUPPORTED: |
| 1819 | tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL; |
| 1820 | tagInfo.ReparseTag = 0; |
| 1821 | break; |
| 1822 | default: |
| 1823 | retval = -1; |
| 1824 | goto cleanup; |
| 1825 | } |
| 1826 | } else if (tagInfo.FileAttributes & |
| 1827 | FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1828 | if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1829 | if (isUnhandledTag) { |
| 1830 | /* Traversing previously failed for either this link |
| 1831 | or its target. */ |
| 1832 | SetLastError(ERROR_CANT_ACCESS_FILE); |
| 1833 | retval = -1; |
| 1834 | goto cleanup; |
| 1835 | } |
| 1836 | /* Traverse a non-link, but not if traversing already failed |
| 1837 | for an unhandled tag. */ |
| 1838 | } else if (!isUnhandledTag) { |
| 1839 | CloseHandle(hFile); |
| 1840 | return win32_xstat_impl(path, result, TRUE); |
| 1841 | } |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | if (!GetFileInformationByHandle(hFile, &fileInfo)) { |
| 1846 | switch (GetLastError()) { |
| 1847 | case ERROR_INVALID_PARAMETER: |
| 1848 | case ERROR_INVALID_FUNCTION: |
| 1849 | case ERROR_NOT_SUPPORTED: |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1850 | /* Volumes and physical disks are block devices, e.g. |
| 1851 | \\.\C: and \\.\PhysicalDrive0. */ |
| 1852 | memset(result, 0, sizeof(*result)); |
| 1853 | result->st_mode = 0x6000; /* S_IFBLK */ |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1854 | goto cleanup; |
| 1855 | } |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1856 | retval = -1; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1857 | goto cleanup; |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result); |
| 1862 | |
| 1863 | if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1864 | /* Fix the file execute permissions. This hack sets S_IEXEC if |
| 1865 | the filename has an extension that is commonly used by files |
| 1866 | that CreateProcessW can execute. A real implementation calls |
| 1867 | GetSecurityInfo, OpenThreadToken/OpenProcessToken, and |
| 1868 | AccessCheck to check for generic read, write, and execute |
| 1869 | access. */ |
| 1870 | const wchar_t *fileExtension = wcsrchr(path, '.'); |
| 1871 | if (fileExtension) { |
| 1872 | if (_wcsicmp(fileExtension, L".exe") == 0 || |
| 1873 | _wcsicmp(fileExtension, L".bat") == 0 || |
| 1874 | _wcsicmp(fileExtension, L".cmd") == 0 || |
| 1875 | _wcsicmp(fileExtension, L".com") == 0) { |
| 1876 | result->st_mode |= 0111; |
| 1877 | } |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | cleanup: |
| 1882 | if (hFile != INVALID_HANDLE_VALUE) { |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1883 | /* Preserve last error if we are failing */ |
| 1884 | error = retval ? GetLastError() : 0; |
| 1885 | if (!CloseHandle(hFile)) { |
| 1886 | retval = -1; |
| 1887 | } else if (retval) { |
| 1888 | /* Restore last error */ |
| 1889 | SetLastError(error); |
| 1890 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | return retval; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1897 | 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] | 1898 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1899 | /* Protocol violation: we explicitly clear errno, instead of |
| 1900 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1901 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1902 | errno = 0; |
| 1903 | return code; |
| 1904 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1905 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1906 | |
| 1907 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1908 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1909 | default does not traverse symlinks and instead returns attributes for |
| 1910 | the symlink. |
| 1911 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1912 | Instead, we will open the file (which *does* traverse symlinks by default) |
| 1913 | and GetFileInformationByHandle(). */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1914 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1915 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1916 | 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] | 1917 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1918 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1921 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1922 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1923 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1924 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1927 | #endif /* MS_WINDOWS */ |
| 1928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1929 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1930 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1931 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1932 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1933 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1934 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1935 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1936 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1937 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1938 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1939 | |
| 1940 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1941 | {"st_mode", "protection bits"}, |
| 1942 | {"st_ino", "inode"}, |
| 1943 | {"st_dev", "device"}, |
| 1944 | {"st_nlink", "number of hard links"}, |
| 1945 | {"st_uid", "user ID of owner"}, |
| 1946 | {"st_gid", "group ID of owner"}, |
| 1947 | {"st_size", "total size, in bytes"}, |
| 1948 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1949 | {NULL, "integer time of last access"}, |
| 1950 | {NULL, "integer time of last modification"}, |
| 1951 | {NULL, "integer time of last change"}, |
| 1952 | {"st_atime", "time of last access"}, |
| 1953 | {"st_mtime", "time of last modification"}, |
| 1954 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1955 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1956 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1957 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1958 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1959 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1960 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1961 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1962 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1963 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1964 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1965 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1966 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1967 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1968 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1969 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1970 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1971 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1972 | #endif |
| 1973 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1974 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1975 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1976 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1977 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1978 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1979 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1980 | {"st_fstype", "Type of filesystem"}, |
| 1981 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1982 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 1983 | {"st_reparse_tag", "Windows reparse tag"}, |
| 1984 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1985 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1986 | }; |
| 1987 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1988 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1989 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1990 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1991 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1992 | #endif |
| 1993 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1994 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1995 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1996 | #else |
| 1997 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1998 | #endif |
| 1999 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2000 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2001 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 2002 | #else |
| 2003 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 2004 | #endif |
| 2005 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2006 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 2007 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 2008 | #else |
| 2009 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 2010 | #endif |
| 2011 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2012 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2013 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2014 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2015 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2016 | #endif |
| 2017 | |
| 2018 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 2019 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 2020 | #else |
| 2021 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 2022 | #endif |
| 2023 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2024 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2025 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 2026 | #else |
| 2027 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 2028 | #endif |
| 2029 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2030 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2031 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 2032 | #else |
| 2033 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 2034 | #endif |
| 2035 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2036 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2037 | #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1) |
| 2038 | #else |
| 2039 | #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX |
| 2040 | #endif |
| 2041 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2042 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2043 | "stat_result", /* name */ |
| 2044 | stat_result__doc__, /* doc */ |
| 2045 | stat_result_fields, |
| 2046 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2047 | }; |
| 2048 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2049 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2050 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2051 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2052 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2053 | 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] | 2054 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2055 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2056 | |
| 2057 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2058 | {"f_bsize", }, |
| 2059 | {"f_frsize", }, |
| 2060 | {"f_blocks", }, |
| 2061 | {"f_bfree", }, |
| 2062 | {"f_bavail", }, |
| 2063 | {"f_files", }, |
| 2064 | {"f_ffree", }, |
| 2065 | {"f_favail", }, |
| 2066 | {"f_flag", }, |
| 2067 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 2068 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2069 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2070 | }; |
| 2071 | |
| 2072 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2073 | "statvfs_result", /* name */ |
| 2074 | statvfs_result__doc__, /* doc */ |
| 2075 | statvfs_result_fields, |
| 2076 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2077 | }; |
| 2078 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2079 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2080 | PyDoc_STRVAR(waitid_result__doc__, |
| 2081 | "waitid_result: Result from waitid.\n\n\ |
| 2082 | This object may be accessed either as a tuple of\n\ |
| 2083 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2084 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2085 | \n\ |
| 2086 | See os.waitid for more information."); |
| 2087 | |
| 2088 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2089 | {"si_pid", }, |
| 2090 | {"si_uid", }, |
| 2091 | {"si_signo", }, |
| 2092 | {"si_status", }, |
| 2093 | {"si_code", }, |
| 2094 | {0} |
| 2095 | }; |
| 2096 | |
| 2097 | static PyStructSequence_Desc waitid_result_desc = { |
| 2098 | "waitid_result", /* name */ |
| 2099 | waitid_result__doc__, /* doc */ |
| 2100 | waitid_result_fields, |
| 2101 | 5 |
| 2102 | }; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2103 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2104 | static newfunc structseq_new; |
| 2105 | |
| 2106 | static PyObject * |
| 2107 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2108 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2109 | PyStructSequence *result; |
| 2110 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2111 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2112 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2113 | if (!result) |
| 2114 | return NULL; |
| 2115 | /* If we have been initialized from a tuple, |
| 2116 | st_?time might be set to None. Initialize it |
| 2117 | from the int slots. */ |
| 2118 | for (i = 7; i <= 9; i++) { |
| 2119 | if (result->ob_item[i+3] == Py_None) { |
| 2120 | Py_DECREF(Py_None); |
| 2121 | Py_INCREF(result->ob_item[i]); |
| 2122 | result->ob_item[i+3] = result->ob_item[i]; |
| 2123 | } |
| 2124 | } |
| 2125 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2128 | static int |
| 2129 | _posix_clear(PyObject *module) |
| 2130 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2131 | _posixstate *state = get_posix_state(module); |
| 2132 | Py_CLEAR(state->billion); |
| 2133 | Py_CLEAR(state->DirEntryType); |
| 2134 | Py_CLEAR(state->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2135 | #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] | 2136 | Py_CLEAR(state->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2137 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2138 | Py_CLEAR(state->StatResultType); |
| 2139 | Py_CLEAR(state->StatVFSResultType); |
| 2140 | Py_CLEAR(state->TerminalSizeType); |
| 2141 | Py_CLEAR(state->TimesResultType); |
| 2142 | Py_CLEAR(state->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2143 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2144 | Py_CLEAR(state->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2145 | #endif |
| 2146 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2147 | Py_CLEAR(state->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2148 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2149 | Py_CLEAR(state->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2150 | return 0; |
| 2151 | } |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2152 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2153 | static int |
| 2154 | _posix_traverse(PyObject *module, visitproc visit, void *arg) |
| 2155 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2156 | _posixstate *state = get_posix_state(module); |
| 2157 | Py_VISIT(state->billion); |
| 2158 | Py_VISIT(state->DirEntryType); |
| 2159 | Py_VISIT(state->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2160 | #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] | 2161 | Py_VISIT(state->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2162 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2163 | Py_VISIT(state->StatResultType); |
| 2164 | Py_VISIT(state->StatVFSResultType); |
| 2165 | Py_VISIT(state->TerminalSizeType); |
| 2166 | Py_VISIT(state->TimesResultType); |
| 2167 | Py_VISIT(state->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2168 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2169 | Py_VISIT(state->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2170 | #endif |
| 2171 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2172 | Py_VISIT(state->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2173 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2174 | Py_VISIT(state->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2175 | return 0; |
| 2176 | } |
| 2177 | |
| 2178 | static void |
| 2179 | _posix_free(void *module) |
| 2180 | { |
| 2181 | _posix_clear((PyObject *)module); |
| 2182 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2183 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2184 | static void |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2185 | 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] | 2186 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2187 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2188 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2189 | PyObject *s_in_ns = NULL; |
| 2190 | PyObject *ns_total = NULL; |
| 2191 | PyObject *float_s = NULL; |
| 2192 | |
| 2193 | if (!(s && ns_fractional)) |
| 2194 | goto exit; |
| 2195 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2196 | s_in_ns = PyNumber_Multiply(s, get_posix_state(module)->billion); |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2197 | if (!s_in_ns) |
| 2198 | goto exit; |
| 2199 | |
| 2200 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2201 | if (!ns_total) |
| 2202 | goto exit; |
| 2203 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2204 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2205 | if (!float_s) { |
| 2206 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | PyStructSequence_SET_ITEM(v, index, s); |
| 2210 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2211 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2212 | s = NULL; |
| 2213 | float_s = NULL; |
| 2214 | ns_total = NULL; |
| 2215 | exit: |
| 2216 | Py_XDECREF(s); |
| 2217 | Py_XDECREF(ns_fractional); |
| 2218 | Py_XDECREF(s_in_ns); |
| 2219 | Py_XDECREF(ns_total); |
| 2220 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2223 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2224 | (used by posix_stat() and posix_fstat()) */ |
| 2225 | static PyObject* |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2226 | _pystat_fromstructstat(PyObject *module, STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2227 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2228 | unsigned long ansec, mnsec, cnsec; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2229 | PyObject *StatResultType = get_posix_state(module)->StatResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2230 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2231 | if (v == NULL) |
| 2232 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2233 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2234 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2235 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2236 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2237 | #ifdef MS_WINDOWS |
| 2238 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2239 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2240 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2241 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2242 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2243 | #if defined(MS_WINDOWS) |
| 2244 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2245 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2246 | #else |
| 2247 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2248 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2249 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2250 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2251 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2252 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2253 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2254 | ansec = st->st_atim.tv_nsec; |
| 2255 | mnsec = st->st_mtim.tv_nsec; |
| 2256 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2257 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2258 | ansec = st->st_atimespec.tv_nsec; |
| 2259 | mnsec = st->st_mtimespec.tv_nsec; |
| 2260 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2261 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2262 | ansec = st->st_atime_nsec; |
| 2263 | mnsec = st->st_mtime_nsec; |
| 2264 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2265 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2266 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2267 | #endif |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2268 | fill_time(module, v, 7, st->st_atime, ansec); |
| 2269 | fill_time(module, v, 8, st->st_mtime, mnsec); |
| 2270 | fill_time(module, v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2271 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2272 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2273 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2274 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2275 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2276 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2277 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2278 | PyLong_FromLong((long)st->st_blocks)); |
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_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2281 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2282 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2283 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2284 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2285 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2286 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2287 | #endif |
| 2288 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2289 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2290 | PyObject *val; |
| 2291 | unsigned long bsec,bnsec; |
| 2292 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2293 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2294 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2295 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2296 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2297 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2298 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2299 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2300 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2301 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2302 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2303 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2304 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2305 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2306 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2307 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2308 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2309 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2310 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2311 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2312 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2313 | PyUnicode_FromString(st->st_fstype)); |
| 2314 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2315 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2316 | PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX, |
| 2317 | PyLong_FromUnsignedLong(st->st_reparse_tag)); |
| 2318 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2319 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2320 | if (PyErr_Occurred()) { |
| 2321 | Py_DECREF(v); |
| 2322 | return NULL; |
| 2323 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2324 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2325 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2328 | /* POSIX methods */ |
| 2329 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2330 | |
| 2331 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2332 | posix_do_stat(PyObject *module, const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2333 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2334 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2335 | STRUCT_STAT st; |
| 2336 | int result; |
| 2337 | |
| 2338 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2339 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2340 | return NULL; |
| 2341 | #endif |
| 2342 | |
| 2343 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2344 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2345 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2346 | return NULL; |
| 2347 | |
| 2348 | Py_BEGIN_ALLOW_THREADS |
| 2349 | if (path->fd != -1) |
| 2350 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2351 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2352 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2353 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2354 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2355 | result = win32_lstat(path->wide, &st); |
| 2356 | #else |
| 2357 | else |
| 2358 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2359 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2360 | result = LSTAT(path->narrow, &st); |
| 2361 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2362 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2363 | #ifdef HAVE_FSTATAT |
| 2364 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2365 | result = fstatat(dir_fd, path->narrow, &st, |
| 2366 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2367 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2368 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2369 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2370 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2371 | Py_END_ALLOW_THREADS |
| 2372 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2373 | if (result != 0) { |
| 2374 | return path_error(path); |
| 2375 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2376 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2377 | return _pystat_fromstructstat(module, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2378 | } |
| 2379 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2380 | /*[python input] |
| 2381 | |
| 2382 | for s in """ |
| 2383 | |
| 2384 | FACCESSAT |
| 2385 | FCHMODAT |
| 2386 | FCHOWNAT |
| 2387 | FSTATAT |
| 2388 | LINKAT |
| 2389 | MKDIRAT |
| 2390 | MKFIFOAT |
| 2391 | MKNODAT |
| 2392 | OPENAT |
| 2393 | READLINKAT |
| 2394 | SYMLINKAT |
| 2395 | UNLINKAT |
| 2396 | |
| 2397 | """.strip().split(): |
| 2398 | s = s.strip() |
| 2399 | print(""" |
| 2400 | #ifdef HAVE_{s} |
| 2401 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2402 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2403 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2404 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2405 | """.rstrip().format(s=s)) |
| 2406 | |
| 2407 | for s in """ |
| 2408 | |
| 2409 | FCHDIR |
| 2410 | FCHMOD |
| 2411 | FCHOWN |
| 2412 | FDOPENDIR |
| 2413 | FEXECVE |
| 2414 | FPATHCONF |
| 2415 | FSTATVFS |
| 2416 | FTRUNCATE |
| 2417 | |
| 2418 | """.strip().split(): |
| 2419 | s = s.strip() |
| 2420 | print(""" |
| 2421 | #ifdef HAVE_{s} |
| 2422 | #define PATH_HAVE_{s} 1 |
| 2423 | #else |
| 2424 | #define PATH_HAVE_{s} 0 |
| 2425 | #endif |
| 2426 | |
| 2427 | """.rstrip().format(s=s)) |
| 2428 | [python start generated code]*/ |
| 2429 | |
| 2430 | #ifdef HAVE_FACCESSAT |
| 2431 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2432 | #else |
| 2433 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2434 | #endif |
| 2435 | |
| 2436 | #ifdef HAVE_FCHMODAT |
| 2437 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2438 | #else |
| 2439 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2440 | #endif |
| 2441 | |
| 2442 | #ifdef HAVE_FCHOWNAT |
| 2443 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2444 | #else |
| 2445 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2446 | #endif |
| 2447 | |
| 2448 | #ifdef HAVE_FSTATAT |
| 2449 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2450 | #else |
| 2451 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2452 | #endif |
| 2453 | |
| 2454 | #ifdef HAVE_LINKAT |
| 2455 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2456 | #else |
| 2457 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2458 | #endif |
| 2459 | |
| 2460 | #ifdef HAVE_MKDIRAT |
| 2461 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2462 | #else |
| 2463 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2464 | #endif |
| 2465 | |
| 2466 | #ifdef HAVE_MKFIFOAT |
| 2467 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2468 | #else |
| 2469 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2470 | #endif |
| 2471 | |
| 2472 | #ifdef HAVE_MKNODAT |
| 2473 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2474 | #else |
| 2475 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2476 | #endif |
| 2477 | |
| 2478 | #ifdef HAVE_OPENAT |
| 2479 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2480 | #else |
| 2481 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2482 | #endif |
| 2483 | |
| 2484 | #ifdef HAVE_READLINKAT |
| 2485 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2486 | #else |
| 2487 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2488 | #endif |
| 2489 | |
| 2490 | #ifdef HAVE_SYMLINKAT |
| 2491 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2492 | #else |
| 2493 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2494 | #endif |
| 2495 | |
| 2496 | #ifdef HAVE_UNLINKAT |
| 2497 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2498 | #else |
| 2499 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2500 | #endif |
| 2501 | |
| 2502 | #ifdef HAVE_FCHDIR |
| 2503 | #define PATH_HAVE_FCHDIR 1 |
| 2504 | #else |
| 2505 | #define PATH_HAVE_FCHDIR 0 |
| 2506 | #endif |
| 2507 | |
| 2508 | #ifdef HAVE_FCHMOD |
| 2509 | #define PATH_HAVE_FCHMOD 1 |
| 2510 | #else |
| 2511 | #define PATH_HAVE_FCHMOD 0 |
| 2512 | #endif |
| 2513 | |
| 2514 | #ifdef HAVE_FCHOWN |
| 2515 | #define PATH_HAVE_FCHOWN 1 |
| 2516 | #else |
| 2517 | #define PATH_HAVE_FCHOWN 0 |
| 2518 | #endif |
| 2519 | |
| 2520 | #ifdef HAVE_FDOPENDIR |
| 2521 | #define PATH_HAVE_FDOPENDIR 1 |
| 2522 | #else |
| 2523 | #define PATH_HAVE_FDOPENDIR 0 |
| 2524 | #endif |
| 2525 | |
| 2526 | #ifdef HAVE_FEXECVE |
| 2527 | #define PATH_HAVE_FEXECVE 1 |
| 2528 | #else |
| 2529 | #define PATH_HAVE_FEXECVE 0 |
| 2530 | #endif |
| 2531 | |
| 2532 | #ifdef HAVE_FPATHCONF |
| 2533 | #define PATH_HAVE_FPATHCONF 1 |
| 2534 | #else |
| 2535 | #define PATH_HAVE_FPATHCONF 0 |
| 2536 | #endif |
| 2537 | |
| 2538 | #ifdef HAVE_FSTATVFS |
| 2539 | #define PATH_HAVE_FSTATVFS 1 |
| 2540 | #else |
| 2541 | #define PATH_HAVE_FSTATVFS 0 |
| 2542 | #endif |
| 2543 | |
| 2544 | #ifdef HAVE_FTRUNCATE |
| 2545 | #define PATH_HAVE_FTRUNCATE 1 |
| 2546 | #else |
| 2547 | #define PATH_HAVE_FTRUNCATE 0 |
| 2548 | #endif |
| 2549 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2550 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2551 | #ifdef MS_WINDOWS |
| 2552 | #undef PATH_HAVE_FTRUNCATE |
| 2553 | #define PATH_HAVE_FTRUNCATE 1 |
| 2554 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2555 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2556 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2557 | |
| 2558 | class path_t_converter(CConverter): |
| 2559 | |
| 2560 | type = "path_t" |
| 2561 | impl_by_reference = True |
| 2562 | parse_by_reference = True |
| 2563 | |
| 2564 | converter = 'path_converter' |
| 2565 | |
| 2566 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2567 | # right now path_t doesn't support default values. |
| 2568 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2569 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2570 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2571 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2572 | if self.c_default not in (None, 'Py_None'): |
| 2573 | 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] | 2574 | |
| 2575 | self.nullable = nullable |
| 2576 | self.allow_fd = allow_fd |
| 2577 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2578 | def pre_render(self): |
| 2579 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2580 | if isinstance(value, str): |
| 2581 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2582 | return str(int(bool(value))) |
| 2583 | |
| 2584 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2585 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2586 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2587 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2588 | strify(self.nullable), |
| 2589 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2590 | ) |
| 2591 | |
| 2592 | def cleanup(self): |
| 2593 | return "path_cleanup(&" + self.name + ");\n" |
| 2594 | |
| 2595 | |
| 2596 | class dir_fd_converter(CConverter): |
| 2597 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2599 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2600 | if self.default in (unspecified, None): |
| 2601 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2602 | if isinstance(requires, str): |
| 2603 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2604 | else: |
| 2605 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2606 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2607 | class fildes_converter(CConverter): |
| 2608 | type = 'int' |
| 2609 | converter = 'fildes_converter' |
| 2610 | |
| 2611 | class uid_t_converter(CConverter): |
| 2612 | type = "uid_t" |
| 2613 | converter = '_Py_Uid_Converter' |
| 2614 | |
| 2615 | class gid_t_converter(CConverter): |
| 2616 | type = "gid_t" |
| 2617 | converter = '_Py_Gid_Converter' |
| 2618 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2619 | class dev_t_converter(CConverter): |
| 2620 | type = 'dev_t' |
| 2621 | converter = '_Py_Dev_Converter' |
| 2622 | |
| 2623 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2624 | type = 'dev_t' |
| 2625 | conversion_fn = '_PyLong_FromDev' |
| 2626 | unsigned_cast = '(dev_t)' |
| 2627 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2628 | class FSConverter_converter(CConverter): |
| 2629 | type = 'PyObject *' |
| 2630 | converter = 'PyUnicode_FSConverter' |
| 2631 | def converter_init(self): |
| 2632 | if self.default is not unspecified: |
| 2633 | fail("FSConverter_converter does not support default values") |
| 2634 | self.c_default = 'NULL' |
| 2635 | |
| 2636 | def cleanup(self): |
| 2637 | return "Py_XDECREF(" + self.name + ");\n" |
| 2638 | |
| 2639 | class pid_t_converter(CConverter): |
| 2640 | type = 'pid_t' |
| 2641 | format_unit = '" _Py_PARSE_PID "' |
| 2642 | |
| 2643 | class idtype_t_converter(int_converter): |
| 2644 | type = 'idtype_t' |
| 2645 | |
| 2646 | class id_t_converter(CConverter): |
| 2647 | type = 'id_t' |
| 2648 | format_unit = '" _Py_PARSE_PID "' |
| 2649 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2650 | class intptr_t_converter(CConverter): |
| 2651 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2652 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2653 | |
| 2654 | class Py_off_t_converter(CConverter): |
| 2655 | type = 'Py_off_t' |
| 2656 | converter = 'Py_off_t_converter' |
| 2657 | |
| 2658 | class Py_off_t_return_converter(long_return_converter): |
| 2659 | type = 'Py_off_t' |
| 2660 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2661 | |
| 2662 | class path_confname_converter(CConverter): |
| 2663 | type="int" |
| 2664 | converter="conv_path_confname" |
| 2665 | |
| 2666 | class confstr_confname_converter(path_confname_converter): |
| 2667 | converter='conv_confstr_confname' |
| 2668 | |
| 2669 | class sysconf_confname_converter(path_confname_converter): |
| 2670 | converter="conv_sysconf_confname" |
| 2671 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2672 | [python start generated code]*/ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2673 | /*[python end generated code: output=da39a3ee5e6b4b0d input=f1c8ae8d744f6c8b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2674 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2675 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2676 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2677 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2678 | |
| 2679 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2680 | 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] | 2681 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2682 | |
| 2683 | * |
| 2684 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2685 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2686 | If not None, it should be a file descriptor open to a directory, |
| 2687 | and path should be a relative string; path will then be relative to |
| 2688 | that directory. |
| 2689 | |
| 2690 | follow_symlinks: bool = True |
| 2691 | If False, and the last element of the path is a symbolic link, |
| 2692 | stat will examine the symbolic link itself instead of the file |
| 2693 | the link points to. |
| 2694 | |
| 2695 | Perform a stat system call on the given path. |
| 2696 | |
| 2697 | dir_fd and follow_symlinks may not be implemented |
| 2698 | on your platform. If they are unavailable, using them will raise a |
| 2699 | NotImplementedError. |
| 2700 | |
| 2701 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2702 | an open file descriptor. |
| 2703 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2704 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2705 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2706 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2707 | 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] | 2708 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2709 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2710 | return posix_do_stat(module, "stat", path, dir_fd, follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2711 | } |
| 2712 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2713 | |
| 2714 | /*[clinic input] |
| 2715 | os.lstat |
| 2716 | |
| 2717 | path : path_t |
| 2718 | |
| 2719 | * |
| 2720 | |
| 2721 | dir_fd : dir_fd(requires='fstatat') = None |
| 2722 | |
| 2723 | Perform a stat system call on the given path, without following symbolic links. |
| 2724 | |
| 2725 | Like stat(), but do not follow symbolic links. |
| 2726 | Equivalent to stat(path, follow_symlinks=False). |
| 2727 | [clinic start generated code]*/ |
| 2728 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2729 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2730 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2731 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2732 | { |
| 2733 | int follow_symlinks = 0; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2734 | return posix_do_stat(module, "lstat", path, dir_fd, follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2735 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2736 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2737 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2738 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2739 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2740 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2741 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2742 | 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] | 2743 | |
| 2744 | mode: int |
| 2745 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2746 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2747 | |
| 2748 | * |
| 2749 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2750 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2751 | If not None, it should be a file descriptor open to a directory, |
| 2752 | and path should be relative; path will then be relative to that |
| 2753 | directory. |
| 2754 | |
| 2755 | effective_ids: bool = False |
| 2756 | If True, access will use the effective uid/gid instead of |
| 2757 | the real uid/gid. |
| 2758 | |
| 2759 | follow_symlinks: bool = True |
| 2760 | If False, and the last element of the path is a symbolic link, |
| 2761 | access will examine the symbolic link itself instead of the file |
| 2762 | the link points to. |
| 2763 | |
| 2764 | Use the real uid/gid to test for access to a path. |
| 2765 | |
| 2766 | {parameters} |
| 2767 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2768 | on your platform. If they are unavailable, using them will raise a |
| 2769 | NotImplementedError. |
| 2770 | |
| 2771 | Note that most operations will use the effective uid/gid, therefore this |
| 2772 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2773 | has the specified access to the path. |
| 2774 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2775 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2776 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2777 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2778 | 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] | 2779 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2780 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2781 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2782 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2783 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2784 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2785 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2786 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2787 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2788 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2789 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2790 | #ifndef HAVE_FACCESSAT |
| 2791 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2792 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2793 | |
| 2794 | if (effective_ids) { |
| 2795 | argument_unavailable_error("access", "effective_ids"); |
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 | #endif |
| 2799 | |
| 2800 | #ifdef MS_WINDOWS |
| 2801 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2802 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2803 | Py_END_ALLOW_THREADS |
| 2804 | |
| 2805 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2806 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2807 | * * we didn't get a -1, and |
| 2808 | * * write access wasn't requested, |
| 2809 | * * or the file isn't read-only, |
| 2810 | * * or it's a directory. |
| 2811 | * (Directories cannot be read-only on Windows.) |
| 2812 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2813 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2814 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2815 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2816 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2817 | #else |
| 2818 | |
| 2819 | Py_BEGIN_ALLOW_THREADS |
| 2820 | #ifdef HAVE_FACCESSAT |
| 2821 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2822 | effective_ids || |
| 2823 | !follow_symlinks) { |
| 2824 | int flags = 0; |
| 2825 | if (!follow_symlinks) |
| 2826 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2827 | if (effective_ids) |
| 2828 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2829 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2830 | } |
| 2831 | else |
| 2832 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2833 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2834 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2835 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2836 | #endif |
| 2837 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2838 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2841 | #ifndef F_OK |
| 2842 | #define F_OK 0 |
| 2843 | #endif |
| 2844 | #ifndef R_OK |
| 2845 | #define R_OK 4 |
| 2846 | #endif |
| 2847 | #ifndef W_OK |
| 2848 | #define W_OK 2 |
| 2849 | #endif |
| 2850 | #ifndef X_OK |
| 2851 | #define X_OK 1 |
| 2852 | #endif |
| 2853 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2854 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2855 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2856 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2857 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2858 | |
| 2859 | fd: int |
| 2860 | Integer file descriptor handle. |
| 2861 | |
| 2862 | / |
| 2863 | |
| 2864 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2865 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2866 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2867 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2868 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2869 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2870 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2871 | |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2872 | long size = sysconf(_SC_TTY_NAME_MAX); |
| 2873 | if (size == -1) { |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2874 | return posix_error(); |
| 2875 | } |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2876 | char *buffer = (char *)PyMem_RawMalloc(size); |
| 2877 | if (buffer == NULL) { |
| 2878 | return PyErr_NoMemory(); |
| 2879 | } |
| 2880 | int ret = ttyname_r(fd, buffer, size); |
| 2881 | if (ret != 0) { |
| 2882 | PyMem_RawFree(buffer); |
| 2883 | errno = ret; |
| 2884 | return posix_error(); |
| 2885 | } |
| 2886 | PyObject *res = PyUnicode_DecodeFSDefault(buffer); |
| 2887 | PyMem_RawFree(buffer); |
| 2888 | return res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2889 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2890 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2891 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2892 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2893 | /*[clinic input] |
| 2894 | os.ctermid |
| 2895 | |
| 2896 | Return the name of the controlling terminal for this process. |
| 2897 | [clinic start generated code]*/ |
| 2898 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2899 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2900 | os_ctermid_impl(PyObject *module) |
| 2901 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2902 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2903 | char *ret; |
| 2904 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2905 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2906 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2907 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2908 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2909 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2910 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2911 | if (ret == NULL) |
| 2912 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2913 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2914 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2915 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2916 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2917 | |
| 2918 | /*[clinic input] |
| 2919 | os.chdir |
| 2920 | |
| 2921 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2922 | |
| 2923 | Change the current working directory to the specified path. |
| 2924 | |
| 2925 | path may always be specified as a string. |
| 2926 | On some platforms, path may also be specified as an open file descriptor. |
| 2927 | If this functionality is unavailable, using it raises an exception. |
| 2928 | [clinic start generated code]*/ |
| 2929 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2930 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2931 | os_chdir_impl(PyObject *module, path_t *path) |
| 2932 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2933 | { |
| 2934 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2935 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 2936 | if (PySys_Audit("os.chdir", "(O)", path->object) < 0) { |
| 2937 | return NULL; |
| 2938 | } |
| 2939 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2940 | Py_BEGIN_ALLOW_THREADS |
| 2941 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2942 | /* on unix, success = 0, on windows, success = !0 */ |
| 2943 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2944 | #else |
| 2945 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2946 | if (path->fd != -1) |
| 2947 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2948 | else |
| 2949 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2950 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2951 | #endif |
| 2952 | Py_END_ALLOW_THREADS |
| 2953 | |
| 2954 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2955 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2958 | Py_RETURN_NONE; |
| 2959 | } |
| 2960 | |
| 2961 | |
| 2962 | #ifdef HAVE_FCHDIR |
| 2963 | /*[clinic input] |
| 2964 | os.fchdir |
| 2965 | |
| 2966 | fd: fildes |
| 2967 | |
| 2968 | Change to the directory of the given file descriptor. |
| 2969 | |
| 2970 | fd must be opened on a directory, not a file. |
| 2971 | Equivalent to os.chdir(fd). |
| 2972 | |
| 2973 | [clinic start generated code]*/ |
| 2974 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2975 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2976 | os_fchdir_impl(PyObject *module, int fd) |
| 2977 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2978 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 2979 | if (PySys_Audit("os.chdir", "(i)", fd) < 0) { |
| 2980 | return NULL; |
| 2981 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2982 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2983 | } |
| 2984 | #endif /* HAVE_FCHDIR */ |
| 2985 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2986 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2987 | /*[clinic input] |
| 2988 | os.chmod |
| 2989 | |
| 2990 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2991 | 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] | 2992 | On some platforms, path may also be specified as an open file descriptor. |
| 2993 | If this functionality is unavailable, using it raises an exception. |
| 2994 | |
| 2995 | mode: int |
| 2996 | Operating-system mode bitfield. |
| 2997 | |
| 2998 | * |
| 2999 | |
| 3000 | dir_fd : dir_fd(requires='fchmodat') = None |
| 3001 | If not None, it should be a file descriptor open to a directory, |
| 3002 | and path should be relative; path will then be relative to that |
| 3003 | directory. |
| 3004 | |
| 3005 | follow_symlinks: bool = True |
| 3006 | If False, and the last element of the path is a symbolic link, |
| 3007 | chmod will modify the symbolic link itself instead of the file |
| 3008 | the link points to. |
| 3009 | |
| 3010 | Change the access permissions of a file. |
| 3011 | |
| 3012 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3013 | an open file descriptor. |
| 3014 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3015 | If they are unavailable, using them will raise a NotImplementedError. |
| 3016 | |
| 3017 | [clinic start generated code]*/ |
| 3018 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3019 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3020 | 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] | 3021 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3022 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3023 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3024 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3025 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3026 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3027 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3028 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3029 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3030 | #ifdef HAVE_FCHMODAT |
| 3031 | int fchmodat_nofollow_unsupported = 0; |
| 3032 | #endif |
| 3033 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3034 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 3035 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3036 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3037 | #endif |
| 3038 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3039 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, |
| 3040 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3041 | return NULL; |
| 3042 | } |
| 3043 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3044 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3045 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3046 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 3047 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3048 | result = 0; |
| 3049 | else { |
| 3050 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3051 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 3052 | else |
| 3053 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3054 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3055 | } |
| 3056 | Py_END_ALLOW_THREADS |
| 3057 | |
| 3058 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3059 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3060 | } |
| 3061 | #else /* MS_WINDOWS */ |
| 3062 | Py_BEGIN_ALLOW_THREADS |
| 3063 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3064 | if (path->fd != -1) |
| 3065 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3066 | else |
| 3067 | #endif |
| 3068 | #ifdef HAVE_LCHMOD |
| 3069 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3070 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3071 | else |
| 3072 | #endif |
| 3073 | #ifdef HAVE_FCHMODAT |
| 3074 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 3075 | /* |
| 3076 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 3077 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3078 | * and then says it isn't implemented yet. |
| 3079 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3080 | * |
| 3081 | * Once it is supported, os.chmod will automatically |
| 3082 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 3083 | * Until then, we need to be careful what exception we raise. |
| 3084 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3085 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3086 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3087 | /* |
| 3088 | * But wait! We can't throw the exception without allowing threads, |
| 3089 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 3090 | */ |
| 3091 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3092 | result && |
| 3093 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 3094 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3095 | } |
| 3096 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3097 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3098 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3099 | Py_END_ALLOW_THREADS |
| 3100 | |
| 3101 | if (result) { |
| 3102 | #ifdef HAVE_FCHMODAT |
| 3103 | if (fchmodat_nofollow_unsupported) { |
| 3104 | if (dir_fd != DEFAULT_DIR_FD) |
| 3105 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3106 | dir_fd, follow_symlinks); |
| 3107 | else |
| 3108 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 3109 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3110 | } |
| 3111 | else |
| 3112 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3113 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3114 | } |
| 3115 | #endif |
| 3116 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3117 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3120 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3121 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3122 | /*[clinic input] |
| 3123 | os.fchmod |
| 3124 | |
| 3125 | fd: int |
| 3126 | mode: int |
| 3127 | |
| 3128 | Change the access permissions of the file given by file descriptor fd. |
| 3129 | |
| 3130 | Equivalent to os.chmod(fd, mode). |
| 3131 | [clinic start generated code]*/ |
| 3132 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3133 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3134 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 3135 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3136 | { |
| 3137 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3138 | int async_err = 0; |
| 3139 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3140 | if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) { |
| 3141 | return NULL; |
| 3142 | } |
| 3143 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3144 | do { |
| 3145 | Py_BEGIN_ALLOW_THREADS |
| 3146 | res = fchmod(fd, mode); |
| 3147 | Py_END_ALLOW_THREADS |
| 3148 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3149 | if (res != 0) |
| 3150 | return (!async_err) ? posix_error() : NULL; |
| 3151 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3152 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3153 | } |
| 3154 | #endif /* HAVE_FCHMOD */ |
| 3155 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3156 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3157 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3158 | /*[clinic input] |
| 3159 | os.lchmod |
| 3160 | |
| 3161 | path: path_t |
| 3162 | mode: int |
| 3163 | |
| 3164 | Change the access permissions of a file, without following symbolic links. |
| 3165 | |
| 3166 | If path is a symlink, this affects the link itself rather than the target. |
| 3167 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 3168 | [clinic start generated code]*/ |
| 3169 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3170 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3171 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 3172 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3173 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3174 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3175 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) { |
| 3176 | return NULL; |
| 3177 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3178 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3179 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3180 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3181 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3182 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3183 | return NULL; |
| 3184 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3185 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3186 | } |
| 3187 | #endif /* HAVE_LCHMOD */ |
| 3188 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3189 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3190 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3191 | /*[clinic input] |
| 3192 | os.chflags |
| 3193 | |
| 3194 | path: path_t |
| 3195 | flags: unsigned_long(bitwise=True) |
| 3196 | follow_symlinks: bool=True |
| 3197 | |
| 3198 | Set file flags. |
| 3199 | |
| 3200 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3201 | link, chflags will change flags on the symbolic link itself instead of the |
| 3202 | file the link points to. |
| 3203 | follow_symlinks may not be implemented on your platform. If it is |
| 3204 | unavailable, using it will raise a NotImplementedError. |
| 3205 | |
| 3206 | [clinic start generated code]*/ |
| 3207 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3208 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3209 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3210 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3211 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3212 | { |
| 3213 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3214 | |
| 3215 | #ifndef HAVE_LCHFLAGS |
| 3216 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3217 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3218 | #endif |
| 3219 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3220 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3221 | return NULL; |
| 3222 | } |
| 3223 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3224 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3225 | #ifdef HAVE_LCHFLAGS |
| 3226 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3227 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3228 | else |
| 3229 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3230 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3231 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3232 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3233 | if (result) |
| 3234 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3235 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3236 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3237 | } |
| 3238 | #endif /* HAVE_CHFLAGS */ |
| 3239 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3240 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3241 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3242 | /*[clinic input] |
| 3243 | os.lchflags |
| 3244 | |
| 3245 | path: path_t |
| 3246 | flags: unsigned_long(bitwise=True) |
| 3247 | |
| 3248 | Set file flags. |
| 3249 | |
| 3250 | This function will not follow symbolic links. |
| 3251 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3252 | [clinic start generated code]*/ |
| 3253 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3254 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3255 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3256 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3257 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3258 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3259 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3260 | return NULL; |
| 3261 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3262 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3263 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3264 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3265 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3266 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3267 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3268 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3269 | } |
| 3270 | #endif /* HAVE_LCHFLAGS */ |
| 3271 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3272 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3273 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3274 | /*[clinic input] |
| 3275 | os.chroot |
| 3276 | path: path_t |
| 3277 | |
| 3278 | Change root directory to path. |
| 3279 | |
| 3280 | [clinic start generated code]*/ |
| 3281 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3282 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3283 | os_chroot_impl(PyObject *module, path_t *path) |
| 3284 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3285 | { |
| 3286 | int res; |
| 3287 | Py_BEGIN_ALLOW_THREADS |
| 3288 | res = chroot(path->narrow); |
| 3289 | Py_END_ALLOW_THREADS |
| 3290 | if (res < 0) |
| 3291 | return path_error(path); |
| 3292 | Py_RETURN_NONE; |
| 3293 | } |
| 3294 | #endif /* HAVE_CHROOT */ |
| 3295 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3296 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3297 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3298 | /*[clinic input] |
| 3299 | os.fsync |
| 3300 | |
| 3301 | fd: fildes |
| 3302 | |
| 3303 | Force write of fd to disk. |
| 3304 | [clinic start generated code]*/ |
| 3305 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3306 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3307 | os_fsync_impl(PyObject *module, int fd) |
| 3308 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3309 | { |
| 3310 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3311 | } |
| 3312 | #endif /* HAVE_FSYNC */ |
| 3313 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3314 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3315 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3316 | /*[clinic input] |
| 3317 | os.sync |
| 3318 | |
| 3319 | Force write of everything to disk. |
| 3320 | [clinic start generated code]*/ |
| 3321 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3322 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3323 | os_sync_impl(PyObject *module) |
| 3324 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3325 | { |
| 3326 | Py_BEGIN_ALLOW_THREADS |
| 3327 | sync(); |
| 3328 | Py_END_ALLOW_THREADS |
| 3329 | Py_RETURN_NONE; |
| 3330 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3331 | #endif /* HAVE_SYNC */ |
| 3332 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3333 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3334 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3335 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3336 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3337 | #endif |
| 3338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3339 | /*[clinic input] |
| 3340 | os.fdatasync |
| 3341 | |
| 3342 | fd: fildes |
| 3343 | |
| 3344 | Force write of fd to disk without forcing update of metadata. |
| 3345 | [clinic start generated code]*/ |
| 3346 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3347 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3348 | os_fdatasync_impl(PyObject *module, int fd) |
| 3349 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3350 | { |
| 3351 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3352 | } |
| 3353 | #endif /* HAVE_FDATASYNC */ |
| 3354 | |
| 3355 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3356 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3357 | /*[clinic input] |
| 3358 | os.chown |
| 3359 | |
| 3360 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3361 | 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] | 3362 | |
| 3363 | uid: uid_t |
| 3364 | |
| 3365 | gid: gid_t |
| 3366 | |
| 3367 | * |
| 3368 | |
| 3369 | dir_fd : dir_fd(requires='fchownat') = None |
| 3370 | If not None, it should be a file descriptor open to a directory, |
| 3371 | and path should be relative; path will then be relative to that |
| 3372 | directory. |
| 3373 | |
| 3374 | follow_symlinks: bool = True |
| 3375 | If False, and the last element of the path is a symbolic link, |
| 3376 | stat will examine the symbolic link itself instead of the file |
| 3377 | the link points to. |
| 3378 | |
| 3379 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3380 | |
| 3381 | path may always be specified as a string. |
| 3382 | On some platforms, path may also be specified as an open file descriptor. |
| 3383 | If this functionality is unavailable, using it raises an exception. |
| 3384 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3385 | and path should be relative; path will then be relative to that directory. |
| 3386 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3387 | link, chown will modify the symbolic link itself instead of the file the |
| 3388 | link points to. |
| 3389 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3390 | an open file descriptor. |
| 3391 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3392 | If they are unavailable, using them will raise a NotImplementedError. |
| 3393 | |
| 3394 | [clinic start generated code]*/ |
| 3395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3396 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3397 | 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] | 3398 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3399 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3400 | { |
| 3401 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3402 | |
| 3403 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3404 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3405 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3406 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3407 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3408 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3409 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3410 | |
| 3411 | #ifdef __APPLE__ |
| 3412 | /* |
| 3413 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3414 | * (But we still have an lchown symbol because of weak-linking.) |
| 3415 | * It doesn't have fchownat either. So there's no possibility |
| 3416 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3417 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3418 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3419 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3420 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3421 | } |
| 3422 | #endif |
| 3423 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3424 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, |
| 3425 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3426 | return NULL; |
| 3427 | } |
| 3428 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3429 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3430 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3431 | if (path->fd != -1) |
| 3432 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3433 | else |
| 3434 | #endif |
| 3435 | #ifdef HAVE_LCHOWN |
| 3436 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3437 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3438 | else |
| 3439 | #endif |
| 3440 | #ifdef HAVE_FCHOWNAT |
| 3441 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3442 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3443 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3444 | else |
| 3445 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3446 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3447 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3448 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3449 | if (result) |
| 3450 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3451 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3452 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3453 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3454 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3456 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3457 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3458 | /*[clinic input] |
| 3459 | os.fchown |
| 3460 | |
| 3461 | fd: int |
| 3462 | uid: uid_t |
| 3463 | gid: gid_t |
| 3464 | |
| 3465 | Change the owner and group id of the file specified by file descriptor. |
| 3466 | |
| 3467 | Equivalent to os.chown(fd, uid, gid). |
| 3468 | |
| 3469 | [clinic start generated code]*/ |
| 3470 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3471 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3472 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3473 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3474 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3475 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3476 | int async_err = 0; |
| 3477 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3478 | if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) { |
| 3479 | return NULL; |
| 3480 | } |
| 3481 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3482 | do { |
| 3483 | Py_BEGIN_ALLOW_THREADS |
| 3484 | res = fchown(fd, uid, gid); |
| 3485 | Py_END_ALLOW_THREADS |
| 3486 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3487 | if (res != 0) |
| 3488 | return (!async_err) ? posix_error() : NULL; |
| 3489 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3490 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3491 | } |
| 3492 | #endif /* HAVE_FCHOWN */ |
| 3493 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3494 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3495 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3496 | /*[clinic input] |
| 3497 | os.lchown |
| 3498 | |
| 3499 | path : path_t |
| 3500 | uid: uid_t |
| 3501 | gid: gid_t |
| 3502 | |
| 3503 | Change the owner and group id of path to the numeric uid and gid. |
| 3504 | |
| 3505 | This function will not follow symbolic links. |
| 3506 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3507 | [clinic start generated code]*/ |
| 3508 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3509 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3510 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3511 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3512 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3513 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3514 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) { |
| 3515 | return NULL; |
| 3516 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3517 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3518 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3519 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3520 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3521 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3522 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3523 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3524 | } |
| 3525 | #endif /* HAVE_LCHOWN */ |
| 3526 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3527 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3528 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3529 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3530 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3531 | #ifdef MS_WINDOWS |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3532 | wchar_t wbuf[MAXPATHLEN]; |
| 3533 | wchar_t *wbuf2 = wbuf; |
| 3534 | DWORD len; |
| 3535 | |
| 3536 | Py_BEGIN_ALLOW_THREADS |
| 3537 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
| 3538 | /* If the buffer is large enough, len does not include the |
| 3539 | terminating \0. If the buffer is too small, len includes |
| 3540 | the space needed for the terminator. */ |
| 3541 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | ec3e20a | 2019-06-28 18:01:59 +0200 | [diff] [blame] | 3542 | if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3543 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3544 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3545 | else { |
| 3546 | wbuf2 = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3547 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3548 | if (wbuf2) { |
| 3549 | len = GetCurrentDirectoryW(len, wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3550 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3551 | } |
| 3552 | Py_END_ALLOW_THREADS |
| 3553 | |
| 3554 | if (!wbuf2) { |
| 3555 | PyErr_NoMemory(); |
| 3556 | return NULL; |
| 3557 | } |
| 3558 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3559 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3560 | PyMem_RawFree(wbuf2); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3561 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3562 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3563 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3564 | PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3565 | if (wbuf2 != wbuf) { |
| 3566 | PyMem_RawFree(wbuf2); |
| 3567 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3568 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3569 | if (use_bytes) { |
| 3570 | if (resobj == NULL) { |
| 3571 | return NULL; |
| 3572 | } |
| 3573 | Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj)); |
| 3574 | } |
| 3575 | |
| 3576 | return resobj; |
| 3577 | #else |
| 3578 | const size_t chunk = 1024; |
| 3579 | |
| 3580 | char *buf = NULL; |
| 3581 | char *cwd = NULL; |
| 3582 | size_t buflen = 0; |
| 3583 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3584 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3585 | do { |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3586 | char *newbuf; |
| 3587 | if (buflen <= PY_SSIZE_T_MAX - chunk) { |
| 3588 | buflen += chunk; |
| 3589 | newbuf = PyMem_RawRealloc(buf, buflen); |
| 3590 | } |
| 3591 | else { |
| 3592 | newbuf = NULL; |
| 3593 | } |
| 3594 | if (newbuf == NULL) { |
| 3595 | PyMem_RawFree(buf); |
| 3596 | buf = NULL; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3597 | break; |
| 3598 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3599 | buf = newbuf; |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3600 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3601 | cwd = getcwd(buf, buflen); |
| 3602 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3603 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3604 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3605 | if (buf == NULL) { |
| 3606 | return PyErr_NoMemory(); |
| 3607 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3608 | if (cwd == NULL) { |
| 3609 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3610 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3611 | } |
| 3612 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3613 | PyObject *obj; |
| 3614 | if (use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3615 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3616 | } |
| 3617 | else { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3618 | obj = PyUnicode_DecodeFSDefault(buf); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3619 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3620 | PyMem_RawFree(buf); |
| 3621 | |
| 3622 | return obj; |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3623 | #endif /* !MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3624 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3625 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3626 | |
| 3627 | /*[clinic input] |
| 3628 | os.getcwd |
| 3629 | |
| 3630 | Return a unicode string representing the current working directory. |
| 3631 | [clinic start generated code]*/ |
| 3632 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3633 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3634 | os_getcwd_impl(PyObject *module) |
| 3635 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3636 | { |
| 3637 | return posix_getcwd(0); |
| 3638 | } |
| 3639 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3640 | |
| 3641 | /*[clinic input] |
| 3642 | os.getcwdb |
| 3643 | |
| 3644 | Return a bytes string representing the current working directory. |
| 3645 | [clinic start generated code]*/ |
| 3646 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3647 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3648 | os_getcwdb_impl(PyObject *module) |
| 3649 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3650 | { |
| 3651 | return posix_getcwd(1); |
| 3652 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3653 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3654 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3655 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3656 | #define HAVE_LINK 1 |
| 3657 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3658 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3659 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3660 | /*[clinic input] |
| 3661 | |
| 3662 | os.link |
| 3663 | |
| 3664 | src : path_t |
| 3665 | dst : path_t |
| 3666 | * |
| 3667 | src_dir_fd : dir_fd = None |
| 3668 | dst_dir_fd : dir_fd = None |
| 3669 | follow_symlinks: bool = True |
| 3670 | |
| 3671 | Create a hard link to a file. |
| 3672 | |
| 3673 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3674 | descriptor open to a directory, and the respective path string (src or dst) |
| 3675 | should be relative; the path will then be relative to that directory. |
| 3676 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3677 | link, link will create a link to the symbolic link itself instead of the |
| 3678 | file the link points to. |
| 3679 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3680 | platform. If they are unavailable, using them will raise a |
| 3681 | NotImplementedError. |
| 3682 | [clinic start generated code]*/ |
| 3683 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3684 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3685 | 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] | 3686 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3687 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3688 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3689 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3690 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3691 | #else |
| 3692 | int result; |
| 3693 | #endif |
| 3694 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3695 | #ifndef HAVE_LINKAT |
| 3696 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3697 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3698 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3699 | } |
| 3700 | #endif |
| 3701 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3702 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3703 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3704 | PyErr_SetString(PyExc_NotImplementedError, |
| 3705 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3706 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3707 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3708 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3709 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3710 | if (PySys_Audit("os.link", "OOii", src->object, dst->object, |
| 3711 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 3712 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 3713 | return NULL; |
| 3714 | } |
| 3715 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3716 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3717 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3718 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3719 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3720 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3721 | if (!result) |
| 3722 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3723 | #else |
| 3724 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3725 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3726 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3727 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3728 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3729 | result = linkat(src_dir_fd, src->narrow, |
| 3730 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3731 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3732 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3733 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3734 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3735 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3736 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3737 | if (result) |
| 3738 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3739 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3740 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3741 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3742 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3743 | #endif |
| 3744 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3745 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3746 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3747 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3748 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3749 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3750 | PyObject *v; |
| 3751 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3752 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3753 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3754 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3755 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3756 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3757 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3758 | WIN32_FIND_DATAW wFileData; |
| 3759 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3760 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3761 | if (!path->wide) { /* Default arg: "." */ |
| 3762 | po_wchars = L"."; |
| 3763 | len = 1; |
| 3764 | } else { |
| 3765 | po_wchars = path->wide; |
| 3766 | len = wcslen(path->wide); |
| 3767 | } |
| 3768 | /* The +5 is so we can append "\\*.*\0" */ |
| 3769 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3770 | if (!wnamebuf) { |
| 3771 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3772 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3773 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3774 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3775 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3776 | wchar_t wch = wnamebuf[len-1]; |
| 3777 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3778 | wnamebuf[len++] = SEP; |
| 3779 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3780 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3781 | if ((list = PyList_New(0)) == NULL) { |
| 3782 | goto exit; |
| 3783 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3784 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3785 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3786 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3787 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3788 | int error = GetLastError(); |
| 3789 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3790 | goto exit; |
| 3791 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3792 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3793 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3794 | } |
| 3795 | do { |
| 3796 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3797 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3798 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3799 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3800 | wcslen(wFileData.cFileName)); |
| 3801 | if (path->narrow && v) { |
| 3802 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3803 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3804 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3805 | Py_DECREF(list); |
| 3806 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3807 | break; |
| 3808 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3809 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3810 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3811 | Py_DECREF(list); |
| 3812 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3813 | break; |
| 3814 | } |
| 3815 | Py_DECREF(v); |
| 3816 | } |
| 3817 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3818 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3819 | Py_END_ALLOW_THREADS |
| 3820 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3821 | it got to the end of the directory. */ |
| 3822 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3823 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3824 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3825 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3826 | } |
| 3827 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3828 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3829 | exit: |
| 3830 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3831 | if (FindClose(hFindFile) == FALSE) { |
| 3832 | if (list != NULL) { |
| 3833 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3834 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3835 | } |
| 3836 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3837 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3838 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3839 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3840 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3841 | } /* end of _listdir_windows_no_opendir */ |
| 3842 | |
| 3843 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3844 | |
| 3845 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3846 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3847 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3848 | PyObject *v; |
| 3849 | DIR *dirp = NULL; |
| 3850 | struct dirent *ep; |
| 3851 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3852 | #ifdef HAVE_FDOPENDIR |
| 3853 | int fd = -1; |
| 3854 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3855 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3856 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3857 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3858 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3859 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3860 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3861 | if (fd == -1) |
| 3862 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3863 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3864 | return_str = 1; |
| 3865 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3866 | Py_BEGIN_ALLOW_THREADS |
| 3867 | dirp = fdopendir(fd); |
| 3868 | Py_END_ALLOW_THREADS |
| 3869 | } |
| 3870 | else |
| 3871 | #endif |
| 3872 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3873 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3874 | if (path->narrow) { |
| 3875 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3876 | /* only return bytes if they specified a bytes-like object */ |
| 3877 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3878 | } |
| 3879 | else { |
| 3880 | name = "."; |
| 3881 | return_str = 1; |
| 3882 | } |
| 3883 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3884 | Py_BEGIN_ALLOW_THREADS |
| 3885 | dirp = opendir(name); |
| 3886 | Py_END_ALLOW_THREADS |
| 3887 | } |
| 3888 | |
| 3889 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3890 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3891 | #ifdef HAVE_FDOPENDIR |
| 3892 | if (fd != -1) { |
| 3893 | Py_BEGIN_ALLOW_THREADS |
| 3894 | close(fd); |
| 3895 | Py_END_ALLOW_THREADS |
| 3896 | } |
| 3897 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3898 | goto exit; |
| 3899 | } |
| 3900 | if ((list = PyList_New(0)) == NULL) { |
| 3901 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3902 | } |
| 3903 | for (;;) { |
| 3904 | errno = 0; |
| 3905 | Py_BEGIN_ALLOW_THREADS |
| 3906 | ep = readdir(dirp); |
| 3907 | Py_END_ALLOW_THREADS |
| 3908 | if (ep == NULL) { |
| 3909 | if (errno == 0) { |
| 3910 | break; |
| 3911 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3912 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3913 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3914 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3915 | } |
| 3916 | } |
| 3917 | if (ep->d_name[0] == '.' && |
| 3918 | (NAMLEN(ep) == 1 || |
| 3919 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3920 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3921 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3922 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3923 | else |
| 3924 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3925 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3926 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3927 | break; |
| 3928 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3929 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3930 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3931 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3932 | break; |
| 3933 | } |
| 3934 | Py_DECREF(v); |
| 3935 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3936 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3937 | exit: |
| 3938 | if (dirp != NULL) { |
| 3939 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3940 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3941 | if (fd > -1) |
| 3942 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3943 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3944 | closedir(dirp); |
| 3945 | Py_END_ALLOW_THREADS |
| 3946 | } |
| 3947 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3948 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3949 | } /* end of _posix_listdir */ |
| 3950 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3951 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3952 | |
| 3953 | /*[clinic input] |
| 3954 | os.listdir |
| 3955 | |
| 3956 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3957 | |
| 3958 | Return a list containing the names of the files in the directory. |
| 3959 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3960 | 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] | 3961 | the filenames returned will also be bytes; in all other circumstances |
| 3962 | the filenames returned will be str. |
| 3963 | If path is None, uses the path='.'. |
| 3964 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3965 | the file descriptor must refer to a directory. |
| 3966 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3967 | |
| 3968 | The list is in arbitrary order. It does not include the special |
| 3969 | entries '.' and '..' even if they are present in the directory. |
| 3970 | |
| 3971 | |
| 3972 | [clinic start generated code]*/ |
| 3973 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3974 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3975 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3976 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3977 | { |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 3978 | if (PySys_Audit("os.listdir", "O", |
| 3979 | path->object ? path->object : Py_None) < 0) { |
| 3980 | return NULL; |
| 3981 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3982 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3983 | return _listdir_windows_no_opendir(path, NULL); |
| 3984 | #else |
| 3985 | return _posix_listdir(path, NULL); |
| 3986 | #endif |
| 3987 | } |
| 3988 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3989 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3990 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3991 | /*[clinic input] |
| 3992 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3993 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3994 | path: path_t |
| 3995 | / |
| 3996 | |
| 3997 | [clinic start generated code]*/ |
| 3998 | |
| 3999 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4000 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 4001 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 4002 | { |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4003 | wchar_t *abspath; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4004 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4005 | /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */ |
| 4006 | if (_Py_abspath(path->wide, &abspath) < 0) { |
| 4007 | return win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4008 | } |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4009 | if (abspath == NULL) { |
| 4010 | return PyErr_NoMemory(); |
| 4011 | } |
| 4012 | |
| 4013 | PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath)); |
| 4014 | PyMem_RawFree(abspath); |
| 4015 | if (str == NULL) { |
| 4016 | return NULL; |
| 4017 | } |
| 4018 | if (path->narrow) { |
| 4019 | Py_SETREF(str, PyUnicode_EncodeFSDefault(str)); |
| 4020 | } |
| 4021 | return str; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4022 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4023 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 4024 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4025 | /*[clinic input] |
| 4026 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 4027 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4028 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4029 | / |
| 4030 | |
| 4031 | A helper function for samepath on windows. |
| 4032 | [clinic start generated code]*/ |
| 4033 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4034 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4035 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 4036 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4037 | { |
| 4038 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4039 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 4040 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4041 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4042 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4043 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4044 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4045 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4046 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4047 | 0, /* desired access */ |
| 4048 | 0, /* share mode */ |
| 4049 | NULL, /* security attributes */ |
| 4050 | OPEN_EXISTING, |
| 4051 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 4052 | FILE_FLAG_BACKUP_SEMANTICS, |
| 4053 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4054 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4055 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4056 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4057 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4058 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4059 | |
| 4060 | /* We have a good handle to the target, use it to determine the |
| 4061 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4062 | while (1) { |
| 4063 | Py_BEGIN_ALLOW_THREADS |
| 4064 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 4065 | buf_size, VOLUME_NAME_DOS); |
| 4066 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4067 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4068 | if (!result_length) { |
| 4069 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 4070 | path->object); |
| 4071 | goto cleanup; |
| 4072 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4073 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4074 | if (result_length < buf_size) { |
| 4075 | break; |
| 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 | wchar_t *tmp; |
| 4079 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 4080 | result_length * sizeof(*tmp)); |
| 4081 | if (!tmp) { |
| 4082 | result = PyErr_NoMemory(); |
| 4083 | goto cleanup; |
| 4084 | } |
| 4085 | |
| 4086 | buf_size = result_length; |
| 4087 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4088 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4089 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4090 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4091 | if (result && path->narrow) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4092 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4093 | } |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4094 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4095 | cleanup: |
| 4096 | if (target_path != buf) { |
| 4097 | PyMem_Free(target_path); |
| 4098 | } |
| 4099 | CloseHandle(hFile); |
| 4100 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4101 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 4102 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4103 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4104 | /*[clinic input] |
| 4105 | os._getvolumepathname |
| 4106 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4107 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4108 | |
| 4109 | A helper function for ismount on Win32. |
| 4110 | [clinic start generated code]*/ |
| 4111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4112 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4113 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 4114 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4115 | { |
| 4116 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4117 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4118 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4119 | BOOL ret; |
| 4120 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4121 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4122 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4123 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 4124 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4125 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4126 | return NULL; |
| 4127 | } |
| 4128 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4129 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4130 | if (mountpath == NULL) |
| 4131 | return PyErr_NoMemory(); |
| 4132 | |
| 4133 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4134 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4135 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4136 | Py_END_ALLOW_THREADS |
| 4137 | |
| 4138 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4139 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4140 | goto exit; |
| 4141 | } |
| 4142 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4143 | if (path->narrow) |
| 4144 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4145 | |
| 4146 | exit: |
| 4147 | PyMem_Free(mountpath); |
| 4148 | return result; |
| 4149 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4150 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4151 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4152 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4153 | |
| 4154 | /*[clinic input] |
| 4155 | os.mkdir |
| 4156 | |
| 4157 | path : path_t |
| 4158 | |
| 4159 | mode: int = 0o777 |
| 4160 | |
| 4161 | * |
| 4162 | |
| 4163 | dir_fd : dir_fd(requires='mkdirat') = None |
| 4164 | |
| 4165 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4166 | |
| 4167 | Create a directory. |
| 4168 | |
| 4169 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4170 | and path should be relative; path will then be relative to that directory. |
| 4171 | dir_fd may not be implemented on your platform. |
| 4172 | If it is unavailable, using it will raise a NotImplementedError. |
| 4173 | |
| 4174 | The mode argument is ignored on Windows. |
| 4175 | [clinic start generated code]*/ |
| 4176 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4177 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4178 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 4179 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4180 | { |
| 4181 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4182 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4183 | if (PySys_Audit("os.mkdir", "Oii", path->object, mode, |
| 4184 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4185 | return NULL; |
| 4186 | } |
| 4187 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4188 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4189 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4190 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4191 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4192 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4193 | if (!result) |
| 4194 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4195 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4196 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4197 | #if HAVE_MKDIRAT |
| 4198 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4199 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4200 | else |
| 4201 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4202 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4203 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4204 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4205 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4206 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4207 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4208 | if (result < 0) |
| 4209 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4210 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4211 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4214 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4215 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4216 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4217 | #include <sys/resource.h> |
| 4218 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4219 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4220 | |
| 4221 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4222 | /*[clinic input] |
| 4223 | os.nice |
| 4224 | |
| 4225 | increment: int |
| 4226 | / |
| 4227 | |
| 4228 | Add increment to the priority of process and return the new priority. |
| 4229 | [clinic start generated code]*/ |
| 4230 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4231 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4232 | os_nice_impl(PyObject *module, int increment) |
| 4233 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4234 | { |
| 4235 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4236 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4237 | /* There are two flavours of 'nice': one that returns the new |
| 4238 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4239 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4240 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4241 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4242 | If we are of the nice family that returns the new priority, we |
| 4243 | need to clear errno before the call, and check if errno is filled |
| 4244 | before calling posix_error() on a returnvalue of -1, because the |
| 4245 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4246 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4247 | errno = 0; |
| 4248 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4249 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4250 | if (value == 0) |
| 4251 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4252 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4253 | if (value == -1 && errno != 0) |
| 4254 | /* either nice() or getpriority() returned an error */ |
| 4255 | return posix_error(); |
| 4256 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4257 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4258 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4259 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4260 | |
| 4261 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4262 | /*[clinic input] |
| 4263 | os.getpriority |
| 4264 | |
| 4265 | which: int |
| 4266 | who: int |
| 4267 | |
| 4268 | Return program scheduling priority. |
| 4269 | [clinic start generated code]*/ |
| 4270 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4271 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4272 | os_getpriority_impl(PyObject *module, int which, int who) |
| 4273 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4274 | { |
| 4275 | int retval; |
| 4276 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4277 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4278 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4279 | if (errno != 0) |
| 4280 | return posix_error(); |
| 4281 | return PyLong_FromLong((long)retval); |
| 4282 | } |
| 4283 | #endif /* HAVE_GETPRIORITY */ |
| 4284 | |
| 4285 | |
| 4286 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4287 | /*[clinic input] |
| 4288 | os.setpriority |
| 4289 | |
| 4290 | which: int |
| 4291 | who: int |
| 4292 | priority: int |
| 4293 | |
| 4294 | Set program scheduling priority. |
| 4295 | [clinic start generated code]*/ |
| 4296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4297 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4298 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4299 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4300 | { |
| 4301 | int retval; |
| 4302 | |
| 4303 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4304 | if (retval == -1) |
| 4305 | return posix_error(); |
| 4306 | Py_RETURN_NONE; |
| 4307 | } |
| 4308 | #endif /* HAVE_SETPRIORITY */ |
| 4309 | |
| 4310 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4311 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4312 | 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] | 4313 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4314 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4315 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4316 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4317 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4318 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4319 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4320 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4321 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4322 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4323 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4324 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4325 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4326 | #ifndef HAVE_RENAMEAT |
| 4327 | if (dir_fd_specified) { |
| 4328 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4329 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4330 | } |
| 4331 | #endif |
| 4332 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4333 | if (PySys_Audit("os.rename", "OOii", src->object, dst->object, |
| 4334 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 4335 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 4336 | return NULL; |
| 4337 | } |
| 4338 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4339 | #ifdef MS_WINDOWS |
| 4340 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4341 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4342 | Py_END_ALLOW_THREADS |
| 4343 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4344 | if (!result) |
| 4345 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4346 | |
| 4347 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4348 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4349 | PyErr_Format(PyExc_ValueError, |
| 4350 | "%s: src and dst must be the same type", function_name); |
| 4351 | return NULL; |
| 4352 | } |
| 4353 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4354 | Py_BEGIN_ALLOW_THREADS |
| 4355 | #ifdef HAVE_RENAMEAT |
| 4356 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4357 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4358 | else |
| 4359 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4360 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4361 | Py_END_ALLOW_THREADS |
| 4362 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4363 | if (result) |
| 4364 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4365 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4366 | Py_RETURN_NONE; |
| 4367 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4369 | |
| 4370 | /*[clinic input] |
| 4371 | os.rename |
| 4372 | |
| 4373 | src : path_t |
| 4374 | dst : path_t |
| 4375 | * |
| 4376 | src_dir_fd : dir_fd = None |
| 4377 | dst_dir_fd : dir_fd = None |
| 4378 | |
| 4379 | Rename a file or directory. |
| 4380 | |
| 4381 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4382 | descriptor open to a directory, and the respective path string (src or dst) |
| 4383 | should be relative; the path will then be relative to that directory. |
| 4384 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4385 | If they are unavailable, using them will raise a NotImplementedError. |
| 4386 | [clinic start generated code]*/ |
| 4387 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4388 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4389 | 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] | 4390 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4391 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4392 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4393 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4394 | } |
| 4395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4396 | |
| 4397 | /*[clinic input] |
| 4398 | os.replace = os.rename |
| 4399 | |
| 4400 | Rename a file or directory, overwriting the destination. |
| 4401 | |
| 4402 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4403 | descriptor open to a directory, and the respective path string (src or dst) |
| 4404 | should be relative; the path will then be relative to that directory. |
| 4405 | 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] | 4406 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4407 | [clinic start generated code]*/ |
| 4408 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4409 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4410 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4411 | int dst_dir_fd) |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4412 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4413 | { |
| 4414 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4415 | } |
| 4416 | |
| 4417 | |
| 4418 | /*[clinic input] |
| 4419 | os.rmdir |
| 4420 | |
| 4421 | path: path_t |
| 4422 | * |
| 4423 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4424 | |
| 4425 | Remove a directory. |
| 4426 | |
| 4427 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4428 | and path should be relative; path will then be relative to that directory. |
| 4429 | dir_fd may not be implemented on your platform. |
| 4430 | If it is unavailable, using it will raise a NotImplementedError. |
| 4431 | [clinic start generated code]*/ |
| 4432 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4433 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4434 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4435 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4436 | { |
| 4437 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4438 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4439 | if (PySys_Audit("os.rmdir", "Oi", path->object, |
| 4440 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4441 | return NULL; |
| 4442 | } |
| 4443 | |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4444 | Py_BEGIN_ALLOW_THREADS |
| 4445 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4446 | /* Windows, success=1, UNIX, success=0 */ |
| 4447 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4448 | #else |
| 4449 | #ifdef HAVE_UNLINKAT |
| 4450 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4451 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4452 | else |
| 4453 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4454 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4455 | #endif |
| 4456 | Py_END_ALLOW_THREADS |
| 4457 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4458 | if (result) |
| 4459 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4461 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4464 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4465 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4466 | #ifdef MS_WINDOWS |
| 4467 | /*[clinic input] |
| 4468 | os.system -> long |
| 4469 | |
| 4470 | command: Py_UNICODE |
| 4471 | |
| 4472 | Execute the command in a subshell. |
| 4473 | [clinic start generated code]*/ |
| 4474 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4475 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4476 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4477 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4478 | { |
| 4479 | long result; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4480 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4481 | if (PySys_Audit("os.system", "(u)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4482 | return -1; |
| 4483 | } |
| 4484 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4485 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4486 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4487 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4488 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4489 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4490 | return result; |
| 4491 | } |
| 4492 | #else /* MS_WINDOWS */ |
| 4493 | /*[clinic input] |
| 4494 | os.system -> long |
| 4495 | |
| 4496 | command: FSConverter |
| 4497 | |
| 4498 | Execute the command in a subshell. |
| 4499 | [clinic start generated code]*/ |
| 4500 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4501 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4502 | os_system_impl(PyObject *module, PyObject *command) |
| 4503 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4504 | { |
| 4505 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4506 | const char *bytes = PyBytes_AsString(command); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4507 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4508 | if (PySys_Audit("os.system", "(O)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4509 | return -1; |
| 4510 | } |
| 4511 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4512 | Py_BEGIN_ALLOW_THREADS |
| 4513 | result = system(bytes); |
| 4514 | Py_END_ALLOW_THREADS |
| 4515 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4516 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4517 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4518 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4519 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4520 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4521 | /*[clinic input] |
| 4522 | os.umask |
| 4523 | |
| 4524 | mask: int |
| 4525 | / |
| 4526 | |
| 4527 | Set the current numeric umask and return the previous umask. |
| 4528 | [clinic start generated code]*/ |
| 4529 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4530 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4531 | os_umask_impl(PyObject *module, int mask) |
| 4532 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4533 | { |
| 4534 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4535 | if (i < 0) |
| 4536 | return posix_error(); |
| 4537 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4540 | #ifdef MS_WINDOWS |
| 4541 | |
| 4542 | /* override the default DeleteFileW behavior so that directory |
| 4543 | symlinks can be removed with this function, the same as with |
| 4544 | Unix symlinks */ |
| 4545 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4546 | { |
| 4547 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4548 | WIN32_FIND_DATAW find_data; |
| 4549 | HANDLE find_data_handle; |
| 4550 | int is_directory = 0; |
| 4551 | int is_link = 0; |
| 4552 | |
| 4553 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4554 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4555 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4556 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4557 | it is a symlink */ |
| 4558 | if(is_directory && |
| 4559 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4560 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4561 | |
| 4562 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4563 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4564 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4565 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4566 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4567 | FindClose(find_data_handle); |
| 4568 | } |
| 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | if (is_directory && is_link) |
| 4573 | return RemoveDirectoryW(lpFileName); |
| 4574 | |
| 4575 | return DeleteFileW(lpFileName); |
| 4576 | } |
| 4577 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4578 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4579 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4580 | /*[clinic input] |
| 4581 | os.unlink |
| 4582 | |
| 4583 | path: path_t |
| 4584 | * |
| 4585 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4586 | |
| 4587 | Remove a file (same as remove()). |
| 4588 | |
| 4589 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4590 | and path should be relative; path will then be relative to that directory. |
| 4591 | dir_fd may not be implemented on your platform. |
| 4592 | If it is unavailable, using it will raise a NotImplementedError. |
| 4593 | |
| 4594 | [clinic start generated code]*/ |
| 4595 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4596 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4597 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4598 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4599 | { |
| 4600 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4601 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4602 | if (PySys_Audit("os.remove", "Oi", path->object, |
| 4603 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4604 | return NULL; |
| 4605 | } |
| 4606 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4607 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4608 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4609 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4610 | /* Windows, success=1, UNIX, success=0 */ |
| 4611 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4612 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4613 | #ifdef HAVE_UNLINKAT |
| 4614 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4615 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4616 | else |
| 4617 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4618 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4619 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4620 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4621 | Py_END_ALLOW_THREADS |
| 4622 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4623 | if (result) |
| 4624 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4625 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4626 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4627 | } |
| 4628 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4629 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4630 | /*[clinic input] |
| 4631 | os.remove = os.unlink |
| 4632 | |
| 4633 | Remove a file (same as unlink()). |
| 4634 | |
| 4635 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4636 | and path should be relative; path will then be relative to that directory. |
| 4637 | dir_fd may not be implemented on your platform. |
| 4638 | If it is unavailable, using it will raise a NotImplementedError. |
| 4639 | [clinic start generated code]*/ |
| 4640 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4641 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4642 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4643 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4644 | { |
| 4645 | return os_unlink_impl(module, path, dir_fd); |
| 4646 | } |
| 4647 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4648 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4649 | static PyStructSequence_Field uname_result_fields[] = { |
| 4650 | {"sysname", "operating system name"}, |
| 4651 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4652 | {"release", "operating system release"}, |
| 4653 | {"version", "operating system version"}, |
| 4654 | {"machine", "hardware identifier"}, |
| 4655 | {NULL} |
| 4656 | }; |
| 4657 | |
| 4658 | PyDoc_STRVAR(uname_result__doc__, |
| 4659 | "uname_result: Result from os.uname().\n\n\ |
| 4660 | This object may be accessed either as a tuple of\n\ |
| 4661 | (sysname, nodename, release, version, machine),\n\ |
| 4662 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4663 | \n\ |
| 4664 | See os.uname for more information."); |
| 4665 | |
| 4666 | static PyStructSequence_Desc uname_result_desc = { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4667 | MODNAME ".uname_result", /* name */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4668 | uname_result__doc__, /* doc */ |
| 4669 | uname_result_fields, |
| 4670 | 5 |
| 4671 | }; |
| 4672 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4673 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4674 | /*[clinic input] |
| 4675 | os.uname |
| 4676 | |
| 4677 | Return an object identifying the current operating system. |
| 4678 | |
| 4679 | The object behaves like a named tuple with the following fields: |
| 4680 | (sysname, nodename, release, version, machine) |
| 4681 | |
| 4682 | [clinic start generated code]*/ |
| 4683 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4684 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4685 | os_uname_impl(PyObject *module) |
| 4686 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4687 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4688 | struct utsname u; |
| 4689 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4690 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4691 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4692 | Py_BEGIN_ALLOW_THREADS |
| 4693 | res = uname(&u); |
| 4694 | Py_END_ALLOW_THREADS |
| 4695 | if (res < 0) |
| 4696 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4697 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 4698 | PyObject *UnameResultType = get_posix_state(module)->UnameResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4699 | value = PyStructSequence_New((PyTypeObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4700 | if (value == NULL) |
| 4701 | return NULL; |
| 4702 | |
| 4703 | #define SET(i, field) \ |
| 4704 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4705 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4706 | if (!o) { \ |
| 4707 | Py_DECREF(value); \ |
| 4708 | return NULL; \ |
| 4709 | } \ |
| 4710 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4711 | } \ |
| 4712 | |
| 4713 | SET(0, u.sysname); |
| 4714 | SET(1, u.nodename); |
| 4715 | SET(2, u.release); |
| 4716 | SET(3, u.version); |
| 4717 | SET(4, u.machine); |
| 4718 | |
| 4719 | #undef SET |
| 4720 | |
| 4721 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4722 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4723 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4724 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4725 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4726 | |
| 4727 | typedef struct { |
| 4728 | int now; |
| 4729 | time_t atime_s; |
| 4730 | long atime_ns; |
| 4731 | time_t mtime_s; |
| 4732 | long mtime_ns; |
| 4733 | } utime_t; |
| 4734 | |
| 4735 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4736 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4737 | * they also intentionally leak the declaration of a pointer named "time" |
| 4738 | */ |
| 4739 | #define UTIME_TO_TIMESPEC \ |
| 4740 | struct timespec ts[2]; \ |
| 4741 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4742 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4743 | time = NULL; \ |
| 4744 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4745 | ts[0].tv_sec = ut->atime_s; \ |
| 4746 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4747 | ts[1].tv_sec = ut->mtime_s; \ |
| 4748 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4749 | time = ts; \ |
| 4750 | } \ |
| 4751 | |
| 4752 | #define UTIME_TO_TIMEVAL \ |
| 4753 | struct timeval tv[2]; \ |
| 4754 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4755 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4756 | time = NULL; \ |
| 4757 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4758 | tv[0].tv_sec = ut->atime_s; \ |
| 4759 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4760 | tv[1].tv_sec = ut->mtime_s; \ |
| 4761 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4762 | time = tv; \ |
| 4763 | } \ |
| 4764 | |
| 4765 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4766 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4767 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4768 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4769 | time = NULL; \ |
| 4770 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4771 | u.actime = ut->atime_s; \ |
| 4772 | u.modtime = ut->mtime_s; \ |
| 4773 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | #define UTIME_TO_TIME_T \ |
| 4777 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4778 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4779 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4780 | time = NULL; \ |
| 4781 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4782 | timet[0] = ut->atime_s; \ |
| 4783 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4784 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4785 | } \ |
| 4786 | |
| 4787 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4788 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4789 | |
| 4790 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4791 | 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] | 4792 | { |
| 4793 | #ifdef HAVE_UTIMENSAT |
| 4794 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4795 | UTIME_TO_TIMESPEC; |
| 4796 | return utimensat(dir_fd, path, time, flags); |
| 4797 | #elif defined(HAVE_FUTIMESAT) |
| 4798 | UTIME_TO_TIMEVAL; |
| 4799 | /* |
| 4800 | * follow_symlinks will never be false here; |
| 4801 | * we only allow !follow_symlinks and dir_fd together |
| 4802 | * if we have utimensat() |
| 4803 | */ |
| 4804 | assert(follow_symlinks); |
| 4805 | return futimesat(dir_fd, path, time); |
| 4806 | #endif |
| 4807 | } |
| 4808 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4809 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4810 | #else |
| 4811 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4812 | #endif |
| 4813 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4814 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4815 | |
| 4816 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4817 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4818 | { |
| 4819 | #ifdef HAVE_FUTIMENS |
| 4820 | UTIME_TO_TIMESPEC; |
| 4821 | return futimens(fd, time); |
| 4822 | #else |
| 4823 | UTIME_TO_TIMEVAL; |
| 4824 | return futimes(fd, time); |
| 4825 | #endif |
| 4826 | } |
| 4827 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4828 | #define PATH_UTIME_HAVE_FD 1 |
| 4829 | #else |
| 4830 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4831 | #endif |
| 4832 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4833 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4834 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4835 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4836 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4837 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4838 | |
| 4839 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4840 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4841 | { |
| 4842 | #ifdef HAVE_UTIMENSAT |
| 4843 | UTIME_TO_TIMESPEC; |
| 4844 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4845 | #else |
| 4846 | UTIME_TO_TIMEVAL; |
| 4847 | return lutimes(path, time); |
| 4848 | #endif |
| 4849 | } |
| 4850 | |
| 4851 | #endif |
| 4852 | |
| 4853 | #ifndef MS_WINDOWS |
| 4854 | |
| 4855 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4856 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4857 | { |
| 4858 | #ifdef HAVE_UTIMENSAT |
| 4859 | UTIME_TO_TIMESPEC; |
| 4860 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4861 | #elif defined(HAVE_UTIMES) |
| 4862 | UTIME_TO_TIMEVAL; |
| 4863 | return utimes(path, time); |
| 4864 | #elif defined(HAVE_UTIME_H) |
| 4865 | UTIME_TO_UTIMBUF; |
| 4866 | return utime(path, time); |
| 4867 | #else |
| 4868 | UTIME_TO_TIME_T; |
| 4869 | return utime(path, time); |
| 4870 | #endif |
| 4871 | } |
| 4872 | |
| 4873 | #endif |
| 4874 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4875 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4876 | 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] | 4877 | { |
| 4878 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4879 | PyObject *divmod; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4880 | divmod = PyNumber_Divmod(py_long, get_posix_state(module)->billion); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4881 | if (!divmod) |
| 4882 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4883 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4884 | PyErr_Format(PyExc_TypeError, |
| 4885 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4886 | _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod))); |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4887 | goto exit; |
| 4888 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4889 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4890 | if ((*s == -1) && PyErr_Occurred()) |
| 4891 | goto exit; |
| 4892 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4893 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4894 | goto exit; |
| 4895 | |
| 4896 | result = 1; |
| 4897 | exit: |
| 4898 | Py_XDECREF(divmod); |
| 4899 | return result; |
| 4900 | } |
| 4901 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4902 | |
| 4903 | /*[clinic input] |
| 4904 | os.utime |
| 4905 | |
| 4906 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4907 | times: object = None |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4908 | * |
| 4909 | ns: object = NULL |
| 4910 | dir_fd: dir_fd(requires='futimensat') = None |
| 4911 | follow_symlinks: bool=True |
| 4912 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4913 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4914 | |
| 4915 | Set the access and modified time of path. |
| 4916 | |
| 4917 | path may always be specified as a string. |
| 4918 | On some platforms, path may also be specified as an open file descriptor. |
| 4919 | If this functionality is unavailable, using it raises an exception. |
| 4920 | |
| 4921 | If times is not None, it must be a tuple (atime, mtime); |
| 4922 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4923 | 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] | 4924 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4925 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4926 | 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] | 4927 | Specifying tuples for both times and ns is an error. |
| 4928 | |
| 4929 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4930 | and path should be relative; path will then be relative to that directory. |
| 4931 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4932 | link, utime will modify the symbolic link itself instead of the file the |
| 4933 | link points to. |
| 4934 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4935 | as an open file descriptor. |
| 4936 | dir_fd and follow_symlinks may not be available on your platform. |
| 4937 | If they are unavailable, using them will raise a NotImplementedError. |
| 4938 | |
| 4939 | [clinic start generated code]*/ |
| 4940 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4941 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4942 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4943 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4944 | /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4945 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4946 | #ifdef MS_WINDOWS |
| 4947 | HANDLE hFile; |
| 4948 | FILETIME atime, mtime; |
| 4949 | #else |
| 4950 | int result; |
| 4951 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4952 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4953 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4954 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4955 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4956 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4957 | if (times != Py_None && ns) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4958 | PyErr_SetString(PyExc_ValueError, |
| 4959 | "utime: you may specify either 'times'" |
| 4960 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4961 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4962 | } |
| 4963 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4964 | if (times != Py_None) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4965 | time_t a_sec, m_sec; |
| 4966 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4967 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4968 | PyErr_SetString(PyExc_TypeError, |
| 4969 | "utime: 'times' must be either" |
| 4970 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4971 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4972 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4973 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4974 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4975 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4976 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4977 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4978 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4979 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4980 | utime.atime_s = a_sec; |
| 4981 | utime.atime_ns = a_nsec; |
| 4982 | utime.mtime_s = m_sec; |
| 4983 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4984 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4985 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4986 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4987 | PyErr_SetString(PyExc_TypeError, |
| 4988 | "utime: 'ns' must be a tuple of two ints"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4989 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4990 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4991 | utime.now = 0; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4992 | 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] | 4993 | &utime.atime_s, &utime.atime_ns) || |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4994 | !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] | 4995 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4996 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4997 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4998 | } |
| 4999 | else { |
| 5000 | /* times and ns are both None/unspecified. use "now". */ |
| 5001 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5002 | } |
| 5003 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 5004 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5005 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5006 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5007 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 5008 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5009 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 5010 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 5011 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5012 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5013 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5014 | #if !defined(HAVE_UTIMENSAT) |
| 5015 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 5016 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5017 | "utime: cannot use dir_fd and follow_symlinks " |
| 5018 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5019 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5020 | } |
| 5021 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5022 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5023 | if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None, |
| 5024 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 5025 | return NULL; |
| 5026 | } |
| 5027 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5028 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5029 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5030 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 5031 | NULL, OPEN_EXISTING, |
| 5032 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5033 | Py_END_ALLOW_THREADS |
| 5034 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5035 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5036 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 5037 | } |
| 5038 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5039 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 5040 | GetSystemTimeAsFileTime(&mtime); |
| 5041 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5042 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5043 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 5044 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 5045 | _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] | 5046 | } |
| 5047 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 5048 | /* Avoid putting the file name into the error here, |
| 5049 | as that may confuse the user into believing that |
| 5050 | something is wrong with the file, when it also |
| 5051 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 5052 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5053 | CloseHandle(hFile); |
| 5054 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5055 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5056 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5057 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5058 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 5059 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 5060 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5061 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5062 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5063 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 5064 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5065 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5066 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5067 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5068 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5069 | else |
| 5070 | #endif |
| 5071 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5072 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5073 | if (path->fd != -1) |
| 5074 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5075 | else |
| 5076 | #endif |
| 5077 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5078 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5079 | |
| 5080 | Py_END_ALLOW_THREADS |
| 5081 | |
| 5082 | if (result < 0) { |
| 5083 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5084 | posix_error(); |
| 5085 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5086 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5087 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5088 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5089 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5090 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5091 | } |
| 5092 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5093 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5094 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5095 | |
| 5096 | /*[clinic input] |
| 5097 | os._exit |
| 5098 | |
| 5099 | status: int |
| 5100 | |
| 5101 | Exit to the system with specified status, without normal exit processing. |
| 5102 | [clinic start generated code]*/ |
| 5103 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5104 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5105 | os__exit_impl(PyObject *module, int status) |
| 5106 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5107 | { |
| 5108 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5109 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5110 | } |
| 5111 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5112 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5113 | #define EXECV_CHAR wchar_t |
| 5114 | #else |
| 5115 | #define EXECV_CHAR char |
| 5116 | #endif |
| 5117 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5118 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5119 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5120 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5121 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5122 | Py_ssize_t i; |
| 5123 | for (i = 0; i < count; i++) |
| 5124 | PyMem_Free(array[i]); |
| 5125 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5126 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5127 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5128 | static int |
| 5129 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5130 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5131 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5132 | PyObject *ub; |
| 5133 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5134 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5135 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5136 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5137 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 5138 | if (*out) |
| 5139 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5140 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5141 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5142 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5143 | size = PyBytes_GET_SIZE(ub); |
| 5144 | *out = PyMem_Malloc(size + 1); |
| 5145 | if (*out) { |
| 5146 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 5147 | result = 1; |
| 5148 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5149 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5150 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5151 | Py_DECREF(ub); |
| 5152 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5153 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5154 | #endif |
| 5155 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5156 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5157 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5158 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5159 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5160 | Py_ssize_t i, pos, envc; |
| 5161 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5162 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5163 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5164 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5165 | i = PyMapping_Size(env); |
| 5166 | if (i < 0) |
| 5167 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5168 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5169 | if (envlist == NULL) { |
| 5170 | PyErr_NoMemory(); |
| 5171 | return NULL; |
| 5172 | } |
| 5173 | envc = 0; |
| 5174 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5175 | if (!keys) |
| 5176 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5177 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5178 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5179 | goto error; |
| 5180 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5181 | PyErr_Format(PyExc_TypeError, |
| 5182 | "env.keys() or env.values() is not a list"); |
| 5183 | goto error; |
| 5184 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5185 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5186 | for (pos = 0; pos < i; pos++) { |
| 5187 | key = PyList_GetItem(keys, pos); |
| 5188 | val = PyList_GetItem(vals, pos); |
| 5189 | if (!key || !val) |
| 5190 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5191 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5192 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5193 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 5194 | goto error; |
| 5195 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 5196 | Py_DECREF(key2); |
| 5197 | goto error; |
| 5198 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5199 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 5200 | defining hidden environment variables. */ |
| 5201 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 5202 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 5203 | { |
| 5204 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5205 | Py_DECREF(key2); |
| 5206 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5207 | goto error; |
| 5208 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5209 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 5210 | #else |
| 5211 | if (!PyUnicode_FSConverter(key, &key2)) |
| 5212 | goto error; |
| 5213 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 5214 | Py_DECREF(key2); |
| 5215 | goto error; |
| 5216 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5217 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 5218 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 5219 | { |
| 5220 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5221 | Py_DECREF(key2); |
| 5222 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5223 | goto error; |
| 5224 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5225 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 5226 | PyBytes_AS_STRING(val2)); |
| 5227 | #endif |
| 5228 | Py_DECREF(key2); |
| 5229 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5230 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5231 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5232 | |
| 5233 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 5234 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5235 | goto error; |
| 5236 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5237 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5238 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5239 | } |
| 5240 | Py_DECREF(vals); |
| 5241 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5242 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5243 | envlist[envc] = 0; |
| 5244 | *envc_ptr = envc; |
| 5245 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5246 | |
| 5247 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5248 | Py_XDECREF(keys); |
| 5249 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5250 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5251 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5252 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5253 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5254 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5255 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5256 | { |
| 5257 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5258 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5259 | if (argvlist == NULL) { |
| 5260 | PyErr_NoMemory(); |
| 5261 | return NULL; |
| 5262 | } |
| 5263 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5264 | PyObject* item = PySequence_ITEM(argv, i); |
| 5265 | if (item == NULL) |
| 5266 | goto fail; |
| 5267 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5268 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5269 | goto fail; |
| 5270 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5271 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5272 | } |
| 5273 | argvlist[*argc] = NULL; |
| 5274 | return argvlist; |
| 5275 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5276 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5277 | free_string_array(argvlist, *argc); |
| 5278 | return NULL; |
| 5279 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5280 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5281 | #endif |
| 5282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5283 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5284 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5285 | /*[clinic input] |
| 5286 | os.execv |
| 5287 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5288 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5289 | Path of executable file. |
| 5290 | argv: object |
| 5291 | Tuple or list of strings. |
| 5292 | / |
| 5293 | |
| 5294 | Execute an executable path with arguments, replacing current process. |
| 5295 | [clinic start generated code]*/ |
| 5296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5297 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5298 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5299 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5300 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5301 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5302 | Py_ssize_t argc; |
| 5303 | |
| 5304 | /* execv has two arguments: (path, argv), where |
| 5305 | argv is a list or tuple of strings. */ |
| 5306 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5307 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5308 | PyErr_SetString(PyExc_TypeError, |
| 5309 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5310 | return NULL; |
| 5311 | } |
| 5312 | argc = PySequence_Size(argv); |
| 5313 | if (argc < 1) { |
| 5314 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5315 | return NULL; |
| 5316 | } |
| 5317 | |
| 5318 | argvlist = parse_arglist(argv, &argc); |
| 5319 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5320 | return NULL; |
| 5321 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5322 | if (!argvlist[0][0]) { |
| 5323 | PyErr_SetString(PyExc_ValueError, |
| 5324 | "execv() arg 2 first element cannot be empty"); |
| 5325 | free_string_array(argvlist, argc); |
| 5326 | return NULL; |
| 5327 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5328 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5329 | if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5330 | free_string_array(argvlist, argc); |
| 5331 | return NULL; |
| 5332 | } |
| 5333 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5334 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5335 | #ifdef HAVE_WEXECV |
| 5336 | _wexecv(path->wide, argvlist); |
| 5337 | #else |
| 5338 | execv(path->narrow, argvlist); |
| 5339 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5340 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5341 | |
| 5342 | /* If we get here it's definitely an error */ |
| 5343 | |
| 5344 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5345 | return posix_error(); |
| 5346 | } |
| 5347 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5348 | |
| 5349 | /*[clinic input] |
| 5350 | os.execve |
| 5351 | |
| 5352 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5353 | Path of executable file. |
| 5354 | argv: object |
| 5355 | Tuple or list of strings. |
| 5356 | env: object |
| 5357 | Dictionary of strings mapping to strings. |
| 5358 | |
| 5359 | Execute an executable path with arguments, replacing current process. |
| 5360 | [clinic start generated code]*/ |
| 5361 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5362 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5363 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5364 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5365 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5366 | EXECV_CHAR **argvlist = NULL; |
| 5367 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5368 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5369 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5370 | /* execve has three arguments: (path, argv, env), where |
| 5371 | argv is a list or tuple of strings and env is a dictionary |
| 5372 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5373 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5374 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5375 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5376 | "execve: argv must be a tuple or list"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5377 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5378 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5379 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5380 | if (argc < 1) { |
| 5381 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5382 | return NULL; |
| 5383 | } |
| 5384 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5385 | if (!PyMapping_Check(env)) { |
| 5386 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5387 | "execve: environment must be a mapping object"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5388 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5389 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5390 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5391 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5392 | if (argvlist == NULL) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5393 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5394 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5395 | if (!argvlist[0][0]) { |
| 5396 | PyErr_SetString(PyExc_ValueError, |
| 5397 | "execve: argv first element cannot be empty"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5398 | goto fail_0; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5399 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5400 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5401 | envlist = parse_envlist(env, &envc); |
| 5402 | if (envlist == NULL) |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5403 | goto fail_0; |
| 5404 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5405 | if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5406 | goto fail_1; |
| 5407 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5408 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5409 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5410 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5411 | if (path->fd > -1) |
| 5412 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5413 | else |
| 5414 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5415 | #ifdef HAVE_WEXECV |
| 5416 | _wexecve(path->wide, argvlist, envlist); |
| 5417 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5418 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5419 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5420 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5421 | |
| 5422 | /* If we get here it's definitely an error */ |
| 5423 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5424 | posix_path_error(path); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5425 | fail_1: |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5426 | free_string_array(envlist, envc); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5427 | fail_0: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5428 | if (argvlist) |
| 5429 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5430 | return NULL; |
| 5431 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5432 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5433 | #endif /* HAVE_EXECV */ |
| 5434 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5435 | #ifdef HAVE_POSIX_SPAWN |
| 5436 | |
| 5437 | enum posix_spawn_file_actions_identifier { |
| 5438 | POSIX_SPAWN_OPEN, |
| 5439 | POSIX_SPAWN_CLOSE, |
| 5440 | POSIX_SPAWN_DUP2 |
| 5441 | }; |
| 5442 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5443 | #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] | 5444 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5445 | convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5446 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5447 | |
| 5448 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5449 | parse_posix_spawn_flags(PyObject *module, const char *func_name, PyObject *setpgroup, |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5450 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5451 | PyObject *setsigdef, PyObject *scheduler, |
| 5452 | posix_spawnattr_t *attrp) |
| 5453 | { |
| 5454 | long all_flags = 0; |
| 5455 | |
| 5456 | errno = posix_spawnattr_init(attrp); |
| 5457 | if (errno) { |
| 5458 | posix_error(); |
| 5459 | return -1; |
| 5460 | } |
| 5461 | |
| 5462 | if (setpgroup) { |
| 5463 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5464 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5465 | goto fail; |
| 5466 | } |
| 5467 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5468 | if (errno) { |
| 5469 | posix_error(); |
| 5470 | goto fail; |
| 5471 | } |
| 5472 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5473 | } |
| 5474 | |
| 5475 | if (resetids) { |
| 5476 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5477 | } |
| 5478 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5479 | if (setsid) { |
| 5480 | #ifdef POSIX_SPAWN_SETSID |
| 5481 | all_flags |= POSIX_SPAWN_SETSID; |
| 5482 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5483 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5484 | #else |
| 5485 | argument_unavailable_error(func_name, "setsid"); |
| 5486 | return -1; |
| 5487 | #endif |
| 5488 | } |
| 5489 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5490 | if (setsigmask) { |
| 5491 | sigset_t set; |
| 5492 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5493 | goto fail; |
| 5494 | } |
| 5495 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5496 | if (errno) { |
| 5497 | posix_error(); |
| 5498 | goto fail; |
| 5499 | } |
| 5500 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5501 | } |
| 5502 | |
| 5503 | if (setsigdef) { |
| 5504 | sigset_t set; |
| 5505 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5506 | goto fail; |
| 5507 | } |
| 5508 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5509 | if (errno) { |
| 5510 | posix_error(); |
| 5511 | goto fail; |
| 5512 | } |
| 5513 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5514 | } |
| 5515 | |
| 5516 | if (scheduler) { |
| 5517 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5518 | PyObject *py_schedpolicy; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5519 | PyObject *schedparam_obj; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5520 | struct sched_param schedparam; |
| 5521 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5522 | if (!PyArg_ParseTuple(scheduler, "OO" |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5523 | ";A scheduler tuple must have two elements", |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5524 | &py_schedpolicy, &schedparam_obj)) { |
| 5525 | goto fail; |
| 5526 | } |
| 5527 | if (!convert_sched_param(module, schedparam_obj, &schedparam)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5528 | goto fail; |
| 5529 | } |
| 5530 | if (py_schedpolicy != Py_None) { |
| 5531 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5532 | |
| 5533 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5534 | goto fail; |
| 5535 | } |
| 5536 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5537 | if (errno) { |
| 5538 | posix_error(); |
| 5539 | goto fail; |
| 5540 | } |
| 5541 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5542 | } |
| 5543 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5544 | if (errno) { |
| 5545 | posix_error(); |
| 5546 | goto fail; |
| 5547 | } |
| 5548 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5549 | #else |
| 5550 | PyErr_SetString(PyExc_NotImplementedError, |
| 5551 | "The scheduler option is not supported in this system."); |
| 5552 | goto fail; |
| 5553 | #endif |
| 5554 | } |
| 5555 | |
| 5556 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5557 | if (errno) { |
| 5558 | posix_error(); |
| 5559 | goto fail; |
| 5560 | } |
| 5561 | |
| 5562 | return 0; |
| 5563 | |
| 5564 | fail: |
| 5565 | (void)posix_spawnattr_destroy(attrp); |
| 5566 | return -1; |
| 5567 | } |
| 5568 | |
| 5569 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5570 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5571 | posix_spawn_file_actions_t *file_actionsp, |
| 5572 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5573 | { |
| 5574 | PyObject *seq; |
| 5575 | PyObject *file_action = NULL; |
| 5576 | PyObject *tag_obj; |
| 5577 | |
| 5578 | seq = PySequence_Fast(file_actions, |
| 5579 | "file_actions must be a sequence or None"); |
| 5580 | if (seq == NULL) { |
| 5581 | return -1; |
| 5582 | } |
| 5583 | |
| 5584 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5585 | if (errno) { |
| 5586 | posix_error(); |
| 5587 | Py_DECREF(seq); |
| 5588 | return -1; |
| 5589 | } |
| 5590 | |
Zackery Spytz | d52a83a | 2019-06-26 14:54:20 -0600 | [diff] [blame] | 5591 | 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] | 5592 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5593 | Py_INCREF(file_action); |
| 5594 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5595 | PyErr_SetString(PyExc_TypeError, |
| 5596 | "Each file_actions element must be a non-empty tuple"); |
| 5597 | goto fail; |
| 5598 | } |
| 5599 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5600 | if (tag == -1 && PyErr_Occurred()) { |
| 5601 | goto fail; |
| 5602 | } |
| 5603 | |
| 5604 | /* Populate the file_actions object */ |
| 5605 | switch (tag) { |
| 5606 | case POSIX_SPAWN_OPEN: { |
| 5607 | int fd, oflag; |
| 5608 | PyObject *path; |
| 5609 | unsigned long mode; |
| 5610 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5611 | ";A open file_action tuple must have 5 elements", |
| 5612 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5613 | &oflag, &mode)) |
| 5614 | { |
| 5615 | goto fail; |
| 5616 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5617 | if (PyList_Append(temp_buffer, path)) { |
| 5618 | Py_DECREF(path); |
| 5619 | goto fail; |
| 5620 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5621 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5622 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5623 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5624 | if (errno) { |
| 5625 | posix_error(); |
| 5626 | goto fail; |
| 5627 | } |
| 5628 | break; |
| 5629 | } |
| 5630 | case POSIX_SPAWN_CLOSE: { |
| 5631 | int fd; |
| 5632 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5633 | ";A close file_action tuple must have 2 elements", |
| 5634 | &tag_obj, &fd)) |
| 5635 | { |
| 5636 | goto fail; |
| 5637 | } |
| 5638 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5639 | if (errno) { |
| 5640 | posix_error(); |
| 5641 | goto fail; |
| 5642 | } |
| 5643 | break; |
| 5644 | } |
| 5645 | case POSIX_SPAWN_DUP2: { |
| 5646 | int fd1, fd2; |
| 5647 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5648 | ";A dup2 file_action tuple must have 3 elements", |
| 5649 | &tag_obj, &fd1, &fd2)) |
| 5650 | { |
| 5651 | goto fail; |
| 5652 | } |
| 5653 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5654 | fd1, fd2); |
| 5655 | if (errno) { |
| 5656 | posix_error(); |
| 5657 | goto fail; |
| 5658 | } |
| 5659 | break; |
| 5660 | } |
| 5661 | default: { |
| 5662 | PyErr_SetString(PyExc_TypeError, |
| 5663 | "Unknown file_actions identifier"); |
| 5664 | goto fail; |
| 5665 | } |
| 5666 | } |
| 5667 | Py_DECREF(file_action); |
| 5668 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5669 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5670 | Py_DECREF(seq); |
| 5671 | return 0; |
| 5672 | |
| 5673 | fail: |
| 5674 | Py_DECREF(seq); |
| 5675 | Py_DECREF(file_action); |
| 5676 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5677 | return -1; |
| 5678 | } |
| 5679 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5680 | |
| 5681 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5682 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 5683 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5684 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5685 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5686 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5687 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5688 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5689 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5690 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5691 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5692 | posix_spawnattr_t attr; |
| 5693 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5694 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5695 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5696 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5697 | pid_t pid; |
| 5698 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5699 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5700 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5701 | 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] | 5702 | like posix.environ. */ |
| 5703 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5704 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5705 | PyErr_Format(PyExc_TypeError, |
| 5706 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5707 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5708 | } |
| 5709 | argc = PySequence_Size(argv); |
| 5710 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5711 | PyErr_Format(PyExc_ValueError, |
| 5712 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5713 | return NULL; |
| 5714 | } |
| 5715 | |
| 5716 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5717 | PyErr_Format(PyExc_TypeError, |
| 5718 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5719 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | argvlist = parse_arglist(argv, &argc); |
| 5723 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5724 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5725 | } |
| 5726 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5727 | PyErr_Format(PyExc_ValueError, |
| 5728 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5729 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5730 | } |
| 5731 | |
| 5732 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5733 | if (envlist == NULL) { |
| 5734 | goto exit; |
| 5735 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5736 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 5737 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5738 | /* There is a bug in old versions of glibc that makes some of the |
| 5739 | * helper functions for manipulating file actions not copy the provided |
| 5740 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5741 | * copy the value of path for some old versions of glibc (<2.20). |
| 5742 | * The use of temp_buffer here is a workaround that keeps the |
| 5743 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5744 | * Check https://bugs.python.org/issue33630 and |
| 5745 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5746 | temp_buffer = PyList_New(0); |
| 5747 | if (!temp_buffer) { |
| 5748 | goto exit; |
| 5749 | } |
| 5750 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5751 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5752 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5753 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5754 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5755 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5756 | if (parse_posix_spawn_flags(module, func_name, setpgroup, resetids, setsid, |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5757 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5758 | goto exit; |
| 5759 | } |
| 5760 | attrp = &attr; |
| 5761 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5762 | if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5763 | goto exit; |
| 5764 | } |
| 5765 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5766 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5767 | #ifdef HAVE_POSIX_SPAWNP |
| 5768 | if (use_posix_spawnp) { |
| 5769 | err_code = posix_spawnp(&pid, path->narrow, |
| 5770 | file_actionsp, attrp, argvlist, envlist); |
| 5771 | } |
| 5772 | else |
| 5773 | #endif /* HAVE_POSIX_SPAWNP */ |
| 5774 | { |
| 5775 | err_code = posix_spawn(&pid, path->narrow, |
| 5776 | file_actionsp, attrp, argvlist, envlist); |
| 5777 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5778 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5779 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5780 | if (err_code) { |
| 5781 | errno = err_code; |
| 5782 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5783 | goto exit; |
| 5784 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5785 | #ifdef _Py_MEMORY_SANITIZER |
| 5786 | __msan_unpoison(&pid, sizeof(pid)); |
| 5787 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5788 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5789 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5790 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5791 | if (file_actionsp) { |
| 5792 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5793 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5794 | if (attrp) { |
| 5795 | (void)posix_spawnattr_destroy(attrp); |
| 5796 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5797 | if (envlist) { |
| 5798 | free_string_array(envlist, envc); |
| 5799 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5800 | if (argvlist) { |
| 5801 | free_string_array(argvlist, argc); |
| 5802 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5803 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5804 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5805 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5806 | |
| 5807 | |
| 5808 | /*[clinic input] |
| 5809 | |
| 5810 | os.posix_spawn |
| 5811 | path: path_t |
| 5812 | Path of executable file. |
| 5813 | argv: object |
| 5814 | Tuple or list of strings. |
| 5815 | env: object |
| 5816 | Dictionary of strings mapping to strings. |
| 5817 | / |
| 5818 | * |
| 5819 | file_actions: object(c_default='NULL') = () |
| 5820 | A sequence of file action tuples. |
| 5821 | setpgroup: object = NULL |
| 5822 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5823 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5824 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 5825 | setsid: bool(accept={int}) = False |
| 5826 | 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] | 5827 | setsigmask: object(c_default='NULL') = () |
| 5828 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5829 | setsigdef: object(c_default='NULL') = () |
| 5830 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5831 | scheduler: object = NULL |
| 5832 | A tuple with the scheduler policy (optional) and parameters. |
| 5833 | |
| 5834 | Execute the program specified by path in a new process. |
| 5835 | [clinic start generated code]*/ |
| 5836 | |
| 5837 | static PyObject * |
| 5838 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5839 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5840 | PyObject *setpgroup, int resetids, int setsid, |
| 5841 | PyObject *setsigmask, PyObject *setsigdef, |
| 5842 | PyObject *scheduler) |
| 5843 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5844 | { |
| 5845 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5846 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5847 | scheduler); |
| 5848 | } |
| 5849 | #endif /* HAVE_POSIX_SPAWN */ |
| 5850 | |
| 5851 | |
| 5852 | |
| 5853 | #ifdef HAVE_POSIX_SPAWNP |
| 5854 | /*[clinic input] |
| 5855 | |
| 5856 | os.posix_spawnp |
| 5857 | path: path_t |
| 5858 | Path of executable file. |
| 5859 | argv: object |
| 5860 | Tuple or list of strings. |
| 5861 | env: object |
| 5862 | Dictionary of strings mapping to strings. |
| 5863 | / |
| 5864 | * |
| 5865 | file_actions: object(c_default='NULL') = () |
| 5866 | A sequence of file action tuples. |
| 5867 | setpgroup: object = NULL |
| 5868 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5869 | resetids: bool(accept={int}) = False |
| 5870 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5871 | setsid: bool(accept={int}) = False |
| 5872 | 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] | 5873 | setsigmask: object(c_default='NULL') = () |
| 5874 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5875 | setsigdef: object(c_default='NULL') = () |
| 5876 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5877 | scheduler: object = NULL |
| 5878 | A tuple with the scheduler policy (optional) and parameters. |
| 5879 | |
| 5880 | Execute the program specified by path in a new process. |
| 5881 | [clinic start generated code]*/ |
| 5882 | |
| 5883 | static PyObject * |
| 5884 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5885 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5886 | PyObject *setpgroup, int resetids, int setsid, |
| 5887 | PyObject *setsigmask, PyObject *setsigdef, |
| 5888 | PyObject *scheduler) |
| 5889 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5890 | { |
| 5891 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5892 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5893 | scheduler); |
| 5894 | } |
| 5895 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5896 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5897 | #ifdef HAVE_RTPSPAWN |
| 5898 | static intptr_t |
| 5899 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], |
| 5900 | const char *envp[]) |
| 5901 | { |
| 5902 | RTP_ID rtpid; |
| 5903 | int status; |
| 5904 | pid_t res; |
| 5905 | int async_err = 0; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5906 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5907 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
| 5908 | uStackSize=0 cannot be used, the default stack size is too small for |
| 5909 | Python. */ |
| 5910 | if (envp) { |
| 5911 | rtpid = rtpSpawn(rtpFileName, argv, envp, |
| 5912 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5913 | } |
| 5914 | else { |
| 5915 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, |
| 5916 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5917 | } |
| 5918 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { |
| 5919 | do { |
| 5920 | res = waitpid((pid_t)rtpid, &status, 0); |
| 5921 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 5922 | |
| 5923 | if (res < 0) |
| 5924 | return RTP_ID_ERROR; |
| 5925 | return ((intptr_t)status); |
| 5926 | } |
| 5927 | return ((intptr_t)rtpid); |
| 5928 | } |
| 5929 | #endif |
| 5930 | |
| 5931 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5932 | /*[clinic input] |
| 5933 | os.spawnv |
| 5934 | |
| 5935 | mode: int |
| 5936 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5937 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5938 | Path of executable file. |
| 5939 | argv: object |
| 5940 | Tuple or list of strings. |
| 5941 | / |
| 5942 | |
| 5943 | Execute the program specified by path in a new process. |
| 5944 | [clinic start generated code]*/ |
| 5945 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5946 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5947 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5948 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5949 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5950 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5951 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5952 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5953 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5954 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5955 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5956 | /* spawnv has three arguments: (mode, path, argv), where |
| 5957 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5958 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5959 | if (PyList_Check(argv)) { |
| 5960 | argc = PyList_Size(argv); |
| 5961 | getitem = PyList_GetItem; |
| 5962 | } |
| 5963 | else if (PyTuple_Check(argv)) { |
| 5964 | argc = PyTuple_Size(argv); |
| 5965 | getitem = PyTuple_GetItem; |
| 5966 | } |
| 5967 | else { |
| 5968 | PyErr_SetString(PyExc_TypeError, |
| 5969 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5970 | return NULL; |
| 5971 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5972 | if (argc == 0) { |
| 5973 | PyErr_SetString(PyExc_ValueError, |
| 5974 | "spawnv() arg 2 cannot be empty"); |
| 5975 | return NULL; |
| 5976 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5977 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5978 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5979 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5980 | return PyErr_NoMemory(); |
| 5981 | } |
| 5982 | for (i = 0; i < argc; i++) { |
| 5983 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5984 | &argvlist[i])) { |
| 5985 | free_string_array(argvlist, i); |
| 5986 | PyErr_SetString( |
| 5987 | PyExc_TypeError, |
| 5988 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5989 | return NULL; |
| 5990 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5991 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5992 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5993 | PyErr_SetString( |
| 5994 | PyExc_ValueError, |
| 5995 | "spawnv() arg 2 first element cannot be empty"); |
| 5996 | return NULL; |
| 5997 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5998 | } |
| 5999 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6000 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6001 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6002 | if (mode == _OLD_P_OVERLAY) |
| 6003 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6004 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6005 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6006 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6007 | Py_None) < 0) { |
| 6008 | free_string_array(argvlist, argc); |
| 6009 | return NULL; |
| 6010 | } |
| 6011 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6012 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6013 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6014 | #ifdef HAVE_WSPAWNV |
| 6015 | spawnval = _wspawnv(mode, path->wide, argvlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6016 | #elif defined(HAVE_RTPSPAWN) |
| 6017 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6018 | #else |
| 6019 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 6020 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6021 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6022 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6023 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6024 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6025 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6026 | if (spawnval == -1) |
| 6027 | return posix_error(); |
| 6028 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6029 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6030 | } |
| 6031 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6032 | /*[clinic input] |
| 6033 | os.spawnve |
| 6034 | |
| 6035 | mode: int |
| 6036 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6037 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6038 | Path of executable file. |
| 6039 | argv: object |
| 6040 | Tuple or list of strings. |
| 6041 | env: object |
| 6042 | Dictionary of strings mapping to strings. |
| 6043 | / |
| 6044 | |
| 6045 | Execute the program specified by path in a new process. |
| 6046 | [clinic start generated code]*/ |
| 6047 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6048 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6049 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6050 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6051 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6052 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6053 | EXECV_CHAR **argvlist; |
| 6054 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6055 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 6056 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6057 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6058 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6059 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6060 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6061 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 6062 | argv is a list or tuple of strings and env is a dictionary |
| 6063 | like posix.environ. */ |
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 | if (PyList_Check(argv)) { |
| 6066 | argc = PyList_Size(argv); |
| 6067 | getitem = PyList_GetItem; |
| 6068 | } |
| 6069 | else if (PyTuple_Check(argv)) { |
| 6070 | argc = PyTuple_Size(argv); |
| 6071 | getitem = PyTuple_GetItem; |
| 6072 | } |
| 6073 | else { |
| 6074 | PyErr_SetString(PyExc_TypeError, |
| 6075 | "spawnve() arg 2 must be a tuple or list"); |
| 6076 | goto fail_0; |
| 6077 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 6078 | if (argc == 0) { |
| 6079 | PyErr_SetString(PyExc_ValueError, |
| 6080 | "spawnve() arg 2 cannot be empty"); |
| 6081 | goto fail_0; |
| 6082 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6083 | if (!PyMapping_Check(env)) { |
| 6084 | PyErr_SetString(PyExc_TypeError, |
| 6085 | "spawnve() arg 3 must be a mapping object"); |
| 6086 | goto fail_0; |
| 6087 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6088 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6089 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6090 | if (argvlist == NULL) { |
| 6091 | PyErr_NoMemory(); |
| 6092 | goto fail_0; |
| 6093 | } |
| 6094 | for (i = 0; i < argc; i++) { |
| 6095 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 6096 | &argvlist[i])) |
| 6097 | { |
| 6098 | lastarg = i; |
| 6099 | goto fail_1; |
| 6100 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6101 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6102 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6103 | PyErr_SetString( |
| 6104 | PyExc_ValueError, |
| 6105 | "spawnv() arg 2 first element cannot be empty"); |
| 6106 | goto fail_1; |
| 6107 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6108 | } |
| 6109 | lastarg = argc; |
| 6110 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6111 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6112 | envlist = parse_envlist(env, &envc); |
| 6113 | if (envlist == NULL) |
| 6114 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6115 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6116 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6117 | if (mode == _OLD_P_OVERLAY) |
| 6118 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6119 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6120 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6121 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6122 | goto fail_2; |
| 6123 | } |
| 6124 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6125 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6126 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6127 | #ifdef HAVE_WSPAWNV |
| 6128 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6129 | #elif defined(HAVE_RTPSPAWN) |
| 6130 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, |
| 6131 | (const char **)envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6132 | #else |
| 6133 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 6134 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6135 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6136 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6137 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6138 | if (spawnval == -1) |
| 6139 | (void) posix_error(); |
| 6140 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6141 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6142 | |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6143 | fail_2: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6144 | while (--envc >= 0) |
| 6145 | PyMem_DEL(envlist[envc]); |
| 6146 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 6147 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6148 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6149 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6150 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6151 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6152 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6153 | #endif /* HAVE_SPAWNV */ |
| 6154 | |
| 6155 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6156 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6157 | |
| 6158 | /* Helper function to validate arguments. |
| 6159 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 6160 | If obj is non-NULL it must be callable. */ |
| 6161 | static int |
| 6162 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 6163 | { |
| 6164 | if (obj && !PyCallable_Check(obj)) { |
| 6165 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6166 | obj_name, _PyType_Name(Py_TYPE(obj))); |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6167 | return -1; |
| 6168 | } |
| 6169 | return 0; |
| 6170 | } |
| 6171 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6172 | /*[clinic input] |
| 6173 | os.register_at_fork |
| 6174 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6175 | * |
| 6176 | before: object=NULL |
| 6177 | A callable to be called in the parent before the fork() syscall. |
| 6178 | after_in_child: object=NULL |
| 6179 | A callable to be called in the child after fork(). |
| 6180 | after_in_parent: object=NULL |
| 6181 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6182 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6183 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6184 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6185 | 'before' callbacks are called in reverse order. |
| 6186 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6187 | |
| 6188 | [clinic start generated code]*/ |
| 6189 | |
| 6190 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6191 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 6192 | PyObject *after_in_child, PyObject *after_in_parent) |
| 6193 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6194 | { |
| 6195 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6196 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6197 | if (!before && !after_in_child && !after_in_parent) { |
| 6198 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 6199 | return NULL; |
| 6200 | } |
| 6201 | if (check_null_or_callable(before, "before") || |
| 6202 | check_null_or_callable(after_in_child, "after_in_child") || |
| 6203 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6204 | return NULL; |
| 6205 | } |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6206 | interp = _PyInterpreterState_GET(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6207 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6208 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6209 | return NULL; |
| 6210 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6211 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6212 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6213 | } |
| 6214 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 6215 | return NULL; |
| 6216 | } |
| 6217 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6218 | } |
| 6219 | #endif /* HAVE_FORK */ |
| 6220 | |
| 6221 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6222 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6223 | /*[clinic input] |
| 6224 | os.fork1 |
| 6225 | |
| 6226 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 6227 | |
| 6228 | Return 0 to child process and PID of child to parent process. |
| 6229 | [clinic start generated code]*/ |
| 6230 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6231 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6232 | os_fork1_impl(PyObject *module) |
| 6233 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6234 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6235 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6236 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6237 | if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6238 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6239 | return NULL; |
| 6240 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6241 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6242 | pid = fork1(); |
| 6243 | if (pid == 0) { |
| 6244 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6245 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6246 | } else { |
| 6247 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6248 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6249 | } |
| 6250 | if (pid == -1) |
| 6251 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6252 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6253 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6254 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6255 | |
| 6256 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6257 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6258 | /*[clinic input] |
| 6259 | os.fork |
| 6260 | |
| 6261 | Fork a child process. |
| 6262 | |
| 6263 | Return 0 to child process and PID of child to parent process. |
| 6264 | [clinic start generated code]*/ |
| 6265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6266 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6267 | os_fork_impl(PyObject *module) |
| 6268 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6269 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6270 | pid_t pid; |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 6271 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 6272 | if (interp->config._isolated_interpreter) { |
| 6273 | PyErr_SetString(PyExc_RuntimeError, |
| 6274 | "fork not supported for isolated subinterpreters"); |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6275 | return NULL; |
| 6276 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6277 | if (PySys_Audit("os.fork", NULL) < 0) { |
| 6278 | return NULL; |
| 6279 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6280 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6281 | pid = fork(); |
| 6282 | if (pid == 0) { |
| 6283 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6284 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6285 | } else { |
| 6286 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6287 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6288 | } |
| 6289 | if (pid == -1) |
| 6290 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6291 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6292 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6293 | #endif /* HAVE_FORK */ |
| 6294 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6295 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6296 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6297 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6298 | /*[clinic input] |
| 6299 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6300 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6301 | policy: int |
| 6302 | |
| 6303 | Get the maximum scheduling priority for policy. |
| 6304 | [clinic start generated code]*/ |
| 6305 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6306 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6307 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 6308 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6309 | { |
| 6310 | int max; |
| 6311 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6312 | max = sched_get_priority_max(policy); |
| 6313 | if (max < 0) |
| 6314 | return posix_error(); |
| 6315 | return PyLong_FromLong(max); |
| 6316 | } |
| 6317 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6318 | |
| 6319 | /*[clinic input] |
| 6320 | os.sched_get_priority_min |
| 6321 | |
| 6322 | policy: int |
| 6323 | |
| 6324 | Get the minimum scheduling priority for policy. |
| 6325 | [clinic start generated code]*/ |
| 6326 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6327 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6328 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 6329 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6330 | { |
| 6331 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6332 | if (min < 0) |
| 6333 | return posix_error(); |
| 6334 | return PyLong_FromLong(min); |
| 6335 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6336 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 6337 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6339 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 6340 | /*[clinic input] |
| 6341 | os.sched_getscheduler |
| 6342 | pid: pid_t |
| 6343 | / |
| 6344 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6345 | Get the scheduling policy for the process identified by pid. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6346 | |
| 6347 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6348 | [clinic start generated code]*/ |
| 6349 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6350 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6351 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6352 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6353 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6354 | int policy; |
| 6355 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6356 | policy = sched_getscheduler(pid); |
| 6357 | if (policy < 0) |
| 6358 | return posix_error(); |
| 6359 | return PyLong_FromLong(policy); |
| 6360 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6361 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6362 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6363 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6364 | #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] | 6365 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6366 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6367 | |
| 6368 | @classmethod |
| 6369 | os.sched_param.__new__ |
| 6370 | |
| 6371 | sched_priority: object |
| 6372 | A scheduling parameter. |
| 6373 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6374 | Currently has only one field: sched_priority |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6375 | [clinic start generated code]*/ |
| 6376 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6377 | static PyObject * |
| 6378 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6379 | /*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6380 | { |
| 6381 | PyObject *res; |
| 6382 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6383 | res = PyStructSequence_New(type); |
| 6384 | if (!res) |
| 6385 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6386 | Py_INCREF(sched_priority); |
| 6387 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6388 | return res; |
| 6389 | } |
| 6390 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6391 | PyDoc_VAR(os_sched_param__doc__); |
| 6392 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6393 | static PyStructSequence_Field sched_param_fields[] = { |
| 6394 | {"sched_priority", "the scheduling priority"}, |
| 6395 | {0} |
| 6396 | }; |
| 6397 | |
| 6398 | static PyStructSequence_Desc sched_param_desc = { |
| 6399 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6400 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6401 | sched_param_fields, |
| 6402 | 1 |
| 6403 | }; |
| 6404 | |
| 6405 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6406 | convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6407 | { |
| 6408 | long priority; |
| 6409 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6410 | if (!Py_IS_TYPE(param, (PyTypeObject *)get_posix_state(module)->SchedParamType)) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6411 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6412 | return 0; |
| 6413 | } |
| 6414 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6415 | if (priority == -1 && PyErr_Occurred()) |
| 6416 | return 0; |
| 6417 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6418 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6419 | return 0; |
| 6420 | } |
| 6421 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6422 | return 1; |
| 6423 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6424 | #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] | 6425 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6426 | |
| 6427 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6428 | /*[clinic input] |
| 6429 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6430 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6431 | pid: pid_t |
| 6432 | policy: int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6433 | param as param_obj: object |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6434 | / |
| 6435 | |
| 6436 | Set the scheduling policy for the process identified by pid. |
| 6437 | |
| 6438 | If pid is 0, the calling process is changed. |
| 6439 | param is an instance of sched_param. |
| 6440 | [clinic start generated code]*/ |
| 6441 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6442 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6443 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6444 | PyObject *param_obj) |
| 6445 | /*[clinic end generated code: output=cde27faa55dc993e input=73013d731bd8fbe9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6446 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6447 | struct sched_param param; |
| 6448 | if (!convert_sched_param(module, param_obj, ¶m)) { |
| 6449 | return NULL; |
| 6450 | } |
| 6451 | |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6452 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6453 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6454 | ** scheduling policy under Solaris/Illumos, and others. |
| 6455 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6456 | */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6457 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6458 | return posix_error(); |
| 6459 | Py_RETURN_NONE; |
| 6460 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6461 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6462 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6463 | |
| 6464 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6465 | /*[clinic input] |
| 6466 | os.sched_getparam |
| 6467 | pid: pid_t |
| 6468 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6469 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6470 | Returns scheduling parameters for the process identified by pid. |
| 6471 | |
| 6472 | If pid is 0, returns parameters for the calling process. |
| 6473 | Return value is an instance of sched_param. |
| 6474 | [clinic start generated code]*/ |
| 6475 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6476 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6477 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6478 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6479 | { |
| 6480 | struct sched_param param; |
| 6481 | PyObject *result; |
| 6482 | PyObject *priority; |
| 6483 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6484 | if (sched_getparam(pid, ¶m)) |
| 6485 | return posix_error(); |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6486 | PyObject *SchedParamType = get_posix_state(module)->SchedParamType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6487 | result = PyStructSequence_New((PyTypeObject *)SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6488 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6489 | return NULL; |
| 6490 | priority = PyLong_FromLong(param.sched_priority); |
| 6491 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6492 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6493 | return NULL; |
| 6494 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6495 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6496 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6497 | } |
| 6498 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6499 | |
| 6500 | /*[clinic input] |
| 6501 | os.sched_setparam |
| 6502 | pid: pid_t |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6503 | param as param_obj: object |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6504 | / |
| 6505 | |
| 6506 | Set scheduling parameters for the process identified by pid. |
| 6507 | |
| 6508 | If pid is 0, sets parameters for the calling process. |
| 6509 | param should be an instance of sched_param. |
| 6510 | [clinic start generated code]*/ |
| 6511 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6512 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6513 | os_sched_setparam_impl(PyObject *module, pid_t pid, PyObject *param_obj) |
| 6514 | /*[clinic end generated code: output=f19fe020a53741c1 input=27b98337c8b2dcc7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6515 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6516 | struct sched_param param; |
| 6517 | if (!convert_sched_param(module, param_obj, ¶m)) { |
| 6518 | return NULL; |
| 6519 | } |
| 6520 | |
| 6521 | if (sched_setparam(pid, ¶m)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6522 | return posix_error(); |
| 6523 | Py_RETURN_NONE; |
| 6524 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6525 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6526 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6527 | |
| 6528 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6529 | /*[clinic input] |
| 6530 | os.sched_rr_get_interval -> double |
| 6531 | pid: pid_t |
| 6532 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6533 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6534 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6535 | |
| 6536 | Value returned is a float. |
| 6537 | [clinic start generated code]*/ |
| 6538 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6539 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6540 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6541 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6542 | { |
| 6543 | struct timespec interval; |
| 6544 | if (sched_rr_get_interval(pid, &interval)) { |
| 6545 | posix_error(); |
| 6546 | return -1.0; |
| 6547 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6548 | #ifdef _Py_MEMORY_SANITIZER |
| 6549 | __msan_unpoison(&interval, sizeof(interval)); |
| 6550 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6551 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6552 | } |
| 6553 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6554 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6555 | |
| 6556 | /*[clinic input] |
| 6557 | os.sched_yield |
| 6558 | |
| 6559 | Voluntarily relinquish the CPU. |
| 6560 | [clinic start generated code]*/ |
| 6561 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6562 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6563 | os_sched_yield_impl(PyObject *module) |
| 6564 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6565 | { |
| 6566 | if (sched_yield()) |
| 6567 | return posix_error(); |
| 6568 | Py_RETURN_NONE; |
| 6569 | } |
| 6570 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6571 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6572 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6573 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6575 | /*[clinic input] |
| 6576 | os.sched_setaffinity |
| 6577 | pid: pid_t |
| 6578 | mask : object |
| 6579 | / |
| 6580 | |
| 6581 | Set the CPU affinity of the process identified by pid to mask. |
| 6582 | |
| 6583 | mask should be an iterable of integers identifying CPUs. |
| 6584 | [clinic start generated code]*/ |
| 6585 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6586 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6587 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6588 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6589 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6590 | int ncpus; |
| 6591 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6592 | cpu_set_t *cpu_set = NULL; |
| 6593 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6594 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6595 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6596 | if (iterator == NULL) |
| 6597 | return NULL; |
| 6598 | |
| 6599 | ncpus = NCPUS_START; |
| 6600 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6601 | cpu_set = CPU_ALLOC(ncpus); |
| 6602 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6603 | PyErr_NoMemory(); |
| 6604 | goto error; |
| 6605 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6606 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6607 | |
| 6608 | while ((item = PyIter_Next(iterator))) { |
| 6609 | long cpu; |
| 6610 | if (!PyLong_Check(item)) { |
| 6611 | PyErr_Format(PyExc_TypeError, |
| 6612 | "expected an iterator of ints, " |
| 6613 | "but iterator yielded %R", |
| 6614 | Py_TYPE(item)); |
| 6615 | Py_DECREF(item); |
| 6616 | goto error; |
| 6617 | } |
| 6618 | cpu = PyLong_AsLong(item); |
| 6619 | Py_DECREF(item); |
| 6620 | if (cpu < 0) { |
| 6621 | if (!PyErr_Occurred()) |
| 6622 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6623 | goto error; |
| 6624 | } |
| 6625 | if (cpu > INT_MAX - 1) { |
| 6626 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6627 | goto error; |
| 6628 | } |
| 6629 | if (cpu >= ncpus) { |
| 6630 | /* Grow CPU mask to fit the CPU number */ |
| 6631 | int newncpus = ncpus; |
| 6632 | cpu_set_t *newmask; |
| 6633 | size_t newsetsize; |
| 6634 | while (newncpus <= cpu) { |
| 6635 | if (newncpus > INT_MAX / 2) |
| 6636 | newncpus = cpu + 1; |
| 6637 | else |
| 6638 | newncpus = newncpus * 2; |
| 6639 | } |
| 6640 | newmask = CPU_ALLOC(newncpus); |
| 6641 | if (newmask == NULL) { |
| 6642 | PyErr_NoMemory(); |
| 6643 | goto error; |
| 6644 | } |
| 6645 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6646 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6647 | memcpy(newmask, cpu_set, setsize); |
| 6648 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6649 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6650 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6651 | ncpus = newncpus; |
| 6652 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6653 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6654 | } |
Brandt Bucher | 45a30af | 2019-06-27 09:10:57 -0700 | [diff] [blame] | 6655 | if (PyErr_Occurred()) { |
| 6656 | goto error; |
| 6657 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6658 | Py_CLEAR(iterator); |
| 6659 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6660 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6661 | posix_error(); |
| 6662 | goto error; |
| 6663 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6664 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6665 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6666 | |
| 6667 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6668 | if (cpu_set) |
| 6669 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6670 | Py_XDECREF(iterator); |
| 6671 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6672 | } |
| 6673 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6674 | |
| 6675 | /*[clinic input] |
| 6676 | os.sched_getaffinity |
| 6677 | pid: pid_t |
| 6678 | / |
| 6679 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6680 | 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] | 6681 | |
| 6682 | The affinity is returned as a set of CPU identifiers. |
| 6683 | [clinic start generated code]*/ |
| 6684 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6685 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6686 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6687 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6688 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6689 | int cpu, ncpus, count; |
| 6690 | size_t setsize; |
| 6691 | cpu_set_t *mask = NULL; |
| 6692 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6693 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6694 | ncpus = NCPUS_START; |
| 6695 | while (1) { |
| 6696 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6697 | mask = CPU_ALLOC(ncpus); |
| 6698 | if (mask == NULL) |
| 6699 | return PyErr_NoMemory(); |
| 6700 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6701 | break; |
| 6702 | CPU_FREE(mask); |
| 6703 | if (errno != EINVAL) |
| 6704 | return posix_error(); |
| 6705 | if (ncpus > INT_MAX / 2) { |
| 6706 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6707 | "a large enough CPU set"); |
| 6708 | return NULL; |
| 6709 | } |
| 6710 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6711 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6712 | |
| 6713 | res = PySet_New(NULL); |
| 6714 | if (res == NULL) |
| 6715 | goto error; |
| 6716 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6717 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6718 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6719 | --count; |
| 6720 | if (cpu_num == NULL) |
| 6721 | goto error; |
| 6722 | if (PySet_Add(res, cpu_num)) { |
| 6723 | Py_DECREF(cpu_num); |
| 6724 | goto error; |
| 6725 | } |
| 6726 | Py_DECREF(cpu_num); |
| 6727 | } |
| 6728 | } |
| 6729 | CPU_FREE(mask); |
| 6730 | return res; |
| 6731 | |
| 6732 | error: |
| 6733 | if (mask) |
| 6734 | CPU_FREE(mask); |
| 6735 | Py_XDECREF(res); |
| 6736 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6737 | } |
| 6738 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6739 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6740 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6741 | #endif /* HAVE_SCHED_H */ |
| 6742 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6743 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6744 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6745 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6746 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6747 | #define DEV_PTY_FILE "/dev/ptc" |
| 6748 | #define HAVE_DEV_PTMX |
| 6749 | #else |
| 6750 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6751 | #endif |
| 6752 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6753 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6754 | #ifdef HAVE_PTY_H |
| 6755 | #include <pty.h> |
| 6756 | #else |
| 6757 | #ifdef HAVE_LIBUTIL_H |
| 6758 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6759 | #else |
| 6760 | #ifdef HAVE_UTIL_H |
| 6761 | #include <util.h> |
| 6762 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6763 | #endif /* HAVE_LIBUTIL_H */ |
| 6764 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6765 | #ifdef HAVE_STROPTS_H |
| 6766 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6767 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6768 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6769 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6770 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6771 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6772 | /*[clinic input] |
| 6773 | os.openpty |
| 6774 | |
| 6775 | Open a pseudo-terminal. |
| 6776 | |
| 6777 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6778 | for both the master and slave ends. |
| 6779 | [clinic start generated code]*/ |
| 6780 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6781 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6782 | os_openpty_impl(PyObject *module) |
| 6783 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6784 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6785 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6786 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6787 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6788 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6789 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6790 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6791 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6792 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6793 | #endif |
| 6794 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6795 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6796 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6797 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6798 | goto posix_error; |
| 6799 | |
| 6800 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6801 | goto error; |
| 6802 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6803 | goto error; |
| 6804 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6805 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6806 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6807 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6808 | goto posix_error; |
| 6809 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6810 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6811 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6812 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6813 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6814 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6815 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6816 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6817 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6818 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6819 | goto posix_error; |
| 6820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6821 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6822 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6823 | /* change permission of slave */ |
| 6824 | if (grantpt(master_fd) < 0) { |
| 6825 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6826 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6827 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6828 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6829 | /* unlock slave */ |
| 6830 | if (unlockpt(master_fd) < 0) { |
| 6831 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6832 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6833 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6834 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6835 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6836 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6837 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6838 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6839 | goto posix_error; |
| 6840 | |
| 6841 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6842 | if (slave_fd == -1) |
| 6843 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6844 | |
| 6845 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6846 | goto posix_error; |
| 6847 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6848 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6849 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6850 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6851 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6852 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6853 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6854 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6855 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6856 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6857 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6858 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6859 | posix_error: |
| 6860 | posix_error(); |
| 6861 | error: |
| 6862 | if (master_fd != -1) |
| 6863 | close(master_fd); |
| 6864 | if (slave_fd != -1) |
| 6865 | close(slave_fd); |
| 6866 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6867 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6868 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6869 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6870 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6871 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6872 | /*[clinic input] |
| 6873 | os.forkpty |
| 6874 | |
| 6875 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6876 | |
| 6877 | Returns a tuple of (pid, master_fd). |
| 6878 | Like fork(), return pid of 0 to the child process, |
| 6879 | and pid of child to the parent process. |
| 6880 | To both, return fd of newly opened pseudo-terminal. |
| 6881 | [clinic start generated code]*/ |
| 6882 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6883 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6884 | os_forkpty_impl(PyObject *module) |
| 6885 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6886 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6887 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6888 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6889 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6890 | if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6891 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6892 | return NULL; |
| 6893 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6894 | if (PySys_Audit("os.forkpty", NULL) < 0) { |
| 6895 | return NULL; |
| 6896 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6897 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6898 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6899 | if (pid == 0) { |
| 6900 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6901 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6902 | } else { |
| 6903 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6904 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6905 | } |
| 6906 | if (pid == -1) |
| 6907 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6908 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6909 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6910 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6911 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6912 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6913 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6914 | /*[clinic input] |
| 6915 | os.getegid |
| 6916 | |
| 6917 | Return the current process's effective group id. |
| 6918 | [clinic start generated code]*/ |
| 6919 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6920 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6921 | os_getegid_impl(PyObject *module) |
| 6922 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6923 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6924 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6925 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6926 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6927 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6928 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6929 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6930 | /*[clinic input] |
| 6931 | os.geteuid |
| 6932 | |
| 6933 | Return the current process's effective user id. |
| 6934 | [clinic start generated code]*/ |
| 6935 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6936 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6937 | os_geteuid_impl(PyObject *module) |
| 6938 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6939 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6940 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6941 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6942 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6943 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6944 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6945 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6946 | /*[clinic input] |
| 6947 | os.getgid |
| 6948 | |
| 6949 | Return the current process's group id. |
| 6950 | [clinic start generated code]*/ |
| 6951 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6952 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6953 | os_getgid_impl(PyObject *module) |
| 6954 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6955 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6956 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6957 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6958 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6959 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6960 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6961 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6962 | /*[clinic input] |
| 6963 | os.getpid |
| 6964 | |
| 6965 | Return the current process id. |
| 6966 | [clinic start generated code]*/ |
| 6967 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6968 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6969 | os_getpid_impl(PyObject *module) |
| 6970 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6971 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6972 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6973 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6974 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6975 | |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6976 | #ifdef NGROUPS_MAX |
| 6977 | #define MAX_GROUPS NGROUPS_MAX |
| 6978 | #else |
| 6979 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6980 | #define MAX_GROUPS 64 |
| 6981 | #endif |
| 6982 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6983 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6984 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 6985 | #ifdef __APPLE__ |
| 6986 | /*[clinic input] |
| 6987 | os.getgrouplist |
| 6988 | |
| 6989 | user: str |
| 6990 | username to lookup |
| 6991 | group as basegid: int |
| 6992 | base group id of the user |
| 6993 | / |
| 6994 | |
| 6995 | Returns a list of groups to which a user belongs. |
| 6996 | [clinic start generated code]*/ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6997 | |
| 6998 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 6999 | os_getgrouplist_impl(PyObject *module, const char *user, int basegid) |
| 7000 | /*[clinic end generated code: output=6e734697b8c26de0 input=f8d870374b09a490]*/ |
| 7001 | #else |
| 7002 | /*[clinic input] |
| 7003 | os.getgrouplist |
| 7004 | |
| 7005 | user: str |
| 7006 | username to lookup |
| 7007 | group as basegid: gid_t |
| 7008 | base group id of the user |
| 7009 | / |
| 7010 | |
| 7011 | Returns a list of groups to which a user belongs. |
| 7012 | [clinic start generated code]*/ |
| 7013 | |
| 7014 | static PyObject * |
| 7015 | os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid) |
| 7016 | /*[clinic end generated code: output=0ebd7fb70115575b input=cc61d5c20b08958d]*/ |
| 7017 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7018 | { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7019 | int i, ngroups; |
| 7020 | PyObject *list; |
| 7021 | #ifdef __APPLE__ |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7022 | int *groups; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7023 | #else |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7024 | gid_t *groups; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7025 | #endif |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 7026 | |
| 7027 | /* |
| 7028 | * NGROUPS_MAX is defined by POSIX.1 as the maximum |
| 7029 | * number of supplimental groups a users can belong to. |
| 7030 | * We have to increment it by one because |
| 7031 | * getgrouplist() returns both the supplemental groups |
| 7032 | * and the primary group, i.e. all of the groups the |
| 7033 | * user belongs to. |
| 7034 | */ |
| 7035 | ngroups = 1 + MAX_GROUPS; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7036 | |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7037 | while (1) { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7038 | #ifdef __APPLE__ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7039 | groups = PyMem_New(int, ngroups); |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7040 | #else |
| 7041 | groups = PyMem_New(gid_t, ngroups); |
| 7042 | #endif |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7043 | if (groups == NULL) { |
| 7044 | return PyErr_NoMemory(); |
| 7045 | } |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7046 | |
| 7047 | int old_ngroups = ngroups; |
| 7048 | if (getgrouplist(user, basegid, groups, &ngroups) != -1) { |
| 7049 | /* Success */ |
| 7050 | break; |
| 7051 | } |
| 7052 | |
| 7053 | /* getgrouplist() fails if the group list is too small */ |
| 7054 | PyMem_Free(groups); |
| 7055 | |
| 7056 | if (ngroups > old_ngroups) { |
| 7057 | /* If the group list is too small, the glibc implementation of |
| 7058 | getgrouplist() sets ngroups to the total number of groups and |
| 7059 | returns -1. */ |
| 7060 | } |
| 7061 | else { |
| 7062 | /* Double the group list size */ |
| 7063 | if (ngroups > INT_MAX / 2) { |
| 7064 | return PyErr_NoMemory(); |
| 7065 | } |
| 7066 | ngroups *= 2; |
| 7067 | } |
| 7068 | |
| 7069 | /* Retry getgrouplist() with a larger group list */ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7070 | } |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7071 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 7072 | #ifdef _Py_MEMORY_SANITIZER |
| 7073 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 7074 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 7075 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 7076 | #endif |
| 7077 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7078 | list = PyList_New(ngroups); |
| 7079 | if (list == NULL) { |
| 7080 | PyMem_Del(groups); |
| 7081 | return NULL; |
| 7082 | } |
| 7083 | |
| 7084 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7085 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7086 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7087 | #else |
| 7088 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 7089 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7090 | if (o == NULL) { |
| 7091 | Py_DECREF(list); |
| 7092 | PyMem_Del(groups); |
| 7093 | return NULL; |
| 7094 | } |
| 7095 | PyList_SET_ITEM(list, i, o); |
| 7096 | } |
| 7097 | |
| 7098 | PyMem_Del(groups); |
| 7099 | |
| 7100 | return list; |
| 7101 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7102 | #endif /* HAVE_GETGROUPLIST */ |
| 7103 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7104 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7105 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7106 | /*[clinic input] |
| 7107 | os.getgroups |
| 7108 | |
| 7109 | Return list of supplemental group IDs for the process. |
| 7110 | [clinic start generated code]*/ |
| 7111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7112 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7113 | os_getgroups_impl(PyObject *module) |
| 7114 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7115 | { |
| 7116 | PyObject *result = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7117 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7118 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7119 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7120 | * This is a helper variable to store the intermediate result when |
| 7121 | * that happens. |
| 7122 | * |
| 7123 | * To keep the code readable the OSX behaviour is unconditional, |
| 7124 | * according to the POSIX spec this should be safe on all unix-y |
| 7125 | * systems. |
| 7126 | */ |
| 7127 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7128 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7129 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7130 | #ifdef __APPLE__ |
| 7131 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 7132 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 7133 | * always first call getgroups with length 0 to get the actual number |
| 7134 | * of groups. |
| 7135 | */ |
| 7136 | n = getgroups(0, NULL); |
| 7137 | if (n < 0) { |
| 7138 | return posix_error(); |
| 7139 | } else if (n <= MAX_GROUPS) { |
| 7140 | /* groups will fit in existing array */ |
| 7141 | alt_grouplist = grouplist; |
| 7142 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7143 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7144 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7145 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7146 | } |
| 7147 | } |
| 7148 | |
| 7149 | n = getgroups(n, alt_grouplist); |
| 7150 | if (n == -1) { |
| 7151 | if (alt_grouplist != grouplist) { |
| 7152 | PyMem_Free(alt_grouplist); |
| 7153 | } |
| 7154 | return posix_error(); |
| 7155 | } |
| 7156 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7157 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7158 | if (n < 0) { |
| 7159 | if (errno == EINVAL) { |
| 7160 | n = getgroups(0, NULL); |
| 7161 | if (n == -1) { |
| 7162 | return posix_error(); |
| 7163 | } |
| 7164 | if (n == 0) { |
| 7165 | /* Avoid malloc(0) */ |
| 7166 | alt_grouplist = grouplist; |
| 7167 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7168 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7169 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7170 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7171 | } |
| 7172 | n = getgroups(n, alt_grouplist); |
| 7173 | if (n == -1) { |
| 7174 | PyMem_Free(alt_grouplist); |
| 7175 | return posix_error(); |
| 7176 | } |
| 7177 | } |
| 7178 | } else { |
| 7179 | return posix_error(); |
| 7180 | } |
| 7181 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7182 | #endif |
| 7183 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7184 | result = PyList_New(n); |
| 7185 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7186 | int i; |
| 7187 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7188 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7189 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7190 | Py_DECREF(result); |
| 7191 | result = NULL; |
| 7192 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7193 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7194 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7195 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7196 | } |
| 7197 | |
| 7198 | if (alt_grouplist != grouplist) { |
| 7199 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7200 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7201 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7202 | return result; |
| 7203 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7204 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7205 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7206 | #ifdef HAVE_INITGROUPS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7207 | #ifdef __APPLE__ |
| 7208 | /*[clinic input] |
| 7209 | os.initgroups |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7210 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7211 | username as oname: FSConverter |
| 7212 | gid: int |
| 7213 | / |
| 7214 | |
| 7215 | Initialize the group access list. |
| 7216 | |
| 7217 | Call the system initgroups() to initialize the group access list with all of |
| 7218 | the groups of which the specified username is a member, plus the specified |
| 7219 | group id. |
| 7220 | [clinic start generated code]*/ |
| 7221 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7222 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7223 | os_initgroups_impl(PyObject *module, PyObject *oname, int gid) |
| 7224 | /*[clinic end generated code: output=7f074d30a425fd3a input=df3d54331b0af204]*/ |
| 7225 | #else |
| 7226 | /*[clinic input] |
| 7227 | os.initgroups |
| 7228 | |
| 7229 | username as oname: FSConverter |
| 7230 | gid: gid_t |
| 7231 | / |
| 7232 | |
| 7233 | Initialize the group access list. |
| 7234 | |
| 7235 | Call the system initgroups() to initialize the group access list with all of |
| 7236 | the groups of which the specified username is a member, plus the specified |
| 7237 | group id. |
| 7238 | [clinic start generated code]*/ |
| 7239 | |
| 7240 | static PyObject * |
| 7241 | os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid) |
| 7242 | /*[clinic end generated code: output=59341244521a9e3f input=0cb91bdc59a4c564]*/ |
| 7243 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7244 | { |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7245 | const char *username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7246 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7247 | if (initgroups(username, gid) == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7248 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7249 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7250 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7251 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7252 | #endif /* HAVE_INITGROUPS */ |
| 7253 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7254 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7255 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7256 | /*[clinic input] |
| 7257 | os.getpgid |
| 7258 | |
| 7259 | pid: pid_t |
| 7260 | |
| 7261 | Call the system call getpgid(), and return the result. |
| 7262 | [clinic start generated code]*/ |
| 7263 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7264 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7265 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 7266 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7267 | { |
| 7268 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7269 | if (pgid < 0) |
| 7270 | return posix_error(); |
| 7271 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7272 | } |
| 7273 | #endif /* HAVE_GETPGID */ |
| 7274 | |
| 7275 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7276 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7277 | /*[clinic input] |
| 7278 | os.getpgrp |
| 7279 | |
| 7280 | Return the current process group id. |
| 7281 | [clinic start generated code]*/ |
| 7282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7283 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7284 | os_getpgrp_impl(PyObject *module) |
| 7285 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7286 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7287 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7288 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7289 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7290 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7291 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7292 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7293 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7294 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7295 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7296 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7297 | /*[clinic input] |
| 7298 | os.setpgrp |
| 7299 | |
| 7300 | Make the current process the leader of its process group. |
| 7301 | [clinic start generated code]*/ |
| 7302 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7303 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7304 | os_setpgrp_impl(PyObject *module) |
| 7305 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7306 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7307 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7308 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7309 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7310 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7311 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7312 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7313 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7314 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7315 | #endif /* HAVE_SETPGRP */ |
| 7316 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7317 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7318 | |
| 7319 | #ifdef MS_WINDOWS |
| 7320 | #include <tlhelp32.h> |
| 7321 | |
| 7322 | static PyObject* |
| 7323 | win32_getppid() |
| 7324 | { |
| 7325 | HANDLE snapshot; |
| 7326 | pid_t mypid; |
| 7327 | PyObject* result = NULL; |
| 7328 | BOOL have_record; |
| 7329 | PROCESSENTRY32 pe; |
| 7330 | |
| 7331 | mypid = getpid(); /* This function never fails */ |
| 7332 | |
| 7333 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 7334 | if (snapshot == INVALID_HANDLE_VALUE) |
| 7335 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 7336 | |
| 7337 | pe.dwSize = sizeof(pe); |
| 7338 | have_record = Process32First(snapshot, &pe); |
| 7339 | while (have_record) { |
| 7340 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 7341 | /* We could cache the ulong value in a static variable. */ |
| 7342 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 7343 | break; |
| 7344 | } |
| 7345 | |
| 7346 | have_record = Process32Next(snapshot, &pe); |
| 7347 | } |
| 7348 | |
| 7349 | /* If our loop exits and our pid was not found (result will be NULL) |
| 7350 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 7351 | * error anyway, so let's raise it. */ |
| 7352 | if (!result) |
| 7353 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7354 | |
| 7355 | CloseHandle(snapshot); |
| 7356 | |
| 7357 | return result; |
| 7358 | } |
| 7359 | #endif /*MS_WINDOWS*/ |
| 7360 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7361 | |
| 7362 | /*[clinic input] |
| 7363 | os.getppid |
| 7364 | |
| 7365 | Return the parent's process id. |
| 7366 | |
| 7367 | If the parent process has already exited, Windows machines will still |
| 7368 | return its id; others systems will return the id of the 'init' process (1). |
| 7369 | [clinic start generated code]*/ |
| 7370 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7371 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7372 | os_getppid_impl(PyObject *module) |
| 7373 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7374 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7375 | #ifdef MS_WINDOWS |
| 7376 | return win32_getppid(); |
| 7377 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7378 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7379 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7380 | } |
| 7381 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7382 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7383 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7384 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7385 | /*[clinic input] |
| 7386 | os.getlogin |
| 7387 | |
| 7388 | Return the actual login name. |
| 7389 | [clinic start generated code]*/ |
| 7390 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7391 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7392 | os_getlogin_impl(PyObject *module) |
| 7393 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7394 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7395 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7396 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7397 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7398 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7399 | |
| 7400 | if (GetUserNameW(user_name, &num_chars)) { |
| 7401 | /* num_chars is the number of unicode chars plus null terminator */ |
| 7402 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7403 | } |
| 7404 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7405 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7406 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7407 | char *name; |
| 7408 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7409 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7410 | errno = 0; |
| 7411 | name = getlogin(); |
| 7412 | if (name == NULL) { |
| 7413 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7414 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7415 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7416 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7417 | } |
| 7418 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7419 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7420 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7421 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7422 | return result; |
| 7423 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7424 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7425 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7426 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7427 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7428 | /*[clinic input] |
| 7429 | os.getuid |
| 7430 | |
| 7431 | Return the current process's user id. |
| 7432 | [clinic start generated code]*/ |
| 7433 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7434 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7435 | os_getuid_impl(PyObject *module) |
| 7436 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7437 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7438 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7439 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7440 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7441 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7442 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7443 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7444 | #define HAVE_KILL |
| 7445 | #endif /* MS_WINDOWS */ |
| 7446 | |
| 7447 | #ifdef HAVE_KILL |
| 7448 | /*[clinic input] |
| 7449 | os.kill |
| 7450 | |
| 7451 | pid: pid_t |
| 7452 | signal: Py_ssize_t |
| 7453 | / |
| 7454 | |
| 7455 | Kill a process with a signal. |
| 7456 | [clinic start generated code]*/ |
| 7457 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7458 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7459 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7460 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7461 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7462 | if (PySys_Audit("os.kill", "in", pid, signal) < 0) { |
| 7463 | return NULL; |
| 7464 | } |
| 7465 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7466 | if (kill(pid, (int)signal) == -1) |
| 7467 | return posix_error(); |
| 7468 | Py_RETURN_NONE; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7469 | #else /* !MS_WINDOWS */ |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7470 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7471 | DWORD sig = (DWORD)signal; |
| 7472 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7473 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7474 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7475 | /* Console processes which share a common console can be sent CTRL+C or |
| 7476 | CTRL+BREAK events, provided they handle said events. */ |
| 7477 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7478 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7479 | err = GetLastError(); |
| 7480 | PyErr_SetFromWindowsErr(err); |
| 7481 | } |
| 7482 | else |
| 7483 | Py_RETURN_NONE; |
| 7484 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7485 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7486 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7487 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7488 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7489 | if (handle == NULL) { |
| 7490 | err = GetLastError(); |
| 7491 | return PyErr_SetFromWindowsErr(err); |
| 7492 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7493 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7494 | if (TerminateProcess(handle, sig) == 0) { |
| 7495 | err = GetLastError(); |
| 7496 | result = PyErr_SetFromWindowsErr(err); |
| 7497 | } else { |
| 7498 | Py_INCREF(Py_None); |
| 7499 | result = Py_None; |
| 7500 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7501 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7502 | CloseHandle(handle); |
| 7503 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7504 | #endif /* !MS_WINDOWS */ |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7505 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7506 | #endif /* HAVE_KILL */ |
| 7507 | |
| 7508 | |
| 7509 | #ifdef HAVE_KILLPG |
| 7510 | /*[clinic input] |
| 7511 | os.killpg |
| 7512 | |
| 7513 | pgid: pid_t |
| 7514 | signal: int |
| 7515 | / |
| 7516 | |
| 7517 | Kill a process group with a signal. |
| 7518 | [clinic start generated code]*/ |
| 7519 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7520 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7521 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7522 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7523 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7524 | if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) { |
| 7525 | return NULL; |
| 7526 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7527 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7528 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7529 | take the same type. Moreover, pid_t is always at least as wide as |
| 7530 | int (else compilation of this module fails), which is safe. */ |
| 7531 | if (killpg(pgid, signal) == -1) |
| 7532 | return posix_error(); |
| 7533 | Py_RETURN_NONE; |
| 7534 | } |
| 7535 | #endif /* HAVE_KILLPG */ |
| 7536 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7537 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7538 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7539 | #ifdef HAVE_SYS_LOCK_H |
| 7540 | #include <sys/lock.h> |
| 7541 | #endif |
| 7542 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7543 | /*[clinic input] |
| 7544 | os.plock |
| 7545 | op: int |
| 7546 | / |
| 7547 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7548 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7549 | [clinic start generated code]*/ |
| 7550 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7551 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7552 | os_plock_impl(PyObject *module, int op) |
| 7553 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7554 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7555 | if (plock(op) == -1) |
| 7556 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7557 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7558 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7559 | #endif /* HAVE_PLOCK */ |
| 7560 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7561 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7562 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7563 | /*[clinic input] |
| 7564 | os.setuid |
| 7565 | |
| 7566 | uid: uid_t |
| 7567 | / |
| 7568 | |
| 7569 | Set the current process's user id. |
| 7570 | [clinic start generated code]*/ |
| 7571 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7572 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7573 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7574 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7575 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7576 | if (setuid(uid) < 0) |
| 7577 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7578 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7579 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7580 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7581 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7582 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7583 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7584 | /*[clinic input] |
| 7585 | os.seteuid |
| 7586 | |
| 7587 | euid: uid_t |
| 7588 | / |
| 7589 | |
| 7590 | Set the current process's effective user id. |
| 7591 | [clinic start generated code]*/ |
| 7592 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7593 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7594 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7595 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7596 | { |
| 7597 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7598 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7599 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7600 | } |
| 7601 | #endif /* HAVE_SETEUID */ |
| 7602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7603 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7604 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7605 | /*[clinic input] |
| 7606 | os.setegid |
| 7607 | |
| 7608 | egid: gid_t |
| 7609 | / |
| 7610 | |
| 7611 | Set the current process's effective group id. |
| 7612 | [clinic start generated code]*/ |
| 7613 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7614 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7615 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7616 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7617 | { |
| 7618 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7619 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7620 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7621 | } |
| 7622 | #endif /* HAVE_SETEGID */ |
| 7623 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7624 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7625 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7626 | /*[clinic input] |
| 7627 | os.setreuid |
| 7628 | |
| 7629 | ruid: uid_t |
| 7630 | euid: uid_t |
| 7631 | / |
| 7632 | |
| 7633 | Set the current process's real and effective user ids. |
| 7634 | [clinic start generated code]*/ |
| 7635 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7636 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7637 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7638 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7639 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7640 | if (setreuid(ruid, euid) < 0) { |
| 7641 | return posix_error(); |
| 7642 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7643 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7644 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7645 | } |
| 7646 | #endif /* HAVE_SETREUID */ |
| 7647 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7648 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7649 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7650 | /*[clinic input] |
| 7651 | os.setregid |
| 7652 | |
| 7653 | rgid: gid_t |
| 7654 | egid: gid_t |
| 7655 | / |
| 7656 | |
| 7657 | Set the current process's real and effective group ids. |
| 7658 | [clinic start generated code]*/ |
| 7659 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7660 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7661 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7662 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7663 | { |
| 7664 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7665 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7666 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7667 | } |
| 7668 | #endif /* HAVE_SETREGID */ |
| 7669 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7670 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7671 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7672 | /*[clinic input] |
| 7673 | os.setgid |
| 7674 | gid: gid_t |
| 7675 | / |
| 7676 | |
| 7677 | Set the current process's group id. |
| 7678 | [clinic start generated code]*/ |
| 7679 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7680 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7681 | os_setgid_impl(PyObject *module, gid_t gid) |
| 7682 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7683 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7684 | if (setgid(gid) < 0) |
| 7685 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7686 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7687 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7688 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7689 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7690 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7691 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7692 | /*[clinic input] |
| 7693 | os.setgroups |
| 7694 | |
| 7695 | groups: object |
| 7696 | / |
| 7697 | |
| 7698 | Set the groups of the current process to list. |
| 7699 | [clinic start generated code]*/ |
| 7700 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7701 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7702 | os_setgroups(PyObject *module, PyObject *groups) |
| 7703 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7704 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7705 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7706 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7707 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7708 | if (!PySequence_Check(groups)) { |
| 7709 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7710 | return NULL; |
| 7711 | } |
| 7712 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7713 | if (len < 0) { |
| 7714 | return NULL; |
| 7715 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7716 | if (len > MAX_GROUPS) { |
| 7717 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7718 | return NULL; |
| 7719 | } |
| 7720 | for(i = 0; i < len; i++) { |
| 7721 | PyObject *elem; |
| 7722 | elem = PySequence_GetItem(groups, i); |
| 7723 | if (!elem) |
| 7724 | return NULL; |
| 7725 | if (!PyLong_Check(elem)) { |
| 7726 | PyErr_SetString(PyExc_TypeError, |
| 7727 | "groups must be integers"); |
| 7728 | Py_DECREF(elem); |
| 7729 | return NULL; |
| 7730 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7731 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7732 | Py_DECREF(elem); |
| 7733 | return NULL; |
| 7734 | } |
| 7735 | } |
| 7736 | Py_DECREF(elem); |
| 7737 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7738 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7739 | if (setgroups(len, grouplist) < 0) |
| 7740 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7741 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7742 | } |
| 7743 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7744 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7745 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7746 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7747 | wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7748 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7749 | PyObject *result; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7750 | PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7751 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7752 | if (pid == -1) |
| 7753 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7754 | |
Zackery Spytz | 682107c | 2019-09-09 09:48:32 -0600 | [diff] [blame] | 7755 | // If wait succeeded but no child was ready to report status, ru will not |
| 7756 | // have been populated. |
| 7757 | if (pid == 0) { |
| 7758 | memset(ru, 0, sizeof(*ru)); |
| 7759 | } |
| 7760 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7761 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7762 | if (m == NULL) |
| 7763 | return NULL; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7764 | struct_rusage = PyObject_GetAttr(m, get_posix_state(module)->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7765 | Py_DECREF(m); |
| 7766 | if (struct_rusage == NULL) |
| 7767 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7768 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7769 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7770 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
Eddie Elizondo | e4db1f0 | 2019-11-25 19:07:37 -0800 | [diff] [blame] | 7771 | Py_DECREF(struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7772 | if (!result) |
| 7773 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7774 | |
| 7775 | #ifndef doubletime |
| 7776 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7777 | #endif |
| 7778 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7779 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7780 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7781 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7782 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7783 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7784 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7785 | SET_INT(result, 2, ru->ru_maxrss); |
| 7786 | SET_INT(result, 3, ru->ru_ixrss); |
| 7787 | SET_INT(result, 4, ru->ru_idrss); |
| 7788 | SET_INT(result, 5, ru->ru_isrss); |
| 7789 | SET_INT(result, 6, ru->ru_minflt); |
| 7790 | SET_INT(result, 7, ru->ru_majflt); |
| 7791 | SET_INT(result, 8, ru->ru_nswap); |
| 7792 | SET_INT(result, 9, ru->ru_inblock); |
| 7793 | SET_INT(result, 10, ru->ru_oublock); |
| 7794 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7795 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7796 | SET_INT(result, 13, ru->ru_nsignals); |
| 7797 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7798 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7799 | #undef SET_INT |
| 7800 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7801 | if (PyErr_Occurred()) { |
| 7802 | Py_DECREF(result); |
| 7803 | return NULL; |
| 7804 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7805 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7806 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7807 | } |
| 7808 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7809 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7810 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7811 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7812 | /*[clinic input] |
| 7813 | os.wait3 |
| 7814 | |
| 7815 | options: int |
| 7816 | Wait for completion of a child process. |
| 7817 | |
| 7818 | Returns a tuple of information about the child process: |
| 7819 | (pid, status, rusage) |
| 7820 | [clinic start generated code]*/ |
| 7821 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7822 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7823 | os_wait3_impl(PyObject *module, int options) |
| 7824 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7825 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7826 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7827 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7828 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7829 | WAIT_TYPE status; |
| 7830 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7831 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7832 | do { |
| 7833 | Py_BEGIN_ALLOW_THREADS |
| 7834 | pid = wait3(&status, options, &ru); |
| 7835 | Py_END_ALLOW_THREADS |
| 7836 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7837 | if (pid < 0) |
| 7838 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7839 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7840 | return wait_helper(module, pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7841 | } |
| 7842 | #endif /* HAVE_WAIT3 */ |
| 7843 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7844 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7845 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7846 | /*[clinic input] |
| 7847 | |
| 7848 | os.wait4 |
| 7849 | |
| 7850 | pid: pid_t |
| 7851 | options: int |
| 7852 | |
| 7853 | Wait for completion of a specific child process. |
| 7854 | |
| 7855 | Returns a tuple of information about the child process: |
| 7856 | (pid, status, rusage) |
| 7857 | [clinic start generated code]*/ |
| 7858 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7859 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7860 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7861 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7862 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7863 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7864 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7865 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7866 | WAIT_TYPE status; |
| 7867 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7868 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7869 | do { |
| 7870 | Py_BEGIN_ALLOW_THREADS |
| 7871 | res = wait4(pid, &status, options, &ru); |
| 7872 | Py_END_ALLOW_THREADS |
| 7873 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7874 | if (res < 0) |
| 7875 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7876 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7877 | return wait_helper(module, res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7878 | } |
| 7879 | #endif /* HAVE_WAIT4 */ |
| 7880 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7881 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7882 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7883 | /*[clinic input] |
| 7884 | os.waitid |
| 7885 | |
| 7886 | idtype: idtype_t |
| 7887 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7888 | id: id_t |
| 7889 | The id to wait on. |
| 7890 | options: int |
| 7891 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7892 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7893 | / |
| 7894 | |
| 7895 | Returns the result of waiting for a process or processes. |
| 7896 | |
| 7897 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7898 | no children in a waitable state. |
| 7899 | [clinic start generated code]*/ |
| 7900 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7901 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7902 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7903 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7904 | { |
| 7905 | PyObject *result; |
| 7906 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7907 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7908 | siginfo_t si; |
| 7909 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7910 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7911 | do { |
| 7912 | Py_BEGIN_ALLOW_THREADS |
| 7913 | res = waitid(idtype, id, &si, options); |
| 7914 | Py_END_ALLOW_THREADS |
| 7915 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7916 | if (res < 0) |
| 7917 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7918 | |
| 7919 | if (si.si_pid == 0) |
| 7920 | Py_RETURN_NONE; |
| 7921 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 7922 | PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7923 | result = PyStructSequence_New((PyTypeObject *)WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7924 | if (!result) |
| 7925 | return NULL; |
| 7926 | |
| 7927 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7928 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7929 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7930 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7931 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7932 | if (PyErr_Occurred()) { |
| 7933 | Py_DECREF(result); |
| 7934 | return NULL; |
| 7935 | } |
| 7936 | |
| 7937 | return result; |
| 7938 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7939 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7940 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7941 | |
| 7942 | #if defined(HAVE_WAITPID) |
| 7943 | /*[clinic input] |
| 7944 | os.waitpid |
| 7945 | pid: pid_t |
| 7946 | options: int |
| 7947 | / |
| 7948 | |
| 7949 | Wait for completion of a given child process. |
| 7950 | |
| 7951 | Returns a tuple of information regarding the child process: |
| 7952 | (pid, status) |
| 7953 | |
| 7954 | The options argument is ignored on Windows. |
| 7955 | [clinic start generated code]*/ |
| 7956 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7957 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7958 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7959 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7960 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7961 | pid_t res; |
| 7962 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7963 | WAIT_TYPE status; |
| 7964 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7965 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7966 | do { |
| 7967 | Py_BEGIN_ALLOW_THREADS |
| 7968 | res = waitpid(pid, &status, options); |
| 7969 | Py_END_ALLOW_THREADS |
| 7970 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7971 | if (res < 0) |
| 7972 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7973 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7974 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7975 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7976 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7977 | /* 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] | 7978 | /*[clinic input] |
| 7979 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7980 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7981 | options: int |
| 7982 | / |
| 7983 | |
| 7984 | Wait for completion of a given process. |
| 7985 | |
| 7986 | Returns a tuple of information regarding the process: |
| 7987 | (pid, status << 8) |
| 7988 | |
| 7989 | The options argument is ignored on Windows. |
| 7990 | [clinic start generated code]*/ |
| 7991 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7992 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7993 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7994 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7995 | { |
| 7996 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7997 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7998 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7999 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8000 | do { |
| 8001 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 8002 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8003 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 8004 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8005 | Py_END_ALLOW_THREADS |
| 8006 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8007 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8008 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8009 | |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 8010 | unsigned long long ustatus = (unsigned int)status; |
| 8011 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8012 | /* 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] | 8013 | return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8014 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8015 | #endif |
| 8016 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8017 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8018 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8019 | /*[clinic input] |
| 8020 | os.wait |
| 8021 | |
| 8022 | Wait for completion of a child process. |
| 8023 | |
| 8024 | Returns a tuple of information about the child process: |
| 8025 | (pid, status) |
| 8026 | [clinic start generated code]*/ |
| 8027 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8028 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8029 | os_wait_impl(PyObject *module) |
| 8030 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 8031 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8032 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8033 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8034 | WAIT_TYPE status; |
| 8035 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8036 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8037 | do { |
| 8038 | Py_BEGIN_ALLOW_THREADS |
| 8039 | pid = wait(&status); |
| 8040 | Py_END_ALLOW_THREADS |
| 8041 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8042 | if (pid < 0) |
| 8043 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8044 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8045 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8046 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8047 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8048 | |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 8049 | #if defined(__linux__) && defined(__NR_pidfd_open) |
| 8050 | /*[clinic input] |
| 8051 | os.pidfd_open |
| 8052 | pid: pid_t |
| 8053 | flags: unsigned_int = 0 |
| 8054 | |
| 8055 | Return a file descriptor referring to the process *pid*. |
| 8056 | |
| 8057 | The descriptor can be used to perform process management without races and |
| 8058 | signals. |
| 8059 | [clinic start generated code]*/ |
| 8060 | |
| 8061 | static PyObject * |
| 8062 | os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags) |
| 8063 | /*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/ |
| 8064 | { |
| 8065 | int fd = syscall(__NR_pidfd_open, pid, flags); |
| 8066 | if (fd < 0) { |
| 8067 | return posix_error(); |
| 8068 | } |
| 8069 | return PyLong_FromLong(fd); |
| 8070 | } |
| 8071 | #endif |
| 8072 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8073 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8074 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8075 | /*[clinic input] |
| 8076 | os.readlink |
| 8077 | |
| 8078 | path: path_t |
| 8079 | * |
| 8080 | dir_fd: dir_fd(requires='readlinkat') = None |
| 8081 | |
| 8082 | Return a string representing the path to which the symbolic link points. |
| 8083 | |
| 8084 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8085 | and path should be relative; path will then be relative to that directory. |
| 8086 | |
| 8087 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 8088 | using it will raise a NotImplementedError. |
| 8089 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8090 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8091 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8092 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 8093 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8094 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8095 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 8096 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8097 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8098 | |
| 8099 | Py_BEGIN_ALLOW_THREADS |
| 8100 | #ifdef HAVE_READLINKAT |
| 8101 | if (dir_fd != DEFAULT_DIR_FD) |
| 8102 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 8103 | else |
| 8104 | #endif |
| 8105 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 8106 | Py_END_ALLOW_THREADS |
| 8107 | |
| 8108 | if (length < 0) { |
| 8109 | return path_error(path); |
| 8110 | } |
| 8111 | buffer[length] = '\0'; |
| 8112 | |
| 8113 | if (PyUnicode_Check(path->object)) |
| 8114 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 8115 | else |
| 8116 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8117 | #elif defined(MS_WINDOWS) |
| 8118 | DWORD n_bytes_returned; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8119 | DWORD io_result = 0; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8120 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8121 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 8122 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8123 | PyObject *result = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8124 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8125 | /* First get a handle to the reparse point */ |
| 8126 | Py_BEGIN_ALLOW_THREADS |
| 8127 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8128 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8129 | 0, |
| 8130 | 0, |
| 8131 | 0, |
| 8132 | OPEN_EXISTING, |
| 8133 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 8134 | 0); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8135 | if (reparse_point_handle != INVALID_HANDLE_VALUE) { |
| 8136 | /* New call DeviceIoControl to read the reparse point */ |
| 8137 | io_result = DeviceIoControl( |
| 8138 | reparse_point_handle, |
| 8139 | FSCTL_GET_REPARSE_POINT, |
| 8140 | 0, 0, /* in buffer */ |
| 8141 | target_buffer, sizeof(target_buffer), |
| 8142 | &n_bytes_returned, |
| 8143 | 0 /* we're not using OVERLAPPED_IO */ |
| 8144 | ); |
| 8145 | CloseHandle(reparse_point_handle); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8146 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8147 | Py_END_ALLOW_THREADS |
| 8148 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8149 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8150 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8151 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8152 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8153 | wchar_t *name = NULL; |
| 8154 | Py_ssize_t nameLen = 0; |
| 8155 | if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) |
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 | name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 8158 | rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 8159 | nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
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 | else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) |
| 8162 | { |
| 8163 | name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer + |
| 8164 | rdb->MountPointReparseBuffer.SubstituteNameOffset); |
| 8165 | nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
| 8166 | } |
| 8167 | else |
| 8168 | { |
| 8169 | PyErr_SetString(PyExc_ValueError, "not a symbolic link"); |
| 8170 | } |
| 8171 | if (name) { |
| 8172 | if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) { |
| 8173 | /* Our buffer is mutable, so this is okay */ |
| 8174 | name[1] = L'\\'; |
| 8175 | } |
| 8176 | result = PyUnicode_FromWideChar(name, nameLen); |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8177 | if (result && path->narrow) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8178 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
| 8179 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8180 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8181 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8182 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8183 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8184 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8185 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8186 | #if defined(MS_WINDOWS) |
| 8187 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8188 | /* Remove the last portion of the path - return 0 on success */ |
| 8189 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8190 | _dirnameW(WCHAR *path) |
| 8191 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8192 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8193 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 8194 | if (length == MAX_PATH) { |
| 8195 | return -1; |
| 8196 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8197 | |
| 8198 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8199 | for(ptr = path + length; ptr != path; ptr--) { |
| 8200 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8201 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8202 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8203 | } |
| 8204 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8205 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8206 | } |
| 8207 | |
Minmin Gong | 7f21c9a | 2020-05-18 09:17:19 -0700 | [diff] [blame] | 8208 | #endif |
| 8209 | |
| 8210 | #ifdef HAVE_SYMLINK |
| 8211 | |
| 8212 | #if defined(MS_WINDOWS) |
| 8213 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8214 | /* Is this path absolute? */ |
| 8215 | static int |
| 8216 | _is_absW(const WCHAR *path) |
| 8217 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8218 | return path[0] == L'\\' || path[0] == L'/' || |
| 8219 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8220 | } |
| 8221 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8222 | /* join root and rest with a backslash - return 0 on success */ |
| 8223 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8224 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 8225 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8226 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8227 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8228 | } |
| 8229 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8230 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 8231 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8232 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8233 | |
| 8234 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 8235 | return -1; |
| 8236 | } |
| 8237 | |
| 8238 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8239 | } |
| 8240 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8241 | /* Return True if the path at src relative to dest is a directory */ |
| 8242 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8243 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8244 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8245 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 8246 | WCHAR dest_parent[MAX_PATH]; |
| 8247 | WCHAR src_resolved[MAX_PATH] = L""; |
| 8248 | |
| 8249 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8250 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 8251 | _dirnameW(dest_parent)) { |
| 8252 | return 0; |
| 8253 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8254 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8255 | if (_joinW(src_resolved, dest_parent, src)) { |
| 8256 | return 0; |
| 8257 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8258 | return ( |
| 8259 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 8260 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 8261 | ); |
| 8262 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8263 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8264 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8265 | |
| 8266 | /*[clinic input] |
| 8267 | os.symlink |
| 8268 | src: path_t |
| 8269 | dst: path_t |
| 8270 | target_is_directory: bool = False |
| 8271 | * |
| 8272 | dir_fd: dir_fd(requires='symlinkat')=None |
| 8273 | |
| 8274 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 8275 | |
| 8276 | Create a symbolic link pointing to src named dst. |
| 8277 | |
| 8278 | target_is_directory is required on Windows if the target is to be |
| 8279 | interpreted as a directory. (On Windows, symlink requires |
| 8280 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 8281 | target_is_directory is ignored on non-Windows platforms. |
| 8282 | |
| 8283 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8284 | and path should be relative; path will then be relative to that directory. |
| 8285 | dir_fd may not be implemented on your platform. |
| 8286 | If it is unavailable, using it will raise a NotImplementedError. |
| 8287 | |
| 8288 | [clinic start generated code]*/ |
| 8289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8290 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8291 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8292 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8293 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8294 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8295 | #ifdef MS_WINDOWS |
| 8296 | DWORD result; |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8297 | DWORD flags = 0; |
| 8298 | |
| 8299 | /* Assumed true, set to false if detected to not be available. */ |
| 8300 | static int windows_has_symlink_unprivileged_flag = TRUE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8301 | #else |
| 8302 | int result; |
| 8303 | #endif |
| 8304 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 8305 | if (PySys_Audit("os.symlink", "OOi", src->object, dst->object, |
| 8306 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 8307 | return NULL; |
| 8308 | } |
| 8309 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8310 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8311 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8312 | if (windows_has_symlink_unprivileged_flag) { |
| 8313 | /* Allow non-admin symlinks if system allows it. */ |
| 8314 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 8315 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8316 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8317 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8318 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8319 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
| 8320 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { |
| 8321 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 8322 | } |
| 8323 | |
| 8324 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8325 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8326 | Py_END_ALLOW_THREADS |
| 8327 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8328 | if (windows_has_symlink_unprivileged_flag && !result && |
| 8329 | ERROR_INVALID_PARAMETER == GetLastError()) { |
| 8330 | |
| 8331 | Py_BEGIN_ALLOW_THREADS |
| 8332 | _Py_BEGIN_SUPPRESS_IPH |
| 8333 | /* This error might be caused by |
| 8334 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. |
| 8335 | Try again, and update windows_has_symlink_unprivileged_flag if we |
| 8336 | are successful this time. |
| 8337 | |
| 8338 | NOTE: There is a risk of a race condition here if there are other |
| 8339 | conditions than the flag causing ERROR_INVALID_PARAMETER, and |
| 8340 | another process (or thread) changes that condition in between our |
| 8341 | calls to CreateSymbolicLink. |
| 8342 | */ |
| 8343 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 8344 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
| 8345 | _Py_END_SUPPRESS_IPH |
| 8346 | Py_END_ALLOW_THREADS |
| 8347 | |
| 8348 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { |
| 8349 | windows_has_symlink_unprivileged_flag = FALSE; |
| 8350 | } |
| 8351 | } |
| 8352 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8353 | if (!result) |
| 8354 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8355 | |
| 8356 | #else |
| 8357 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8358 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 8359 | PyErr_SetString(PyExc_ValueError, |
| 8360 | "symlink: src and dst must be the same type"); |
| 8361 | return NULL; |
| 8362 | } |
| 8363 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8364 | Py_BEGIN_ALLOW_THREADS |
| 8365 | #if HAVE_SYMLINKAT |
| 8366 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8367 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8368 | else |
| 8369 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8370 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8371 | Py_END_ALLOW_THREADS |
| 8372 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8373 | if (result) |
| 8374 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8375 | #endif |
| 8376 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8377 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8378 | } |
| 8379 | #endif /* HAVE_SYMLINK */ |
| 8380 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8381 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8382 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8383 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8384 | static PyStructSequence_Field times_result_fields[] = { |
| 8385 | {"user", "user time"}, |
| 8386 | {"system", "system time"}, |
| 8387 | {"children_user", "user time of children"}, |
| 8388 | {"children_system", "system time of children"}, |
| 8389 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 8390 | {NULL} |
| 8391 | }; |
| 8392 | |
| 8393 | PyDoc_STRVAR(times_result__doc__, |
| 8394 | "times_result: Result from os.times().\n\n\ |
| 8395 | This object may be accessed either as a tuple of\n\ |
| 8396 | (user, system, children_user, children_system, elapsed),\n\ |
| 8397 | or via the attributes user, system, children_user, children_system,\n\ |
| 8398 | and elapsed.\n\ |
| 8399 | \n\ |
| 8400 | See os.times for more information."); |
| 8401 | |
| 8402 | static PyStructSequence_Desc times_result_desc = { |
| 8403 | "times_result", /* name */ |
| 8404 | times_result__doc__, /* doc */ |
| 8405 | times_result_fields, |
| 8406 | 5 |
| 8407 | }; |
| 8408 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8409 | #ifdef MS_WINDOWS |
| 8410 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 8411 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8412 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8413 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8414 | |
| 8415 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8416 | build_times_result(PyObject *module, double user, double system, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8417 | double children_user, double children_system, |
| 8418 | double elapsed) |
| 8419 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8420 | PyObject *TimesResultType = get_posix_state(module)->TimesResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8421 | PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8422 | if (value == NULL) |
| 8423 | return NULL; |
| 8424 | |
| 8425 | #define SET(i, field) \ |
| 8426 | { \ |
| 8427 | PyObject *o = PyFloat_FromDouble(field); \ |
| 8428 | if (!o) { \ |
| 8429 | Py_DECREF(value); \ |
| 8430 | return NULL; \ |
| 8431 | } \ |
| 8432 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 8433 | } \ |
| 8434 | |
| 8435 | SET(0, user); |
| 8436 | SET(1, system); |
| 8437 | SET(2, children_user); |
| 8438 | SET(3, children_system); |
| 8439 | SET(4, elapsed); |
| 8440 | |
| 8441 | #undef SET |
| 8442 | |
| 8443 | return value; |
| 8444 | } |
| 8445 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8446 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8447 | #ifndef MS_WINDOWS |
| 8448 | #define NEED_TICKS_PER_SECOND |
| 8449 | static long ticks_per_second = -1; |
| 8450 | #endif /* MS_WINDOWS */ |
| 8451 | |
| 8452 | /*[clinic input] |
| 8453 | os.times |
| 8454 | |
| 8455 | Return a collection containing process timing information. |
| 8456 | |
| 8457 | The object returned behaves like a named tuple with these fields: |
| 8458 | (utime, stime, cutime, cstime, elapsed_time) |
| 8459 | All fields are floating point numbers. |
| 8460 | [clinic start generated code]*/ |
| 8461 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8462 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8463 | os_times_impl(PyObject *module) |
| 8464 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8465 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8466 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8467 | FILETIME create, exit, kernel, user; |
| 8468 | HANDLE hProc; |
| 8469 | hProc = GetCurrentProcess(); |
| 8470 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8471 | /* The fields of a FILETIME structure are the hi and lo part |
| 8472 | of a 64-bit value expressed in 100 nanosecond units. |
| 8473 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8474 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8475 | */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8476 | return build_times_result(module, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8477 | (double)(user.dwHighDateTime*429.4967296 + |
| 8478 | user.dwLowDateTime*1e-7), |
| 8479 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8480 | kernel.dwLowDateTime*1e-7), |
| 8481 | (double)0, |
| 8482 | (double)0, |
| 8483 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8484 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8485 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8486 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8487 | |
| 8488 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8489 | struct tms t; |
| 8490 | clock_t c; |
| 8491 | errno = 0; |
| 8492 | c = times(&t); |
| 8493 | if (c == (clock_t) -1) |
| 8494 | return posix_error(); |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8495 | return build_times_result(module, |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8496 | (double)t.tms_utime / ticks_per_second, |
| 8497 | (double)t.tms_stime / ticks_per_second, |
| 8498 | (double)t.tms_cutime / ticks_per_second, |
| 8499 | (double)t.tms_cstime / ticks_per_second, |
| 8500 | (double)c / ticks_per_second); |
| 8501 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8502 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8503 | #endif /* HAVE_TIMES */ |
| 8504 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8505 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8506 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8507 | /*[clinic input] |
| 8508 | os.getsid |
| 8509 | |
| 8510 | pid: pid_t |
| 8511 | / |
| 8512 | |
| 8513 | Call the system call getsid(pid) and return the result. |
| 8514 | [clinic start generated code]*/ |
| 8515 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8516 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8517 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8518 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8519 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8520 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8521 | sid = getsid(pid); |
| 8522 | if (sid < 0) |
| 8523 | return posix_error(); |
| 8524 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8525 | } |
| 8526 | #endif /* HAVE_GETSID */ |
| 8527 | |
| 8528 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8529 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8530 | /*[clinic input] |
| 8531 | os.setsid |
| 8532 | |
| 8533 | Call the system call setsid(). |
| 8534 | [clinic start generated code]*/ |
| 8535 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8536 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8537 | os_setsid_impl(PyObject *module) |
| 8538 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8539 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8540 | if (setsid() < 0) |
| 8541 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8542 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8543 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8544 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8545 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8546 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8547 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8548 | /*[clinic input] |
| 8549 | os.setpgid |
| 8550 | |
| 8551 | pid: pid_t |
| 8552 | pgrp: pid_t |
| 8553 | / |
| 8554 | |
| 8555 | Call the system call setpgid(pid, pgrp). |
| 8556 | [clinic start generated code]*/ |
| 8557 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8558 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8559 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8560 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8561 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8562 | if (setpgid(pid, pgrp) < 0) |
| 8563 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8564 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8565 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8566 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8567 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8568 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8569 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8570 | /*[clinic input] |
| 8571 | os.tcgetpgrp |
| 8572 | |
| 8573 | fd: int |
| 8574 | / |
| 8575 | |
| 8576 | Return the process group associated with the terminal specified by fd. |
| 8577 | [clinic start generated code]*/ |
| 8578 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8579 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8580 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8581 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8582 | { |
| 8583 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8584 | if (pgid < 0) |
| 8585 | return posix_error(); |
| 8586 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8587 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8588 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8589 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8590 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8591 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8592 | /*[clinic input] |
| 8593 | os.tcsetpgrp |
| 8594 | |
| 8595 | fd: int |
| 8596 | pgid: pid_t |
| 8597 | / |
| 8598 | |
| 8599 | Set the process group associated with the terminal specified by fd. |
| 8600 | [clinic start generated code]*/ |
| 8601 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8602 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8603 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8604 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8605 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8606 | if (tcsetpgrp(fd, pgid) < 0) |
| 8607 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8608 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8609 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8610 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8611 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8612 | /* Functions acting on file descriptors */ |
| 8613 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8614 | #ifdef O_CLOEXEC |
| 8615 | extern int _Py_open_cloexec_works; |
| 8616 | #endif |
| 8617 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8618 | |
| 8619 | /*[clinic input] |
| 8620 | os.open -> int |
| 8621 | path: path_t |
| 8622 | flags: int |
| 8623 | mode: int = 0o777 |
| 8624 | * |
| 8625 | dir_fd: dir_fd(requires='openat') = None |
| 8626 | |
| 8627 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8628 | |
| 8629 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8630 | |
| 8631 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8632 | and path should be relative; path will then be relative to that directory. |
| 8633 | dir_fd may not be implemented on your platform. |
| 8634 | If it is unavailable, using it will raise a NotImplementedError. |
| 8635 | [clinic start generated code]*/ |
| 8636 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8637 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8638 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8639 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8640 | { |
| 8641 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8642 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8643 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8644 | #ifdef O_CLOEXEC |
| 8645 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8646 | #elif !defined(MS_WINDOWS) |
| 8647 | int *atomic_flag_works = NULL; |
| 8648 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8649 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8650 | #ifdef MS_WINDOWS |
| 8651 | flags |= O_NOINHERIT; |
| 8652 | #elif defined(O_CLOEXEC) |
| 8653 | flags |= O_CLOEXEC; |
| 8654 | #endif |
| 8655 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 8656 | if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) { |
| 8657 | return -1; |
| 8658 | } |
| 8659 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8660 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8661 | do { |
| 8662 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8663 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8664 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8665 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8666 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8667 | if (dir_fd != DEFAULT_DIR_FD) |
| 8668 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8669 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8670 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8671 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8672 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8673 | Py_END_ALLOW_THREADS |
| 8674 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8675 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8676 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8677 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8678 | if (!async_err) |
| 8679 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8680 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8681 | } |
| 8682 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8683 | #ifndef MS_WINDOWS |
| 8684 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8685 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8686 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8687 | } |
| 8688 | #endif |
| 8689 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8690 | return fd; |
| 8691 | } |
| 8692 | |
| 8693 | |
| 8694 | /*[clinic input] |
| 8695 | os.close |
| 8696 | |
| 8697 | fd: int |
| 8698 | |
| 8699 | Close a file descriptor. |
| 8700 | [clinic start generated code]*/ |
| 8701 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8702 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8703 | os_close_impl(PyObject *module, int fd) |
| 8704 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8705 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8706 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8707 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8708 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8709 | * for more details. |
| 8710 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8711 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8712 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8713 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8714 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8715 | Py_END_ALLOW_THREADS |
| 8716 | if (res < 0) |
| 8717 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8718 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8719 | } |
| 8720 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8721 | |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8722 | #ifdef HAVE_FDWALK |
| 8723 | static int |
| 8724 | _fdwalk_close_func(void *lohi, int fd) |
| 8725 | { |
| 8726 | int lo = ((int *)lohi)[0]; |
| 8727 | int hi = ((int *)lohi)[1]; |
| 8728 | |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8729 | if (fd >= hi) { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8730 | return 1; |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8731 | } |
| 8732 | else if (fd >= lo) { |
| 8733 | /* Ignore errors */ |
| 8734 | (void)close(fd); |
| 8735 | } |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8736 | return 0; |
| 8737 | } |
| 8738 | #endif /* HAVE_FDWALK */ |
| 8739 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8740 | /*[clinic input] |
| 8741 | os.closerange |
| 8742 | |
| 8743 | fd_low: int |
| 8744 | fd_high: int |
| 8745 | / |
| 8746 | |
| 8747 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8748 | [clinic start generated code]*/ |
| 8749 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8750 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8751 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8752 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8753 | { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8754 | #ifdef HAVE_FDWALK |
| 8755 | int lohi[2]; |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8756 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8757 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8758 | _Py_BEGIN_SUPPRESS_IPH |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8759 | #ifdef HAVE_FDWALK |
| 8760 | lohi[0] = Py_MAX(fd_low, 0); |
| 8761 | lohi[1] = fd_high; |
| 8762 | fdwalk(_fdwalk_close_func, lohi); |
| 8763 | #else |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8764 | fd_low = Py_MAX(fd_low, 0); |
| 8765 | #ifdef __FreeBSD__ |
| 8766 | if (fd_high >= sysconf(_SC_OPEN_MAX)) { |
| 8767 | /* Any errors encountered while closing file descriptors are ignored */ |
| 8768 | closefrom(fd_low); |
| 8769 | } |
| 8770 | else |
| 8771 | #endif |
| 8772 | { |
| 8773 | for (int i = fd_low; i < fd_high; i++) { |
| 8774 | /* Ignore errors */ |
| 8775 | (void)close(i); |
| 8776 | } |
| 8777 | } |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8778 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8779 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8780 | Py_END_ALLOW_THREADS |
| 8781 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8782 | } |
| 8783 | |
| 8784 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8785 | /*[clinic input] |
| 8786 | os.dup -> int |
| 8787 | |
| 8788 | fd: int |
| 8789 | / |
| 8790 | |
| 8791 | Return a duplicate of a file descriptor. |
| 8792 | [clinic start generated code]*/ |
| 8793 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8794 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8795 | os_dup_impl(PyObject *module, int fd) |
| 8796 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8797 | { |
| 8798 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8799 | } |
| 8800 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8801 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8802 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8803 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8804 | fd: int |
| 8805 | fd2: int |
| 8806 | inheritable: bool=True |
| 8807 | |
| 8808 | Duplicate file descriptor. |
| 8809 | [clinic start generated code]*/ |
| 8810 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8811 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8812 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8813 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8814 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8815 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8816 | #if defined(HAVE_DUP3) && \ |
| 8817 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8818 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8819 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8820 | #endif |
| 8821 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8822 | if (fd < 0 || fd2 < 0) { |
| 8823 | posix_error(); |
| 8824 | return -1; |
| 8825 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8826 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8827 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8828 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8829 | * upon close(), and therefore below. |
| 8830 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8831 | #ifdef MS_WINDOWS |
| 8832 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8833 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8834 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8835 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8836 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8837 | if (res < 0) { |
| 8838 | posix_error(); |
| 8839 | return -1; |
| 8840 | } |
| 8841 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8842 | |
| 8843 | /* Character files like console cannot be make non-inheritable */ |
| 8844 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8845 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8846 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8847 | } |
| 8848 | |
| 8849 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8850 | Py_BEGIN_ALLOW_THREADS |
| 8851 | if (!inheritable) |
| 8852 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8853 | else |
| 8854 | res = dup2(fd, fd2); |
| 8855 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8856 | if (res < 0) { |
| 8857 | posix_error(); |
| 8858 | return -1; |
| 8859 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8860 | |
| 8861 | #else |
| 8862 | |
| 8863 | #ifdef HAVE_DUP3 |
| 8864 | if (!inheritable && dup3_works != 0) { |
| 8865 | Py_BEGIN_ALLOW_THREADS |
| 8866 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8867 | Py_END_ALLOW_THREADS |
| 8868 | if (res < 0) { |
| 8869 | if (dup3_works == -1) |
| 8870 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8871 | if (dup3_works) { |
| 8872 | posix_error(); |
| 8873 | return -1; |
| 8874 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8875 | } |
| 8876 | } |
| 8877 | |
| 8878 | if (inheritable || dup3_works == 0) |
| 8879 | { |
| 8880 | #endif |
| 8881 | Py_BEGIN_ALLOW_THREADS |
| 8882 | res = dup2(fd, fd2); |
| 8883 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8884 | if (res < 0) { |
| 8885 | posix_error(); |
| 8886 | return -1; |
| 8887 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8888 | |
| 8889 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8890 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8891 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8892 | } |
| 8893 | #ifdef HAVE_DUP3 |
| 8894 | } |
| 8895 | #endif |
| 8896 | |
| 8897 | #endif |
| 8898 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8899 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8900 | } |
| 8901 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8902 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8903 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8904 | /*[clinic input] |
| 8905 | os.lockf |
| 8906 | |
| 8907 | fd: int |
| 8908 | An open file descriptor. |
| 8909 | command: int |
| 8910 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8911 | length: Py_off_t |
| 8912 | The number of bytes to lock, starting at the current position. |
| 8913 | / |
| 8914 | |
| 8915 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8916 | |
| 8917 | [clinic start generated code]*/ |
| 8918 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8919 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8920 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8921 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8922 | { |
| 8923 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8924 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 8925 | if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) { |
| 8926 | return NULL; |
| 8927 | } |
| 8928 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8929 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8930 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8931 | Py_END_ALLOW_THREADS |
| 8932 | |
| 8933 | if (res < 0) |
| 8934 | return posix_error(); |
| 8935 | |
| 8936 | Py_RETURN_NONE; |
| 8937 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8938 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8939 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8940 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8941 | /*[clinic input] |
| 8942 | os.lseek -> Py_off_t |
| 8943 | |
| 8944 | fd: int |
| 8945 | position: Py_off_t |
| 8946 | how: int |
| 8947 | / |
| 8948 | |
| 8949 | Set the position of a file descriptor. Return the new position. |
| 8950 | |
| 8951 | Return the new cursor position in number of bytes |
| 8952 | relative to the beginning of the file. |
| 8953 | [clinic start generated code]*/ |
| 8954 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8955 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8956 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8957 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8958 | { |
| 8959 | Py_off_t result; |
| 8960 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8961 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8962 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8963 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8964 | case 0: how = SEEK_SET; break; |
| 8965 | case 1: how = SEEK_CUR; break; |
| 8966 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8967 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8968 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8969 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8970 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8971 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8972 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8973 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8974 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8975 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8976 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8977 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8978 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8979 | if (result < 0) |
| 8980 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8981 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8982 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8983 | } |
| 8984 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8986 | /*[clinic input] |
| 8987 | os.read |
| 8988 | fd: int |
| 8989 | length: Py_ssize_t |
| 8990 | / |
| 8991 | |
| 8992 | Read from a file descriptor. Returns a bytes object. |
| 8993 | [clinic start generated code]*/ |
| 8994 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8995 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8996 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 8997 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8998 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8999 | Py_ssize_t n; |
| 9000 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9001 | |
| 9002 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9003 | errno = EINVAL; |
| 9004 | return posix_error(); |
| 9005 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9006 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 9007 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9008 | |
| 9009 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9010 | if (buffer == NULL) |
| 9011 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9012 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9013 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 9014 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9015 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9016 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9017 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9018 | |
| 9019 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9020 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9021 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9022 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9023 | } |
| 9024 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9025 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9026 | || defined(__APPLE__))) \ |
| 9027 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 9028 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9029 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9030 | 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] | 9031 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9032 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9033 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9034 | *iov = PyMem_New(struct iovec, cnt); |
| 9035 | if (*iov == NULL) { |
| 9036 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9037 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9038 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9039 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9040 | *buf = PyMem_New(Py_buffer, cnt); |
| 9041 | if (*buf == NULL) { |
| 9042 | PyMem_Del(*iov); |
| 9043 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9044 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9045 | } |
| 9046 | |
| 9047 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9048 | PyObject *item = PySequence_GetItem(seq, i); |
| 9049 | if (item == NULL) |
| 9050 | goto fail; |
| 9051 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 9052 | Py_DECREF(item); |
| 9053 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9054 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9055 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9056 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9057 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9058 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9059 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9060 | |
| 9061 | fail: |
| 9062 | PyMem_Del(*iov); |
| 9063 | for (j = 0; j < i; j++) { |
| 9064 | PyBuffer_Release(&(*buf)[j]); |
| 9065 | } |
| 9066 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9067 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9068 | } |
| 9069 | |
| 9070 | static void |
| 9071 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 9072 | { |
| 9073 | int i; |
| 9074 | PyMem_Del(iov); |
| 9075 | for (i = 0; i < cnt; i++) { |
| 9076 | PyBuffer_Release(&buf[i]); |
| 9077 | } |
| 9078 | PyMem_Del(buf); |
| 9079 | } |
| 9080 | #endif |
| 9081 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9082 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9083 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9084 | /*[clinic input] |
| 9085 | os.readv -> Py_ssize_t |
| 9086 | |
| 9087 | fd: int |
| 9088 | buffers: object |
| 9089 | / |
| 9090 | |
| 9091 | Read from a file descriptor fd into an iterable of buffers. |
| 9092 | |
| 9093 | The buffers should be mutable buffers accepting bytes. |
| 9094 | readv will transfer data into each buffer until it is full |
| 9095 | and then move on to the next buffer in the sequence to hold |
| 9096 | the rest of the data. |
| 9097 | |
| 9098 | readv returns the total number of bytes read, |
| 9099 | which may be less than the total capacity of all the buffers. |
| 9100 | [clinic start generated code]*/ |
| 9101 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9102 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9103 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 9104 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9105 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9106 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9107 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9108 | struct iovec *iov; |
| 9109 | Py_buffer *buf; |
| 9110 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9111 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9112 | PyErr_SetString(PyExc_TypeError, |
| 9113 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9114 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9115 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9116 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9117 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9118 | if (cnt < 0) |
| 9119 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9120 | |
| 9121 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 9122 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9123 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9124 | do { |
| 9125 | Py_BEGIN_ALLOW_THREADS |
| 9126 | n = readv(fd, iov, cnt); |
| 9127 | Py_END_ALLOW_THREADS |
| 9128 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9129 | |
| 9130 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9131 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9132 | if (!async_err) |
| 9133 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9134 | return -1; |
| 9135 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9136 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9137 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9138 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9139 | #endif /* HAVE_READV */ |
| 9140 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9141 | |
| 9142 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9143 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9144 | os.pread |
| 9145 | |
| 9146 | fd: int |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9147 | length: Py_ssize_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9148 | offset: Py_off_t |
| 9149 | / |
| 9150 | |
| 9151 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 9152 | |
| 9153 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 9154 | the beginning of the file. The file offset remains unchanged. |
| 9155 | [clinic start generated code]*/ |
| 9156 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9157 | static PyObject * |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9158 | os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset) |
| 9159 | /*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9160 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9161 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9162 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9163 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9164 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9165 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9166 | errno = EINVAL; |
| 9167 | return posix_error(); |
| 9168 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9169 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9170 | if (buffer == NULL) |
| 9171 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9172 | |
| 9173 | do { |
| 9174 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9175 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9176 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9177 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9178 | Py_END_ALLOW_THREADS |
| 9179 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9180 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9181 | if (n < 0) { |
| 9182 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9183 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9184 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9185 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9186 | _PyBytes_Resize(&buffer, n); |
| 9187 | return buffer; |
| 9188 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9189 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9190 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9191 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 9192 | /*[clinic input] |
| 9193 | os.preadv -> Py_ssize_t |
| 9194 | |
| 9195 | fd: int |
| 9196 | buffers: object |
| 9197 | offset: Py_off_t |
| 9198 | flags: int = 0 |
| 9199 | / |
| 9200 | |
| 9201 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 9202 | |
| 9203 | Combines the functionality of readv() and pread(). As readv(), it will |
| 9204 | transfer data into each buffer until it is full and then move on to the next |
| 9205 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 9206 | specifies the file offset at which the input operation is to be performed. It |
| 9207 | will return the total number of bytes read (which can be less than the total |
| 9208 | capacity of all the objects). |
| 9209 | |
| 9210 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9211 | |
| 9212 | - RWF_HIPRI |
| 9213 | - RWF_NOWAIT |
| 9214 | |
| 9215 | Using non-zero flags requires Linux 4.6 or newer. |
| 9216 | [clinic start generated code]*/ |
| 9217 | |
| 9218 | static Py_ssize_t |
| 9219 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9220 | int flags) |
| 9221 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 9222 | { |
| 9223 | Py_ssize_t cnt, n; |
| 9224 | int async_err = 0; |
| 9225 | struct iovec *iov; |
| 9226 | Py_buffer *buf; |
| 9227 | |
| 9228 | if (!PySequence_Check(buffers)) { |
| 9229 | PyErr_SetString(PyExc_TypeError, |
| 9230 | "preadv2() arg 2 must be a sequence"); |
| 9231 | return -1; |
| 9232 | } |
| 9233 | |
| 9234 | cnt = PySequence_Size(buffers); |
| 9235 | if (cnt < 0) { |
| 9236 | return -1; |
| 9237 | } |
| 9238 | |
| 9239 | #ifndef HAVE_PREADV2 |
| 9240 | if(flags != 0) { |
| 9241 | argument_unavailable_error("preadv2", "flags"); |
| 9242 | return -1; |
| 9243 | } |
| 9244 | #endif |
| 9245 | |
| 9246 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 9247 | return -1; |
| 9248 | } |
| 9249 | #ifdef HAVE_PREADV2 |
| 9250 | do { |
| 9251 | Py_BEGIN_ALLOW_THREADS |
| 9252 | _Py_BEGIN_SUPPRESS_IPH |
| 9253 | n = preadv2(fd, iov, cnt, offset, flags); |
| 9254 | _Py_END_SUPPRESS_IPH |
| 9255 | Py_END_ALLOW_THREADS |
| 9256 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9257 | #else |
| 9258 | do { |
| 9259 | Py_BEGIN_ALLOW_THREADS |
| 9260 | _Py_BEGIN_SUPPRESS_IPH |
| 9261 | n = preadv(fd, iov, cnt, offset); |
| 9262 | _Py_END_SUPPRESS_IPH |
| 9263 | Py_END_ALLOW_THREADS |
| 9264 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9265 | #endif |
| 9266 | |
| 9267 | iov_cleanup(iov, buf, cnt); |
| 9268 | if (n < 0) { |
| 9269 | if (!async_err) { |
| 9270 | posix_error(); |
| 9271 | } |
| 9272 | return -1; |
| 9273 | } |
| 9274 | |
| 9275 | return n; |
| 9276 | } |
| 9277 | #endif /* HAVE_PREADV */ |
| 9278 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9279 | |
| 9280 | /*[clinic input] |
| 9281 | os.write -> Py_ssize_t |
| 9282 | |
| 9283 | fd: int |
| 9284 | data: Py_buffer |
| 9285 | / |
| 9286 | |
| 9287 | Write a bytes object to a file descriptor. |
| 9288 | [clinic start generated code]*/ |
| 9289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9290 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9291 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 9292 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9293 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9294 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9295 | } |
| 9296 | |
| 9297 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9298 | #ifdef __APPLE__ |
| 9299 | /*[clinic input] |
| 9300 | os.sendfile |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9301 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9302 | out_fd: int |
| 9303 | in_fd: int |
| 9304 | offset: Py_off_t |
| 9305 | count as sbytes: Py_off_t |
| 9306 | headers: object(c_default="NULL") = () |
| 9307 | trailers: object(c_default="NULL") = () |
| 9308 | flags: int = 0 |
| 9309 | |
| 9310 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9311 | [clinic start generated code]*/ |
| 9312 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9313 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9314 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, |
| 9315 | Py_off_t sbytes, PyObject *headers, PyObject *trailers, |
| 9316 | int flags) |
| 9317 | /*[clinic end generated code: output=81c4bcd143f5c82b input=b0d72579d4c69afa]*/ |
| 9318 | #elif defined(__FreeBSD__) || defined(__DragonFly__) |
| 9319 | /*[clinic input] |
| 9320 | os.sendfile |
| 9321 | |
| 9322 | out_fd: int |
| 9323 | in_fd: int |
| 9324 | offset: Py_off_t |
| 9325 | count: Py_ssize_t |
| 9326 | headers: object(c_default="NULL") = () |
| 9327 | trailers: object(c_default="NULL") = () |
| 9328 | flags: int = 0 |
| 9329 | |
| 9330 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9331 | [clinic start generated code]*/ |
| 9332 | |
| 9333 | static PyObject * |
| 9334 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, |
| 9335 | Py_ssize_t count, PyObject *headers, PyObject *trailers, |
| 9336 | int flags) |
| 9337 | /*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/ |
| 9338 | #else |
| 9339 | /*[clinic input] |
| 9340 | os.sendfile |
| 9341 | |
| 9342 | out_fd: int |
| 9343 | in_fd: int |
| 9344 | offset as offobj: object |
| 9345 | count: Py_ssize_t |
| 9346 | |
| 9347 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9348 | [clinic start generated code]*/ |
| 9349 | |
| 9350 | static PyObject * |
| 9351 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj, |
| 9352 | Py_ssize_t count) |
| 9353 | /*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/ |
| 9354 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9355 | { |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9356 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9357 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9358 | |
| 9359 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 9360 | #ifndef __APPLE__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9361 | off_t sbytes; |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9362 | #endif |
| 9363 | Py_buffer *hbuf, *tbuf; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9364 | struct sf_hdtr sf; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9365 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 9366 | sf.headers = NULL; |
| 9367 | sf.trailers = NULL; |
| 9368 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9369 | if (headers != NULL) { |
| 9370 | if (!PySequence_Check(headers)) { |
| 9371 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9372 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9373 | return NULL; |
| 9374 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9375 | Py_ssize_t i = PySequence_Size(headers); |
| 9376 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9377 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9378 | if (i > INT_MAX) { |
| 9379 | PyErr_SetString(PyExc_OverflowError, |
| 9380 | "sendfile() header is too large"); |
| 9381 | return NULL; |
| 9382 | } |
| 9383 | if (i > 0) { |
| 9384 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9385 | if (iov_setup(&(sf.headers), &hbuf, |
| 9386 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9387 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9388 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9389 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 9390 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 9391 | # define OFF_T_MAX 0x7fffffffffffffff |
| 9392 | if (sbytes >= OFF_T_MAX - blen) { |
| 9393 | PyErr_SetString(PyExc_OverflowError, |
| 9394 | "sendfile() header is too large"); |
| 9395 | return NULL; |
| 9396 | } |
| 9397 | sbytes += blen; |
| 9398 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9399 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9400 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9401 | } |
| 9402 | } |
| 9403 | if (trailers != NULL) { |
| 9404 | if (!PySequence_Check(trailers)) { |
| 9405 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9406 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9407 | return NULL; |
| 9408 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9409 | Py_ssize_t i = PySequence_Size(trailers); |
| 9410 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9411 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9412 | if (i > INT_MAX) { |
| 9413 | PyErr_SetString(PyExc_OverflowError, |
| 9414 | "sendfile() trailer is too large"); |
| 9415 | return NULL; |
| 9416 | } |
| 9417 | if (i > 0) { |
| 9418 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9419 | if (iov_setup(&(sf.trailers), &tbuf, |
| 9420 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9421 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9422 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9423 | } |
| 9424 | } |
| 9425 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9426 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9427 | do { |
| 9428 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9429 | #ifdef __APPLE__ |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9430 | ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9431 | #else |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9432 | ret = sendfile(in_fd, out_fd, offset, count, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9433 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9434 | Py_END_ALLOW_THREADS |
| 9435 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9436 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9437 | |
| 9438 | if (sf.headers != NULL) |
| 9439 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 9440 | if (sf.trailers != NULL) |
| 9441 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 9442 | |
| 9443 | if (ret < 0) { |
| 9444 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 9445 | if (sbytes != 0) { |
| 9446 | // some data has been sent |
| 9447 | goto done; |
| 9448 | } |
| 9449 | else { |
| 9450 | // no data has been sent; upper application is supposed |
| 9451 | // to retry on EAGAIN or EBUSY |
| 9452 | return posix_error(); |
| 9453 | } |
| 9454 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9455 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9456 | } |
| 9457 | goto done; |
| 9458 | |
| 9459 | done: |
| 9460 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 9461 | return Py_BuildValue("l", sbytes); |
| 9462 | #else |
| 9463 | return Py_BuildValue("L", sbytes); |
| 9464 | #endif |
| 9465 | |
| 9466 | #else |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9467 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9468 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9469 | do { |
| 9470 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9471 | ret = sendfile(out_fd, in_fd, NULL, count); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9472 | Py_END_ALLOW_THREADS |
| 9473 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9474 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9475 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9476 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9477 | } |
| 9478 | #endif |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9479 | off_t offset; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9480 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9481 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9482 | |
| 9483 | do { |
| 9484 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9485 | ret = sendfile(out_fd, in_fd, &offset, count); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9486 | Py_END_ALLOW_THREADS |
| 9487 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9488 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9489 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9490 | return Py_BuildValue("n", ret); |
| 9491 | #endif |
| 9492 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9493 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9494 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9495 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9496 | #if defined(__APPLE__) |
| 9497 | /*[clinic input] |
| 9498 | os._fcopyfile |
| 9499 | |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9500 | in_fd: int |
| 9501 | out_fd: int |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9502 | flags: int |
| 9503 | / |
| 9504 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9505 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9506 | [clinic start generated code]*/ |
| 9507 | |
| 9508 | static PyObject * |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9509 | os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags) |
| 9510 | /*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9511 | { |
| 9512 | int ret; |
| 9513 | |
| 9514 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9515 | ret = fcopyfile(in_fd, out_fd, NULL, flags); |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9516 | Py_END_ALLOW_THREADS |
| 9517 | if (ret < 0) |
| 9518 | return posix_error(); |
| 9519 | Py_RETURN_NONE; |
| 9520 | } |
| 9521 | #endif |
| 9522 | |
| 9523 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9524 | /*[clinic input] |
| 9525 | os.fstat |
| 9526 | |
| 9527 | fd : int |
| 9528 | |
| 9529 | Perform a stat system call on the given file descriptor. |
| 9530 | |
| 9531 | Like stat(), but for an open file descriptor. |
| 9532 | Equivalent to os.stat(fd). |
| 9533 | [clinic start generated code]*/ |
| 9534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9535 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9536 | os_fstat_impl(PyObject *module, int fd) |
| 9537 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9538 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9539 | STRUCT_STAT st; |
| 9540 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9541 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9542 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9543 | do { |
| 9544 | Py_BEGIN_ALLOW_THREADS |
| 9545 | res = FSTAT(fd, &st); |
| 9546 | Py_END_ALLOW_THREADS |
| 9547 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9548 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9549 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9550 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9551 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9552 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9553 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9554 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9555 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 9556 | return _pystat_fromstructstat(module, &st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9557 | } |
| 9558 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9559 | |
| 9560 | /*[clinic input] |
| 9561 | os.isatty -> bool |
| 9562 | fd: int |
| 9563 | / |
| 9564 | |
| 9565 | Return True if the fd is connected to a terminal. |
| 9566 | |
| 9567 | Return True if the file descriptor is an open file descriptor |
| 9568 | connected to the slave end of a terminal. |
| 9569 | [clinic start generated code]*/ |
| 9570 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9571 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9572 | os_isatty_impl(PyObject *module, int fd) |
| 9573 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9574 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9575 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9576 | _Py_BEGIN_SUPPRESS_IPH |
| 9577 | return_value = isatty(fd); |
| 9578 | _Py_END_SUPPRESS_IPH |
| 9579 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9580 | } |
| 9581 | |
| 9582 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9583 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9584 | /*[clinic input] |
| 9585 | os.pipe |
| 9586 | |
| 9587 | Create a pipe. |
| 9588 | |
| 9589 | Returns a tuple of two file descriptors: |
| 9590 | (read_fd, write_fd) |
| 9591 | [clinic start generated code]*/ |
| 9592 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9593 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9594 | os_pipe_impl(PyObject *module) |
| 9595 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9596 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9597 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9598 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9599 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9600 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9601 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9602 | #else |
| 9603 | int res; |
| 9604 | #endif |
| 9605 | |
| 9606 | #ifdef MS_WINDOWS |
| 9607 | attr.nLength = sizeof(attr); |
| 9608 | attr.lpSecurityDescriptor = NULL; |
| 9609 | attr.bInheritHandle = FALSE; |
| 9610 | |
| 9611 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9612 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9613 | ok = CreatePipe(&read, &write, &attr, 0); |
| 9614 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9615 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 9616 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9617 | if (fds[0] == -1 || fds[1] == -1) { |
| 9618 | CloseHandle(read); |
| 9619 | CloseHandle(write); |
| 9620 | ok = 0; |
| 9621 | } |
| 9622 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9623 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9624 | Py_END_ALLOW_THREADS |
| 9625 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9626 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9627 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9628 | #else |
| 9629 | |
| 9630 | #ifdef HAVE_PIPE2 |
| 9631 | Py_BEGIN_ALLOW_THREADS |
| 9632 | res = pipe2(fds, O_CLOEXEC); |
| 9633 | Py_END_ALLOW_THREADS |
| 9634 | |
| 9635 | if (res != 0 && errno == ENOSYS) |
| 9636 | { |
| 9637 | #endif |
| 9638 | Py_BEGIN_ALLOW_THREADS |
| 9639 | res = pipe(fds); |
| 9640 | Py_END_ALLOW_THREADS |
| 9641 | |
| 9642 | if (res == 0) { |
| 9643 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9644 | close(fds[0]); |
| 9645 | close(fds[1]); |
| 9646 | return NULL; |
| 9647 | } |
| 9648 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9649 | close(fds[0]); |
| 9650 | close(fds[1]); |
| 9651 | return NULL; |
| 9652 | } |
| 9653 | } |
| 9654 | #ifdef HAVE_PIPE2 |
| 9655 | } |
| 9656 | #endif |
| 9657 | |
| 9658 | if (res != 0) |
| 9659 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9660 | #endif /* !MS_WINDOWS */ |
| 9661 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9662 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9663 | #endif /* HAVE_PIPE */ |
| 9664 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9665 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9666 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9667 | /*[clinic input] |
| 9668 | os.pipe2 |
| 9669 | |
| 9670 | flags: int |
| 9671 | / |
| 9672 | |
| 9673 | Create a pipe with flags set atomically. |
| 9674 | |
| 9675 | Returns a tuple of two file descriptors: |
| 9676 | (read_fd, write_fd) |
| 9677 | |
| 9678 | flags can be constructed by ORing together one or more of these values: |
| 9679 | O_NONBLOCK, O_CLOEXEC. |
| 9680 | [clinic start generated code]*/ |
| 9681 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9682 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9683 | os_pipe2_impl(PyObject *module, int flags) |
| 9684 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9685 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9686 | int fds[2]; |
| 9687 | int res; |
| 9688 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9689 | res = pipe2(fds, flags); |
| 9690 | if (res != 0) |
| 9691 | return posix_error(); |
| 9692 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9693 | } |
| 9694 | #endif /* HAVE_PIPE2 */ |
| 9695 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9696 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9697 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9698 | /*[clinic input] |
| 9699 | os.writev -> Py_ssize_t |
| 9700 | fd: int |
| 9701 | buffers: object |
| 9702 | / |
| 9703 | |
| 9704 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9705 | |
| 9706 | Returns the total number of bytes written. |
| 9707 | buffers must be a sequence of bytes-like objects. |
| 9708 | [clinic start generated code]*/ |
| 9709 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9710 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9711 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9712 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9713 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9714 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9715 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9716 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9717 | struct iovec *iov; |
| 9718 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9719 | |
| 9720 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9721 | PyErr_SetString(PyExc_TypeError, |
| 9722 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9723 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9724 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9725 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9726 | if (cnt < 0) |
| 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 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9730 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9731 | } |
| 9732 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9733 | do { |
| 9734 | Py_BEGIN_ALLOW_THREADS |
| 9735 | result = writev(fd, iov, cnt); |
| 9736 | Py_END_ALLOW_THREADS |
| 9737 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9738 | |
| 9739 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9740 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9741 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9742 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9743 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9744 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9745 | #endif /* HAVE_WRITEV */ |
| 9746 | |
| 9747 | |
| 9748 | #ifdef HAVE_PWRITE |
| 9749 | /*[clinic input] |
| 9750 | os.pwrite -> Py_ssize_t |
| 9751 | |
| 9752 | fd: int |
| 9753 | buffer: Py_buffer |
| 9754 | offset: Py_off_t |
| 9755 | / |
| 9756 | |
| 9757 | Write bytes to a file descriptor starting at a particular offset. |
| 9758 | |
| 9759 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9760 | the file. Returns the number of bytes writte. Does not change the |
| 9761 | current file offset. |
| 9762 | [clinic start generated code]*/ |
| 9763 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9764 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9765 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9766 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9767 | { |
| 9768 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9769 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9770 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9771 | do { |
| 9772 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9773 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9774 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9775 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9776 | Py_END_ALLOW_THREADS |
| 9777 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9778 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9779 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9780 | posix_error(); |
| 9781 | return size; |
| 9782 | } |
| 9783 | #endif /* HAVE_PWRITE */ |
| 9784 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9785 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9786 | /*[clinic input] |
| 9787 | os.pwritev -> Py_ssize_t |
| 9788 | |
| 9789 | fd: int |
| 9790 | buffers: object |
| 9791 | offset: Py_off_t |
| 9792 | flags: int = 0 |
| 9793 | / |
| 9794 | |
| 9795 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9796 | |
| 9797 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9798 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9799 | buffer is written before proceeding to second, and so on. The operating system may |
| 9800 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9801 | This function writes the contents of each object to the file descriptor and returns |
| 9802 | the total number of bytes written. |
| 9803 | |
| 9804 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9805 | |
| 9806 | - RWF_DSYNC |
| 9807 | - RWF_SYNC |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 9808 | - RWF_APPEND |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9809 | |
| 9810 | Using non-zero flags requires Linux 4.7 or newer. |
| 9811 | [clinic start generated code]*/ |
| 9812 | |
| 9813 | static Py_ssize_t |
| 9814 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9815 | int flags) |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 9816 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=35358c327e1a2a8e]*/ |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9817 | { |
| 9818 | Py_ssize_t cnt; |
| 9819 | Py_ssize_t result; |
| 9820 | int async_err = 0; |
| 9821 | struct iovec *iov; |
| 9822 | Py_buffer *buf; |
| 9823 | |
| 9824 | if (!PySequence_Check(buffers)) { |
| 9825 | PyErr_SetString(PyExc_TypeError, |
| 9826 | "pwritev() arg 2 must be a sequence"); |
| 9827 | return -1; |
| 9828 | } |
| 9829 | |
| 9830 | cnt = PySequence_Size(buffers); |
| 9831 | if (cnt < 0) { |
| 9832 | return -1; |
| 9833 | } |
| 9834 | |
| 9835 | #ifndef HAVE_PWRITEV2 |
| 9836 | if(flags != 0) { |
| 9837 | argument_unavailable_error("pwritev2", "flags"); |
| 9838 | return -1; |
| 9839 | } |
| 9840 | #endif |
| 9841 | |
| 9842 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9843 | return -1; |
| 9844 | } |
| 9845 | #ifdef HAVE_PWRITEV2 |
| 9846 | do { |
| 9847 | Py_BEGIN_ALLOW_THREADS |
| 9848 | _Py_BEGIN_SUPPRESS_IPH |
| 9849 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9850 | _Py_END_SUPPRESS_IPH |
| 9851 | Py_END_ALLOW_THREADS |
| 9852 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9853 | #else |
| 9854 | do { |
| 9855 | Py_BEGIN_ALLOW_THREADS |
| 9856 | _Py_BEGIN_SUPPRESS_IPH |
| 9857 | result = pwritev(fd, iov, cnt, offset); |
| 9858 | _Py_END_SUPPRESS_IPH |
| 9859 | Py_END_ALLOW_THREADS |
| 9860 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9861 | #endif |
| 9862 | |
| 9863 | iov_cleanup(iov, buf, cnt); |
| 9864 | if (result < 0) { |
| 9865 | if (!async_err) { |
| 9866 | posix_error(); |
| 9867 | } |
| 9868 | return -1; |
| 9869 | } |
| 9870 | |
| 9871 | return result; |
| 9872 | } |
| 9873 | #endif /* HAVE_PWRITEV */ |
| 9874 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9875 | #ifdef HAVE_COPY_FILE_RANGE |
| 9876 | /*[clinic input] |
| 9877 | |
| 9878 | os.copy_file_range |
| 9879 | src: int |
| 9880 | Source file descriptor. |
| 9881 | dst: int |
| 9882 | Destination file descriptor. |
| 9883 | count: Py_ssize_t |
| 9884 | Number of bytes to copy. |
| 9885 | offset_src: object = None |
| 9886 | Starting offset in src. |
| 9887 | offset_dst: object = None |
| 9888 | Starting offset in dst. |
| 9889 | |
| 9890 | Copy count bytes from one file descriptor to another. |
| 9891 | |
| 9892 | If offset_src is None, then src is read from the current position; |
| 9893 | respectively for offset_dst. |
| 9894 | [clinic start generated code]*/ |
| 9895 | |
| 9896 | static PyObject * |
| 9897 | os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, |
| 9898 | PyObject *offset_src, PyObject *offset_dst) |
| 9899 | /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ |
| 9900 | { |
| 9901 | off_t offset_src_val, offset_dst_val; |
| 9902 | off_t *p_offset_src = NULL; |
| 9903 | off_t *p_offset_dst = NULL; |
| 9904 | Py_ssize_t ret; |
| 9905 | int async_err = 0; |
| 9906 | /* The flags argument is provided to allow |
| 9907 | * for future extensions and currently must be to 0. */ |
| 9908 | int flags = 0; |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9909 | |
| 9910 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9911 | if (count < 0) { |
| 9912 | PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); |
| 9913 | return NULL; |
| 9914 | } |
| 9915 | |
| 9916 | if (offset_src != Py_None) { |
| 9917 | if (!Py_off_t_converter(offset_src, &offset_src_val)) { |
| 9918 | return NULL; |
| 9919 | } |
| 9920 | p_offset_src = &offset_src_val; |
| 9921 | } |
| 9922 | |
| 9923 | if (offset_dst != Py_None) { |
| 9924 | if (!Py_off_t_converter(offset_dst, &offset_dst_val)) { |
| 9925 | return NULL; |
| 9926 | } |
| 9927 | p_offset_dst = &offset_dst_val; |
| 9928 | } |
| 9929 | |
| 9930 | do { |
| 9931 | Py_BEGIN_ALLOW_THREADS |
| 9932 | ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags); |
| 9933 | Py_END_ALLOW_THREADS |
| 9934 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9935 | |
| 9936 | if (ret < 0) { |
| 9937 | return (!async_err) ? posix_error() : NULL; |
| 9938 | } |
| 9939 | |
| 9940 | return PyLong_FromSsize_t(ret); |
| 9941 | } |
| 9942 | #endif /* HAVE_COPY_FILE_RANGE*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9943 | |
| 9944 | #ifdef HAVE_MKFIFO |
| 9945 | /*[clinic input] |
| 9946 | os.mkfifo |
| 9947 | |
| 9948 | path: path_t |
| 9949 | mode: int=0o666 |
| 9950 | * |
| 9951 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9952 | |
| 9953 | Create a "fifo" (a POSIX named pipe). |
| 9954 | |
| 9955 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9956 | and path should be relative; path will then be relative to that directory. |
| 9957 | dir_fd may not be implemented on your platform. |
| 9958 | If it is unavailable, using it will raise a NotImplementedError. |
| 9959 | [clinic start generated code]*/ |
| 9960 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9961 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9962 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9963 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9964 | { |
| 9965 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9966 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9967 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9968 | do { |
| 9969 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9970 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9971 | if (dir_fd != DEFAULT_DIR_FD) |
| 9972 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9973 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9974 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9975 | result = mkfifo(path->narrow, mode); |
| 9976 | Py_END_ALLOW_THREADS |
| 9977 | } while (result != 0 && errno == EINTR && |
| 9978 | !(async_err = PyErr_CheckSignals())); |
| 9979 | if (result != 0) |
| 9980 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9981 | |
| 9982 | Py_RETURN_NONE; |
| 9983 | } |
| 9984 | #endif /* HAVE_MKFIFO */ |
| 9985 | |
| 9986 | |
| 9987 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9988 | /*[clinic input] |
| 9989 | os.mknod |
| 9990 | |
| 9991 | path: path_t |
| 9992 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9993 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9994 | * |
| 9995 | dir_fd: dir_fd(requires='mknodat')=None |
| 9996 | |
| 9997 | Create a node in the file system. |
| 9998 | |
| 9999 | Create a node in the file system (file, device special file or named pipe) |
| 10000 | at path. mode specifies both the permissions to use and the |
| 10001 | type of node to be created, being combined (bitwise OR) with one of |
| 10002 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 10003 | device defines the newly created device special file (probably using |
| 10004 | os.makedev()). Otherwise device is ignored. |
| 10005 | |
| 10006 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 10007 | and path should be relative; path will then be relative to that directory. |
| 10008 | dir_fd may not be implemented on your platform. |
| 10009 | If it is unavailable, using it will raise a NotImplementedError. |
| 10010 | [clinic start generated code]*/ |
| 10011 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10012 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10013 | 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] | 10014 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10015 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10016 | { |
| 10017 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10018 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10019 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10020 | do { |
| 10021 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10022 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10023 | if (dir_fd != DEFAULT_DIR_FD) |
| 10024 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 10025 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10026 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10027 | result = mknod(path->narrow, mode, device); |
| 10028 | Py_END_ALLOW_THREADS |
| 10029 | } while (result != 0 && errno == EINTR && |
| 10030 | !(async_err = PyErr_CheckSignals())); |
| 10031 | if (result != 0) |
| 10032 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10033 | |
| 10034 | Py_RETURN_NONE; |
| 10035 | } |
| 10036 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 10037 | |
| 10038 | |
| 10039 | #ifdef HAVE_DEVICE_MACROS |
| 10040 | /*[clinic input] |
| 10041 | os.major -> unsigned_int |
| 10042 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10043 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10044 | / |
| 10045 | |
| 10046 | Extracts a device major number from a raw device number. |
| 10047 | [clinic start generated code]*/ |
| 10048 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10049 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10050 | os_major_impl(PyObject *module, dev_t device) |
| 10051 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10052 | { |
| 10053 | return major(device); |
| 10054 | } |
| 10055 | |
| 10056 | |
| 10057 | /*[clinic input] |
| 10058 | os.minor -> unsigned_int |
| 10059 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10060 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10061 | / |
| 10062 | |
| 10063 | Extracts a device minor number from a raw device number. |
| 10064 | [clinic start generated code]*/ |
| 10065 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10066 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10067 | os_minor_impl(PyObject *module, dev_t device) |
| 10068 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10069 | { |
| 10070 | return minor(device); |
| 10071 | } |
| 10072 | |
| 10073 | |
| 10074 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10075 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10076 | |
| 10077 | major: int |
| 10078 | minor: int |
| 10079 | / |
| 10080 | |
| 10081 | Composes a raw device number from the major and minor device numbers. |
| 10082 | [clinic start generated code]*/ |
| 10083 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10084 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10085 | os_makedev_impl(PyObject *module, int major, int minor) |
| 10086 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10087 | { |
| 10088 | return makedev(major, minor); |
| 10089 | } |
| 10090 | #endif /* HAVE_DEVICE_MACROS */ |
| 10091 | |
| 10092 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10093 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10094 | /*[clinic input] |
| 10095 | os.ftruncate |
| 10096 | |
| 10097 | fd: int |
| 10098 | length: Py_off_t |
| 10099 | / |
| 10100 | |
| 10101 | Truncate a file, specified by file descriptor, to a specific length. |
| 10102 | [clinic start generated code]*/ |
| 10103 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10104 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10105 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 10106 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10107 | { |
| 10108 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10109 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10110 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10111 | if (PySys_Audit("os.truncate", "in", fd, length) < 0) { |
| 10112 | return NULL; |
| 10113 | } |
| 10114 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10115 | do { |
| 10116 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10117 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10118 | #ifdef MS_WINDOWS |
| 10119 | result = _chsize_s(fd, length); |
| 10120 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10121 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10122 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10123 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10124 | Py_END_ALLOW_THREADS |
| 10125 | } while (result != 0 && errno == EINTR && |
| 10126 | !(async_err = PyErr_CheckSignals())); |
| 10127 | if (result != 0) |
| 10128 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10129 | Py_RETURN_NONE; |
| 10130 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10131 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10132 | |
| 10133 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10134 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10135 | /*[clinic input] |
| 10136 | os.truncate |
| 10137 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 10138 | length: Py_off_t |
| 10139 | |
| 10140 | Truncate a file, specified by path, to a specific length. |
| 10141 | |
| 10142 | On some platforms, path may also be specified as an open file descriptor. |
| 10143 | If this functionality is unavailable, using it raises an exception. |
| 10144 | [clinic start generated code]*/ |
| 10145 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10146 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10147 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 10148 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10149 | { |
| 10150 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10151 | #ifdef MS_WINDOWS |
| 10152 | int fd; |
| 10153 | #endif |
| 10154 | |
| 10155 | if (path->fd != -1) |
| 10156 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10157 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10158 | if (PySys_Audit("os.truncate", "On", path->object, length) < 0) { |
| 10159 | return NULL; |
| 10160 | } |
| 10161 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10162 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10163 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10164 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10165 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 10166 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10167 | result = -1; |
| 10168 | else { |
| 10169 | result = _chsize_s(fd, length); |
| 10170 | close(fd); |
| 10171 | if (result < 0) |
| 10172 | errno = result; |
| 10173 | } |
| 10174 | #else |
| 10175 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10176 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10177 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10178 | Py_END_ALLOW_THREADS |
| 10179 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 10180 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10181 | |
| 10182 | Py_RETURN_NONE; |
| 10183 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10184 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10185 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10186 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10187 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 10188 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 10189 | defined, which is the case in Python on AIX. AIX bug report: |
| 10190 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 10191 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 10192 | # define POSIX_FADVISE_AIX_BUG |
| 10193 | #endif |
| 10194 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10195 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10196 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10197 | /*[clinic input] |
| 10198 | os.posix_fallocate |
| 10199 | |
| 10200 | fd: int |
| 10201 | offset: Py_off_t |
| 10202 | length: Py_off_t |
| 10203 | / |
| 10204 | |
| 10205 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 10206 | |
| 10207 | Ensure that the file specified by fd encompasses a range of bytes |
| 10208 | starting at offset bytes from the beginning and continuing for length bytes. |
| 10209 | [clinic start generated code]*/ |
| 10210 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10211 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10212 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10213 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10214 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10215 | { |
| 10216 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10217 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10218 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10219 | do { |
| 10220 | Py_BEGIN_ALLOW_THREADS |
| 10221 | result = posix_fallocate(fd, offset, length); |
| 10222 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10223 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10224 | |
| 10225 | if (result == 0) |
| 10226 | Py_RETURN_NONE; |
| 10227 | |
| 10228 | if (async_err) |
| 10229 | return NULL; |
| 10230 | |
| 10231 | errno = result; |
| 10232 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10233 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10234 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10235 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10236 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10237 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10238 | /*[clinic input] |
| 10239 | os.posix_fadvise |
| 10240 | |
| 10241 | fd: int |
| 10242 | offset: Py_off_t |
| 10243 | length: Py_off_t |
| 10244 | advice: int |
| 10245 | / |
| 10246 | |
| 10247 | Announce an intention to access data in a specific pattern. |
| 10248 | |
| 10249 | Announce an intention to access data in a specific pattern, thus allowing |
| 10250 | the kernel to make optimizations. |
| 10251 | The advice applies to the region of the file specified by fd starting at |
| 10252 | offset and continuing for length bytes. |
| 10253 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 10254 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 10255 | POSIX_FADV_DONTNEED. |
| 10256 | [clinic start generated code]*/ |
| 10257 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10258 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10259 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10260 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10261 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10262 | { |
| 10263 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10264 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10265 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10266 | do { |
| 10267 | Py_BEGIN_ALLOW_THREADS |
| 10268 | result = posix_fadvise(fd, offset, length, advice); |
| 10269 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10270 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10271 | |
| 10272 | if (result == 0) |
| 10273 | Py_RETURN_NONE; |
| 10274 | |
| 10275 | if (async_err) |
| 10276 | return NULL; |
| 10277 | |
| 10278 | errno = result; |
| 10279 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10280 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10281 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10282 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10283 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 10284 | #ifdef MS_WINDOWS |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10285 | static PyObject* |
| 10286 | win32_putenv(PyObject *name, PyObject *value) |
| 10287 | { |
| 10288 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 10289 | defining hidden environment variables. */ |
| 10290 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 10291 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 10292 | { |
| 10293 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10294 | return NULL; |
| 10295 | } |
| 10296 | PyObject *unicode; |
| 10297 | if (value != NULL) { |
| 10298 | unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 10299 | } |
| 10300 | else { |
| 10301 | unicode = PyUnicode_FromFormat("%U=", name); |
| 10302 | } |
| 10303 | if (unicode == NULL) { |
| 10304 | return NULL; |
| 10305 | } |
| 10306 | |
| 10307 | Py_ssize_t size; |
| 10308 | /* PyUnicode_AsWideCharString() rejects embedded null characters */ |
| 10309 | wchar_t *env = PyUnicode_AsWideCharString(unicode, &size); |
| 10310 | Py_DECREF(unicode); |
| 10311 | |
| 10312 | if (env == NULL) { |
| 10313 | return NULL; |
| 10314 | } |
| 10315 | if (size > _MAX_ENV) { |
| 10316 | PyErr_Format(PyExc_ValueError, |
| 10317 | "the environment variable is longer than %u characters", |
| 10318 | _MAX_ENV); |
| 10319 | PyMem_Free(env); |
| 10320 | return NULL; |
| 10321 | } |
| 10322 | |
| 10323 | /* _wputenv() and SetEnvironmentVariableW() update the environment in the |
| 10324 | Process Environment Block (PEB). _wputenv() also updates CRT 'environ' |
| 10325 | and '_wenviron' variables, whereas SetEnvironmentVariableW() does not. |
| 10326 | |
| 10327 | Prefer _wputenv() to be compatible with C libraries using CRT |
| 10328 | variables and CRT functions using these variables (ex: getenv()). */ |
| 10329 | int err = _wputenv(env); |
| 10330 | PyMem_Free(env); |
| 10331 | |
| 10332 | if (err) { |
| 10333 | posix_error(); |
| 10334 | return NULL; |
| 10335 | } |
| 10336 | |
| 10337 | Py_RETURN_NONE; |
| 10338 | } |
| 10339 | #endif |
| 10340 | |
| 10341 | |
| 10342 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10343 | /*[clinic input] |
| 10344 | os.putenv |
| 10345 | |
| 10346 | name: unicode |
| 10347 | value: unicode |
| 10348 | / |
| 10349 | |
| 10350 | Change or add an environment variable. |
| 10351 | [clinic start generated code]*/ |
| 10352 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10353 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10354 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10355 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10356 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10357 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10358 | return NULL; |
| 10359 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10360 | return win32_putenv(name, value); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10361 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10362 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10363 | /*[clinic input] |
| 10364 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10365 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10366 | name: FSConverter |
| 10367 | value: FSConverter |
| 10368 | / |
| 10369 | |
| 10370 | Change or add an environment variable. |
| 10371 | [clinic start generated code]*/ |
| 10372 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10373 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10374 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10375 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10376 | { |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10377 | const char *name_string = PyBytes_AS_STRING(name); |
| 10378 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10379 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10380 | if (strchr(name_string, '=') != NULL) { |
| 10381 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10382 | return NULL; |
| 10383 | } |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10384 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10385 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10386 | return NULL; |
| 10387 | } |
| 10388 | |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10389 | if (setenv(name_string, value_string, 1)) { |
| 10390 | return posix_error(); |
| 10391 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10392 | Py_RETURN_NONE; |
| 10393 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10394 | #endif /* !defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10395 | |
| 10396 | |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10397 | #ifdef MS_WINDOWS |
| 10398 | /*[clinic input] |
| 10399 | os.unsetenv |
| 10400 | name: unicode |
| 10401 | / |
| 10402 | |
| 10403 | Delete an environment variable. |
| 10404 | [clinic start generated code]*/ |
| 10405 | |
| 10406 | static PyObject * |
| 10407 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10408 | /*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/ |
| 10409 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10410 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10411 | return NULL; |
| 10412 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10413 | return win32_putenv(name, NULL); |
| 10414 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10415 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10416 | /*[clinic input] |
| 10417 | os.unsetenv |
| 10418 | name: FSConverter |
| 10419 | / |
| 10420 | |
| 10421 | Delete an environment variable. |
| 10422 | [clinic start generated code]*/ |
| 10423 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10424 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10425 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10426 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10427 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10428 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10429 | return NULL; |
| 10430 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10431 | #ifdef HAVE_BROKEN_UNSETENV |
| 10432 | unsetenv(PyBytes_AS_STRING(name)); |
| 10433 | #else |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10434 | int err = unsetenv(PyBytes_AS_STRING(name)); |
| 10435 | if (err) { |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10436 | return posix_error(); |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10437 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10438 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10439 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10440 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10441 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10442 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10443 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10444 | |
| 10445 | /*[clinic input] |
| 10446 | os.strerror |
| 10447 | |
| 10448 | code: int |
| 10449 | / |
| 10450 | |
| 10451 | Translate an error code to a message string. |
| 10452 | [clinic start generated code]*/ |
| 10453 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10454 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10455 | os_strerror_impl(PyObject *module, int code) |
| 10456 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10457 | { |
| 10458 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10459 | if (message == NULL) { |
| 10460 | PyErr_SetString(PyExc_ValueError, |
| 10461 | "strerror() argument out of range"); |
| 10462 | return NULL; |
| 10463 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 10464 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10465 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10466 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10467 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10468 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10469 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10470 | /*[clinic input] |
| 10471 | os.WCOREDUMP -> bool |
| 10472 | |
| 10473 | status: int |
| 10474 | / |
| 10475 | |
| 10476 | Return True if the process returning status was dumped to a core file. |
| 10477 | [clinic start generated code]*/ |
| 10478 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10479 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10480 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 10481 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10482 | { |
| 10483 | WAIT_TYPE wait_status; |
| 10484 | WAIT_STATUS_INT(wait_status) = status; |
| 10485 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10486 | } |
| 10487 | #endif /* WCOREDUMP */ |
| 10488 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10489 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10490 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10491 | /*[clinic input] |
| 10492 | os.WIFCONTINUED -> bool |
| 10493 | |
| 10494 | status: int |
| 10495 | |
| 10496 | Return True if a particular process was continued from a job control stop. |
| 10497 | |
| 10498 | Return True if the process returning status was continued from a |
| 10499 | job control stop. |
| 10500 | [clinic start generated code]*/ |
| 10501 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10502 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10503 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 10504 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10505 | { |
| 10506 | WAIT_TYPE wait_status; |
| 10507 | WAIT_STATUS_INT(wait_status) = status; |
| 10508 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10509 | } |
| 10510 | #endif /* WIFCONTINUED */ |
| 10511 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10512 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10513 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10514 | /*[clinic input] |
| 10515 | os.WIFSTOPPED -> bool |
| 10516 | |
| 10517 | status: int |
| 10518 | |
| 10519 | Return True if the process returning status was stopped. |
| 10520 | [clinic start generated code]*/ |
| 10521 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10522 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10523 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 10524 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10525 | { |
| 10526 | WAIT_TYPE wait_status; |
| 10527 | WAIT_STATUS_INT(wait_status) = status; |
| 10528 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10529 | } |
| 10530 | #endif /* WIFSTOPPED */ |
| 10531 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10532 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10533 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10534 | /*[clinic input] |
| 10535 | os.WIFSIGNALED -> bool |
| 10536 | |
| 10537 | status: int |
| 10538 | |
| 10539 | Return True if the process returning status was terminated by a signal. |
| 10540 | [clinic start generated code]*/ |
| 10541 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10542 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10543 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 10544 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10545 | { |
| 10546 | WAIT_TYPE wait_status; |
| 10547 | WAIT_STATUS_INT(wait_status) = status; |
| 10548 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10549 | } |
| 10550 | #endif /* WIFSIGNALED */ |
| 10551 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10552 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10553 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10554 | /*[clinic input] |
| 10555 | os.WIFEXITED -> bool |
| 10556 | |
| 10557 | status: int |
| 10558 | |
| 10559 | Return True if the process returning status exited via the exit() system call. |
| 10560 | [clinic start generated code]*/ |
| 10561 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10562 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10563 | os_WIFEXITED_impl(PyObject *module, int status) |
| 10564 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10565 | { |
| 10566 | WAIT_TYPE wait_status; |
| 10567 | WAIT_STATUS_INT(wait_status) = status; |
| 10568 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10569 | } |
| 10570 | #endif /* WIFEXITED */ |
| 10571 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10572 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10573 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10574 | /*[clinic input] |
| 10575 | os.WEXITSTATUS -> int |
| 10576 | |
| 10577 | status: int |
| 10578 | |
| 10579 | Return the process return code from status. |
| 10580 | [clinic start generated code]*/ |
| 10581 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10582 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10583 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 10584 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10585 | { |
| 10586 | WAIT_TYPE wait_status; |
| 10587 | WAIT_STATUS_INT(wait_status) = status; |
| 10588 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10589 | } |
| 10590 | #endif /* WEXITSTATUS */ |
| 10591 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10592 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10593 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10594 | /*[clinic input] |
| 10595 | os.WTERMSIG -> int |
| 10596 | |
| 10597 | status: int |
| 10598 | |
| 10599 | Return the signal that terminated the process that provided the status value. |
| 10600 | [clinic start generated code]*/ |
| 10601 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10602 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10603 | os_WTERMSIG_impl(PyObject *module, int status) |
| 10604 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10605 | { |
| 10606 | WAIT_TYPE wait_status; |
| 10607 | WAIT_STATUS_INT(wait_status) = status; |
| 10608 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10609 | } |
| 10610 | #endif /* WTERMSIG */ |
| 10611 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10612 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10613 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10614 | /*[clinic input] |
| 10615 | os.WSTOPSIG -> int |
| 10616 | |
| 10617 | status: int |
| 10618 | |
| 10619 | Return the signal that stopped the process that provided the status value. |
| 10620 | [clinic start generated code]*/ |
| 10621 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10622 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10623 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 10624 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10625 | { |
| 10626 | WAIT_TYPE wait_status; |
| 10627 | WAIT_STATUS_INT(wait_status) = status; |
| 10628 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10629 | } |
| 10630 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10631 | #endif /* HAVE_SYS_WAIT_H */ |
| 10632 | |
| 10633 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10634 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10635 | #ifdef _SCO_DS |
| 10636 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 10637 | needed definitions in sys/statvfs.h */ |
| 10638 | #define _SVID3 |
| 10639 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10640 | #include <sys/statvfs.h> |
| 10641 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10642 | static PyObject* |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10643 | _pystatvfs_fromstructstatvfs(PyObject *module, struct statvfs st) { |
| 10644 | PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 10645 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10646 | if (v == NULL) |
| 10647 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10648 | |
| 10649 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10650 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10651 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10652 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 10653 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 10654 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 10655 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 10656 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 10657 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 10658 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10659 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10660 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10661 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10662 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10663 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10664 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10665 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10666 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10667 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10668 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10669 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10670 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10671 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10672 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10673 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10674 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10675 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10676 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10677 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10678 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 10679 | * (issue #32390). */ |
| 10680 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 10681 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 10682 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10683 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10684 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10685 | if (PyErr_Occurred()) { |
| 10686 | Py_DECREF(v); |
| 10687 | return NULL; |
| 10688 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10689 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10690 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10691 | } |
| 10692 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10693 | |
| 10694 | /*[clinic input] |
| 10695 | os.fstatvfs |
| 10696 | fd: int |
| 10697 | / |
| 10698 | |
| 10699 | Perform an fstatvfs system call on the given fd. |
| 10700 | |
| 10701 | Equivalent to statvfs(fd). |
| 10702 | [clinic start generated code]*/ |
| 10703 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10704 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10705 | os_fstatvfs_impl(PyObject *module, int fd) |
| 10706 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10707 | { |
| 10708 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10709 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10710 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10711 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10712 | do { |
| 10713 | Py_BEGIN_ALLOW_THREADS |
| 10714 | result = fstatvfs(fd, &st); |
| 10715 | Py_END_ALLOW_THREADS |
| 10716 | } while (result != 0 && errno == EINTR && |
| 10717 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10718 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10719 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10720 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10721 | return _pystatvfs_fromstructstatvfs(module, st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10722 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10723 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10724 | |
| 10725 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10726 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10727 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10728 | /*[clinic input] |
| 10729 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10730 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10731 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10732 | |
| 10733 | Perform a statvfs system call on the given path. |
| 10734 | |
| 10735 | path may always be specified as a string. |
| 10736 | On some platforms, path may also be specified as an open file descriptor. |
| 10737 | If this functionality is unavailable, using it raises an exception. |
| 10738 | [clinic start generated code]*/ |
| 10739 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10740 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10741 | os_statvfs_impl(PyObject *module, path_t *path) |
| 10742 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10743 | { |
| 10744 | int result; |
| 10745 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10746 | |
| 10747 | Py_BEGIN_ALLOW_THREADS |
| 10748 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10749 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10750 | #ifdef __APPLE__ |
| 10751 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10752 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10753 | fd_specified("statvfs", path->fd); |
| 10754 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10755 | } |
| 10756 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10757 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10758 | } |
| 10759 | else |
| 10760 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10761 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10762 | Py_END_ALLOW_THREADS |
| 10763 | |
| 10764 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10765 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10766 | } |
| 10767 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10768 | return _pystatvfs_fromstructstatvfs(module, st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10769 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10770 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10771 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10772 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10773 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10774 | /*[clinic input] |
| 10775 | os._getdiskusage |
| 10776 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10777 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10778 | |
| 10779 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10780 | [clinic start generated code]*/ |
| 10781 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10782 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10783 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10784 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10785 | { |
| 10786 | BOOL retval; |
| 10787 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10788 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10789 | |
| 10790 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10791 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10792 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10793 | if (retval == 0) { |
| 10794 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10795 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10796 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10797 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10798 | if (dir_path == NULL) { |
| 10799 | return PyErr_NoMemory(); |
| 10800 | } |
| 10801 | |
| 10802 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10803 | |
| 10804 | if (_dirnameW(dir_path) != -1) { |
| 10805 | Py_BEGIN_ALLOW_THREADS |
| 10806 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10807 | Py_END_ALLOW_THREADS |
| 10808 | } |
| 10809 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10810 | err = GetLastError(); |
| 10811 | PyMem_Free(dir_path); |
| 10812 | if (retval) { |
| 10813 | goto success; |
| 10814 | } |
| 10815 | } |
| 10816 | return PyErr_SetFromWindowsErr(err); |
| 10817 | } |
| 10818 | |
| 10819 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10820 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10821 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10822 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10823 | |
| 10824 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10825 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10826 | * It maps strings representing configuration variable names to |
| 10827 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10828 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10829 | * rarely-used constants. There are three separate tables that use |
| 10830 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10831 | * |
| 10832 | * This code is always included, even if none of the interfaces that |
| 10833 | * need it are included. The #if hackery needed to avoid it would be |
| 10834 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10835 | */ |
| 10836 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10837 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10838 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10839 | }; |
| 10840 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10841 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10842 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10843 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10844 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10845 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10846 | int value = _PyLong_AsInt(arg); |
| 10847 | if (value == -1 && PyErr_Occurred()) |
| 10848 | return 0; |
| 10849 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10850 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10851 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10852 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10853 | /* look up the value in the table using a binary search */ |
| 10854 | size_t lo = 0; |
| 10855 | size_t mid; |
| 10856 | size_t hi = tablesize; |
| 10857 | int cmp; |
| 10858 | const char *confname; |
| 10859 | if (!PyUnicode_Check(arg)) { |
| 10860 | PyErr_SetString(PyExc_TypeError, |
| 10861 | "configuration names must be strings or integers"); |
| 10862 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10863 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10864 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10865 | if (confname == NULL) |
| 10866 | return 0; |
| 10867 | while (lo < hi) { |
| 10868 | mid = (lo + hi) / 2; |
| 10869 | cmp = strcmp(confname, table[mid].name); |
| 10870 | if (cmp < 0) |
| 10871 | hi = mid; |
| 10872 | else if (cmp > 0) |
| 10873 | lo = mid + 1; |
| 10874 | else { |
| 10875 | *valuep = table[mid].value; |
| 10876 | return 1; |
| 10877 | } |
| 10878 | } |
| 10879 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10880 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10881 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10882 | } |
| 10883 | |
| 10884 | |
| 10885 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10886 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10887 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10888 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10889 | #endif |
| 10890 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10891 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10892 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10893 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10894 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10895 | #endif |
| 10896 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10897 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10898 | #endif |
| 10899 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10900 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10901 | #endif |
| 10902 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10903 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10904 | #endif |
| 10905 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10906 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10907 | #endif |
| 10908 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10909 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10910 | #endif |
| 10911 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10912 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10913 | #endif |
| 10914 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10915 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10916 | #endif |
| 10917 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10918 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10919 | #endif |
| 10920 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10921 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10922 | #endif |
| 10923 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10924 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10925 | #endif |
| 10926 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10927 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10928 | #endif |
| 10929 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10930 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10931 | #endif |
| 10932 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10933 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10934 | #endif |
| 10935 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10936 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10937 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10938 | #ifdef _PC_ACL_ENABLED |
| 10939 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10940 | #endif |
| 10941 | #ifdef _PC_MIN_HOLE_SIZE |
| 10942 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10943 | #endif |
| 10944 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10945 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10946 | #endif |
| 10947 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10948 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10949 | #endif |
| 10950 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10951 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10952 | #endif |
| 10953 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10954 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10955 | #endif |
| 10956 | #ifdef _PC_REC_XFER_ALIGN |
| 10957 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10958 | #endif |
| 10959 | #ifdef _PC_SYMLINK_MAX |
| 10960 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10961 | #endif |
| 10962 | #ifdef _PC_XATTR_ENABLED |
| 10963 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10964 | #endif |
| 10965 | #ifdef _PC_XATTR_EXISTS |
| 10966 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10967 | #endif |
| 10968 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10969 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10970 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10971 | }; |
| 10972 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10973 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10974 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10975 | { |
| 10976 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10977 | sizeof(posix_constants_pathconf) |
| 10978 | / sizeof(struct constdef)); |
| 10979 | } |
| 10980 | #endif |
| 10981 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10982 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10983 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10984 | /*[clinic input] |
| 10985 | os.fpathconf -> long |
| 10986 | |
| 10987 | fd: int |
| 10988 | name: path_confname |
| 10989 | / |
| 10990 | |
| 10991 | Return the configuration limit name for the file descriptor fd. |
| 10992 | |
| 10993 | If there is no limit, return -1. |
| 10994 | [clinic start generated code]*/ |
| 10995 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10996 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10997 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 10998 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10999 | { |
| 11000 | long limit; |
| 11001 | |
| 11002 | errno = 0; |
| 11003 | limit = fpathconf(fd, name); |
| 11004 | if (limit == -1 && errno != 0) |
| 11005 | posix_error(); |
| 11006 | |
| 11007 | return limit; |
| 11008 | } |
| 11009 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11010 | |
| 11011 | |
| 11012 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11013 | /*[clinic input] |
| 11014 | os.pathconf -> long |
| 11015 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 11016 | name: path_confname |
| 11017 | |
| 11018 | Return the configuration limit name for the file or directory path. |
| 11019 | |
| 11020 | If there is no limit, return -1. |
| 11021 | On some platforms, path may also be specified as an open file descriptor. |
| 11022 | If this functionality is unavailable, using it raises an exception. |
| 11023 | [clinic start generated code]*/ |
| 11024 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11025 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11026 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 11027 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11028 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11029 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11030 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11031 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11032 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11033 | if (path->fd != -1) |
| 11034 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11035 | else |
| 11036 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11037 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11038 | if (limit == -1 && errno != 0) { |
| 11039 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 11040 | /* could be a path or name problem */ |
| 11041 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11042 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11043 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11044 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11045 | |
| 11046 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11047 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11048 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11049 | |
| 11050 | #ifdef HAVE_CONFSTR |
| 11051 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11052 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11053 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11054 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11055 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11056 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11057 | #endif |
| 11058 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11059 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11060 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11061 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11062 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11063 | #endif |
| 11064 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11065 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11066 | #endif |
| 11067 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11068 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11069 | #endif |
| 11070 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11071 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11072 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11073 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11074 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11075 | #endif |
| 11076 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11077 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11078 | #endif |
| 11079 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11080 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11081 | #endif |
| 11082 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11083 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11084 | #endif |
| 11085 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11086 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11087 | #endif |
| 11088 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11089 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11090 | #endif |
| 11091 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11092 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11093 | #endif |
| 11094 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11095 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11096 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11097 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11098 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11099 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11100 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11101 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11102 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11103 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11104 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11105 | #endif |
| 11106 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11107 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11108 | #endif |
| 11109 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11110 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11111 | #endif |
| 11112 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11113 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11114 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11115 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11116 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11117 | #endif |
| 11118 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11119 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11120 | #endif |
| 11121 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11122 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11123 | #endif |
| 11124 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11125 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11126 | #endif |
| 11127 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11128 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11129 | #endif |
| 11130 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11131 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11132 | #endif |
| 11133 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11134 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11135 | #endif |
| 11136 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11137 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11138 | #endif |
| 11139 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11140 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11141 | #endif |
| 11142 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11143 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11144 | #endif |
| 11145 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11146 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11147 | #endif |
| 11148 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11149 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11150 | #endif |
| 11151 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11152 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11153 | #endif |
| 11154 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11155 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11156 | #endif |
| 11157 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11158 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11159 | #endif |
| 11160 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11161 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11162 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11163 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11164 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11165 | #endif |
| 11166 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11167 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11168 | #endif |
| 11169 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11170 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11171 | #endif |
| 11172 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11173 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11174 | #endif |
| 11175 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11176 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11177 | #endif |
| 11178 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11179 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11180 | #endif |
| 11181 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11182 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11183 | #endif |
| 11184 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11185 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11186 | #endif |
| 11187 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11188 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11189 | #endif |
| 11190 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11191 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11192 | #endif |
| 11193 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11194 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11195 | #endif |
| 11196 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11197 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11198 | #endif |
| 11199 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11200 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11201 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11202 | }; |
| 11203 | |
| 11204 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11205 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11206 | { |
| 11207 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 11208 | sizeof(posix_constants_confstr) |
| 11209 | / sizeof(struct constdef)); |
| 11210 | } |
| 11211 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11212 | |
| 11213 | /*[clinic input] |
| 11214 | os.confstr |
| 11215 | |
| 11216 | name: confstr_confname |
| 11217 | / |
| 11218 | |
| 11219 | Return a string-valued system configuration variable. |
| 11220 | [clinic start generated code]*/ |
| 11221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11222 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11223 | os_confstr_impl(PyObject *module, int name) |
| 11224 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11225 | { |
| 11226 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11227 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11228 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11229 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11230 | errno = 0; |
| 11231 | len = confstr(name, buffer, sizeof(buffer)); |
| 11232 | if (len == 0) { |
| 11233 | if (errno) { |
| 11234 | posix_error(); |
| 11235 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11236 | } |
| 11237 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11238 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11239 | } |
| 11240 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11241 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11242 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11243 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11244 | char *buf = PyMem_Malloc(len); |
| 11245 | if (buf == NULL) |
| 11246 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11247 | len2 = confstr(name, buf, len); |
| 11248 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 11249 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11250 | PyMem_Free(buf); |
| 11251 | } |
| 11252 | else |
| 11253 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11254 | return result; |
| 11255 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11256 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11257 | |
| 11258 | |
| 11259 | #ifdef HAVE_SYSCONF |
| 11260 | static struct constdef posix_constants_sysconf[] = { |
| 11261 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11262 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11263 | #endif |
| 11264 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11265 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11266 | #endif |
| 11267 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11268 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11269 | #endif |
| 11270 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11271 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11272 | #endif |
| 11273 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11274 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11275 | #endif |
| 11276 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11277 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11278 | #endif |
| 11279 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11280 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11281 | #endif |
| 11282 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11283 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11284 | #endif |
| 11285 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11286 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11287 | #endif |
| 11288 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11289 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11290 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11291 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11292 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11293 | #endif |
| 11294 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11295 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11296 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11297 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11298 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11299 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11300 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11301 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11302 | #endif |
| 11303 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11304 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11305 | #endif |
| 11306 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11307 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11308 | #endif |
| 11309 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11310 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11311 | #endif |
| 11312 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11313 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11314 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11315 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11316 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11317 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11318 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11319 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11320 | #endif |
| 11321 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11322 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11323 | #endif |
| 11324 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11325 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11326 | #endif |
| 11327 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11328 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11329 | #endif |
| 11330 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11331 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11332 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11333 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11334 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11335 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11336 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11337 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11338 | #endif |
| 11339 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11340 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11341 | #endif |
| 11342 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11343 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11344 | #endif |
| 11345 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11346 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11347 | #endif |
| 11348 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11349 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11350 | #endif |
| 11351 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11352 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11353 | #endif |
| 11354 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11355 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11356 | #endif |
| 11357 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11358 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11359 | #endif |
| 11360 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11361 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11362 | #endif |
| 11363 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11364 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11365 | #endif |
| 11366 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11367 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11368 | #endif |
| 11369 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11370 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11371 | #endif |
| 11372 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11373 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11374 | #endif |
| 11375 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11376 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11377 | #endif |
| 11378 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11379 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11380 | #endif |
| 11381 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11382 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11383 | #endif |
| 11384 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11385 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11386 | #endif |
| 11387 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11388 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11389 | #endif |
| 11390 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11391 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11392 | #endif |
| 11393 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11394 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11395 | #endif |
| 11396 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11397 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11398 | #endif |
| 11399 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11400 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11401 | #endif |
| 11402 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11403 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11404 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11405 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11406 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11407 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11408 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11409 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11410 | #endif |
| 11411 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11412 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11413 | #endif |
| 11414 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11415 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11416 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11417 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11418 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11419 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11420 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11421 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11422 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11423 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11424 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11425 | #endif |
| 11426 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11427 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11428 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11429 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11430 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11431 | #endif |
| 11432 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11433 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11434 | #endif |
| 11435 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11436 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11437 | #endif |
| 11438 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11439 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11440 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11441 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11442 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11443 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11444 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11445 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11446 | #endif |
| 11447 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11448 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11449 | #endif |
| 11450 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11451 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11452 | #endif |
| 11453 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11454 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11455 | #endif |
| 11456 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11457 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11458 | #endif |
| 11459 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11460 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11461 | #endif |
| 11462 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11463 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11464 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11465 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11466 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11467 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11468 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11469 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11470 | #endif |
| 11471 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11472 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11473 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11474 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11475 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11476 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11477 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11478 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11479 | #endif |
| 11480 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11481 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11482 | #endif |
| 11483 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11484 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11485 | #endif |
| 11486 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11487 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11488 | #endif |
| 11489 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11490 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11491 | #endif |
| 11492 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11493 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11494 | #endif |
| 11495 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11496 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11497 | #endif |
| 11498 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11499 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11500 | #endif |
| 11501 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11502 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11503 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11504 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11505 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11506 | #endif |
| 11507 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11508 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11509 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11510 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11511 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11512 | #endif |
| 11513 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11514 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11515 | #endif |
| 11516 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11517 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11518 | #endif |
| 11519 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11520 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11521 | #endif |
Batuhan Taşkaya | 909f4a3 | 2020-04-05 03:40:49 +0300 | [diff] [blame] | 11522 | #ifdef _SC_AIX_REALMEM |
| 11523 | {"SC_AIX_REALMEM", _SC_AIX_REALMEM}, |
| 11524 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11525 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11526 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11527 | #endif |
| 11528 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11529 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11530 | #endif |
| 11531 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11532 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11533 | #endif |
| 11534 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11535 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11536 | #endif |
| 11537 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11538 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11539 | #endif |
| 11540 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11541 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11542 | #endif |
| 11543 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11544 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11545 | #endif |
| 11546 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11547 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11548 | #endif |
| 11549 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11550 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11551 | #endif |
| 11552 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11553 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11554 | #endif |
| 11555 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11556 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11557 | #endif |
| 11558 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11559 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11560 | #endif |
| 11561 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11562 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11563 | #endif |
| 11564 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11565 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11566 | #endif |
| 11567 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11568 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11569 | #endif |
| 11570 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11571 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11572 | #endif |
| 11573 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11574 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11575 | #endif |
| 11576 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11577 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11578 | #endif |
| 11579 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11580 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11581 | #endif |
| 11582 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11583 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11584 | #endif |
| 11585 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11586 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11587 | #endif |
| 11588 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11589 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11590 | #endif |
| 11591 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11592 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11593 | #endif |
| 11594 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11595 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11596 | #endif |
| 11597 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11598 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11599 | #endif |
| 11600 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11601 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11602 | #endif |
| 11603 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11604 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11605 | #endif |
| 11606 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11607 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11608 | #endif |
| 11609 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11610 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11611 | #endif |
| 11612 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11613 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11614 | #endif |
| 11615 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11616 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11617 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11618 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11619 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11620 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11621 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11622 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11623 | #endif |
| 11624 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11625 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11626 | #endif |
| 11627 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11628 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11629 | #endif |
| 11630 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11631 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11632 | #endif |
| 11633 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11634 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11635 | #endif |
| 11636 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11637 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11638 | #endif |
| 11639 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11640 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11641 | #endif |
| 11642 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11643 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11644 | #endif |
| 11645 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11646 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11647 | #endif |
| 11648 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11649 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11650 | #endif |
| 11651 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11652 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11653 | #endif |
| 11654 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11655 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11656 | #endif |
| 11657 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11658 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11659 | #endif |
| 11660 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11661 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11662 | #endif |
| 11663 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11664 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11665 | #endif |
| 11666 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11667 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11668 | #endif |
| 11669 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11670 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11671 | #endif |
| 11672 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11673 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11674 | #endif |
| 11675 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11676 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11677 | #endif |
| 11678 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11679 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11680 | #endif |
| 11681 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11682 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11683 | #endif |
| 11684 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11685 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11686 | #endif |
| 11687 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11688 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11689 | #endif |
| 11690 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11691 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11692 | #endif |
| 11693 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11694 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11695 | #endif |
| 11696 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11697 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11698 | #endif |
| 11699 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11700 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11701 | #endif |
| 11702 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11703 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11704 | #endif |
| 11705 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11706 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11707 | #endif |
| 11708 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11709 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11710 | #endif |
| 11711 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11712 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11713 | #endif |
| 11714 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11715 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11716 | #endif |
| 11717 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11718 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11719 | #endif |
| 11720 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11721 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11722 | #endif |
| 11723 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11724 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11725 | #endif |
| 11726 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11727 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11728 | #endif |
| 11729 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11730 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11731 | #endif |
| 11732 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11733 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11734 | #endif |
| 11735 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11736 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11737 | #endif |
| 11738 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11739 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11740 | #endif |
| 11741 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11742 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11743 | #endif |
| 11744 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11745 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11746 | #endif |
| 11747 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11748 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11749 | #endif |
| 11750 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11751 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11752 | #endif |
| 11753 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11754 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11755 | #endif |
| 11756 | }; |
| 11757 | |
| 11758 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11759 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11760 | { |
| 11761 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11762 | sizeof(posix_constants_sysconf) |
| 11763 | / sizeof(struct constdef)); |
| 11764 | } |
| 11765 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11766 | |
| 11767 | /*[clinic input] |
| 11768 | os.sysconf -> long |
| 11769 | name: sysconf_confname |
| 11770 | / |
| 11771 | |
| 11772 | Return an integer-valued system configuration variable. |
| 11773 | [clinic start generated code]*/ |
| 11774 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11775 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11776 | os_sysconf_impl(PyObject *module, int name) |
| 11777 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11778 | { |
| 11779 | long value; |
| 11780 | |
| 11781 | errno = 0; |
| 11782 | value = sysconf(name); |
| 11783 | if (value == -1 && errno != 0) |
| 11784 | posix_error(); |
| 11785 | return value; |
| 11786 | } |
| 11787 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11788 | |
| 11789 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11790 | /* 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] | 11791 | * 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] | 11792 | * the exported dictionaries that are used to publish information about the |
| 11793 | * names available on the host platform. |
| 11794 | * |
| 11795 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11796 | * when used, even for platforms we're not able to test on. It also makes |
| 11797 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11798 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11799 | |
| 11800 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11801 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11802 | { |
| 11803 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11804 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11805 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11806 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11807 | |
| 11808 | return strcmp(c1->name, c2->name); |
| 11809 | } |
| 11810 | |
| 11811 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11812 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11813 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11814 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11815 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11816 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11817 | |
| 11818 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11819 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11820 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11821 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11822 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11823 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11824 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11825 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11826 | Py_XDECREF(o); |
| 11827 | Py_DECREF(d); |
| 11828 | return -1; |
| 11829 | } |
| 11830 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11831 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11832 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11833 | } |
| 11834 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11835 | /* Return -1 on failure, 0 on success. */ |
| 11836 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11837 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11838 | { |
| 11839 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11840 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11841 | sizeof(posix_constants_pathconf) |
| 11842 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11843 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11844 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11845 | #endif |
| 11846 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11847 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11848 | sizeof(posix_constants_confstr) |
| 11849 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11850 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11851 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11852 | #endif |
| 11853 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11854 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11855 | sizeof(posix_constants_sysconf) |
| 11856 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11857 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11858 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11859 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11860 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11861 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11862 | |
| 11863 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11864 | /*[clinic input] |
| 11865 | os.abort |
| 11866 | |
| 11867 | Abort the interpreter immediately. |
| 11868 | |
| 11869 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11870 | on the hosting operating system. This function never returns. |
| 11871 | [clinic start generated code]*/ |
| 11872 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11873 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11874 | os_abort_impl(PyObject *module) |
| 11875 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11876 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11877 | abort(); |
| 11878 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11879 | #ifndef __clang__ |
| 11880 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11881 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11882 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11883 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11884 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11885 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11886 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11887 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11888 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11889 | /* Grab ShellExecute dynamically from shell32 */ |
| 11890 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11891 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11892 | LPCWSTR, INT); |
| 11893 | static int |
| 11894 | check_ShellExecute() |
| 11895 | { |
| 11896 | HINSTANCE hShell32; |
| 11897 | |
| 11898 | /* only recheck */ |
| 11899 | if (-1 == has_ShellExecute) { |
| 11900 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11901 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11902 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11903 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11904 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11905 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11906 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11907 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11908 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11909 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11910 | } else { |
| 11911 | has_ShellExecute = 0; |
| 11912 | } |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 11913 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11914 | } |
| 11915 | return has_ShellExecute; |
| 11916 | } |
| 11917 | |
| 11918 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11919 | /*[clinic input] |
| 11920 | os.startfile |
| 11921 | filepath: path_t |
| 11922 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11923 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11924 | Start a file with its associated application. |
| 11925 | |
| 11926 | When "operation" is not specified or "open", this acts like |
| 11927 | double-clicking the file in Explorer, or giving the file name as an |
| 11928 | argument to the DOS "start" command: the file is opened with whatever |
| 11929 | application (if any) its extension is associated. |
| 11930 | When another "operation" is given, it specifies what should be done with |
| 11931 | the file. A typical operation is "print". |
| 11932 | |
| 11933 | startfile returns as soon as the associated application is launched. |
| 11934 | There is no option to wait for the application to close, and no way |
| 11935 | to retrieve the application's exit status. |
| 11936 | |
| 11937 | The filepath is relative to the current directory. If you want to use |
| 11938 | an absolute path, make sure the first character is not a slash ("/"); |
| 11939 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11940 | [clinic start generated code]*/ |
| 11941 | |
| 11942 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11943 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11944 | const Py_UNICODE *operation) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 11945 | /*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11946 | { |
| 11947 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11948 | |
| 11949 | if(!check_ShellExecute()) { |
| 11950 | /* If the OS doesn't have ShellExecute, return a |
| 11951 | NotImplementedError. */ |
| 11952 | return PyErr_Format(PyExc_NotImplementedError, |
| 11953 | "startfile not available on this platform"); |
| 11954 | } |
| 11955 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 11956 | if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 11957 | return NULL; |
| 11958 | } |
| 11959 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11960 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11961 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11962 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11963 | Py_END_ALLOW_THREADS |
| 11964 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11965 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11966 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11967 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11968 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11969 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11970 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11971 | #endif /* MS_WINDOWS */ |
| 11972 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11973 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11974 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11975 | /*[clinic input] |
| 11976 | os.getloadavg |
| 11977 | |
| 11978 | Return average recent system load information. |
| 11979 | |
| 11980 | Return the number of processes in the system run queue averaged over |
| 11981 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11982 | Raises OSError if the load average was unobtainable. |
| 11983 | [clinic start generated code]*/ |
| 11984 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11985 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11986 | os_getloadavg_impl(PyObject *module) |
| 11987 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11988 | { |
| 11989 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11990 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11991 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11992 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11993 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11994 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11995 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11996 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11997 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11998 | |
| 11999 | /*[clinic input] |
| 12000 | os.device_encoding |
| 12001 | fd: int |
| 12002 | |
| 12003 | Return a string describing the encoding of a terminal's file descriptor. |
| 12004 | |
| 12005 | The file descriptor must be attached to a terminal. |
| 12006 | If the device is not a terminal, return None. |
| 12007 | [clinic start generated code]*/ |
| 12008 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12009 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12010 | os_device_encoding_impl(PyObject *module, int fd) |
| 12011 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12012 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 12013 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 12014 | } |
| 12015 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12016 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12017 | #ifdef HAVE_SETRESUID |
| 12018 | /*[clinic input] |
| 12019 | os.setresuid |
| 12020 | |
| 12021 | ruid: uid_t |
| 12022 | euid: uid_t |
| 12023 | suid: uid_t |
| 12024 | / |
| 12025 | |
| 12026 | Set the current process's real, effective, and saved user ids. |
| 12027 | [clinic start generated code]*/ |
| 12028 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12029 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12030 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 12031 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12032 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12033 | if (setresuid(ruid, euid, suid) < 0) |
| 12034 | return posix_error(); |
| 12035 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12036 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12037 | #endif /* HAVE_SETRESUID */ |
| 12038 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12039 | |
| 12040 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12041 | /*[clinic input] |
| 12042 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12043 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12044 | rgid: gid_t |
| 12045 | egid: gid_t |
| 12046 | sgid: gid_t |
| 12047 | / |
| 12048 | |
| 12049 | Set the current process's real, effective, and saved group ids. |
| 12050 | [clinic start generated code]*/ |
| 12051 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12052 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12053 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 12054 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12055 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12056 | if (setresgid(rgid, egid, sgid) < 0) |
| 12057 | return posix_error(); |
| 12058 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12059 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12060 | #endif /* HAVE_SETRESGID */ |
| 12061 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12062 | |
| 12063 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12064 | /*[clinic input] |
| 12065 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12066 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12067 | Return a tuple of the current process's real, effective, and saved user ids. |
| 12068 | [clinic start generated code]*/ |
| 12069 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12070 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12071 | os_getresuid_impl(PyObject *module) |
| 12072 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12073 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12074 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12075 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 12076 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 12077 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 12078 | _PyLong_FromUid(euid), |
| 12079 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12080 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12081 | #endif /* HAVE_GETRESUID */ |
| 12082 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12083 | |
| 12084 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12085 | /*[clinic input] |
| 12086 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12087 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12088 | Return a tuple of the current process's real, effective, and saved group ids. |
| 12089 | [clinic start generated code]*/ |
| 12090 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12091 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12092 | os_getresgid_impl(PyObject *module) |
| 12093 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12094 | { |
| 12095 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12096 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 12097 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 12098 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 12099 | _PyLong_FromGid(egid), |
| 12100 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12101 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12102 | #endif /* HAVE_GETRESGID */ |
| 12103 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12104 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12105 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12106 | /*[clinic input] |
| 12107 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12108 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12109 | path: path_t(allow_fd=True) |
| 12110 | attribute: path_t |
| 12111 | * |
| 12112 | follow_symlinks: bool = True |
| 12113 | |
| 12114 | Return the value of extended attribute attribute on path. |
| 12115 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12116 | 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] | 12117 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12118 | link, getxattr will examine the symbolic link itself instead of the file |
| 12119 | the link points to. |
| 12120 | |
| 12121 | [clinic start generated code]*/ |
| 12122 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12123 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12124 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12125 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12126 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12127 | { |
| 12128 | Py_ssize_t i; |
| 12129 | PyObject *buffer = NULL; |
| 12130 | |
| 12131 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 12132 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12133 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12134 | if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) { |
| 12135 | return NULL; |
| 12136 | } |
| 12137 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12138 | for (i = 0; ; i++) { |
| 12139 | void *ptr; |
| 12140 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12141 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12142 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12143 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12144 | path_error(path); |
| 12145 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12146 | } |
| 12147 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 12148 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12149 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12150 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12151 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12152 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12153 | if (path->fd >= 0) |
| 12154 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12155 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12156 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12157 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12158 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12159 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12160 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12161 | if (result < 0) { |
| 12162 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12163 | if (errno == ERANGE) |
| 12164 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12165 | path_error(path); |
| 12166 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12167 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12168 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12169 | if (result != buffer_size) { |
| 12170 | /* Can only shrink. */ |
| 12171 | _PyBytes_Resize(&buffer, result); |
| 12172 | } |
| 12173 | break; |
| 12174 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12175 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12176 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12177 | } |
| 12178 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12179 | |
| 12180 | /*[clinic input] |
| 12181 | os.setxattr |
| 12182 | |
| 12183 | path: path_t(allow_fd=True) |
| 12184 | attribute: path_t |
| 12185 | value: Py_buffer |
| 12186 | flags: int = 0 |
| 12187 | * |
| 12188 | follow_symlinks: bool = True |
| 12189 | |
| 12190 | Set extended attribute attribute on path to value. |
| 12191 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12192 | 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] | 12193 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12194 | link, setxattr will modify the symbolic link itself instead of the file |
| 12195 | the link points to. |
| 12196 | |
| 12197 | [clinic start generated code]*/ |
| 12198 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12199 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12200 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12201 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12202 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12203 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12204 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12205 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12206 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12207 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12208 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12209 | if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object, |
| 12210 | value->buf, value->len, flags) < 0) { |
| 12211 | return NULL; |
| 12212 | } |
| 12213 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12214 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12215 | if (path->fd > -1) |
| 12216 | result = fsetxattr(path->fd, attribute->narrow, |
| 12217 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12218 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12219 | result = setxattr(path->narrow, attribute->narrow, |
| 12220 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12221 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12222 | result = lsetxattr(path->narrow, attribute->narrow, |
| 12223 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12224 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12225 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12226 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12227 | path_error(path); |
| 12228 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12229 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12230 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12231 | Py_RETURN_NONE; |
| 12232 | } |
| 12233 | |
| 12234 | |
| 12235 | /*[clinic input] |
| 12236 | os.removexattr |
| 12237 | |
| 12238 | path: path_t(allow_fd=True) |
| 12239 | attribute: path_t |
| 12240 | * |
| 12241 | follow_symlinks: bool = True |
| 12242 | |
| 12243 | Remove extended attribute attribute on path. |
| 12244 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12245 | 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] | 12246 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12247 | link, removexattr will modify the symbolic link itself instead of the file |
| 12248 | the link points to. |
| 12249 | |
| 12250 | [clinic start generated code]*/ |
| 12251 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12252 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12253 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12254 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12255 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12256 | { |
| 12257 | ssize_t result; |
| 12258 | |
| 12259 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 12260 | return NULL; |
| 12261 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12262 | if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) { |
| 12263 | return NULL; |
| 12264 | } |
| 12265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12266 | Py_BEGIN_ALLOW_THREADS; |
| 12267 | if (path->fd > -1) |
| 12268 | result = fremovexattr(path->fd, attribute->narrow); |
| 12269 | else if (follow_symlinks) |
| 12270 | result = removexattr(path->narrow, attribute->narrow); |
| 12271 | else |
| 12272 | result = lremovexattr(path->narrow, attribute->narrow); |
| 12273 | Py_END_ALLOW_THREADS; |
| 12274 | |
| 12275 | if (result) { |
| 12276 | return path_error(path); |
| 12277 | } |
| 12278 | |
| 12279 | Py_RETURN_NONE; |
| 12280 | } |
| 12281 | |
| 12282 | |
| 12283 | /*[clinic input] |
| 12284 | os.listxattr |
| 12285 | |
| 12286 | path: path_t(allow_fd=True, nullable=True) = None |
| 12287 | * |
| 12288 | follow_symlinks: bool = True |
| 12289 | |
| 12290 | Return a list of extended attributes on path. |
| 12291 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12292 | 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] | 12293 | if path is None, listxattr will examine the current directory. |
| 12294 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12295 | link, listxattr will examine the symbolic link itself instead of the file |
| 12296 | the link points to. |
| 12297 | [clinic start generated code]*/ |
| 12298 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12299 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12300 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12301 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12302 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12303 | Py_ssize_t i; |
| 12304 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12305 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12306 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12307 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12308 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12309 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12310 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12311 | if (PySys_Audit("os.listxattr", "(O)", |
| 12312 | path->object ? path->object : Py_None) < 0) { |
| 12313 | return NULL; |
| 12314 | } |
| 12315 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12316 | name = path->narrow ? path->narrow : "."; |
| 12317 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12318 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12319 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12320 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12321 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12322 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12323 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 12324 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12325 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12326 | break; |
| 12327 | } |
| 12328 | buffer = PyMem_MALLOC(buffer_size); |
| 12329 | if (!buffer) { |
| 12330 | PyErr_NoMemory(); |
| 12331 | break; |
| 12332 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12333 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12334 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12335 | if (path->fd > -1) |
| 12336 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12337 | else if (follow_symlinks) |
| 12338 | length = listxattr(name, buffer, buffer_size); |
| 12339 | else |
| 12340 | length = llistxattr(name, buffer, buffer_size); |
| 12341 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12342 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12343 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12344 | if (errno == ERANGE) { |
| 12345 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 12346 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12347 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12348 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12349 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12350 | break; |
| 12351 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12352 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12353 | result = PyList_New(0); |
| 12354 | if (!result) { |
| 12355 | goto exit; |
| 12356 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12357 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12358 | end = buffer + length; |
| 12359 | for (trace = start = buffer; trace != end; trace++) { |
| 12360 | if (!*trace) { |
| 12361 | int error; |
| 12362 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 12363 | trace - start); |
| 12364 | if (!attribute) { |
| 12365 | Py_DECREF(result); |
| 12366 | result = NULL; |
| 12367 | goto exit; |
| 12368 | } |
| 12369 | error = PyList_Append(result, attribute); |
| 12370 | Py_DECREF(attribute); |
| 12371 | if (error) { |
| 12372 | Py_DECREF(result); |
| 12373 | result = NULL; |
| 12374 | goto exit; |
| 12375 | } |
| 12376 | start = trace + 1; |
| 12377 | } |
| 12378 | } |
| 12379 | break; |
| 12380 | } |
| 12381 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12382 | if (buffer) |
| 12383 | PyMem_FREE(buffer); |
| 12384 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12385 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12386 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12387 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12388 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12389 | /*[clinic input] |
| 12390 | os.urandom |
| 12391 | |
| 12392 | size: Py_ssize_t |
| 12393 | / |
| 12394 | |
| 12395 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 12396 | [clinic start generated code]*/ |
| 12397 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12398 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12399 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 12400 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12401 | { |
| 12402 | PyObject *bytes; |
| 12403 | int result; |
| 12404 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12405 | if (size < 0) |
| 12406 | return PyErr_Format(PyExc_ValueError, |
| 12407 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12408 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 12409 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12410 | return NULL; |
| 12411 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 12412 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12413 | if (result == -1) { |
| 12414 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12415 | return NULL; |
| 12416 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12417 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12418 | } |
| 12419 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 12420 | #ifdef HAVE_MEMFD_CREATE |
| 12421 | /*[clinic input] |
| 12422 | os.memfd_create |
| 12423 | |
| 12424 | name: FSConverter |
| 12425 | flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC |
| 12426 | |
| 12427 | [clinic start generated code]*/ |
| 12428 | |
| 12429 | static PyObject * |
| 12430 | os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags) |
| 12431 | /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/ |
| 12432 | { |
| 12433 | int fd; |
| 12434 | const char *bytes = PyBytes_AS_STRING(name); |
| 12435 | Py_BEGIN_ALLOW_THREADS |
| 12436 | fd = memfd_create(bytes, flags); |
| 12437 | Py_END_ALLOW_THREADS |
| 12438 | if (fd == -1) { |
| 12439 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12440 | } |
| 12441 | return PyLong_FromLong(fd); |
| 12442 | } |
| 12443 | #endif |
| 12444 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12445 | /* Terminal size querying */ |
| 12446 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12447 | PyDoc_STRVAR(TerminalSize_docstring, |
| 12448 | "A tuple of (columns, lines) for holding terminal window size"); |
| 12449 | |
| 12450 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 12451 | {"columns", "width of the terminal window in characters"}, |
| 12452 | {"lines", "height of the terminal window in characters"}, |
| 12453 | {NULL, NULL} |
| 12454 | }; |
| 12455 | |
| 12456 | static PyStructSequence_Desc TerminalSize_desc = { |
| 12457 | "os.terminal_size", |
| 12458 | TerminalSize_docstring, |
| 12459 | TerminalSize_fields, |
| 12460 | 2, |
| 12461 | }; |
| 12462 | |
| 12463 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12464 | /*[clinic input] |
| 12465 | os.get_terminal_size |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12466 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12467 | fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1 |
| 12468 | / |
| 12469 | |
| 12470 | Return the size of the terminal window as (columns, lines). |
| 12471 | |
| 12472 | The optional argument fd (default standard output) specifies |
| 12473 | which file descriptor should be queried. |
| 12474 | |
| 12475 | If the file descriptor is not connected to a terminal, an OSError |
| 12476 | is thrown. |
| 12477 | |
| 12478 | This function will only be defined if an implementation is |
| 12479 | available for this system. |
| 12480 | |
| 12481 | shutil.get_terminal_size is the high-level function which should |
| 12482 | normally be used, os.get_terminal_size is the low-level implementation. |
| 12483 | [clinic start generated code]*/ |
| 12484 | |
| 12485 | static PyObject * |
| 12486 | os_get_terminal_size_impl(PyObject *module, int fd) |
| 12487 | /*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12488 | { |
| 12489 | int columns, lines; |
| 12490 | PyObject *termsize; |
| 12491 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12492 | /* Under some conditions stdout may not be connected and |
| 12493 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 12494 | * GUI apps don't have valid standard streams by default. |
| 12495 | * |
| 12496 | * If this happens, and the optional fd argument is not present, |
| 12497 | * the ioctl below will fail returning EBADF. This is what we want. |
| 12498 | */ |
| 12499 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12500 | #ifdef TERMSIZE_USE_IOCTL |
| 12501 | { |
| 12502 | struct winsize w; |
| 12503 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 12504 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12505 | columns = w.ws_col; |
| 12506 | lines = w.ws_row; |
| 12507 | } |
| 12508 | #endif /* TERMSIZE_USE_IOCTL */ |
| 12509 | |
| 12510 | #ifdef TERMSIZE_USE_CONIO |
| 12511 | { |
| 12512 | DWORD nhandle; |
| 12513 | HANDLE handle; |
| 12514 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 12515 | switch (fd) { |
| 12516 | case 0: nhandle = STD_INPUT_HANDLE; |
| 12517 | break; |
| 12518 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 12519 | break; |
| 12520 | case 2: nhandle = STD_ERROR_HANDLE; |
| 12521 | break; |
| 12522 | default: |
| 12523 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 12524 | } |
| 12525 | handle = GetStdHandle(nhandle); |
| 12526 | if (handle == NULL) |
| 12527 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 12528 | if (handle == INVALID_HANDLE_VALUE) |
| 12529 | return PyErr_SetFromWindowsErr(0); |
| 12530 | |
| 12531 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 12532 | return PyErr_SetFromWindowsErr(0); |
| 12533 | |
| 12534 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 12535 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 12536 | } |
| 12537 | #endif /* TERMSIZE_USE_CONIO */ |
| 12538 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12539 | PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12540 | termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12541 | if (termsize == NULL) |
| 12542 | return NULL; |
| 12543 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 12544 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 12545 | if (PyErr_Occurred()) { |
| 12546 | Py_DECREF(termsize); |
| 12547 | return NULL; |
| 12548 | } |
| 12549 | return termsize; |
| 12550 | } |
| 12551 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 12552 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12553 | |
| 12554 | /*[clinic input] |
| 12555 | os.cpu_count |
| 12556 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 12557 | Return the number of CPUs in the system; return None if indeterminable. |
| 12558 | |
| 12559 | This number is not equivalent to the number of CPUs the current process can |
| 12560 | use. The number of usable CPUs can be obtained with |
| 12561 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12562 | [clinic start generated code]*/ |
| 12563 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12564 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12565 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12566 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12567 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12568 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12569 | #ifdef MS_WINDOWS |
Steve Dower | aa92927 | 2019-09-11 16:15:39 +0100 | [diff] [blame] | 12570 | ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12571 | #elif defined(__hpux) |
| 12572 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 12573 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 12574 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12575 | #elif defined(__DragonFly__) || \ |
| 12576 | defined(__OpenBSD__) || \ |
| 12577 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12578 | defined(__NetBSD__) || \ |
| 12579 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12580 | int mib[2]; |
| 12581 | size_t len = sizeof(ncpu); |
| 12582 | mib[0] = CTL_HW; |
| 12583 | mib[1] = HW_NCPU; |
| 12584 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 12585 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12586 | #endif |
| 12587 | if (ncpu >= 1) |
| 12588 | return PyLong_FromLong(ncpu); |
| 12589 | else |
| 12590 | Py_RETURN_NONE; |
| 12591 | } |
| 12592 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12593 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12594 | /*[clinic input] |
| 12595 | os.get_inheritable -> bool |
| 12596 | |
| 12597 | fd: int |
| 12598 | / |
| 12599 | |
| 12600 | Get the close-on-exe flag of the specified file descriptor. |
| 12601 | [clinic start generated code]*/ |
| 12602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12603 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12604 | os_get_inheritable_impl(PyObject *module, int fd) |
| 12605 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12606 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12607 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12608 | _Py_BEGIN_SUPPRESS_IPH |
| 12609 | return_value = _Py_get_inheritable(fd); |
| 12610 | _Py_END_SUPPRESS_IPH |
| 12611 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12612 | } |
| 12613 | |
| 12614 | |
| 12615 | /*[clinic input] |
| 12616 | os.set_inheritable |
| 12617 | fd: int |
| 12618 | inheritable: int |
| 12619 | / |
| 12620 | |
| 12621 | Set the inheritable flag of the specified file descriptor. |
| 12622 | [clinic start generated code]*/ |
| 12623 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12624 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12625 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 12626 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12627 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12628 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12629 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12630 | _Py_BEGIN_SUPPRESS_IPH |
| 12631 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 12632 | _Py_END_SUPPRESS_IPH |
| 12633 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12634 | return NULL; |
| 12635 | Py_RETURN_NONE; |
| 12636 | } |
| 12637 | |
| 12638 | |
| 12639 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12640 | /*[clinic input] |
| 12641 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12642 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12643 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12644 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12645 | Get the close-on-exe flag of the specified file descriptor. |
| 12646 | [clinic start generated code]*/ |
| 12647 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12648 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12649 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12650 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12651 | { |
| 12652 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12653 | |
| 12654 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 12655 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12656 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12657 | } |
| 12658 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12659 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12660 | } |
| 12661 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12662 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12663 | /*[clinic input] |
| 12664 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12665 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12666 | inheritable: bool |
| 12667 | / |
| 12668 | |
| 12669 | Set the inheritable flag of the specified handle. |
| 12670 | [clinic start generated code]*/ |
| 12671 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12672 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12673 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12674 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12675 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12676 | { |
| 12677 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12678 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 12679 | PyErr_SetFromWindowsErr(0); |
| 12680 | return NULL; |
| 12681 | } |
| 12682 | Py_RETURN_NONE; |
| 12683 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12684 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12685 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12686 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12687 | /*[clinic input] |
| 12688 | os.get_blocking -> bool |
| 12689 | fd: int |
| 12690 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12691 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12692 | Get the blocking mode of the file descriptor. |
| 12693 | |
| 12694 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 12695 | [clinic start generated code]*/ |
| 12696 | |
| 12697 | static int |
| 12698 | os_get_blocking_impl(PyObject *module, int fd) |
| 12699 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12700 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12701 | int blocking; |
| 12702 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12703 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12704 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12705 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12706 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12707 | } |
| 12708 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12709 | /*[clinic input] |
| 12710 | os.set_blocking |
| 12711 | fd: int |
| 12712 | blocking: bool(accept={int}) |
| 12713 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12714 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12715 | Set the blocking mode of the specified file descriptor. |
| 12716 | |
| 12717 | Set the O_NONBLOCK flag if blocking is False, |
| 12718 | clear the O_NONBLOCK flag otherwise. |
| 12719 | [clinic start generated code]*/ |
| 12720 | |
| 12721 | static PyObject * |
| 12722 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 12723 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12724 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12725 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12726 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12727 | _Py_BEGIN_SUPPRESS_IPH |
| 12728 | result = _Py_set_blocking(fd, blocking); |
| 12729 | _Py_END_SUPPRESS_IPH |
| 12730 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12731 | return NULL; |
| 12732 | Py_RETURN_NONE; |
| 12733 | } |
| 12734 | #endif /* !MS_WINDOWS */ |
| 12735 | |
| 12736 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12737 | /*[clinic input] |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12738 | class os.DirEntry "DirEntry *" "DirEntryType" |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12739 | [clinic start generated code]*/ |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12740 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12741 | |
| 12742 | typedef struct { |
| 12743 | PyObject_HEAD |
| 12744 | PyObject *name; |
| 12745 | PyObject *path; |
| 12746 | PyObject *stat; |
| 12747 | PyObject *lstat; |
| 12748 | #ifdef MS_WINDOWS |
| 12749 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12750 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12751 | int got_file_index; |
| 12752 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12753 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12754 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12755 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12756 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12757 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12758 | #endif |
| 12759 | } DirEntry; |
| 12760 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12761 | static PyObject * |
| 12762 | _disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 12763 | { |
| 12764 | PyErr_Format(PyExc_TypeError, |
| 12765 | "cannot create '%.100s' instances", _PyType_Name(type)); |
| 12766 | return NULL; |
| 12767 | } |
| 12768 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12769 | static void |
| 12770 | DirEntry_dealloc(DirEntry *entry) |
| 12771 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12772 | PyTypeObject *tp = Py_TYPE(entry); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12773 | Py_XDECREF(entry->name); |
| 12774 | Py_XDECREF(entry->path); |
| 12775 | Py_XDECREF(entry->stat); |
| 12776 | Py_XDECREF(entry->lstat); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12777 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 12778 | free_func(entry); |
| 12779 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12780 | } |
| 12781 | |
| 12782 | /* Forward reference */ |
| 12783 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12784 | DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self, |
| 12785 | int follow_symlinks, unsigned short mode_bits); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12786 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12787 | /*[clinic input] |
| 12788 | os.DirEntry.is_symlink -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12789 | defining_class: defining_class |
| 12790 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12791 | |
| 12792 | Return True if the entry is a symbolic link; cached per entry. |
| 12793 | [clinic start generated code]*/ |
| 12794 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12795 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12796 | os_DirEntry_is_symlink_impl(DirEntry *self, PyTypeObject *defining_class) |
| 12797 | /*[clinic end generated code: output=293096d589b6d47c input=e9acc5ee4d511113]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12798 | { |
| 12799 | #ifdef MS_WINDOWS |
| 12800 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12801 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12802 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12803 | if (self->d_type != DT_UNKNOWN) |
| 12804 | return self->d_type == DT_LNK; |
| 12805 | else |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12806 | return DirEntry_test_mode(defining_class, self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12807 | #else |
| 12808 | /* POSIX without d_type */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12809 | return DirEntry_test_mode(defining_class, self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12810 | #endif |
| 12811 | } |
| 12812 | |
| 12813 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12814 | DirEntry_fetch_stat(PyObject *module, DirEntry *self, int follow_symlinks) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12815 | { |
| 12816 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12817 | STRUCT_STAT st; |
| 12818 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12819 | |
| 12820 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12821 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12822 | return NULL; |
| 12823 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12824 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12825 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12826 | return NULL; |
| 12827 | const char *path = PyBytes_AS_STRING(ub); |
| 12828 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12829 | #ifdef HAVE_FSTATAT |
| 12830 | result = fstatat(self->dir_fd, path, &st, |
| 12831 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12832 | #else |
| 12833 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12834 | return NULL; |
| 12835 | #endif /* HAVE_FSTATAT */ |
| 12836 | } |
| 12837 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12838 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12839 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12840 | if (follow_symlinks) |
| 12841 | result = STAT(path, &st); |
| 12842 | else |
| 12843 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12844 | } |
| 12845 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12846 | |
| 12847 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12848 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12849 | |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12850 | return _pystat_fromstructstat(module, &st); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12851 | } |
| 12852 | |
| 12853 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12854 | DirEntry_get_lstat(PyTypeObject *defining_class, DirEntry *self) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12855 | { |
| 12856 | if (!self->lstat) { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12857 | PyObject *module = PyType_GetModule(defining_class); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12858 | #ifdef MS_WINDOWS |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12859 | self->lstat = _pystat_fromstructstat(module, &self->win32_lstat); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12860 | #else /* POSIX */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12861 | self->lstat = DirEntry_fetch_stat(module, self, 0); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12862 | #endif |
| 12863 | } |
| 12864 | Py_XINCREF(self->lstat); |
| 12865 | return self->lstat; |
| 12866 | } |
| 12867 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12868 | /*[clinic input] |
| 12869 | os.DirEntry.stat |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12870 | defining_class: defining_class |
| 12871 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12872 | * |
| 12873 | follow_symlinks: bool = True |
| 12874 | |
| 12875 | Return stat_result object for the entry; cached per entry. |
| 12876 | [clinic start generated code]*/ |
| 12877 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12878 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12879 | os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class, |
| 12880 | int follow_symlinks) |
| 12881 | /*[clinic end generated code: output=23f803e19c3e780e input=e816273c4e67ee98]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12882 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12883 | if (!follow_symlinks) { |
| 12884 | return DirEntry_get_lstat(defining_class, self); |
| 12885 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12886 | |
| 12887 | if (!self->stat) { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12888 | int result = os_DirEntry_is_symlink_impl(self, defining_class); |
| 12889 | if (result == -1) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12890 | return NULL; |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12891 | } |
| 12892 | if (result) { |
| 12893 | PyObject *module = PyType_GetModule(defining_class); |
| 12894 | self->stat = DirEntry_fetch_stat(module, self, 1); |
| 12895 | } |
| 12896 | else { |
| 12897 | self->stat = DirEntry_get_lstat(defining_class, self); |
| 12898 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12899 | } |
| 12900 | |
| 12901 | Py_XINCREF(self->stat); |
| 12902 | return self->stat; |
| 12903 | } |
| 12904 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12905 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12906 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12907 | DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self, |
| 12908 | int follow_symlinks, unsigned short mode_bits) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12909 | { |
| 12910 | PyObject *stat = NULL; |
| 12911 | PyObject *st_mode = NULL; |
| 12912 | long mode; |
| 12913 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12914 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12915 | int is_symlink; |
| 12916 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12917 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12918 | #ifdef MS_WINDOWS |
| 12919 | unsigned long dir_bits; |
| 12920 | #endif |
| 12921 | |
| 12922 | #ifdef MS_WINDOWS |
| 12923 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12924 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12925 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12926 | is_symlink = self->d_type == DT_LNK; |
| 12927 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12928 | #endif |
| 12929 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12930 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12931 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12932 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12933 | stat = os_DirEntry_stat_impl(self, defining_class, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12934 | if (!stat) { |
| 12935 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12936 | /* If file doesn't exist (anymore), then return False |
| 12937 | (i.e., say it's not a file/directory) */ |
| 12938 | PyErr_Clear(); |
| 12939 | return 0; |
| 12940 | } |
| 12941 | goto error; |
| 12942 | } |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12943 | _posixstate* state = get_posix_state(PyType_GetModule(defining_class)); |
| 12944 | st_mode = PyObject_GetAttr(stat, state->st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12945 | if (!st_mode) |
| 12946 | goto error; |
| 12947 | |
| 12948 | mode = PyLong_AsLong(st_mode); |
| 12949 | if (mode == -1 && PyErr_Occurred()) |
| 12950 | goto error; |
| 12951 | Py_CLEAR(st_mode); |
| 12952 | Py_CLEAR(stat); |
| 12953 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12954 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12955 | } |
| 12956 | else if (is_symlink) { |
| 12957 | assert(mode_bits != S_IFLNK); |
| 12958 | result = 0; |
| 12959 | } |
| 12960 | else { |
| 12961 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 12962 | #ifdef MS_WINDOWS |
| 12963 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 12964 | if (mode_bits == S_IFDIR) |
| 12965 | result = dir_bits != 0; |
| 12966 | else |
| 12967 | result = dir_bits == 0; |
| 12968 | #else /* POSIX */ |
| 12969 | if (mode_bits == S_IFDIR) |
| 12970 | result = self->d_type == DT_DIR; |
| 12971 | else |
| 12972 | result = self->d_type == DT_REG; |
| 12973 | #endif |
| 12974 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12975 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12976 | |
| 12977 | return result; |
| 12978 | |
| 12979 | error: |
| 12980 | Py_XDECREF(st_mode); |
| 12981 | Py_XDECREF(stat); |
| 12982 | return -1; |
| 12983 | } |
| 12984 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12985 | /*[clinic input] |
| 12986 | os.DirEntry.is_dir -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12987 | defining_class: defining_class |
| 12988 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12989 | * |
| 12990 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12991 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12992 | Return True if the entry is a directory; cached per entry. |
| 12993 | [clinic start generated code]*/ |
| 12994 | |
| 12995 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12996 | os_DirEntry_is_dir_impl(DirEntry *self, PyTypeObject *defining_class, |
| 12997 | int follow_symlinks) |
| 12998 | /*[clinic end generated code: output=0cd453b9c0987fdf input=1a4ffd6dec9920cb]*/ |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12999 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13000 | return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13001 | } |
| 13002 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13003 | /*[clinic input] |
| 13004 | os.DirEntry.is_file -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13005 | defining_class: defining_class |
| 13006 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13007 | * |
| 13008 | follow_symlinks: bool = True |
| 13009 | |
| 13010 | Return True if the entry is a file; cached per entry. |
| 13011 | [clinic start generated code]*/ |
| 13012 | |
| 13013 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13014 | os_DirEntry_is_file_impl(DirEntry *self, PyTypeObject *defining_class, |
| 13015 | int follow_symlinks) |
| 13016 | /*[clinic end generated code: output=f7c277ab5ba80908 input=0a64c5a12e802e3b]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13017 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13018 | return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13019 | } |
| 13020 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13021 | /*[clinic input] |
| 13022 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13023 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13024 | Return inode of the entry; cached per entry. |
| 13025 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13026 | |
| 13027 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13028 | os_DirEntry_inode_impl(DirEntry *self) |
| 13029 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13030 | { |
| 13031 | #ifdef MS_WINDOWS |
| 13032 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13033 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 13034 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13035 | STRUCT_STAT stat; |
| 13036 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13037 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13038 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13039 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13040 | path = PyUnicode_AsUnicode(unicode); |
| 13041 | result = LSTAT(path, &stat); |
| 13042 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13043 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13044 | if (result != 0) |
| 13045 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13046 | |
| 13047 | self->win32_file_index = stat.st_ino; |
| 13048 | self->got_file_index = 1; |
| 13049 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 13050 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 13051 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13052 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 13053 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 13054 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13055 | #endif |
| 13056 | } |
| 13057 | |
| 13058 | static PyObject * |
| 13059 | DirEntry_repr(DirEntry *self) |
| 13060 | { |
| 13061 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 13062 | } |
| 13063 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13064 | /*[clinic input] |
| 13065 | os.DirEntry.__fspath__ |
| 13066 | |
| 13067 | Returns the path for the entry. |
| 13068 | [clinic start generated code]*/ |
| 13069 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 13070 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13071 | os_DirEntry___fspath___impl(DirEntry *self) |
| 13072 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 13073 | { |
| 13074 | Py_INCREF(self->path); |
| 13075 | return self->path; |
| 13076 | } |
| 13077 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13078 | static PyMemberDef DirEntry_members[] = { |
| 13079 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 13080 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 13081 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 13082 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 13083 | {NULL} |
| 13084 | }; |
| 13085 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13086 | #include "clinic/posixmodule.c.h" |
| 13087 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13088 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13089 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 13090 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 13091 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 13092 | OS_DIRENTRY_STAT_METHODDEF |
| 13093 | OS_DIRENTRY_INODE_METHODDEF |
| 13094 | OS_DIRENTRY___FSPATH___METHODDEF |
Batuhan Taşkaya | f9dd51e | 2020-04-08 00:37:19 +0300 | [diff] [blame] | 13095 | {"__class_getitem__", (PyCFunction)Py_GenericAlias, |
| 13096 | METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13097 | {NULL} |
| 13098 | }; |
| 13099 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13100 | static PyType_Slot DirEntryType_slots[] = { |
| 13101 | {Py_tp_new, _disabled_new}, |
| 13102 | {Py_tp_dealloc, DirEntry_dealloc}, |
| 13103 | {Py_tp_repr, DirEntry_repr}, |
| 13104 | {Py_tp_methods, DirEntry_methods}, |
| 13105 | {Py_tp_members, DirEntry_members}, |
| 13106 | {0, 0}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13107 | }; |
| 13108 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13109 | static PyType_Spec DirEntryType_spec = { |
| 13110 | MODNAME ".DirEntry", |
| 13111 | sizeof(DirEntry), |
| 13112 | 0, |
| 13113 | Py_TPFLAGS_DEFAULT, |
| 13114 | DirEntryType_slots |
| 13115 | }; |
| 13116 | |
| 13117 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13118 | #ifdef MS_WINDOWS |
| 13119 | |
| 13120 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 13121 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13122 | { |
| 13123 | Py_ssize_t path_len; |
| 13124 | Py_ssize_t size; |
| 13125 | wchar_t *result; |
| 13126 | wchar_t ch; |
| 13127 | |
| 13128 | if (!path_wide) { /* Default arg: "." */ |
| 13129 | path_wide = L"."; |
| 13130 | path_len = 1; |
| 13131 | } |
| 13132 | else { |
| 13133 | path_len = wcslen(path_wide); |
| 13134 | } |
| 13135 | |
| 13136 | /* The +1's are for the path separator and the NUL */ |
| 13137 | size = path_len + 1 + wcslen(filename) + 1; |
| 13138 | result = PyMem_New(wchar_t, size); |
| 13139 | if (!result) { |
| 13140 | PyErr_NoMemory(); |
| 13141 | return NULL; |
| 13142 | } |
| 13143 | wcscpy(result, path_wide); |
| 13144 | if (path_len > 0) { |
| 13145 | ch = result[path_len - 1]; |
| 13146 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 13147 | result[path_len++] = SEP; |
| 13148 | wcscpy(result + path_len, filename); |
| 13149 | } |
| 13150 | return result; |
| 13151 | } |
| 13152 | |
| 13153 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13154 | 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] | 13155 | { |
| 13156 | DirEntry *entry; |
| 13157 | BY_HANDLE_FILE_INFORMATION file_info; |
| 13158 | ULONG reparse_tag; |
| 13159 | wchar_t *joined_path; |
| 13160 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13161 | PyObject *DirEntryType = get_posix_state(module)->DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13162 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13163 | if (!entry) |
| 13164 | return NULL; |
| 13165 | entry->name = NULL; |
| 13166 | entry->path = NULL; |
| 13167 | entry->stat = NULL; |
| 13168 | entry->lstat = NULL; |
| 13169 | entry->got_file_index = 0; |
| 13170 | |
| 13171 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 13172 | if (!entry->name) |
| 13173 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13174 | if (path->narrow) { |
| 13175 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 13176 | if (!entry->name) |
| 13177 | goto error; |
| 13178 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13179 | |
| 13180 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 13181 | if (!joined_path) |
| 13182 | goto error; |
| 13183 | |
| 13184 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 13185 | PyMem_Free(joined_path); |
| 13186 | if (!entry->path) |
| 13187 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13188 | if (path->narrow) { |
| 13189 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 13190 | if (!entry->path) |
| 13191 | goto error; |
| 13192 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13193 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13194 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13195 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 13196 | |
| 13197 | return (PyObject *)entry; |
| 13198 | |
| 13199 | error: |
| 13200 | Py_DECREF(entry); |
| 13201 | return NULL; |
| 13202 | } |
| 13203 | |
| 13204 | #else /* POSIX */ |
| 13205 | |
| 13206 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 13207 | 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] | 13208 | { |
| 13209 | Py_ssize_t path_len; |
| 13210 | Py_ssize_t size; |
| 13211 | char *result; |
| 13212 | |
| 13213 | if (!path_narrow) { /* Default arg: "." */ |
| 13214 | path_narrow = "."; |
| 13215 | path_len = 1; |
| 13216 | } |
| 13217 | else { |
| 13218 | path_len = strlen(path_narrow); |
| 13219 | } |
| 13220 | |
| 13221 | if (filename_len == -1) |
| 13222 | filename_len = strlen(filename); |
| 13223 | |
| 13224 | /* The +1's are for the path separator and the NUL */ |
| 13225 | size = path_len + 1 + filename_len + 1; |
| 13226 | result = PyMem_New(char, size); |
| 13227 | if (!result) { |
| 13228 | PyErr_NoMemory(); |
| 13229 | return NULL; |
| 13230 | } |
| 13231 | strcpy(result, path_narrow); |
| 13232 | if (path_len > 0 && result[path_len - 1] != '/') |
| 13233 | result[path_len++] = '/'; |
| 13234 | strcpy(result + path_len, filename); |
| 13235 | return result; |
| 13236 | } |
| 13237 | |
| 13238 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13239 | DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name, |
| 13240 | Py_ssize_t name_len, ino_t d_ino |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13241 | #ifdef HAVE_DIRENT_D_TYPE |
| 13242 | , unsigned char d_type |
| 13243 | #endif |
| 13244 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13245 | { |
| 13246 | DirEntry *entry; |
| 13247 | char *joined_path; |
| 13248 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13249 | PyObject *DirEntryType = get_posix_state(module)->DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13250 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13251 | if (!entry) |
| 13252 | return NULL; |
| 13253 | entry->name = NULL; |
| 13254 | entry->path = NULL; |
| 13255 | entry->stat = NULL; |
| 13256 | entry->lstat = NULL; |
| 13257 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13258 | if (path->fd != -1) { |
| 13259 | entry->dir_fd = path->fd; |
| 13260 | joined_path = NULL; |
| 13261 | } |
| 13262 | else { |
| 13263 | entry->dir_fd = DEFAULT_DIR_FD; |
| 13264 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 13265 | if (!joined_path) |
| 13266 | goto error; |
| 13267 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13268 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 13269 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13270 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13271 | if (joined_path) |
| 13272 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13273 | } |
| 13274 | else { |
| 13275 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13276 | if (joined_path) |
| 13277 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13278 | } |
| 13279 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13280 | if (!entry->name) |
| 13281 | goto error; |
| 13282 | |
| 13283 | if (path->fd != -1) { |
| 13284 | entry->path = entry->name; |
| 13285 | Py_INCREF(entry->path); |
| 13286 | } |
| 13287 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13288 | goto error; |
| 13289 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13290 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13291 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13292 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13293 | entry->d_ino = d_ino; |
| 13294 | |
| 13295 | return (PyObject *)entry; |
| 13296 | |
| 13297 | error: |
| 13298 | Py_XDECREF(entry); |
| 13299 | return NULL; |
| 13300 | } |
| 13301 | |
| 13302 | #endif |
| 13303 | |
| 13304 | |
| 13305 | typedef struct { |
| 13306 | PyObject_HEAD |
| 13307 | path_t path; |
| 13308 | #ifdef MS_WINDOWS |
| 13309 | HANDLE handle; |
| 13310 | WIN32_FIND_DATAW file_data; |
| 13311 | int first_time; |
| 13312 | #else /* POSIX */ |
| 13313 | DIR *dirp; |
| 13314 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13315 | #ifdef HAVE_FDOPENDIR |
| 13316 | int fd; |
| 13317 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13318 | } ScandirIterator; |
| 13319 | |
| 13320 | #ifdef MS_WINDOWS |
| 13321 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13322 | static int |
| 13323 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13324 | { |
| 13325 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 13326 | } |
| 13327 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13328 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13329 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13330 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13331 | HANDLE handle = iterator->handle; |
| 13332 | |
| 13333 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13334 | return; |
| 13335 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13336 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13337 | Py_BEGIN_ALLOW_THREADS |
| 13338 | FindClose(handle); |
| 13339 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13340 | } |
| 13341 | |
| 13342 | static PyObject * |
| 13343 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13344 | { |
| 13345 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 13346 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13347 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13348 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13349 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13350 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13351 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13352 | |
| 13353 | while (1) { |
| 13354 | if (!iterator->first_time) { |
| 13355 | Py_BEGIN_ALLOW_THREADS |
| 13356 | success = FindNextFileW(iterator->handle, file_data); |
| 13357 | Py_END_ALLOW_THREADS |
| 13358 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13359 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13360 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13361 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13362 | break; |
| 13363 | } |
| 13364 | } |
| 13365 | iterator->first_time = 0; |
| 13366 | |
| 13367 | /* Skip over . and .. */ |
| 13368 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13369 | wcscmp(file_data->cFileName, L"..") != 0) |
| 13370 | { |
| 13371 | PyObject *module = PyType_GetModule(Py_TYPE(iterator)); |
| 13372 | entry = DirEntry_from_find_data(module, &iterator->path, file_data); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13373 | if (!entry) |
| 13374 | break; |
| 13375 | return entry; |
| 13376 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13377 | |
| 13378 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13379 | } |
| 13380 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13381 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13382 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13383 | return NULL; |
| 13384 | } |
| 13385 | |
| 13386 | #else /* POSIX */ |
| 13387 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13388 | static int |
| 13389 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13390 | { |
| 13391 | return !iterator->dirp; |
| 13392 | } |
| 13393 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13394 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13395 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13396 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13397 | DIR *dirp = iterator->dirp; |
| 13398 | |
| 13399 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13400 | return; |
| 13401 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13402 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13403 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13404 | #ifdef HAVE_FDOPENDIR |
| 13405 | if (iterator->path.fd != -1) |
| 13406 | rewinddir(dirp); |
| 13407 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13408 | closedir(dirp); |
| 13409 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13410 | return; |
| 13411 | } |
| 13412 | |
| 13413 | static PyObject * |
| 13414 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13415 | { |
| 13416 | struct dirent *direntp; |
| 13417 | Py_ssize_t name_len; |
| 13418 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13419 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13420 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13421 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13422 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13423 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13424 | |
| 13425 | while (1) { |
| 13426 | errno = 0; |
| 13427 | Py_BEGIN_ALLOW_THREADS |
| 13428 | direntp = readdir(iterator->dirp); |
| 13429 | Py_END_ALLOW_THREADS |
| 13430 | |
| 13431 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13432 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13433 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13434 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13435 | break; |
| 13436 | } |
| 13437 | |
| 13438 | /* Skip over . and .. */ |
| 13439 | name_len = NAMLEN(direntp); |
| 13440 | is_dot = direntp->d_name[0] == '.' && |
| 13441 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 13442 | if (!is_dot) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13443 | PyObject *module = PyType_GetModule(Py_TYPE(iterator)); |
| 13444 | entry = DirEntry_from_posix_info(module, |
| 13445 | &iterator->path, direntp->d_name, |
| 13446 | name_len, direntp->d_ino |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13447 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13448 | , direntp->d_type |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13449 | #endif |
| 13450 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13451 | if (!entry) |
| 13452 | break; |
| 13453 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13454 | } |
| 13455 | |
| 13456 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13457 | } |
| 13458 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13459 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13460 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13461 | return NULL; |
| 13462 | } |
| 13463 | |
| 13464 | #endif |
| 13465 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13466 | static PyObject * |
| 13467 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 13468 | { |
| 13469 | ScandirIterator_closedir(self); |
| 13470 | Py_RETURN_NONE; |
| 13471 | } |
| 13472 | |
| 13473 | static PyObject * |
| 13474 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 13475 | { |
| 13476 | Py_INCREF(self); |
| 13477 | return self; |
| 13478 | } |
| 13479 | |
| 13480 | static PyObject * |
| 13481 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 13482 | { |
| 13483 | ScandirIterator_closedir(self); |
| 13484 | Py_RETURN_NONE; |
| 13485 | } |
| 13486 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13487 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13488 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 13489 | { |
| 13490 | PyObject *error_type, *error_value, *error_traceback; |
| 13491 | |
| 13492 | /* Save the current exception, if any. */ |
| 13493 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 13494 | |
| 13495 | if (!ScandirIterator_is_closed(iterator)) { |
| 13496 | ScandirIterator_closedir(iterator); |
| 13497 | |
| 13498 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 13499 | "unclosed scandir iterator %R", iterator)) { |
| 13500 | /* Spurious errors can appear at shutdown */ |
| 13501 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 13502 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 13503 | } |
| 13504 | } |
| 13505 | } |
| 13506 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13507 | path_cleanup(&iterator->path); |
| 13508 | |
| 13509 | /* Restore the saved exception. */ |
| 13510 | PyErr_Restore(error_type, error_value, error_traceback); |
| 13511 | } |
| 13512 | |
| 13513 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13514 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 13515 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13516 | PyTypeObject *tp = Py_TYPE(iterator); |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13517 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 13518 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13519 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13520 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 13521 | free_func(iterator); |
| 13522 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13523 | } |
| 13524 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13525 | static PyMethodDef ScandirIterator_methods[] = { |
| 13526 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 13527 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 13528 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 13529 | {NULL} |
| 13530 | }; |
| 13531 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13532 | static PyType_Slot ScandirIteratorType_slots[] = { |
| 13533 | {Py_tp_new, _disabled_new}, |
| 13534 | {Py_tp_dealloc, ScandirIterator_dealloc}, |
| 13535 | {Py_tp_finalize, ScandirIterator_finalize}, |
| 13536 | {Py_tp_iter, PyObject_SelfIter}, |
| 13537 | {Py_tp_iternext, ScandirIterator_iternext}, |
| 13538 | {Py_tp_methods, ScandirIterator_methods}, |
| 13539 | {0, 0}, |
| 13540 | }; |
| 13541 | |
| 13542 | static PyType_Spec ScandirIteratorType_spec = { |
| 13543 | MODNAME ".ScandirIterator", |
| 13544 | sizeof(ScandirIterator), |
| 13545 | 0, |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13546 | // bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since |
| 13547 | // PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance. |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13548 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 13549 | ScandirIteratorType_slots |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13550 | }; |
| 13551 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13552 | /*[clinic input] |
| 13553 | os.scandir |
| 13554 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13555 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13556 | |
| 13557 | Return an iterator of DirEntry objects for given path. |
| 13558 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13559 | 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] | 13560 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 13561 | all other circumstances they will be str. |
| 13562 | |
| 13563 | If path is None, uses the path='.'. |
| 13564 | [clinic start generated code]*/ |
| 13565 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13566 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13567 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13568 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13569 | { |
| 13570 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13571 | #ifdef MS_WINDOWS |
| 13572 | wchar_t *path_strW; |
| 13573 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13574 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13575 | #ifdef HAVE_FDOPENDIR |
| 13576 | int fd = -1; |
| 13577 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13578 | #endif |
| 13579 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 13580 | if (PySys_Audit("os.scandir", "O", |
| 13581 | path->object ? path->object : Py_None) < 0) { |
| 13582 | return NULL; |
| 13583 | } |
| 13584 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 13585 | PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13586 | iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13587 | if (!iterator) |
| 13588 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13589 | |
| 13590 | #ifdef MS_WINDOWS |
| 13591 | iterator->handle = INVALID_HANDLE_VALUE; |
| 13592 | #else |
| 13593 | iterator->dirp = NULL; |
| 13594 | #endif |
| 13595 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13596 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 13597 | /* Move the ownership to iterator->path */ |
| 13598 | path->object = NULL; |
| 13599 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13600 | |
| 13601 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13602 | iterator->first_time = 1; |
| 13603 | |
| 13604 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 13605 | if (!path_strW) |
| 13606 | goto error; |
| 13607 | |
| 13608 | Py_BEGIN_ALLOW_THREADS |
| 13609 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 13610 | Py_END_ALLOW_THREADS |
| 13611 | |
| 13612 | PyMem_Free(path_strW); |
| 13613 | |
| 13614 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 13615 | path_error(&iterator->path); |
| 13616 | goto error; |
| 13617 | } |
| 13618 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13619 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13620 | #ifdef HAVE_FDOPENDIR |
| 13621 | if (path->fd != -1) { |
| 13622 | /* closedir() closes the FD, so we duplicate it */ |
| 13623 | fd = _Py_dup(path->fd); |
| 13624 | if (fd == -1) |
| 13625 | goto error; |
| 13626 | |
| 13627 | Py_BEGIN_ALLOW_THREADS |
| 13628 | iterator->dirp = fdopendir(fd); |
| 13629 | Py_END_ALLOW_THREADS |
| 13630 | } |
| 13631 | else |
| 13632 | #endif |
| 13633 | { |
| 13634 | if (iterator->path.narrow) |
| 13635 | path_str = iterator->path.narrow; |
| 13636 | else |
| 13637 | path_str = "."; |
| 13638 | |
| 13639 | Py_BEGIN_ALLOW_THREADS |
| 13640 | iterator->dirp = opendir(path_str); |
| 13641 | Py_END_ALLOW_THREADS |
| 13642 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13643 | |
| 13644 | if (!iterator->dirp) { |
| 13645 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13646 | #ifdef HAVE_FDOPENDIR |
| 13647 | if (fd != -1) { |
| 13648 | Py_BEGIN_ALLOW_THREADS |
| 13649 | close(fd); |
| 13650 | Py_END_ALLOW_THREADS |
| 13651 | } |
| 13652 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13653 | goto error; |
| 13654 | } |
| 13655 | #endif |
| 13656 | |
| 13657 | return (PyObject *)iterator; |
| 13658 | |
| 13659 | error: |
| 13660 | Py_DECREF(iterator); |
| 13661 | return NULL; |
| 13662 | } |
| 13663 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13664 | /* |
| 13665 | Return the file system path representation of the object. |
| 13666 | |
| 13667 | If the object is str or bytes, then allow it to pass through with |
| 13668 | an incremented refcount. If the object defines __fspath__(), then |
| 13669 | return the result of that method. All other types raise a TypeError. |
| 13670 | */ |
| 13671 | PyObject * |
| 13672 | PyOS_FSPath(PyObject *path) |
| 13673 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13674 | /* For error message reasons, this function is manually inlined in |
| 13675 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13676 | PyObject *func = NULL; |
| 13677 | PyObject *path_repr = NULL; |
| 13678 | |
| 13679 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 13680 | Py_INCREF(path); |
| 13681 | return path; |
| 13682 | } |
| 13683 | |
| 13684 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 13685 | if (NULL == func) { |
| 13686 | return PyErr_Format(PyExc_TypeError, |
| 13687 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13688 | "not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13689 | _PyType_Name(Py_TYPE(path))); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13690 | } |
| 13691 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13692 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13693 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13694 | if (NULL == path_repr) { |
| 13695 | return NULL; |
| 13696 | } |
| 13697 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13698 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 13699 | PyErr_Format(PyExc_TypeError, |
| 13700 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13701 | "not %.200s", _PyType_Name(Py_TYPE(path)), |
| 13702 | _PyType_Name(Py_TYPE(path_repr))); |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13703 | Py_DECREF(path_repr); |
| 13704 | return NULL; |
| 13705 | } |
| 13706 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13707 | return path_repr; |
| 13708 | } |
| 13709 | |
| 13710 | /*[clinic input] |
| 13711 | os.fspath |
| 13712 | |
| 13713 | path: object |
| 13714 | |
| 13715 | Return the file system path representation of the object. |
| 13716 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13717 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 13718 | object defines __fspath__(), then return the result of that method. All other |
| 13719 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13720 | [clinic start generated code]*/ |
| 13721 | |
| 13722 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13723 | os_fspath_impl(PyObject *module, PyObject *path) |
| 13724 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13725 | { |
| 13726 | return PyOS_FSPath(path); |
| 13727 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13728 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13729 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13730 | /*[clinic input] |
| 13731 | os.getrandom |
| 13732 | |
| 13733 | size: Py_ssize_t |
| 13734 | flags: int=0 |
| 13735 | |
| 13736 | Obtain a series of random bytes. |
| 13737 | [clinic start generated code]*/ |
| 13738 | |
| 13739 | static PyObject * |
| 13740 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 13741 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 13742 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13743 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13744 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13745 | |
| 13746 | if (size < 0) { |
| 13747 | errno = EINVAL; |
| 13748 | return posix_error(); |
| 13749 | } |
| 13750 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13751 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13752 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13753 | PyErr_NoMemory(); |
| 13754 | return NULL; |
| 13755 | } |
| 13756 | |
| 13757 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13758 | n = syscall(SYS_getrandom, |
| 13759 | PyBytes_AS_STRING(bytes), |
| 13760 | PyBytes_GET_SIZE(bytes), |
| 13761 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13762 | if (n < 0 && errno == EINTR) { |
| 13763 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13764 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13765 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13766 | |
| 13767 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13768 | continue; |
| 13769 | } |
| 13770 | break; |
| 13771 | } |
| 13772 | |
| 13773 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13774 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13775 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13776 | } |
| 13777 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13778 | if (n != size) { |
| 13779 | _PyBytes_Resize(&bytes, n); |
| 13780 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13781 | |
| 13782 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13783 | |
| 13784 | error: |
| 13785 | Py_DECREF(bytes); |
| 13786 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13787 | } |
| 13788 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13789 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13790 | #ifdef MS_WINDOWS |
| 13791 | /* bpo-36085: Helper functions for managing DLL search directories |
| 13792 | * on win32 |
| 13793 | */ |
| 13794 | |
| 13795 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); |
| 13796 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); |
| 13797 | |
| 13798 | /*[clinic input] |
| 13799 | os._add_dll_directory |
| 13800 | |
| 13801 | path: path_t |
| 13802 | |
| 13803 | Add a path to the DLL search path. |
| 13804 | |
| 13805 | This search path is used when resolving dependencies for imported |
| 13806 | extension modules (the module itself is resolved through sys.path), |
| 13807 | and also by ctypes. |
| 13808 | |
| 13809 | Returns an opaque value that may be passed to os.remove_dll_directory |
| 13810 | to remove this directory from the search path. |
| 13811 | [clinic start generated code]*/ |
| 13812 | |
| 13813 | static PyObject * |
| 13814 | os__add_dll_directory_impl(PyObject *module, path_t *path) |
| 13815 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ |
| 13816 | { |
| 13817 | HMODULE hKernel32; |
| 13818 | PAddDllDirectory AddDllDirectory; |
| 13819 | DLL_DIRECTORY_COOKIE cookie = 0; |
| 13820 | DWORD err = 0; |
| 13821 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 13822 | if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) { |
| 13823 | return NULL; |
| 13824 | } |
| 13825 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13826 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13827 | infrequent operation, just do it each time. Kernel32 is always |
| 13828 | loaded. */ |
| 13829 | Py_BEGIN_ALLOW_THREADS |
| 13830 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13831 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( |
| 13832 | hKernel32, "AddDllDirectory")) || |
| 13833 | !(cookie = (*AddDllDirectory)(path->wide))) { |
| 13834 | err = GetLastError(); |
| 13835 | } |
| 13836 | Py_END_ALLOW_THREADS |
| 13837 | |
| 13838 | if (err) { |
| 13839 | return win32_error_object_err("add_dll_directory", |
| 13840 | path->object, err); |
| 13841 | } |
| 13842 | |
| 13843 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); |
| 13844 | } |
| 13845 | |
| 13846 | /*[clinic input] |
| 13847 | os._remove_dll_directory |
| 13848 | |
| 13849 | cookie: object |
| 13850 | |
| 13851 | Removes a path from the DLL search path. |
| 13852 | |
| 13853 | The parameter is an opaque value that was returned from |
| 13854 | os.add_dll_directory. You can only remove directories that you added |
| 13855 | yourself. |
| 13856 | [clinic start generated code]*/ |
| 13857 | |
| 13858 | static PyObject * |
| 13859 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) |
| 13860 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ |
| 13861 | { |
| 13862 | HMODULE hKernel32; |
| 13863 | PRemoveDllDirectory RemoveDllDirectory; |
| 13864 | DLL_DIRECTORY_COOKIE cookieValue; |
| 13865 | DWORD err = 0; |
| 13866 | |
| 13867 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { |
| 13868 | PyErr_SetString(PyExc_TypeError, |
| 13869 | "Provided cookie was not returned from os.add_dll_directory"); |
| 13870 | return NULL; |
| 13871 | } |
| 13872 | |
| 13873 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( |
| 13874 | cookie, "DLL directory cookie"); |
| 13875 | |
| 13876 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13877 | infrequent operation, just do it each time. Kernel32 is always |
| 13878 | loaded. */ |
| 13879 | Py_BEGIN_ALLOW_THREADS |
| 13880 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13881 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( |
| 13882 | hKernel32, "RemoveDllDirectory")) || |
| 13883 | !(*RemoveDllDirectory)(cookieValue)) { |
| 13884 | err = GetLastError(); |
| 13885 | } |
| 13886 | Py_END_ALLOW_THREADS |
| 13887 | |
| 13888 | if (err) { |
| 13889 | return win32_error_object_err("remove_dll_directory", |
| 13890 | NULL, err); |
| 13891 | } |
| 13892 | |
| 13893 | if (PyCapsule_SetName(cookie, NULL)) { |
| 13894 | return NULL; |
| 13895 | } |
| 13896 | |
| 13897 | Py_RETURN_NONE; |
| 13898 | } |
| 13899 | |
| 13900 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13901 | |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13902 | |
| 13903 | /* Only check if WIFEXITED is available: expect that it comes |
| 13904 | with WEXITSTATUS, WIFSIGNALED, etc. |
| 13905 | |
| 13906 | os.waitstatus_to_exitcode() is implemented in C and not in Python, so |
| 13907 | subprocess can safely call it during late Python finalization without |
| 13908 | risking that used os attributes were set to None by _PyImport_Cleanup(). */ |
| 13909 | #if defined(WIFEXITED) || defined(MS_WINDOWS) |
| 13910 | /*[clinic input] |
| 13911 | os.waitstatus_to_exitcode |
| 13912 | |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13913 | status as status_obj: object |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13914 | |
| 13915 | Convert a wait status to an exit code. |
| 13916 | |
| 13917 | On Unix: |
| 13918 | |
| 13919 | * If WIFEXITED(status) is true, return WEXITSTATUS(status). |
| 13920 | * If WIFSIGNALED(status) is true, return -WTERMSIG(status). |
| 13921 | * Otherwise, raise a ValueError. |
| 13922 | |
| 13923 | On Windows, return status shifted right by 8 bits. |
| 13924 | |
| 13925 | On Unix, if the process is being traced or if waitpid() was called with |
| 13926 | WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true. |
| 13927 | This function must not be called if WIFSTOPPED(status) is true. |
| 13928 | [clinic start generated code]*/ |
| 13929 | |
| 13930 | static PyObject * |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13931 | os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj) |
| 13932 | /*[clinic end generated code: output=db50b1b0ba3c7153 input=7fe2d7fdaea3db42]*/ |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13933 | { |
| 13934 | #ifndef MS_WINDOWS |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13935 | int status = _PyLong_AsInt(status_obj); |
| 13936 | if (status == -1 && PyErr_Occurred()) { |
| 13937 | return NULL; |
| 13938 | } |
| 13939 | |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13940 | WAIT_TYPE wait_status; |
| 13941 | WAIT_STATUS_INT(wait_status) = status; |
| 13942 | int exitcode; |
| 13943 | if (WIFEXITED(wait_status)) { |
| 13944 | exitcode = WEXITSTATUS(wait_status); |
| 13945 | /* Sanity check to provide warranty on the function behavior. |
| 13946 | It should not occur in practice */ |
| 13947 | if (exitcode < 0) { |
| 13948 | PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode); |
| 13949 | return NULL; |
| 13950 | } |
| 13951 | } |
| 13952 | else if (WIFSIGNALED(wait_status)) { |
| 13953 | int signum = WTERMSIG(wait_status); |
| 13954 | /* Sanity check to provide warranty on the function behavior. |
| 13955 | It should not occurs in practice */ |
| 13956 | if (signum <= 0) { |
| 13957 | PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum); |
| 13958 | return NULL; |
| 13959 | } |
| 13960 | exitcode = -signum; |
| 13961 | } else if (WIFSTOPPED(wait_status)) { |
| 13962 | /* Status only received if the process is being traced |
| 13963 | or if waitpid() was called with WUNTRACED option. */ |
| 13964 | int signum = WSTOPSIG(wait_status); |
| 13965 | PyErr_Format(PyExc_ValueError, |
| 13966 | "process stopped by delivery of signal %i", |
| 13967 | signum); |
| 13968 | return NULL; |
| 13969 | } |
| 13970 | else { |
| 13971 | PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status); |
| 13972 | return NULL; |
| 13973 | } |
| 13974 | return PyLong_FromLong(exitcode); |
| 13975 | #else |
| 13976 | /* Windows implementation: see os.waitpid() implementation |
| 13977 | which uses _cwait(). */ |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13978 | unsigned long long status = PyLong_AsUnsignedLongLong(status_obj); |
| 13979 | if (status == (unsigned long long)-1 && PyErr_Occurred()) { |
| 13980 | return NULL; |
| 13981 | } |
| 13982 | |
| 13983 | unsigned long long exitcode = (status >> 8); |
| 13984 | /* ExitProcess() accepts an UINT type: |
| 13985 | reject exit code which doesn't fit in an UINT */ |
| 13986 | if (exitcode > UINT_MAX) { |
| 13987 | PyErr_Format(PyExc_ValueError, "invalid exit code: %llu", exitcode); |
| 13988 | return NULL; |
| 13989 | } |
| 13990 | return PyLong_FromUnsignedLong((unsigned long)exitcode); |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13991 | #endif |
| 13992 | } |
| 13993 | #endif |
| 13994 | |
| 13995 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13996 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13997 | |
| 13998 | OS_STAT_METHODDEF |
| 13999 | OS_ACCESS_METHODDEF |
| 14000 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14001 | OS_CHDIR_METHODDEF |
| 14002 | OS_CHFLAGS_METHODDEF |
| 14003 | OS_CHMOD_METHODDEF |
| 14004 | OS_FCHMOD_METHODDEF |
| 14005 | OS_LCHMOD_METHODDEF |
| 14006 | OS_CHOWN_METHODDEF |
| 14007 | OS_FCHOWN_METHODDEF |
| 14008 | OS_LCHOWN_METHODDEF |
| 14009 | OS_LCHFLAGS_METHODDEF |
| 14010 | OS_CHROOT_METHODDEF |
| 14011 | OS_CTERMID_METHODDEF |
| 14012 | OS_GETCWD_METHODDEF |
| 14013 | OS_GETCWDB_METHODDEF |
| 14014 | OS_LINK_METHODDEF |
| 14015 | OS_LISTDIR_METHODDEF |
| 14016 | OS_LSTAT_METHODDEF |
| 14017 | OS_MKDIR_METHODDEF |
| 14018 | OS_NICE_METHODDEF |
| 14019 | OS_GETPRIORITY_METHODDEF |
| 14020 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14021 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 14022 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 14023 | OS_READLINK_METHODDEF |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 14024 | OS_COPY_FILE_RANGE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14025 | OS_RENAME_METHODDEF |
| 14026 | OS_REPLACE_METHODDEF |
| 14027 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14028 | OS_SYMLINK_METHODDEF |
| 14029 | OS_SYSTEM_METHODDEF |
| 14030 | OS_UMASK_METHODDEF |
| 14031 | OS_UNAME_METHODDEF |
| 14032 | OS_UNLINK_METHODDEF |
| 14033 | OS_REMOVE_METHODDEF |
| 14034 | OS_UTIME_METHODDEF |
| 14035 | OS_TIMES_METHODDEF |
| 14036 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14037 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14038 | OS_EXECV_METHODDEF |
| 14039 | OS_EXECVE_METHODDEF |
| 14040 | OS_SPAWNV_METHODDEF |
| 14041 | OS_SPAWNVE_METHODDEF |
| 14042 | OS_FORK1_METHODDEF |
| 14043 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 14044 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14045 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 14046 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 14047 | OS_SCHED_GETPARAM_METHODDEF |
| 14048 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 14049 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 14050 | OS_SCHED_SETPARAM_METHODDEF |
| 14051 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 14052 | OS_SCHED_YIELD_METHODDEF |
| 14053 | OS_SCHED_SETAFFINITY_METHODDEF |
| 14054 | OS_SCHED_GETAFFINITY_METHODDEF |
| 14055 | OS_OPENPTY_METHODDEF |
| 14056 | OS_FORKPTY_METHODDEF |
| 14057 | OS_GETEGID_METHODDEF |
| 14058 | OS_GETEUID_METHODDEF |
| 14059 | OS_GETGID_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14060 | OS_GETGROUPLIST_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14061 | OS_GETGROUPS_METHODDEF |
| 14062 | OS_GETPID_METHODDEF |
| 14063 | OS_GETPGRP_METHODDEF |
| 14064 | OS_GETPPID_METHODDEF |
| 14065 | OS_GETUID_METHODDEF |
| 14066 | OS_GETLOGIN_METHODDEF |
| 14067 | OS_KILL_METHODDEF |
| 14068 | OS_KILLPG_METHODDEF |
| 14069 | OS_PLOCK_METHODDEF |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 14070 | OS_STARTFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14071 | OS_SETUID_METHODDEF |
| 14072 | OS_SETEUID_METHODDEF |
| 14073 | OS_SETREUID_METHODDEF |
| 14074 | OS_SETGID_METHODDEF |
| 14075 | OS_SETEGID_METHODDEF |
| 14076 | OS_SETREGID_METHODDEF |
| 14077 | OS_SETGROUPS_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14078 | OS_INITGROUPS_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14079 | OS_GETPGID_METHODDEF |
| 14080 | OS_SETPGRP_METHODDEF |
| 14081 | OS_WAIT_METHODDEF |
| 14082 | OS_WAIT3_METHODDEF |
| 14083 | OS_WAIT4_METHODDEF |
| 14084 | OS_WAITID_METHODDEF |
| 14085 | OS_WAITPID_METHODDEF |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 14086 | OS_PIDFD_OPEN_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14087 | OS_GETSID_METHODDEF |
| 14088 | OS_SETSID_METHODDEF |
| 14089 | OS_SETPGID_METHODDEF |
| 14090 | OS_TCGETPGRP_METHODDEF |
| 14091 | OS_TCSETPGRP_METHODDEF |
| 14092 | OS_OPEN_METHODDEF |
| 14093 | OS_CLOSE_METHODDEF |
| 14094 | OS_CLOSERANGE_METHODDEF |
| 14095 | OS_DEVICE_ENCODING_METHODDEF |
| 14096 | OS_DUP_METHODDEF |
| 14097 | OS_DUP2_METHODDEF |
| 14098 | OS_LOCKF_METHODDEF |
| 14099 | OS_LSEEK_METHODDEF |
| 14100 | OS_READ_METHODDEF |
| 14101 | OS_READV_METHODDEF |
| 14102 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14103 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14104 | OS_WRITE_METHODDEF |
| 14105 | OS_WRITEV_METHODDEF |
| 14106 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14107 | OS_PWRITEV_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14108 | OS_SENDFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14109 | OS_FSTAT_METHODDEF |
| 14110 | OS_ISATTY_METHODDEF |
| 14111 | OS_PIPE_METHODDEF |
| 14112 | OS_PIPE2_METHODDEF |
| 14113 | OS_MKFIFO_METHODDEF |
| 14114 | OS_MKNOD_METHODDEF |
| 14115 | OS_MAJOR_METHODDEF |
| 14116 | OS_MINOR_METHODDEF |
| 14117 | OS_MAKEDEV_METHODDEF |
| 14118 | OS_FTRUNCATE_METHODDEF |
| 14119 | OS_TRUNCATE_METHODDEF |
| 14120 | OS_POSIX_FALLOCATE_METHODDEF |
| 14121 | OS_POSIX_FADVISE_METHODDEF |
| 14122 | OS_PUTENV_METHODDEF |
| 14123 | OS_UNSETENV_METHODDEF |
| 14124 | OS_STRERROR_METHODDEF |
| 14125 | OS_FCHDIR_METHODDEF |
| 14126 | OS_FSYNC_METHODDEF |
| 14127 | OS_SYNC_METHODDEF |
| 14128 | OS_FDATASYNC_METHODDEF |
| 14129 | OS_WCOREDUMP_METHODDEF |
| 14130 | OS_WIFCONTINUED_METHODDEF |
| 14131 | OS_WIFSTOPPED_METHODDEF |
| 14132 | OS_WIFSIGNALED_METHODDEF |
| 14133 | OS_WIFEXITED_METHODDEF |
| 14134 | OS_WEXITSTATUS_METHODDEF |
| 14135 | OS_WTERMSIG_METHODDEF |
| 14136 | OS_WSTOPSIG_METHODDEF |
| 14137 | OS_FSTATVFS_METHODDEF |
| 14138 | OS_STATVFS_METHODDEF |
| 14139 | OS_CONFSTR_METHODDEF |
| 14140 | OS_SYSCONF_METHODDEF |
| 14141 | OS_FPATHCONF_METHODDEF |
| 14142 | OS_PATHCONF_METHODDEF |
| 14143 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 14144 | OS__GETFULLPATHNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14145 | OS__GETDISKUSAGE_METHODDEF |
| 14146 | OS__GETFINALPATHNAME_METHODDEF |
| 14147 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 14148 | OS_GETLOADAVG_METHODDEF |
| 14149 | OS_URANDOM_METHODDEF |
| 14150 | OS_SETRESUID_METHODDEF |
| 14151 | OS_SETRESGID_METHODDEF |
| 14152 | OS_GETRESUID_METHODDEF |
| 14153 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 14154 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14155 | OS_GETXATTR_METHODDEF |
| 14156 | OS_SETXATTR_METHODDEF |
| 14157 | OS_REMOVEXATTR_METHODDEF |
| 14158 | OS_LISTXATTR_METHODDEF |
| 14159 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14160 | OS_GET_TERMINAL_SIZE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14161 | OS_CPU_COUNT_METHODDEF |
| 14162 | OS_GET_INHERITABLE_METHODDEF |
| 14163 | OS_SET_INHERITABLE_METHODDEF |
| 14164 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 14165 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 14166 | OS_GET_BLOCKING_METHODDEF |
| 14167 | OS_SET_BLOCKING_METHODDEF |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 14168 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14169 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14170 | OS_GETRANDOM_METHODDEF |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14171 | OS_MEMFD_CREATE_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14172 | OS__ADD_DLL_DIRECTORY_METHODDEF |
| 14173 | OS__REMOVE_DLL_DIRECTORY_METHODDEF |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14174 | OS_WAITSTATUS_TO_EXITCODE_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14175 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14176 | }; |
| 14177 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14178 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14179 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14180 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14181 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14182 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14183 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14184 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14185 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14186 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14187 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14188 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14189 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14190 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14191 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14192 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14193 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14194 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14195 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14196 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14197 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14198 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14199 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14200 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14201 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14202 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14203 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14204 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14205 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14206 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14207 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14208 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14209 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14210 | #endif |
| 14211 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14212 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14213 | #endif |
| 14214 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14215 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14216 | #endif |
| 14217 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14218 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14219 | #endif |
| 14220 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14221 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14222 | #endif |
| 14223 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14224 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14225 | #endif |
| 14226 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14227 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14228 | #endif |
| 14229 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14230 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14231 | #endif |
| 14232 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14233 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14234 | #endif |
| 14235 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14236 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14237 | #endif |
| 14238 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14239 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14240 | #endif |
| 14241 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14242 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14243 | #endif |
| 14244 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14245 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14246 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14247 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14248 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14249 | #endif |
| 14250 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14251 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14252 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14253 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14254 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14255 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14256 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14257 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14258 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14259 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14260 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14261 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14262 | #endif |
| 14263 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14264 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14265 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14266 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14267 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14268 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14269 | #endif |
| 14270 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14271 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14272 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14273 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14274 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14275 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14276 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14277 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14278 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 14279 | #ifdef O_TMPFILE |
| 14280 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 14281 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14282 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14283 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14284 | #endif |
| 14285 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14286 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14287 | #endif |
| 14288 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14289 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14290 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14291 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14292 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14293 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14294 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14295 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14296 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14297 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14298 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14299 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14300 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14301 | #endif |
| 14302 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14303 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14304 | #endif |
| 14305 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14306 | /* MS Windows */ |
| 14307 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14308 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14309 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14310 | #endif |
| 14311 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14312 | /* Optimize for short life (keep in memory). */ |
| 14313 | /* 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] | 14314 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14315 | #endif |
| 14316 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14317 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14318 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14319 | #endif |
| 14320 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14321 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14322 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14323 | #endif |
| 14324 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14325 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14326 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14327 | #endif |
| 14328 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14329 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14330 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14331 | /* Send a SIGIO signal whenever input or output |
| 14332 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14333 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14334 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14335 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14336 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14337 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14338 | #endif |
| 14339 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14340 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14341 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14342 | #endif |
| 14343 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14344 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14345 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14346 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14347 | #ifdef O_NOLINKS |
| 14348 | /* 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] | 14349 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14350 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14351 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14352 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14353 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14354 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 14355 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14356 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14357 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14358 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14359 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14360 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14361 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14362 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14363 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14364 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14365 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14366 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14367 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14368 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14369 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14370 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14371 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14372 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14373 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14374 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14375 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14376 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14377 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14378 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14379 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14380 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14381 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14382 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14383 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14384 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14385 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14386 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14387 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14388 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14389 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14390 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14391 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14392 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14393 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14394 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14395 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14396 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14397 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14398 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14399 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14400 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14401 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14402 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14403 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14404 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14405 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14406 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14407 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14408 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 14409 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14410 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14411 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14412 | #endif /* ST_RDONLY */ |
| 14413 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14414 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14415 | #endif /* ST_NOSUID */ |
| 14416 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 14417 | /* GNU extensions */ |
| 14418 | #ifdef ST_NODEV |
| 14419 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 14420 | #endif /* ST_NODEV */ |
| 14421 | #ifdef ST_NOEXEC |
| 14422 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 14423 | #endif /* ST_NOEXEC */ |
| 14424 | #ifdef ST_SYNCHRONOUS |
| 14425 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 14426 | #endif /* ST_SYNCHRONOUS */ |
| 14427 | #ifdef ST_MANDLOCK |
| 14428 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 14429 | #endif /* ST_MANDLOCK */ |
| 14430 | #ifdef ST_WRITE |
| 14431 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 14432 | #endif /* ST_WRITE */ |
| 14433 | #ifdef ST_APPEND |
| 14434 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 14435 | #endif /* ST_APPEND */ |
| 14436 | #ifdef ST_NOATIME |
| 14437 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 14438 | #endif /* ST_NOATIME */ |
| 14439 | #ifdef ST_NODIRATIME |
| 14440 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 14441 | #endif /* ST_NODIRATIME */ |
| 14442 | #ifdef ST_RELATIME |
| 14443 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 14444 | #endif /* ST_RELATIME */ |
| 14445 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14446 | /* FreeBSD sendfile() constants */ |
| 14447 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14448 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14449 | #endif |
| 14450 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14451 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14452 | #endif |
| 14453 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14454 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14455 | #endif |
| 14456 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14457 | /* constants for posix_fadvise */ |
| 14458 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14459 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14460 | #endif |
| 14461 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14462 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14463 | #endif |
| 14464 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14465 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14466 | #endif |
| 14467 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14468 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14469 | #endif |
| 14470 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14471 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14472 | #endif |
| 14473 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14474 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14475 | #endif |
| 14476 | |
| 14477 | /* constants for waitid */ |
| 14478 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14479 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 14480 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 14481 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 14482 | #ifdef P_PIDFD |
| 14483 | if (PyModule_AddIntMacro(m, P_PIDFD)) return -1; |
| 14484 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14485 | #endif |
| 14486 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14487 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14488 | #endif |
| 14489 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14490 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14491 | #endif |
| 14492 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14493 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14494 | #endif |
| 14495 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14496 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14497 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14498 | #ifdef CLD_KILLED |
| 14499 | if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1; |
| 14500 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14501 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14502 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14503 | #endif |
| 14504 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14505 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14506 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14507 | #ifdef CLD_STOPPED |
| 14508 | if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1; |
| 14509 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14510 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14511 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14512 | #endif |
| 14513 | |
| 14514 | /* constants for lockf */ |
| 14515 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14516 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14517 | #endif |
| 14518 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14519 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14520 | #endif |
| 14521 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14522 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14523 | #endif |
| 14524 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14525 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14526 | #endif |
| 14527 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14528 | #ifdef RWF_DSYNC |
| 14529 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 14530 | #endif |
| 14531 | #ifdef RWF_HIPRI |
| 14532 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 14533 | #endif |
| 14534 | #ifdef RWF_SYNC |
| 14535 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 14536 | #endif |
| 14537 | #ifdef RWF_NOWAIT |
| 14538 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 14539 | #endif |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 14540 | #ifdef RWF_APPEND |
| 14541 | if (PyModule_AddIntConstant(m, "RWF_APPEND", RWF_APPEND)) return -1; |
| 14542 | #endif |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14543 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14544 | /* constants for posix_spawn */ |
| 14545 | #ifdef HAVE_POSIX_SPAWN |
| 14546 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 14547 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 14548 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 14549 | #endif |
| 14550 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14551 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14552 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 14553 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14554 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14555 | #endif |
| 14556 | #ifdef HAVE_SPAWNV |
| 14557 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14558 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 14559 | #endif |
| 14560 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14561 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14562 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14563 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14564 | #endif |
| 14565 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14566 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14567 | #endif |
| 14568 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14569 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14570 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14571 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 14572 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14573 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14574 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14575 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14576 | #endif |
| 14577 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14578 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14579 | #endif |
| 14580 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14581 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14582 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14583 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14584 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14585 | #endif |
| 14586 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14587 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14588 | #endif |
| 14589 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14590 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14591 | #endif |
| 14592 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14593 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14594 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14595 | #endif |
| 14596 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 14597 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14598 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 14599 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 14600 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 14601 | #endif |
| 14602 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14603 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14604 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14605 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14606 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14607 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14608 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14609 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14610 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14611 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14612 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14613 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14614 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14615 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14616 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14617 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14618 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14619 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14620 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14621 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14622 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14623 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 14624 | #if HAVE_DECL_RTLD_MEMBER |
| 14625 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 14626 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14627 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14628 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 14629 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 14630 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 14631 | #endif |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14632 | #ifdef HAVE_MEMFD_CREATE |
| 14633 | if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1; |
| 14634 | if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1; |
| 14635 | #ifdef MFD_HUGETLB |
| 14636 | if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14637 | #endif |
| 14638 | #ifdef MFD_HUGE_SHIFT |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14639 | if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14640 | #endif |
| 14641 | #ifdef MFD_HUGE_MASK |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14642 | if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14643 | #endif |
| 14644 | #ifdef MFD_HUGE_64KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14645 | if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14646 | #endif |
| 14647 | #ifdef MFD_HUGE_512KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14648 | if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14649 | #endif |
| 14650 | #ifdef MFD_HUGE_1MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14651 | if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14652 | #endif |
| 14653 | #ifdef MFD_HUGE_2MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14654 | if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14655 | #endif |
| 14656 | #ifdef MFD_HUGE_8MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14657 | if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14658 | #endif |
| 14659 | #ifdef MFD_HUGE_16MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14660 | if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14661 | #endif |
| 14662 | #ifdef MFD_HUGE_32MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14663 | if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14664 | #endif |
| 14665 | #ifdef MFD_HUGE_256MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14666 | if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14667 | #endif |
| 14668 | #ifdef MFD_HUGE_512MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14669 | if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14670 | #endif |
| 14671 | #ifdef MFD_HUGE_1GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14672 | if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14673 | #endif |
| 14674 | #ifdef MFD_HUGE_2GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14675 | if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14676 | #endif |
| 14677 | #ifdef MFD_HUGE_16GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14678 | if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1; |
| 14679 | #endif |
| 14680 | #endif |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14681 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14682 | #if defined(__APPLE__) |
| 14683 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 14684 | #endif |
| 14685 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14686 | #ifdef MS_WINDOWS |
| 14687 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; |
| 14688 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; |
| 14689 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; |
| 14690 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; |
| 14691 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; |
| 14692 | #endif |
| 14693 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14694 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14695 | } |
| 14696 | |
| 14697 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14698 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14699 | |
| 14700 | #ifdef HAVE_FACCESSAT |
| 14701 | "HAVE_FACCESSAT", |
| 14702 | #endif |
| 14703 | |
| 14704 | #ifdef HAVE_FCHDIR |
| 14705 | "HAVE_FCHDIR", |
| 14706 | #endif |
| 14707 | |
| 14708 | #ifdef HAVE_FCHMOD |
| 14709 | "HAVE_FCHMOD", |
| 14710 | #endif |
| 14711 | |
| 14712 | #ifdef HAVE_FCHMODAT |
| 14713 | "HAVE_FCHMODAT", |
| 14714 | #endif |
| 14715 | |
| 14716 | #ifdef HAVE_FCHOWN |
| 14717 | "HAVE_FCHOWN", |
| 14718 | #endif |
| 14719 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 14720 | #ifdef HAVE_FCHOWNAT |
| 14721 | "HAVE_FCHOWNAT", |
| 14722 | #endif |
| 14723 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14724 | #ifdef HAVE_FEXECVE |
| 14725 | "HAVE_FEXECVE", |
| 14726 | #endif |
| 14727 | |
| 14728 | #ifdef HAVE_FDOPENDIR |
| 14729 | "HAVE_FDOPENDIR", |
| 14730 | #endif |
| 14731 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14732 | #ifdef HAVE_FPATHCONF |
| 14733 | "HAVE_FPATHCONF", |
| 14734 | #endif |
| 14735 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14736 | #ifdef HAVE_FSTATAT |
| 14737 | "HAVE_FSTATAT", |
| 14738 | #endif |
| 14739 | |
| 14740 | #ifdef HAVE_FSTATVFS |
| 14741 | "HAVE_FSTATVFS", |
| 14742 | #endif |
| 14743 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 14744 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14745 | "HAVE_FTRUNCATE", |
| 14746 | #endif |
| 14747 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14748 | #ifdef HAVE_FUTIMENS |
| 14749 | "HAVE_FUTIMENS", |
| 14750 | #endif |
| 14751 | |
| 14752 | #ifdef HAVE_FUTIMES |
| 14753 | "HAVE_FUTIMES", |
| 14754 | #endif |
| 14755 | |
| 14756 | #ifdef HAVE_FUTIMESAT |
| 14757 | "HAVE_FUTIMESAT", |
| 14758 | #endif |
| 14759 | |
| 14760 | #ifdef HAVE_LINKAT |
| 14761 | "HAVE_LINKAT", |
| 14762 | #endif |
| 14763 | |
| 14764 | #ifdef HAVE_LCHFLAGS |
| 14765 | "HAVE_LCHFLAGS", |
| 14766 | #endif |
| 14767 | |
| 14768 | #ifdef HAVE_LCHMOD |
| 14769 | "HAVE_LCHMOD", |
| 14770 | #endif |
| 14771 | |
| 14772 | #ifdef HAVE_LCHOWN |
| 14773 | "HAVE_LCHOWN", |
| 14774 | #endif |
| 14775 | |
| 14776 | #ifdef HAVE_LSTAT |
| 14777 | "HAVE_LSTAT", |
| 14778 | #endif |
| 14779 | |
| 14780 | #ifdef HAVE_LUTIMES |
| 14781 | "HAVE_LUTIMES", |
| 14782 | #endif |
| 14783 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14784 | #ifdef HAVE_MEMFD_CREATE |
| 14785 | "HAVE_MEMFD_CREATE", |
| 14786 | #endif |
| 14787 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14788 | #ifdef HAVE_MKDIRAT |
| 14789 | "HAVE_MKDIRAT", |
| 14790 | #endif |
| 14791 | |
| 14792 | #ifdef HAVE_MKFIFOAT |
| 14793 | "HAVE_MKFIFOAT", |
| 14794 | #endif |
| 14795 | |
| 14796 | #ifdef HAVE_MKNODAT |
| 14797 | "HAVE_MKNODAT", |
| 14798 | #endif |
| 14799 | |
| 14800 | #ifdef HAVE_OPENAT |
| 14801 | "HAVE_OPENAT", |
| 14802 | #endif |
| 14803 | |
| 14804 | #ifdef HAVE_READLINKAT |
| 14805 | "HAVE_READLINKAT", |
| 14806 | #endif |
| 14807 | |
| 14808 | #ifdef HAVE_RENAMEAT |
| 14809 | "HAVE_RENAMEAT", |
| 14810 | #endif |
| 14811 | |
| 14812 | #ifdef HAVE_SYMLINKAT |
| 14813 | "HAVE_SYMLINKAT", |
| 14814 | #endif |
| 14815 | |
| 14816 | #ifdef HAVE_UNLINKAT |
| 14817 | "HAVE_UNLINKAT", |
| 14818 | #endif |
| 14819 | |
| 14820 | #ifdef HAVE_UTIMENSAT |
| 14821 | "HAVE_UTIMENSAT", |
| 14822 | #endif |
| 14823 | |
| 14824 | #ifdef MS_WINDOWS |
| 14825 | "MS_WINDOWS", |
| 14826 | #endif |
| 14827 | |
| 14828 | NULL |
| 14829 | }; |
| 14830 | |
| 14831 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14832 | static int |
| 14833 | posixmodule_exec(PyObject *m) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14834 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14835 | _posixstate *state = get_posix_state(m); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14836 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14837 | /* Initialize environ dictionary */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14838 | PyObject *v = convertenviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14839 | Py_XINCREF(v); |
| 14840 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14841 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14842 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14843 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14844 | if (all_ins(m)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14845 | return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14846 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14847 | if (setup_confname_tables(m)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14848 | return -1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14849 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14850 | Py_INCREF(PyExc_OSError); |
| 14851 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 14852 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14853 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14854 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 14855 | PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc); |
| 14856 | if (WaitidResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14857 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14858 | } |
| 14859 | Py_INCREF(WaitidResultType); |
| 14860 | PyModule_AddObject(m, "waitid_result", WaitidResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14861 | state->WaitidResultType = WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14862 | #endif |
| 14863 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14864 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
| 14865 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 14866 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 14867 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 14868 | PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc); |
| 14869 | if (StatResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14870 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14871 | } |
| 14872 | Py_INCREF(StatResultType); |
| 14873 | PyModule_AddObject(m, "stat_result", StatResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14874 | state->StatResultType = StatResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14875 | structseq_new = ((PyTypeObject *)StatResultType)->tp_new; |
| 14876 | ((PyTypeObject *)StatResultType)->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14877 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14878 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
| 14879 | PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc); |
| 14880 | if (StatVFSResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14881 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14882 | } |
| 14883 | Py_INCREF(StatVFSResultType); |
| 14884 | PyModule_AddObject(m, "statvfs_result", StatVFSResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14885 | state->StatVFSResultType = StatVFSResultType; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14886 | #ifdef NEED_TICKS_PER_SECOND |
| 14887 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14888 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14889 | # elif defined(HZ) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14890 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14891 | # else |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14892 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14893 | # endif |
| 14894 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14895 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14896 | #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] | 14897 | sched_param_desc.name = MODNAME ".sched_param"; |
| 14898 | PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc); |
| 14899 | if (SchedParamType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14900 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14901 | } |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14902 | Py_INCREF(SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14903 | PyModule_AddObject(m, "sched_param", SchedParamType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14904 | state->SchedParamType = SchedParamType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14905 | ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param; |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14906 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14907 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14908 | /* initialize TerminalSize_info */ |
| 14909 | PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc); |
| 14910 | if (TerminalSizeType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14911 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14912 | } |
| 14913 | Py_INCREF(TerminalSizeType); |
| 14914 | PyModule_AddObject(m, "terminal_size", TerminalSizeType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14915 | state->TerminalSizeType = TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14916 | |
| 14917 | /* initialize scandir types */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14918 | PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14919 | if (ScandirIteratorType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14920 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14921 | } |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14922 | state->ScandirIteratorType = ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14923 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14924 | PyObject *DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14925 | if (DirEntryType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14926 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14927 | } |
| 14928 | Py_INCREF(DirEntryType); |
| 14929 | PyModule_AddObject(m, "DirEntry", DirEntryType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14930 | state->DirEntryType = DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14931 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14932 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14933 | PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(×_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14934 | if (TimesResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14935 | return -1; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14936 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14937 | Py_INCREF(TimesResultType); |
| 14938 | PyModule_AddObject(m, "times_result", TimesResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14939 | state->TimesResultType = TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14940 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14941 | PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14942 | if (UnameResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14943 | return -1; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14944 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14945 | Py_INCREF(UnameResultType); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14946 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14947 | state->UnameResultType = (PyObject *)UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14948 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14949 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14950 | /* |
| 14951 | * Step 2 of weak-linking support on Mac OS X. |
| 14952 | * |
| 14953 | * The code below removes functions that are not available on the |
| 14954 | * currently active platform. |
| 14955 | * |
| 14956 | * 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] | 14957 | * 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] | 14958 | * OSX 10.4. |
| 14959 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14960 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14961 | if (fstatvfs == NULL) { |
| 14962 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14963 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14964 | } |
| 14965 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14966 | #endif /* HAVE_FSTATVFS */ |
| 14967 | |
| 14968 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14969 | if (statvfs == NULL) { |
| 14970 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14971 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14972 | } |
| 14973 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14974 | #endif /* HAVE_STATVFS */ |
| 14975 | |
| 14976 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14977 | if (lchown == NULL) { |
| 14978 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14979 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14980 | } |
| 14981 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14982 | #endif /* HAVE_LCHOWN */ |
| 14983 | |
| 14984 | |
| 14985 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14986 | |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14987 | if ((state->billion = PyLong_FromLong(1000000000)) == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14988 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14989 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14990 | state->struct_rusage = PyUnicode_InternFromString("struct_rusage"); |
| 14991 | if (state->struct_rusage == 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 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14994 | state->st_mode = PyUnicode_InternFromString("st_mode"); |
| 14995 | if (state->st_mode == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14996 | return -1; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 14997 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14998 | /* suppress "function not used" warnings */ |
| 14999 | { |
| 15000 | int ignored; |
| 15001 | fd_specified("", -1); |
| 15002 | follow_symlinks_specified("", 1); |
| 15003 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 15004 | dir_fd_converter(Py_None, &ignored); |
| 15005 | dir_fd_unavailable(Py_None, &ignored); |
| 15006 | } |
| 15007 | |
| 15008 | /* |
| 15009 | * provide list of locally available functions |
| 15010 | * so os.py can populate support_* lists |
| 15011 | */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15012 | PyObject *list = PyList_New(0); |
| 15013 | if (!list) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15014 | return -1; |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15015 | } |
| 15016 | for (const char * const *trace = have_functions; *trace; trace++) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15017 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 15018 | if (!unicode) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15019 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15020 | if (PyList_Append(list, unicode)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15021 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15022 | Py_DECREF(unicode); |
| 15023 | } |
| 15024 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 15025 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15026 | return 0; |
| 15027 | } |
| 15028 | |
| 15029 | |
| 15030 | static PyModuleDef_Slot posixmodile_slots[] = { |
| 15031 | {Py_mod_exec, posixmodule_exec}, |
| 15032 | {0, NULL} |
| 15033 | }; |
| 15034 | |
| 15035 | static struct PyModuleDef posixmodule = { |
| 15036 | PyModuleDef_HEAD_INIT, |
| 15037 | .m_name = MODNAME, |
| 15038 | .m_doc = posix__doc__, |
| 15039 | .m_size = sizeof(_posixstate), |
| 15040 | .m_methods = posix_methods, |
| 15041 | .m_slots = posixmodile_slots, |
| 15042 | .m_traverse = _posix_traverse, |
| 15043 | .m_clear = _posix_clear, |
| 15044 | .m_free = _posix_free, |
| 15045 | }; |
| 15046 | |
| 15047 | PyMODINIT_FUNC |
| 15048 | INITFUNC(void) |
| 15049 | { |
| 15050 | return PyModuleDef_Init(&posixmodule); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 15051 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15052 | |
| 15053 | #ifdef __cplusplus |
| 15054 | } |
| 15055 | #endif |