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() |
| 37 | #include "pycore_pystate.h" // _PyInterpreterState_GET() |
| 38 | #include "structmember.h" // PyMemberDef |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 39 | #ifndef MS_WINDOWS |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 40 | # include "posixmodule.h" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 41 | #else |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 42 | # include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 43 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 44 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 45 | /* On android API level 21, 'AT_EACCESS' is not declared although |
| 46 | * HAVE_FACCESSAT is defined. */ |
| 47 | #ifdef __ANDROID__ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 48 | # undef HAVE_FACCESSAT |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 49 | #endif |
| 50 | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | fa76eee | 2016-05-28 21:03:48 +0000 | [diff] [blame] | 51 | #include <stdio.h> /* needed for ctermid() */ |
| 52 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 53 | #ifdef __cplusplus |
| 54 | extern "C" { |
| 55 | #endif |
| 56 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 57 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 58 | "This module provides access to operating system functionality that is\n\ |
| 59 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 60 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 61 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 63 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 64 | #ifdef HAVE_SYS_UIO_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 65 | # include <sys/uio.h> |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 66 | #endif |
| 67 | |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 68 | #ifdef HAVE_SYS_SYSMACROS_H |
| 69 | /* GNU C Library: major(), minor(), makedev() */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 70 | # include <sys/sysmacros.h> |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 71 | #endif |
| 72 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 73 | #ifdef HAVE_SYS_TYPES_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 74 | # include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | #endif /* HAVE_SYS_TYPES_H */ |
| 76 | |
| 77 | #ifdef HAVE_SYS_STAT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 78 | # include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 79 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 81 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 82 | # include <sys/wait.h> // WNOHANG |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 83 | #endif |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 84 | #ifdef HAVE_LINUX_WAIT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 85 | # include <linux/wait.h> // P_PIDFD |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 86 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 87 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 88 | #ifdef HAVE_SIGNAL_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 89 | # include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 90 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 92 | #ifdef HAVE_FCNTL_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 93 | # include <fcntl.h> |
| 94 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 96 | #ifdef HAVE_GRP_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 97 | # include <grp.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 98 | #endif |
| 99 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 100 | #ifdef HAVE_SYSEXITS_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 101 | # include <sysexits.h> |
| 102 | #endif |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 103 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 104 | #ifdef HAVE_SYS_LOADAVG_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 105 | # include <sys/loadavg.h> |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 106 | #endif |
| 107 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 108 | #ifdef HAVE_SYS_SENDFILE_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 109 | # include <sys/sendfile.h> |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 110 | #endif |
| 111 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 112 | #if defined(__APPLE__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 113 | # include <copyfile.h> |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 114 | #endif |
| 115 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 116 | #ifdef HAVE_SCHED_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 117 | # include <sched.h> |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 118 | #endif |
| 119 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 120 | #ifdef HAVE_COPY_FILE_RANGE |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 121 | # include <unistd.h> |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 122 | #endif |
| 123 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 124 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 125 | # undef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 126 | #endif |
| 127 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 128 | #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] | 129 | # define USE_XATTRS |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 130 | #endif |
| 131 | |
| 132 | #ifdef USE_XATTRS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 133 | # include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 134 | #endif |
| 135 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 136 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 137 | # ifdef HAVE_SYS_SOCKET_H |
| 138 | # include <sys/socket.h> |
| 139 | # endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 140 | #endif |
| 141 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 142 | #ifdef HAVE_DLFCN_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 143 | # include <dlfcn.h> |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 144 | #endif |
| 145 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 146 | #ifdef __hpux |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 147 | # include <sys/mpctl.h> |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 148 | #endif |
| 149 | |
| 150 | #if defined(__DragonFly__) || \ |
| 151 | defined(__OpenBSD__) || \ |
| 152 | defined(__FreeBSD__) || \ |
| 153 | defined(__NetBSD__) || \ |
| 154 | defined(__APPLE__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 155 | # include <sys/sysctl.h> |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 156 | #endif |
| 157 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 158 | #ifdef HAVE_LINUX_RANDOM_H |
| 159 | # include <linux/random.h> |
| 160 | #endif |
| 161 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 162 | # include <sys/syscall.h> |
| 163 | #endif |
| 164 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 165 | #if defined(MS_WINDOWS) |
| 166 | # define TERMSIZE_USE_CONIO |
| 167 | #elif defined(HAVE_SYS_IOCTL_H) |
| 168 | # include <sys/ioctl.h> |
| 169 | # if defined(HAVE_TERMIOS_H) |
| 170 | # include <termios.h> |
| 171 | # endif |
| 172 | # if defined(TIOCGWINSZ) |
| 173 | # define TERMSIZE_USE_IOCTL |
| 174 | # endif |
| 175 | #endif /* MS_WINDOWS */ |
| 176 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 177 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 178 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 179 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 180 | # define HAVE_OPENDIR 1 |
| 181 | # define HAVE_SYSTEM 1 |
| 182 | # include <process.h> |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 183 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 184 | # ifdef _MSC_VER |
| 185 | /* Microsoft compiler */ |
| 186 | # define HAVE_GETPPID 1 |
| 187 | # define HAVE_GETLOGIN 1 |
| 188 | # define HAVE_SPAWNV 1 |
| 189 | # define HAVE_EXECV 1 |
| 190 | # define HAVE_WSPAWNV 1 |
| 191 | # define HAVE_WEXECV 1 |
| 192 | # define HAVE_PIPE 1 |
| 193 | # define HAVE_SYSTEM 1 |
| 194 | # define HAVE_CWAIT 1 |
| 195 | # define HAVE_FSYNC 1 |
| 196 | # define fsync _commit |
| 197 | # else |
| 198 | /* Unix functions that the configure script doesn't check for */ |
| 199 | # ifndef __VXWORKS__ |
| 200 | # define HAVE_EXECV 1 |
| 201 | # define HAVE_FORK 1 |
| 202 | # if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 203 | # define HAVE_FORK1 1 |
| 204 | # endif |
| 205 | # endif |
| 206 | # define HAVE_GETEGID 1 |
| 207 | # define HAVE_GETEUID 1 |
| 208 | # define HAVE_GETGID 1 |
| 209 | # define HAVE_GETPPID 1 |
| 210 | # define HAVE_GETUID 1 |
| 211 | # define HAVE_KILL 1 |
| 212 | # define HAVE_OPENDIR 1 |
| 213 | # define HAVE_PIPE 1 |
| 214 | # define HAVE_SYSTEM 1 |
| 215 | # define HAVE_WAIT 1 |
| 216 | # define HAVE_TTYNAME 1 |
| 217 | # endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 218 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 219 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 220 | _Py_IDENTIFIER(__fspath__); |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 221 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 222 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 223 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 224 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 225 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 226 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 227 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 228 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 229 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 230 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 231 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 232 | (default) */ |
| 233 | extern char *ctermid_r(char *); |
| 234 | #endif |
| 235 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 236 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 238 | #if defined(__VXWORKS__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 239 | # include <vxCpuLib.h> |
| 240 | # include <rtpLib.h> |
| 241 | # include <wait.h> |
| 242 | # include <taskLib.h> |
| 243 | # ifndef _P_WAIT |
| 244 | # define _P_WAIT 0 |
| 245 | # define _P_NOWAIT 1 |
| 246 | # define _P_NOWAITO 1 |
| 247 | # endif |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 248 | #endif /* __VXWORKS__ */ |
| 249 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 250 | #ifdef HAVE_POSIX_SPAWN |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 251 | # include <spawn.h> |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 252 | #endif |
| 253 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 254 | #ifdef HAVE_UTIME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 255 | # include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 256 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 258 | #ifdef HAVE_SYS_UTIME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 259 | # include <sys/utime.h> |
| 260 | # 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] | 261 | #endif /* HAVE_SYS_UTIME_H */ |
| 262 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 263 | #ifdef HAVE_SYS_TIMES_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 264 | # include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 265 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | |
| 267 | #ifdef HAVE_SYS_PARAM_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 268 | # include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 269 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 270 | |
| 271 | #ifdef HAVE_SYS_UTSNAME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 272 | # include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 273 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 275 | #ifdef HAVE_DIRENT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 276 | # include <dirent.h> |
| 277 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 278 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 279 | # if defined(__WATCOMC__) && !defined(__QNX__) |
| 280 | # include <direct.h> |
| 281 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
| 282 | # else |
| 283 | # define dirent direct |
| 284 | # define NAMLEN(dirent) (dirent)->d_namlen |
| 285 | # endif |
| 286 | # ifdef HAVE_SYS_NDIR_H |
| 287 | # include <sys/ndir.h> |
| 288 | # endif |
| 289 | # ifdef HAVE_SYS_DIR_H |
| 290 | # include <sys/dir.h> |
| 291 | # endif |
| 292 | # ifdef HAVE_NDIR_H |
| 293 | # include <ndir.h> |
| 294 | # endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 295 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 297 | #ifdef _MSC_VER |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 298 | # ifdef HAVE_DIRECT_H |
| 299 | # include <direct.h> |
| 300 | # endif |
| 301 | # ifdef HAVE_IO_H |
| 302 | # include <io.h> |
| 303 | # endif |
| 304 | # ifdef HAVE_PROCESS_H |
| 305 | # include <process.h> |
| 306 | # endif |
| 307 | # ifndef IO_REPARSE_TAG_SYMLINK |
| 308 | # define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
| 309 | # endif |
| 310 | # ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 311 | # define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 312 | # endif |
| 313 | # include "osdefs.h" // SEP |
| 314 | # include <malloc.h> |
| 315 | # include <windows.h> |
| 316 | # include <shellapi.h> // ShellExecute() |
| 317 | # include <lmcons.h> // UNLEN |
| 318 | # define HAVE_SYMLINK |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 319 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 320 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 321 | #ifndef MAXPATHLEN |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 322 | # if defined(PATH_MAX) && PATH_MAX > 1024 |
| 323 | # define MAXPATHLEN PATH_MAX |
| 324 | # else |
| 325 | # define MAXPATHLEN 1024 |
| 326 | # endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 327 | #endif /* MAXPATHLEN */ |
| 328 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 329 | #ifdef UNION_WAIT |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 330 | /* Emulate some macros on systems that have a union instead of macros */ |
| 331 | # ifndef WIFEXITED |
| 332 | # define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 333 | # endif |
| 334 | # ifndef WEXITSTATUS |
| 335 | # define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 336 | # endif |
| 337 | # ifndef WTERMSIG |
| 338 | # define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 339 | # endif |
| 340 | # define WAIT_TYPE union wait |
| 341 | # define WAIT_STATUS_INT(s) (s.w_status) |
| 342 | #else |
| 343 | /* !UNION_WAIT */ |
| 344 | # define WAIT_TYPE int |
| 345 | # define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 346 | #endif /* UNION_WAIT */ |
| 347 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 348 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 349 | prototype for it, at least on Solaris -- maybe others as well?). */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 350 | #if defined(HAVE_CTERMID_R) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 351 | # define USE_CTERMID_R |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 352 | #endif |
| 353 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 354 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 355 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 356 | #undef FSTAT |
| 357 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 358 | #ifdef MS_WINDOWS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 359 | # define STAT win32_stat |
| 360 | # define LSTAT win32_lstat |
| 361 | # define FSTAT _Py_fstat_noraise |
| 362 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 363 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 364 | # define STAT stat |
| 365 | # define LSTAT lstat |
| 366 | # define FSTAT fstat |
| 367 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 368 | #endif |
| 369 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 370 | #if defined(MAJOR_IN_MKDEV) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 371 | # include <sys/mkdev.h> |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 372 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 373 | # if defined(MAJOR_IN_SYSMACROS) |
| 374 | # include <sys/sysmacros.h> |
| 375 | # endif |
| 376 | # if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 377 | # include <sys/mkdev.h> |
| 378 | # endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 379 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 380 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 381 | #ifdef MS_WINDOWS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 382 | # define INITFUNC PyInit_nt |
| 383 | # define MODNAME "nt" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 384 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 385 | # define INITFUNC PyInit_posix |
| 386 | # define MODNAME "posix" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 387 | #endif |
| 388 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 389 | #if defined(__sun) |
| 390 | /* Something to implement in autoconf, not present in autoconf 2.69 */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 391 | # define HAVE_STRUCT_STAT_ST_FSTYPE 1 |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 392 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 393 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 394 | /* memfd_create is either defined in sys/mman.h or sys/memfd.h |
| 395 | * linux/memfd.h defines additional flags |
| 396 | */ |
| 397 | #ifdef HAVE_SYS_MMAN_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 398 | # include <sys/mman.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 399 | #endif |
| 400 | #ifdef HAVE_SYS_MEMFD_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 401 | # include <sys/memfd.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 402 | #endif |
| 403 | #ifdef HAVE_LINUX_MEMFD_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 404 | # include <linux/memfd.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 405 | #endif |
| 406 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 407 | #ifdef _Py_MEMORY_SANITIZER |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 408 | # include <sanitizer/msan_interface.h> |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 409 | #endif |
| 410 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 411 | #ifdef HAVE_FORK |
| 412 | static void |
| 413 | run_at_forkers(PyObject *lst, int reverse) |
| 414 | { |
| 415 | Py_ssize_t i; |
| 416 | PyObject *cpy; |
| 417 | |
| 418 | if (lst != NULL) { |
| 419 | assert(PyList_CheckExact(lst)); |
| 420 | |
| 421 | /* Use a list copy in case register_at_fork() is called from |
| 422 | * one of the callbacks. |
| 423 | */ |
| 424 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); |
| 425 | if (cpy == NULL) |
| 426 | PyErr_WriteUnraisable(lst); |
| 427 | else { |
| 428 | if (reverse) |
| 429 | PyList_Reverse(cpy); |
| 430 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { |
| 431 | PyObject *func, *res; |
| 432 | func = PyList_GET_ITEM(cpy, i); |
Jeroen Demeyer | 7f41c8e | 2019-07-04 12:35:31 +0200 | [diff] [blame] | 433 | res = _PyObject_CallNoArg(func); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 434 | if (res == NULL) |
| 435 | PyErr_WriteUnraisable(func); |
| 436 | else |
| 437 | Py_DECREF(res); |
| 438 | } |
| 439 | Py_DECREF(cpy); |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | void |
| 445 | PyOS_BeforeFork(void) |
| 446 | { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 447 | run_at_forkers(_PyInterpreterState_GET()->before_forkers, 1); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 448 | |
| 449 | _PyImport_AcquireLock(); |
| 450 | } |
| 451 | |
| 452 | void |
| 453 | PyOS_AfterFork_Parent(void) |
| 454 | { |
| 455 | if (_PyImport_ReleaseLock() <= 0) |
| 456 | Py_FatalError("failed releasing import lock after fork"); |
| 457 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 458 | run_at_forkers(_PyInterpreterState_GET()->after_forkers_parent, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void |
| 462 | PyOS_AfterFork_Child(void) |
| 463 | { |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 464 | _PyRuntimeState *runtime = &_PyRuntime; |
| 465 | _PyGILState_Reinit(runtime); |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 466 | _PyEval_ReInitThreads(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 467 | _PyImport_ReInitLock(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 468 | _PySignal_AfterFork(); |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 469 | _PyRuntimeState_ReInitThreads(runtime); |
Victor Stinner | b49858b | 2019-05-24 15:20:23 +0200 | [diff] [blame] | 470 | _PyInterpreterState_DeleteExceptMain(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 471 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 472 | run_at_forkers(_PyInterpreterState_GET()->after_forkers_child, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static int |
| 476 | register_at_forker(PyObject **lst, PyObject *func) |
| 477 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 478 | if (func == NULL) /* nothing to register? do nothing. */ |
| 479 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 480 | if (*lst == NULL) { |
| 481 | *lst = PyList_New(0); |
| 482 | if (*lst == NULL) |
| 483 | return -1; |
| 484 | } |
| 485 | return PyList_Append(*lst, func); |
| 486 | } |
Victor Stinner | 87255be | 2020-04-07 23:11:49 +0200 | [diff] [blame] | 487 | #endif /* HAVE_FORK */ |
| 488 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 489 | |
| 490 | /* Legacy wrapper */ |
| 491 | void |
| 492 | PyOS_AfterFork(void) |
| 493 | { |
| 494 | #ifdef HAVE_FORK |
| 495 | PyOS_AfterFork_Child(); |
| 496 | #endif |
| 497 | } |
| 498 | |
| 499 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 500 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 501 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 502 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 503 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 504 | ULONG, struct _Py_stat_struct *); |
| 505 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 506 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 507 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 508 | #ifndef MS_WINDOWS |
| 509 | PyObject * |
| 510 | _PyLong_FromUid(uid_t uid) |
| 511 | { |
| 512 | if (uid == (uid_t)-1) |
| 513 | return PyLong_FromLong(-1); |
| 514 | return PyLong_FromUnsignedLong(uid); |
| 515 | } |
| 516 | |
| 517 | PyObject * |
| 518 | _PyLong_FromGid(gid_t gid) |
| 519 | { |
| 520 | if (gid == (gid_t)-1) |
| 521 | return PyLong_FromLong(-1); |
| 522 | return PyLong_FromUnsignedLong(gid); |
| 523 | } |
| 524 | |
| 525 | int |
| 526 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 527 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 528 | uid_t uid; |
| 529 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 530 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 531 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 532 | unsigned long uresult; |
| 533 | |
| 534 | index = PyNumber_Index(obj); |
| 535 | if (index == NULL) { |
| 536 | PyErr_Format(PyExc_TypeError, |
| 537 | "uid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 538 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 539 | return 0; |
| 540 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 541 | |
| 542 | /* |
| 543 | * Handling uid_t is complicated for two reasons: |
| 544 | * * Although uid_t is (always?) unsigned, it still |
| 545 | * accepts -1. |
| 546 | * * We don't know its size in advance--it may be |
| 547 | * bigger than an int, or it may be smaller than |
| 548 | * a long. |
| 549 | * |
| 550 | * So a bit of defensive programming is in order. |
| 551 | * Start with interpreting the value passed |
| 552 | * in as a signed long and see if it works. |
| 553 | */ |
| 554 | |
| 555 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 556 | |
| 557 | if (!overflow) { |
| 558 | uid = (uid_t)result; |
| 559 | |
| 560 | if (result == -1) { |
| 561 | if (PyErr_Occurred()) |
| 562 | goto fail; |
| 563 | /* It's a legitimate -1, we're done. */ |
| 564 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 565 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 566 | |
| 567 | /* Any other negative number is disallowed. */ |
| 568 | if (result < 0) |
| 569 | goto underflow; |
| 570 | |
| 571 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 572 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 573 | (long)uid != result) |
| 574 | goto underflow; |
| 575 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 576 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 577 | |
| 578 | if (overflow < 0) |
| 579 | goto underflow; |
| 580 | |
| 581 | /* |
| 582 | * Okay, the value overflowed a signed long. If it |
| 583 | * fits in an *unsigned* long, it may still be okay, |
| 584 | * as uid_t may be unsigned long on this platform. |
| 585 | */ |
| 586 | uresult = PyLong_AsUnsignedLong(index); |
| 587 | if (PyErr_Occurred()) { |
| 588 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 589 | goto overflow; |
| 590 | goto fail; |
| 591 | } |
| 592 | |
| 593 | uid = (uid_t)uresult; |
| 594 | |
| 595 | /* |
| 596 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 597 | * but this value would get interpreted as (uid_t)-1 by chown |
| 598 | * and its siblings. That's not what the user meant! So we |
| 599 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 600 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 601 | */ |
| 602 | if (uid == (uid_t)-1) |
| 603 | goto overflow; |
| 604 | |
| 605 | /* Ensure the value wasn't truncated. */ |
| 606 | if (sizeof(uid_t) < sizeof(long) && |
| 607 | (unsigned long)uid != uresult) |
| 608 | goto overflow; |
| 609 | /* fallthrough */ |
| 610 | |
| 611 | success: |
| 612 | Py_DECREF(index); |
| 613 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 614 | return 1; |
| 615 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 616 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 617 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 618 | "uid is less than minimum"); |
| 619 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 620 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 621 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 622 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 623 | "uid is greater than maximum"); |
| 624 | /* fallthrough */ |
| 625 | |
| 626 | fail: |
| 627 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | int |
| 632 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 633 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 634 | gid_t gid; |
| 635 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 636 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 637 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 638 | unsigned long uresult; |
| 639 | |
| 640 | index = PyNumber_Index(obj); |
| 641 | if (index == NULL) { |
| 642 | PyErr_Format(PyExc_TypeError, |
| 643 | "gid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 644 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 645 | return 0; |
| 646 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 647 | |
| 648 | /* |
| 649 | * Handling gid_t is complicated for two reasons: |
| 650 | * * Although gid_t is (always?) unsigned, it still |
| 651 | * accepts -1. |
| 652 | * * We don't know its size in advance--it may be |
| 653 | * bigger than an int, or it may be smaller than |
| 654 | * a long. |
| 655 | * |
| 656 | * So a bit of defensive programming is in order. |
| 657 | * Start with interpreting the value passed |
| 658 | * in as a signed long and see if it works. |
| 659 | */ |
| 660 | |
| 661 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 662 | |
| 663 | if (!overflow) { |
| 664 | gid = (gid_t)result; |
| 665 | |
| 666 | if (result == -1) { |
| 667 | if (PyErr_Occurred()) |
| 668 | goto fail; |
| 669 | /* It's a legitimate -1, we're done. */ |
| 670 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 671 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 672 | |
| 673 | /* Any other negative number is disallowed. */ |
| 674 | if (result < 0) { |
| 675 | goto underflow; |
| 676 | } |
| 677 | |
| 678 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 679 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 680 | (long)gid != result) |
| 681 | goto underflow; |
| 682 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 683 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 684 | |
| 685 | if (overflow < 0) |
| 686 | goto underflow; |
| 687 | |
| 688 | /* |
| 689 | * Okay, the value overflowed a signed long. If it |
| 690 | * fits in an *unsigned* long, it may still be okay, |
| 691 | * as gid_t may be unsigned long on this platform. |
| 692 | */ |
| 693 | uresult = PyLong_AsUnsignedLong(index); |
| 694 | if (PyErr_Occurred()) { |
| 695 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 696 | goto overflow; |
| 697 | goto fail; |
| 698 | } |
| 699 | |
| 700 | gid = (gid_t)uresult; |
| 701 | |
| 702 | /* |
| 703 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 704 | * but this value would get interpreted as (gid_t)-1 by chown |
| 705 | * and its siblings. That's not what the user meant! So we |
| 706 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 707 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 708 | */ |
| 709 | if (gid == (gid_t)-1) |
| 710 | goto overflow; |
| 711 | |
| 712 | /* Ensure the value wasn't truncated. */ |
| 713 | if (sizeof(gid_t) < sizeof(long) && |
| 714 | (unsigned long)gid != uresult) |
| 715 | goto overflow; |
| 716 | /* fallthrough */ |
| 717 | |
| 718 | success: |
| 719 | Py_DECREF(index); |
| 720 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 721 | return 1; |
| 722 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 723 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 724 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 725 | "gid is less than minimum"); |
| 726 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 727 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 728 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 729 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 730 | "gid is greater than maximum"); |
| 731 | /* fallthrough */ |
| 732 | |
| 733 | fail: |
| 734 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | #endif /* MS_WINDOWS */ |
| 738 | |
| 739 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 740 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 741 | |
| 742 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 743 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 744 | static int |
| 745 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 746 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 747 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 748 | if (PyErr_Occurred()) |
| 749 | return 0; |
| 750 | return 1; |
| 751 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 752 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 753 | |
| 754 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 755 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 756 | /* |
| 757 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 758 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 759 | * which doesn't play nicely with all the initializer lines in this file that |
| 760 | * look like this: |
| 761 | * int dir_fd = DEFAULT_DIR_FD; |
| 762 | */ |
| 763 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 764 | #else |
| 765 | #define DEFAULT_DIR_FD (-100) |
| 766 | #endif |
| 767 | |
| 768 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 769 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 770 | { |
| 771 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 772 | long long_value; |
| 773 | |
| 774 | PyObject *index = PyNumber_Index(o); |
| 775 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 776 | return 0; |
| 777 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 778 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 779 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 780 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 781 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 782 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 783 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 784 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 785 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 786 | return 0; |
| 787 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 788 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 789 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 790 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 791 | return 0; |
| 792 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 793 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 794 | *p = (int)long_value; |
| 795 | return 1; |
| 796 | } |
| 797 | |
| 798 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 799 | dir_fd_converter(PyObject *o, void *p) |
| 800 | { |
| 801 | if (o == Py_None) { |
| 802 | *(int *)p = DEFAULT_DIR_FD; |
| 803 | return 1; |
| 804 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 805 | else if (PyIndex_Check(o)) { |
| 806 | return _fd_converter(o, (int *)p); |
| 807 | } |
| 808 | else { |
| 809 | PyErr_Format(PyExc_TypeError, |
| 810 | "argument should be integer or None, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 811 | _PyType_Name(Py_TYPE(o))); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 812 | return 0; |
| 813 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 816 | typedef struct { |
| 817 | PyObject *billion; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 818 | PyObject *DirEntryType; |
| 819 | PyObject *ScandirIteratorType; |
| 820 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 821 | PyObject *SchedParamType; |
| 822 | #endif |
| 823 | PyObject *StatResultType; |
| 824 | PyObject *StatVFSResultType; |
| 825 | PyObject *TerminalSizeType; |
| 826 | PyObject *TimesResultType; |
| 827 | PyObject *UnameResultType; |
| 828 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 829 | PyObject *WaitidResultType; |
| 830 | #endif |
| 831 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 832 | PyObject *struct_rusage; |
| 833 | #endif |
| 834 | PyObject *st_mode; |
| 835 | } _posixstate; |
| 836 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 837 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 838 | static inline _posixstate* |
| 839 | get_posix_state(PyObject *module) |
| 840 | { |
| 841 | void *state = PyModule_GetState(module); |
| 842 | assert(state != NULL); |
| 843 | return (_posixstate *)state; |
| 844 | } |
| 845 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 846 | /* |
| 847 | * A PyArg_ParseTuple "converter" function |
| 848 | * that handles filesystem paths in the manner |
| 849 | * preferred by the os module. |
| 850 | * |
| 851 | * path_converter accepts (Unicode) strings and their |
| 852 | * subclasses, and bytes and their subclasses. What |
| 853 | * it does with the argument depends on the platform: |
| 854 | * |
| 855 | * * On Windows, if we get a (Unicode) string we |
| 856 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 857 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 858 | * |
| 859 | * * On all other platforms, strings are encoded |
| 860 | * to bytes using PyUnicode_FSConverter, then we |
| 861 | * extract the char * from the bytes object and |
| 862 | * return that. |
| 863 | * |
| 864 | * path_converter also optionally accepts signed |
| 865 | * integers (representing open file descriptors) instead |
| 866 | * of path strings. |
| 867 | * |
| 868 | * Input fields: |
| 869 | * path.nullable |
| 870 | * If nonzero, the path is permitted to be None. |
| 871 | * path.allow_fd |
| 872 | * If nonzero, the path is permitted to be a file handle |
| 873 | * (a signed int) instead of a string. |
| 874 | * path.function_name |
| 875 | * If non-NULL, path_converter will use that as the name |
| 876 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 877 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 878 | * path.argument_name |
| 879 | * If non-NULL, path_converter will use that as the name |
| 880 | * of the parameter in error messages. |
| 881 | * (If path.argument_name is NULL it uses "path".) |
| 882 | * |
| 883 | * Output fields: |
| 884 | * path.wide |
| 885 | * Points to the path if it was expressed as Unicode |
| 886 | * and was not encoded. (Only used on Windows.) |
| 887 | * path.narrow |
| 888 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 889 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 890 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 891 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 892 | * path.fd |
| 893 | * Contains a file descriptor if path.accept_fd was true |
| 894 | * and the caller provided a signed integer instead of any |
| 895 | * sort of string. |
| 896 | * |
| 897 | * WARNING: if your "path" parameter is optional, and is |
| 898 | * unspecified, path_converter will never get called. |
| 899 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 900 | * yourself! |
| 901 | * path.length |
| 902 | * The length of the path in characters, if specified as |
| 903 | * a string. |
| 904 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 905 | * The original object passed in (if get a PathLike object, |
| 906 | * the result of PyOS_FSPath() is treated as the original object). |
| 907 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 908 | * path.cleanup |
| 909 | * For internal use only. May point to a temporary object. |
| 910 | * (Pay no attention to the man behind the curtain.) |
| 911 | * |
| 912 | * At most one of path.wide or path.narrow will be non-NULL. |
| 913 | * If path was None and path.nullable was set, |
| 914 | * or if path was an integer and path.allow_fd was set, |
| 915 | * both path.wide and path.narrow will be NULL |
| 916 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 917 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 918 | * path_converter takes care to not write to the path_t |
| 919 | * unless it's successful. However it must reset the |
| 920 | * "cleanup" field each time it's called. |
| 921 | * |
| 922 | * Use as follows: |
| 923 | * path_t path; |
| 924 | * memset(&path, 0, sizeof(path)); |
| 925 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 926 | * // ... use values from path ... |
| 927 | * path_cleanup(&path); |
| 928 | * |
| 929 | * (Note that if PyArg_Parse fails you don't need to call |
| 930 | * path_cleanup(). However it is safe to do so.) |
| 931 | */ |
| 932 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 933 | const char *function_name; |
| 934 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 935 | int nullable; |
| 936 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 937 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 938 | #ifdef MS_WINDOWS |
| 939 | BOOL narrow; |
| 940 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 941 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 942 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 943 | int fd; |
| 944 | Py_ssize_t length; |
| 945 | PyObject *object; |
| 946 | PyObject *cleanup; |
| 947 | } path_t; |
| 948 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 949 | #ifdef MS_WINDOWS |
| 950 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 951 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 952 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 953 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 954 | {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] | 955 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 956 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 957 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 958 | path_cleanup(path_t *path) |
| 959 | { |
| 960 | Py_CLEAR(path->object); |
| 961 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 965 | path_converter(PyObject *o, void *p) |
| 966 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 967 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 968 | PyObject *bytes = NULL; |
| 969 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 970 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 971 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 972 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 973 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 974 | const wchar_t *wide; |
| 975 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 976 | |
| 977 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 978 | PyErr_Format(exc, "%s%s" fmt, \ |
| 979 | path->function_name ? path->function_name : "", \ |
| 980 | path->function_name ? ": " : "", \ |
| 981 | path->argument_name ? path->argument_name : "path") |
| 982 | |
| 983 | /* Py_CLEANUP_SUPPORTED support */ |
| 984 | if (o == NULL) { |
| 985 | path_cleanup(path); |
| 986 | return 1; |
| 987 | } |
| 988 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 989 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 990 | path->object = path->cleanup = NULL; |
| 991 | /* path->object owns a reference to the original object */ |
| 992 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 993 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 994 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 995 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 996 | #ifdef MS_WINDOWS |
| 997 | path->narrow = FALSE; |
| 998 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 999 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1000 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1001 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1002 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1005 | /* Only call this here so that we don't treat the return value of |
| 1006 | os.fspath() as an fd or buffer. */ |
| 1007 | is_index = path->allow_fd && PyIndex_Check(o); |
| 1008 | is_buffer = PyObject_CheckBuffer(o); |
| 1009 | is_bytes = PyBytes_Check(o); |
| 1010 | is_unicode = PyUnicode_Check(o); |
| 1011 | |
| 1012 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 1013 | /* Inline PyOS_FSPath() for better error messages. */ |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1014 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1015 | |
| 1016 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 1017 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1018 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1019 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1020 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1021 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1022 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1023 | goto error_exit; |
| 1024 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1025 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1026 | is_unicode = 1; |
| 1027 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1028 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1029 | is_bytes = 1; |
| 1030 | } |
| 1031 | else { |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1032 | PyErr_Format(PyExc_TypeError, |
| 1033 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1034 | "not %.200s", _PyType_Name(Py_TYPE(o)), |
| 1035 | _PyType_Name(Py_TYPE(res))); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1036 | Py_DECREF(res); |
| 1037 | goto error_exit; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1038 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1039 | |
| 1040 | /* still owns a reference to the original object */ |
| 1041 | Py_DECREF(o); |
| 1042 | o = res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1046 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1047 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1048 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1049 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1050 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1051 | if (length > 32767) { |
| 1052 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1053 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1054 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1055 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1056 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1057 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1058 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1059 | |
| 1060 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1061 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1062 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1063 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1064 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1065 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1066 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1067 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1068 | #endif |
| 1069 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1070 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1071 | bytes = o; |
| 1072 | Py_INCREF(bytes); |
| 1073 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1074 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1075 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1076 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1077 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1078 | "%s%s%s should be %s, not %.200s", |
| 1079 | path->function_name ? path->function_name : "", |
| 1080 | path->function_name ? ": " : "", |
| 1081 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1082 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1083 | "integer or None" : |
| 1084 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1085 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1086 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1087 | _PyType_Name(Py_TYPE(o)))) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1088 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1089 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1090 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1091 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1092 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1093 | } |
| 1094 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1095 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1096 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1097 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1098 | } |
| 1099 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1100 | #ifdef MS_WINDOWS |
| 1101 | path->narrow = FALSE; |
| 1102 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1103 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1104 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1105 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1106 | } |
| 1107 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1108 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1109 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1110 | path->function_name ? path->function_name : "", |
| 1111 | path->function_name ? ": " : "", |
| 1112 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1113 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1114 | "integer or None" : |
| 1115 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1116 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1117 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1118 | _PyType_Name(Py_TYPE(o))); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1119 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1122 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1123 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1124 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1125 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1126 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1129 | #ifdef MS_WINDOWS |
| 1130 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1131 | narrow, |
| 1132 | length |
| 1133 | ); |
| 1134 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1135 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1138 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1139 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1140 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1141 | } |
| 1142 | if (length > 32767) { |
| 1143 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1144 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1145 | } |
| 1146 | if (wcslen(wide) != length) { |
| 1147 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1148 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1149 | } |
| 1150 | path->wide = wide; |
| 1151 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1152 | path->cleanup = wo; |
| 1153 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1154 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1155 | path->wide = NULL; |
| 1156 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1157 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1158 | /* Still a reference owned by path->object, don't have to |
| 1159 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1160 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1161 | } |
| 1162 | else { |
| 1163 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1164 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1165 | #endif |
| 1166 | path->fd = -1; |
| 1167 | |
| 1168 | success_exit: |
| 1169 | path->length = length; |
| 1170 | path->object = o; |
| 1171 | return Py_CLEANUP_SUPPORTED; |
| 1172 | |
| 1173 | error_exit: |
| 1174 | Py_XDECREF(o); |
| 1175 | Py_XDECREF(bytes); |
| 1176 | #ifdef MS_WINDOWS |
| 1177 | Py_XDECREF(wo); |
| 1178 | #endif |
| 1179 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1183 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1184 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1185 | PyErr_Format(PyExc_NotImplementedError, |
| 1186 | "%s%s%s unavailable on this platform", |
| 1187 | (function_name != NULL) ? function_name : "", |
| 1188 | (function_name != NULL) ? ": ": "", |
| 1189 | argument_name); |
| 1190 | } |
| 1191 | |
| 1192 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1193 | dir_fd_unavailable(PyObject *o, void *p) |
| 1194 | { |
| 1195 | int dir_fd; |
| 1196 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1197 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1198 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1199 | argument_unavailable_error(NULL, "dir_fd"); |
| 1200 | return 0; |
| 1201 | } |
| 1202 | *(int *)p = dir_fd; |
| 1203 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1207 | fd_specified(const char *function_name, int fd) |
| 1208 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1209 | if (fd == -1) |
| 1210 | return 0; |
| 1211 | |
| 1212 | argument_unavailable_error(function_name, "fd"); |
| 1213 | return 1; |
| 1214 | } |
| 1215 | |
| 1216 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1217 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1218 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1219 | if (follow_symlinks) |
| 1220 | return 0; |
| 1221 | |
| 1222 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1223 | return 1; |
| 1224 | } |
| 1225 | |
| 1226 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1227 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1228 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1229 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1230 | #ifndef MS_WINDOWS |
| 1231 | && !path->narrow |
| 1232 | #endif |
| 1233 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1234 | PyErr_Format(PyExc_ValueError, |
| 1235 | "%s: can't specify dir_fd without matching path", |
| 1236 | function_name); |
| 1237 | return 1; |
| 1238 | } |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1243 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1244 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1245 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1246 | PyErr_Format(PyExc_ValueError, |
| 1247 | "%s: can't specify both dir_fd and fd", |
| 1248 | function_name); |
| 1249 | return 1; |
| 1250 | } |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
| 1254 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1255 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1256 | int follow_symlinks) |
| 1257 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1258 | if ((fd > 0) && (!follow_symlinks)) { |
| 1259 | PyErr_Format(PyExc_ValueError, |
| 1260 | "%s: cannot use fd and follow_symlinks together", |
| 1261 | function_name); |
| 1262 | return 1; |
| 1263 | } |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1268 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1269 | int follow_symlinks) |
| 1270 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1271 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1272 | PyErr_Format(PyExc_ValueError, |
| 1273 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1274 | function_name); |
| 1275 | return 1; |
| 1276 | } |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1280 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1281 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1282 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1283 | typedef off_t Py_off_t; |
| 1284 | #endif |
| 1285 | |
| 1286 | static int |
| 1287 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1288 | { |
| 1289 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1290 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1291 | #else |
| 1292 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1293 | #endif |
| 1294 | if (PyErr_Occurred()) |
| 1295 | return 0; |
| 1296 | return 1; |
| 1297 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1298 | |
| 1299 | static PyObject * |
| 1300 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1301 | { |
| 1302 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1303 | return PyLong_FromLongLong(offset); |
| 1304 | #else |
| 1305 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1306 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1307 | } |
| 1308 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1309 | #ifdef HAVE_SIGSET_T |
| 1310 | /* Convert an iterable of integers to a sigset. |
| 1311 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1312 | int |
| 1313 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1314 | { |
| 1315 | sigset_t *mask = (sigset_t *)addr; |
| 1316 | PyObject *iterator, *item; |
| 1317 | long signum; |
| 1318 | int overflow; |
| 1319 | |
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1320 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
| 1321 | if (sigemptyset(mask) < (0)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1322 | /* Probably only if mask == NULL. */ |
| 1323 | PyErr_SetFromErrno(PyExc_OSError); |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | iterator = PyObject_GetIter(obj); |
| 1328 | if (iterator == NULL) { |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1333 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1334 | Py_DECREF(item); |
| 1335 | if (signum <= 0 || signum >= NSIG) { |
| 1336 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1337 | PyErr_Format(PyExc_ValueError, |
| 1338 | "signal number %ld out of range", signum); |
| 1339 | } |
| 1340 | goto error; |
| 1341 | } |
| 1342 | if (sigaddset(mask, (int)signum)) { |
| 1343 | if (errno != EINVAL) { |
| 1344 | /* Probably impossible */ |
| 1345 | PyErr_SetFromErrno(PyExc_OSError); |
| 1346 | goto error; |
| 1347 | } |
| 1348 | /* For backwards compatibility, allow idioms such as |
| 1349 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1350 | */ |
| 1351 | const char msg[] = |
| 1352 | "invalid signal number %ld, please use valid_signals()"; |
| 1353 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1354 | goto error; |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | if (!PyErr_Occurred()) { |
| 1359 | Py_DECREF(iterator); |
| 1360 | return 1; |
| 1361 | } |
| 1362 | |
| 1363 | error: |
| 1364 | Py_DECREF(iterator); |
| 1365 | return 0; |
| 1366 | } |
| 1367 | #endif /* HAVE_SIGSET_T */ |
| 1368 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1369 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1370 | |
| 1371 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1372 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1373 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1374 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1375 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1376 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1377 | |
| 1378 | if (0 == DeviceIoControl( |
| 1379 | reparse_point_handle, |
| 1380 | FSCTL_GET_REPARSE_POINT, |
| 1381 | NULL, 0, /* in buffer */ |
| 1382 | target_buffer, sizeof(target_buffer), |
| 1383 | &n_bytes_returned, |
| 1384 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1385 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1386 | |
| 1387 | if (reparse_tag) |
| 1388 | *reparse_tag = rdb->ReparseTag; |
| 1389 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1390 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1391 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1392 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1393 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1394 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1395 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1396 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1397 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1398 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1399 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1400 | */ |
| 1401 | #include <crt_externs.h> |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1402 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1403 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1404 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1405 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1406 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1407 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1408 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1409 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1410 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1411 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1412 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1413 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1414 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1415 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1416 | d = PyDict_New(); |
| 1417 | if (d == NULL) |
| 1418 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1419 | #ifdef MS_WINDOWS |
| 1420 | /* _wenviron must be initialized in this way if the program is started |
| 1421 | through main() instead of wmain(). */ |
| 1422 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1423 | e = _wenviron; |
Benoit Hudson | 723f71a | 2019-12-06 14:15:03 -0500 | [diff] [blame] | 1424 | #elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
| 1425 | /* environ is not accessible as an extern in a shared object on OSX; use |
| 1426 | _NSGetEnviron to resolve it. The value changes if you add environment |
| 1427 | variables between calls to Py_Initialize, so don't cache the value. */ |
| 1428 | e = *_NSGetEnviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1429 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1430 | e = environ; |
| 1431 | #endif |
| 1432 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1433 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1434 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1435 | PyObject *k; |
| 1436 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1437 | #ifdef MS_WINDOWS |
| 1438 | const wchar_t *p = wcschr(*e, L'='); |
| 1439 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1440 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1441 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1442 | if (p == NULL) |
| 1443 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1444 | #ifdef MS_WINDOWS |
| 1445 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1446 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1447 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1448 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1449 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1450 | Py_DECREF(d); |
| 1451 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1452 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1453 | #ifdef MS_WINDOWS |
| 1454 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1455 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1456 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1457 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1458 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1459 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1460 | Py_DECREF(d); |
| 1461 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1462 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1463 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1464 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1465 | Py_DECREF(v); |
| 1466 | Py_DECREF(k); |
| 1467 | Py_DECREF(d); |
| 1468 | return NULL; |
| 1469 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1470 | } |
| 1471 | Py_DECREF(k); |
| 1472 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1473 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1474 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1477 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1478 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1479 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1480 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1481 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1482 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1483 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1484 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1485 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1486 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1487 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1488 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1489 | /* XXX We should pass the function name along in the future. |
| 1490 | (winreg.c also wants to pass the function name.) |
| 1491 | This would however require an additional param to the |
| 1492 | Windows error object, which is non-trivial. |
| 1493 | */ |
| 1494 | errno = GetLastError(); |
| 1495 | if (filename) |
| 1496 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1497 | else |
| 1498 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1499 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1500 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1501 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1502 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1503 | { |
| 1504 | /* XXX - see win32_error for comments on 'function' */ |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1505 | if (filename) |
| 1506 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1507 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1508 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1509 | filename); |
| 1510 | else |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1511 | return PyErr_SetFromWindowsErr(err); |
| 1512 | } |
| 1513 | |
| 1514 | static PyObject * |
| 1515 | win32_error_object(const char* function, PyObject* filename) |
| 1516 | { |
| 1517 | errno = GetLastError(); |
| 1518 | return win32_error_object_err(function, filename, errno); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1519 | } |
| 1520 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1521 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1522 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1523 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1524 | posix_path_object_error(PyObject *path) |
| 1525 | { |
| 1526 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1527 | } |
| 1528 | |
| 1529 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1530 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1531 | { |
| 1532 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1533 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1534 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1535 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1536 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1537 | #endif |
| 1538 | } |
| 1539 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1540 | static PyObject * |
| 1541 | path_object_error2(PyObject *path, PyObject *path2) |
| 1542 | { |
| 1543 | #ifdef MS_WINDOWS |
| 1544 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1545 | PyExc_OSError, 0, path, path2); |
| 1546 | #else |
| 1547 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1548 | #endif |
| 1549 | } |
| 1550 | |
| 1551 | static PyObject * |
| 1552 | path_error(path_t *path) |
| 1553 | { |
| 1554 | return path_object_error(path->object); |
| 1555 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1556 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1557 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1558 | posix_path_error(path_t *path) |
| 1559 | { |
| 1560 | return posix_path_object_error(path->object); |
| 1561 | } |
| 1562 | |
| 1563 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1564 | path_error2(path_t *path, path_t *path2) |
| 1565 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1566 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1570 | /* POSIX generic methods */ |
| 1571 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1572 | static int |
| 1573 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1574 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1575 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1576 | int *pointer = (int *)p; |
| 1577 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1578 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1579 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1580 | *pointer = fd; |
| 1581 | return 1; |
| 1582 | } |
| 1583 | |
| 1584 | static PyObject * |
| 1585 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1586 | { |
| 1587 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1588 | int async_err = 0; |
| 1589 | |
| 1590 | do { |
| 1591 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1592 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1593 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1594 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1595 | Py_END_ALLOW_THREADS |
| 1596 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1597 | if (res != 0) |
| 1598 | return (!async_err) ? posix_error() : NULL; |
| 1599 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1600 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1601 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1602 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1603 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1604 | /* This is a reimplementation of the C library's chdir function, |
| 1605 | but one that produces Win32 errors instead of DOS error codes. |
| 1606 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1607 | it also needs to set "magic" environment variables indicating |
| 1608 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1609 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1610 | win32_wchdir(LPCWSTR path) |
| 1611 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1612 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1613 | int result; |
| 1614 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1615 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1616 | if(!SetCurrentDirectoryW(path)) |
| 1617 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1618 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1619 | if (!result) |
| 1620 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1621 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1622 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1623 | if (!new_path) { |
| 1624 | SetLastError(ERROR_OUTOFMEMORY); |
| 1625 | return FALSE; |
| 1626 | } |
| 1627 | result = GetCurrentDirectoryW(result, new_path); |
| 1628 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1629 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1630 | return FALSE; |
| 1631 | } |
| 1632 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1633 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1634 | wcsncmp(new_path, L"//", 2) == 0); |
| 1635 | if (!is_unc_like_path) { |
| 1636 | env[1] = new_path[0]; |
| 1637 | result = SetEnvironmentVariableW(env, new_path); |
| 1638 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1639 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1640 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1641 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1642 | } |
| 1643 | #endif |
| 1644 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1645 | #ifdef MS_WINDOWS |
| 1646 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1647 | - time stamps are restricted to second resolution |
| 1648 | - file modification times suffer from forth-and-back conversions between |
| 1649 | UTC and local time |
| 1650 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1651 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1652 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1653 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1654 | #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1655 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1656 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1657 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1658 | BY_HANDLE_FILE_INFORMATION *info, |
| 1659 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1660 | { |
| 1661 | memset(info, 0, sizeof(*info)); |
| 1662 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1663 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1664 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1665 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1666 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1667 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1668 | /* info->nNumberOfLinks = 1; */ |
| 1669 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1670 | *reparse_tag = pFileData->dwReserved0; |
| 1671 | else |
| 1672 | *reparse_tag = 0; |
| 1673 | } |
| 1674 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1675 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1676 | 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] | 1677 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1678 | HANDLE hFindFile; |
| 1679 | WIN32_FIND_DATAW FileData; |
| 1680 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1681 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1682 | return FALSE; |
| 1683 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1684 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1685 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1688 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1689 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1690 | BOOL traverse) |
| 1691 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1692 | HANDLE hFile; |
| 1693 | BY_HANDLE_FILE_INFORMATION fileInfo; |
| 1694 | FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 }; |
| 1695 | DWORD fileType, error; |
| 1696 | BOOL isUnhandledTag = FALSE; |
| 1697 | int retval = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1698 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1699 | DWORD access = FILE_READ_ATTRIBUTES; |
| 1700 | DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */ |
| 1701 | if (!traverse) { |
| 1702 | flags |= FILE_FLAG_OPEN_REPARSE_POINT; |
| 1703 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1704 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1705 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1706 | if (hFile == INVALID_HANDLE_VALUE) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1707 | /* Either the path doesn't exist, or the caller lacks access. */ |
| 1708 | error = GetLastError(); |
| 1709 | switch (error) { |
| 1710 | case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */ |
| 1711 | case ERROR_SHARING_VIOLATION: /* It's a paging file. */ |
| 1712 | /* Try reading the parent directory. */ |
| 1713 | if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) { |
| 1714 | /* Cannot read the parent directory. */ |
| 1715 | SetLastError(error); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1716 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1717 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1718 | if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1719 | if (traverse || |
| 1720 | !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1721 | /* The stat call has to traverse but cannot, so fail. */ |
| 1722 | SetLastError(error); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1723 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1724 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1725 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1726 | break; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1727 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1728 | case ERROR_INVALID_PARAMETER: |
| 1729 | /* \\.\con requires read or write access. */ |
| 1730 | hFile = CreateFileW(path, access | GENERIC_READ, |
| 1731 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1732 | OPEN_EXISTING, flags, NULL); |
| 1733 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1734 | SetLastError(error); |
| 1735 | return -1; |
| 1736 | } |
| 1737 | break; |
| 1738 | |
| 1739 | case ERROR_CANT_ACCESS_FILE: |
| 1740 | /* bpo37834: open unhandled reparse points if traverse fails. */ |
| 1741 | if (traverse) { |
| 1742 | traverse = FALSE; |
| 1743 | isUnhandledTag = TRUE; |
| 1744 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, |
| 1745 | flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 1746 | } |
| 1747 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1748 | SetLastError(error); |
| 1749 | return -1; |
| 1750 | } |
| 1751 | break; |
| 1752 | |
| 1753 | default: |
| 1754 | return -1; |
| 1755 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1756 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1757 | |
| 1758 | if (hFile != INVALID_HANDLE_VALUE) { |
| 1759 | /* Handle types other than files on disk. */ |
| 1760 | fileType = GetFileType(hFile); |
| 1761 | if (fileType != FILE_TYPE_DISK) { |
| 1762 | if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) { |
| 1763 | retval = -1; |
| 1764 | goto cleanup; |
| 1765 | } |
| 1766 | DWORD fileAttributes = GetFileAttributesW(path); |
| 1767 | memset(result, 0, sizeof(*result)); |
| 1768 | if (fileAttributes != INVALID_FILE_ATTRIBUTES && |
| 1769 | fileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 1770 | /* \\.\pipe\ or \\.\mailslot\ */ |
| 1771 | result->st_mode = _S_IFDIR; |
| 1772 | } else if (fileType == FILE_TYPE_CHAR) { |
| 1773 | /* \\.\nul */ |
| 1774 | result->st_mode = _S_IFCHR; |
| 1775 | } else if (fileType == FILE_TYPE_PIPE) { |
| 1776 | /* \\.\pipe\spam */ |
| 1777 | result->st_mode = _S_IFIFO; |
| 1778 | } |
| 1779 | /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */ |
| 1780 | goto cleanup; |
| 1781 | } |
| 1782 | |
| 1783 | /* Query the reparse tag, and traverse a non-link. */ |
| 1784 | if (!traverse) { |
| 1785 | if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, |
| 1786 | &tagInfo, sizeof(tagInfo))) { |
| 1787 | /* Allow devices that do not support FileAttributeTagInfo. */ |
| 1788 | switch (GetLastError()) { |
| 1789 | case ERROR_INVALID_PARAMETER: |
| 1790 | case ERROR_INVALID_FUNCTION: |
| 1791 | case ERROR_NOT_SUPPORTED: |
| 1792 | tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL; |
| 1793 | tagInfo.ReparseTag = 0; |
| 1794 | break; |
| 1795 | default: |
| 1796 | retval = -1; |
| 1797 | goto cleanup; |
| 1798 | } |
| 1799 | } else if (tagInfo.FileAttributes & |
| 1800 | FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1801 | if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1802 | if (isUnhandledTag) { |
| 1803 | /* Traversing previously failed for either this link |
| 1804 | or its target. */ |
| 1805 | SetLastError(ERROR_CANT_ACCESS_FILE); |
| 1806 | retval = -1; |
| 1807 | goto cleanup; |
| 1808 | } |
| 1809 | /* Traverse a non-link, but not if traversing already failed |
| 1810 | for an unhandled tag. */ |
| 1811 | } else if (!isUnhandledTag) { |
| 1812 | CloseHandle(hFile); |
| 1813 | return win32_xstat_impl(path, result, TRUE); |
| 1814 | } |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | if (!GetFileInformationByHandle(hFile, &fileInfo)) { |
| 1819 | switch (GetLastError()) { |
| 1820 | case ERROR_INVALID_PARAMETER: |
| 1821 | case ERROR_INVALID_FUNCTION: |
| 1822 | case ERROR_NOT_SUPPORTED: |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1823 | /* Volumes and physical disks are block devices, e.g. |
| 1824 | \\.\C: and \\.\PhysicalDrive0. */ |
| 1825 | memset(result, 0, sizeof(*result)); |
| 1826 | result->st_mode = 0x6000; /* S_IFBLK */ |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1827 | goto cleanup; |
| 1828 | } |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1829 | retval = -1; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1830 | goto cleanup; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result); |
| 1835 | |
| 1836 | if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1837 | /* Fix the file execute permissions. This hack sets S_IEXEC if |
| 1838 | the filename has an extension that is commonly used by files |
| 1839 | that CreateProcessW can execute. A real implementation calls |
| 1840 | GetSecurityInfo, OpenThreadToken/OpenProcessToken, and |
| 1841 | AccessCheck to check for generic read, write, and execute |
| 1842 | access. */ |
| 1843 | const wchar_t *fileExtension = wcsrchr(path, '.'); |
| 1844 | if (fileExtension) { |
| 1845 | if (_wcsicmp(fileExtension, L".exe") == 0 || |
| 1846 | _wcsicmp(fileExtension, L".bat") == 0 || |
| 1847 | _wcsicmp(fileExtension, L".cmd") == 0 || |
| 1848 | _wcsicmp(fileExtension, L".com") == 0) { |
| 1849 | result->st_mode |= 0111; |
| 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | cleanup: |
| 1855 | if (hFile != INVALID_HANDLE_VALUE) { |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1856 | /* Preserve last error if we are failing */ |
| 1857 | error = retval ? GetLastError() : 0; |
| 1858 | if (!CloseHandle(hFile)) { |
| 1859 | retval = -1; |
| 1860 | } else if (retval) { |
| 1861 | /* Restore last error */ |
| 1862 | SetLastError(error); |
| 1863 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | return retval; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1870 | 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] | 1871 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1872 | /* Protocol violation: we explicitly clear errno, instead of |
| 1873 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1874 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1875 | errno = 0; |
| 1876 | return code; |
| 1877 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1878 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1879 | |
| 1880 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1881 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1882 | default does not traverse symlinks and instead returns attributes for |
| 1883 | the symlink. |
| 1884 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1885 | Instead, we will open the file (which *does* traverse symlinks by default) |
| 1886 | and GetFileInformationByHandle(). */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1887 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1888 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1889 | 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] | 1890 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1891 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1894 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1895 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1896 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1897 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1900 | #endif /* MS_WINDOWS */ |
| 1901 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1902 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1903 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1904 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1905 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1906 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1907 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1908 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1909 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1910 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1911 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1912 | |
| 1913 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1914 | {"st_mode", "protection bits"}, |
| 1915 | {"st_ino", "inode"}, |
| 1916 | {"st_dev", "device"}, |
| 1917 | {"st_nlink", "number of hard links"}, |
| 1918 | {"st_uid", "user ID of owner"}, |
| 1919 | {"st_gid", "group ID of owner"}, |
| 1920 | {"st_size", "total size, in bytes"}, |
| 1921 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1922 | {NULL, "integer time of last access"}, |
| 1923 | {NULL, "integer time of last modification"}, |
| 1924 | {NULL, "integer time of last change"}, |
| 1925 | {"st_atime", "time of last access"}, |
| 1926 | {"st_mtime", "time of last modification"}, |
| 1927 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1928 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1929 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1930 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1931 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1932 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1933 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1934 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1935 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1936 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1937 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1938 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1939 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1940 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1941 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1942 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1943 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1944 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1945 | #endif |
| 1946 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1947 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1948 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1949 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1950 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1951 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1952 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1953 | {"st_fstype", "Type of filesystem"}, |
| 1954 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1955 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 1956 | {"st_reparse_tag", "Windows reparse tag"}, |
| 1957 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1958 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1959 | }; |
| 1960 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1961 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1962 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1963 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1964 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1965 | #endif |
| 1966 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1967 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1968 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1969 | #else |
| 1970 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1971 | #endif |
| 1972 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1973 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1974 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1975 | #else |
| 1976 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1977 | #endif |
| 1978 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1979 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1980 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1981 | #else |
| 1982 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1983 | #endif |
| 1984 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1985 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1986 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1987 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1988 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1989 | #endif |
| 1990 | |
| 1991 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 1992 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 1993 | #else |
| 1994 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 1995 | #endif |
| 1996 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1997 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1998 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 1999 | #else |
| 2000 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 2001 | #endif |
| 2002 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2003 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2004 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 2005 | #else |
| 2006 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 2007 | #endif |
| 2008 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2009 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2010 | #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1) |
| 2011 | #else |
| 2012 | #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX |
| 2013 | #endif |
| 2014 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2015 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2016 | "stat_result", /* name */ |
| 2017 | stat_result__doc__, /* doc */ |
| 2018 | stat_result_fields, |
| 2019 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2020 | }; |
| 2021 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2022 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2023 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2024 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2025 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2026 | 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] | 2027 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2028 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2029 | |
| 2030 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2031 | {"f_bsize", }, |
| 2032 | {"f_frsize", }, |
| 2033 | {"f_blocks", }, |
| 2034 | {"f_bfree", }, |
| 2035 | {"f_bavail", }, |
| 2036 | {"f_files", }, |
| 2037 | {"f_ffree", }, |
| 2038 | {"f_favail", }, |
| 2039 | {"f_flag", }, |
| 2040 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 2041 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2042 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2043 | }; |
| 2044 | |
| 2045 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2046 | "statvfs_result", /* name */ |
| 2047 | statvfs_result__doc__, /* doc */ |
| 2048 | statvfs_result_fields, |
| 2049 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2050 | }; |
| 2051 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2052 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2053 | PyDoc_STRVAR(waitid_result__doc__, |
| 2054 | "waitid_result: Result from waitid.\n\n\ |
| 2055 | This object may be accessed either as a tuple of\n\ |
| 2056 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2057 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2058 | \n\ |
| 2059 | See os.waitid for more information."); |
| 2060 | |
| 2061 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2062 | {"si_pid", }, |
| 2063 | {"si_uid", }, |
| 2064 | {"si_signo", }, |
| 2065 | {"si_status", }, |
| 2066 | {"si_code", }, |
| 2067 | {0} |
| 2068 | }; |
| 2069 | |
| 2070 | static PyStructSequence_Desc waitid_result_desc = { |
| 2071 | "waitid_result", /* name */ |
| 2072 | waitid_result__doc__, /* doc */ |
| 2073 | waitid_result_fields, |
| 2074 | 5 |
| 2075 | }; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2076 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2077 | static newfunc structseq_new; |
| 2078 | |
| 2079 | static PyObject * |
| 2080 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2081 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2082 | PyStructSequence *result; |
| 2083 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2084 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2085 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2086 | if (!result) |
| 2087 | return NULL; |
| 2088 | /* If we have been initialized from a tuple, |
| 2089 | st_?time might be set to None. Initialize it |
| 2090 | from the int slots. */ |
| 2091 | for (i = 7; i <= 9; i++) { |
| 2092 | if (result->ob_item[i+3] == Py_None) { |
| 2093 | Py_DECREF(Py_None); |
| 2094 | Py_INCREF(result->ob_item[i]); |
| 2095 | result->ob_item[i+3] = result->ob_item[i]; |
| 2096 | } |
| 2097 | } |
| 2098 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2101 | static int |
| 2102 | _posix_clear(PyObject *module) |
| 2103 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2104 | _posixstate *state = get_posix_state(module); |
| 2105 | Py_CLEAR(state->billion); |
| 2106 | Py_CLEAR(state->DirEntryType); |
| 2107 | Py_CLEAR(state->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2108 | #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] | 2109 | Py_CLEAR(state->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2110 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2111 | Py_CLEAR(state->StatResultType); |
| 2112 | Py_CLEAR(state->StatVFSResultType); |
| 2113 | Py_CLEAR(state->TerminalSizeType); |
| 2114 | Py_CLEAR(state->TimesResultType); |
| 2115 | Py_CLEAR(state->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2116 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2117 | Py_CLEAR(state->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2118 | #endif |
| 2119 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2120 | Py_CLEAR(state->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2121 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2122 | Py_CLEAR(state->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2123 | return 0; |
| 2124 | } |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2125 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2126 | static int |
| 2127 | _posix_traverse(PyObject *module, visitproc visit, void *arg) |
| 2128 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2129 | _posixstate *state = get_posix_state(module); |
| 2130 | Py_VISIT(state->billion); |
| 2131 | Py_VISIT(state->DirEntryType); |
| 2132 | Py_VISIT(state->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2133 | #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] | 2134 | Py_VISIT(state->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2135 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2136 | Py_VISIT(state->StatResultType); |
| 2137 | Py_VISIT(state->StatVFSResultType); |
| 2138 | Py_VISIT(state->TerminalSizeType); |
| 2139 | Py_VISIT(state->TimesResultType); |
| 2140 | Py_VISIT(state->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2141 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2142 | Py_VISIT(state->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2143 | #endif |
| 2144 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2145 | Py_VISIT(state->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2146 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2147 | Py_VISIT(state->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2148 | return 0; |
| 2149 | } |
| 2150 | |
| 2151 | static void |
| 2152 | _posix_free(void *module) |
| 2153 | { |
| 2154 | _posix_clear((PyObject *)module); |
| 2155 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2156 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2157 | static void |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2158 | 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] | 2159 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2160 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2161 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2162 | PyObject *s_in_ns = NULL; |
| 2163 | PyObject *ns_total = NULL; |
| 2164 | PyObject *float_s = NULL; |
| 2165 | |
| 2166 | if (!(s && ns_fractional)) |
| 2167 | goto exit; |
| 2168 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2169 | s_in_ns = PyNumber_Multiply(s, get_posix_state(module)->billion); |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2170 | if (!s_in_ns) |
| 2171 | goto exit; |
| 2172 | |
| 2173 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2174 | if (!ns_total) |
| 2175 | goto exit; |
| 2176 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2177 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2178 | if (!float_s) { |
| 2179 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | PyStructSequence_SET_ITEM(v, index, s); |
| 2183 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2184 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2185 | s = NULL; |
| 2186 | float_s = NULL; |
| 2187 | ns_total = NULL; |
| 2188 | exit: |
| 2189 | Py_XDECREF(s); |
| 2190 | Py_XDECREF(ns_fractional); |
| 2191 | Py_XDECREF(s_in_ns); |
| 2192 | Py_XDECREF(ns_total); |
| 2193 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2196 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2197 | (used by posix_stat() and posix_fstat()) */ |
| 2198 | static PyObject* |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2199 | _pystat_fromstructstat(PyObject *module, STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2200 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2201 | unsigned long ansec, mnsec, cnsec; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2202 | PyObject *StatResultType = get_posix_state(module)->StatResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2203 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2204 | if (v == NULL) |
| 2205 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2206 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2207 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2208 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2209 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2210 | #ifdef MS_WINDOWS |
| 2211 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2212 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2213 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2214 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2215 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2216 | #if defined(MS_WINDOWS) |
| 2217 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2218 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2219 | #else |
| 2220 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2221 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2222 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2223 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2224 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2225 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2226 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2227 | ansec = st->st_atim.tv_nsec; |
| 2228 | mnsec = st->st_mtim.tv_nsec; |
| 2229 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2230 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2231 | ansec = st->st_atimespec.tv_nsec; |
| 2232 | mnsec = st->st_mtimespec.tv_nsec; |
| 2233 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2234 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2235 | ansec = st->st_atime_nsec; |
| 2236 | mnsec = st->st_mtime_nsec; |
| 2237 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2238 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2239 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2240 | #endif |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2241 | fill_time(module, v, 7, st->st_atime, ansec); |
| 2242 | fill_time(module, v, 8, st->st_mtime, mnsec); |
| 2243 | fill_time(module, v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2244 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2245 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2246 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2247 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2248 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2249 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2250 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2251 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2252 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2253 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2254 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2255 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2256 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2257 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2258 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2259 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2260 | #endif |
| 2261 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2262 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2263 | PyObject *val; |
| 2264 | unsigned long bsec,bnsec; |
| 2265 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2266 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2267 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2268 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2269 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2270 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2271 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2272 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2273 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2274 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2275 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2276 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2277 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2278 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2279 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2280 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2281 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2282 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2283 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2284 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2285 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2286 | PyUnicode_FromString(st->st_fstype)); |
| 2287 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2288 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2289 | PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX, |
| 2290 | PyLong_FromUnsignedLong(st->st_reparse_tag)); |
| 2291 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2292 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2293 | if (PyErr_Occurred()) { |
| 2294 | Py_DECREF(v); |
| 2295 | return NULL; |
| 2296 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2297 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2298 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2301 | /* POSIX methods */ |
| 2302 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2303 | |
| 2304 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2305 | posix_do_stat(PyObject *module, const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2306 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2307 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2308 | STRUCT_STAT st; |
| 2309 | int result; |
| 2310 | |
| 2311 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2312 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2313 | return NULL; |
| 2314 | #endif |
| 2315 | |
| 2316 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2317 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2318 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2319 | return NULL; |
| 2320 | |
| 2321 | Py_BEGIN_ALLOW_THREADS |
| 2322 | if (path->fd != -1) |
| 2323 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2324 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2325 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2326 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2327 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2328 | result = win32_lstat(path->wide, &st); |
| 2329 | #else |
| 2330 | else |
| 2331 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2332 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2333 | result = LSTAT(path->narrow, &st); |
| 2334 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2335 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2336 | #ifdef HAVE_FSTATAT |
| 2337 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2338 | result = fstatat(dir_fd, path->narrow, &st, |
| 2339 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2340 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2341 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2342 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2343 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2344 | Py_END_ALLOW_THREADS |
| 2345 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2346 | if (result != 0) { |
| 2347 | return path_error(path); |
| 2348 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2349 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2350 | return _pystat_fromstructstat(module, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2353 | /*[python input] |
| 2354 | |
| 2355 | for s in """ |
| 2356 | |
| 2357 | FACCESSAT |
| 2358 | FCHMODAT |
| 2359 | FCHOWNAT |
| 2360 | FSTATAT |
| 2361 | LINKAT |
| 2362 | MKDIRAT |
| 2363 | MKFIFOAT |
| 2364 | MKNODAT |
| 2365 | OPENAT |
| 2366 | READLINKAT |
| 2367 | SYMLINKAT |
| 2368 | UNLINKAT |
| 2369 | |
| 2370 | """.strip().split(): |
| 2371 | s = s.strip() |
| 2372 | print(""" |
| 2373 | #ifdef HAVE_{s} |
| 2374 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2375 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2376 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2377 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2378 | """.rstrip().format(s=s)) |
| 2379 | |
| 2380 | for s in """ |
| 2381 | |
| 2382 | FCHDIR |
| 2383 | FCHMOD |
| 2384 | FCHOWN |
| 2385 | FDOPENDIR |
| 2386 | FEXECVE |
| 2387 | FPATHCONF |
| 2388 | FSTATVFS |
| 2389 | FTRUNCATE |
| 2390 | |
| 2391 | """.strip().split(): |
| 2392 | s = s.strip() |
| 2393 | print(""" |
| 2394 | #ifdef HAVE_{s} |
| 2395 | #define PATH_HAVE_{s} 1 |
| 2396 | #else |
| 2397 | #define PATH_HAVE_{s} 0 |
| 2398 | #endif |
| 2399 | |
| 2400 | """.rstrip().format(s=s)) |
| 2401 | [python start generated code]*/ |
| 2402 | |
| 2403 | #ifdef HAVE_FACCESSAT |
| 2404 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2405 | #else |
| 2406 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2407 | #endif |
| 2408 | |
| 2409 | #ifdef HAVE_FCHMODAT |
| 2410 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2411 | #else |
| 2412 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2413 | #endif |
| 2414 | |
| 2415 | #ifdef HAVE_FCHOWNAT |
| 2416 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2417 | #else |
| 2418 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2419 | #endif |
| 2420 | |
| 2421 | #ifdef HAVE_FSTATAT |
| 2422 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2423 | #else |
| 2424 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2425 | #endif |
| 2426 | |
| 2427 | #ifdef HAVE_LINKAT |
| 2428 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2429 | #else |
| 2430 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2431 | #endif |
| 2432 | |
| 2433 | #ifdef HAVE_MKDIRAT |
| 2434 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2435 | #else |
| 2436 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2437 | #endif |
| 2438 | |
| 2439 | #ifdef HAVE_MKFIFOAT |
| 2440 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2441 | #else |
| 2442 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2443 | #endif |
| 2444 | |
| 2445 | #ifdef HAVE_MKNODAT |
| 2446 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2447 | #else |
| 2448 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2449 | #endif |
| 2450 | |
| 2451 | #ifdef HAVE_OPENAT |
| 2452 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2453 | #else |
| 2454 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2455 | #endif |
| 2456 | |
| 2457 | #ifdef HAVE_READLINKAT |
| 2458 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2459 | #else |
| 2460 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2461 | #endif |
| 2462 | |
| 2463 | #ifdef HAVE_SYMLINKAT |
| 2464 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2465 | #else |
| 2466 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2467 | #endif |
| 2468 | |
| 2469 | #ifdef HAVE_UNLINKAT |
| 2470 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2471 | #else |
| 2472 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2473 | #endif |
| 2474 | |
| 2475 | #ifdef HAVE_FCHDIR |
| 2476 | #define PATH_HAVE_FCHDIR 1 |
| 2477 | #else |
| 2478 | #define PATH_HAVE_FCHDIR 0 |
| 2479 | #endif |
| 2480 | |
| 2481 | #ifdef HAVE_FCHMOD |
| 2482 | #define PATH_HAVE_FCHMOD 1 |
| 2483 | #else |
| 2484 | #define PATH_HAVE_FCHMOD 0 |
| 2485 | #endif |
| 2486 | |
| 2487 | #ifdef HAVE_FCHOWN |
| 2488 | #define PATH_HAVE_FCHOWN 1 |
| 2489 | #else |
| 2490 | #define PATH_HAVE_FCHOWN 0 |
| 2491 | #endif |
| 2492 | |
| 2493 | #ifdef HAVE_FDOPENDIR |
| 2494 | #define PATH_HAVE_FDOPENDIR 1 |
| 2495 | #else |
| 2496 | #define PATH_HAVE_FDOPENDIR 0 |
| 2497 | #endif |
| 2498 | |
| 2499 | #ifdef HAVE_FEXECVE |
| 2500 | #define PATH_HAVE_FEXECVE 1 |
| 2501 | #else |
| 2502 | #define PATH_HAVE_FEXECVE 0 |
| 2503 | #endif |
| 2504 | |
| 2505 | #ifdef HAVE_FPATHCONF |
| 2506 | #define PATH_HAVE_FPATHCONF 1 |
| 2507 | #else |
| 2508 | #define PATH_HAVE_FPATHCONF 0 |
| 2509 | #endif |
| 2510 | |
| 2511 | #ifdef HAVE_FSTATVFS |
| 2512 | #define PATH_HAVE_FSTATVFS 1 |
| 2513 | #else |
| 2514 | #define PATH_HAVE_FSTATVFS 0 |
| 2515 | #endif |
| 2516 | |
| 2517 | #ifdef HAVE_FTRUNCATE |
| 2518 | #define PATH_HAVE_FTRUNCATE 1 |
| 2519 | #else |
| 2520 | #define PATH_HAVE_FTRUNCATE 0 |
| 2521 | #endif |
| 2522 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2523 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2524 | #ifdef MS_WINDOWS |
| 2525 | #undef PATH_HAVE_FTRUNCATE |
| 2526 | #define PATH_HAVE_FTRUNCATE 1 |
| 2527 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2528 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2529 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2530 | |
| 2531 | class path_t_converter(CConverter): |
| 2532 | |
| 2533 | type = "path_t" |
| 2534 | impl_by_reference = True |
| 2535 | parse_by_reference = True |
| 2536 | |
| 2537 | converter = 'path_converter' |
| 2538 | |
| 2539 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2540 | # right now path_t doesn't support default values. |
| 2541 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2542 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2543 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2544 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2545 | if self.c_default not in (None, 'Py_None'): |
| 2546 | 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] | 2547 | |
| 2548 | self.nullable = nullable |
| 2549 | self.allow_fd = allow_fd |
| 2550 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2551 | def pre_render(self): |
| 2552 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2553 | if isinstance(value, str): |
| 2554 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2555 | return str(int(bool(value))) |
| 2556 | |
| 2557 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2558 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2559 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2560 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2561 | strify(self.nullable), |
| 2562 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2563 | ) |
| 2564 | |
| 2565 | def cleanup(self): |
| 2566 | return "path_cleanup(&" + self.name + ");\n" |
| 2567 | |
| 2568 | |
| 2569 | class dir_fd_converter(CConverter): |
| 2570 | type = 'int' |
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 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2573 | if self.default in (unspecified, None): |
| 2574 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2575 | if isinstance(requires, str): |
| 2576 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2577 | else: |
| 2578 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2579 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2580 | class fildes_converter(CConverter): |
| 2581 | type = 'int' |
| 2582 | converter = 'fildes_converter' |
| 2583 | |
| 2584 | class uid_t_converter(CConverter): |
| 2585 | type = "uid_t" |
| 2586 | converter = '_Py_Uid_Converter' |
| 2587 | |
| 2588 | class gid_t_converter(CConverter): |
| 2589 | type = "gid_t" |
| 2590 | converter = '_Py_Gid_Converter' |
| 2591 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2592 | class dev_t_converter(CConverter): |
| 2593 | type = 'dev_t' |
| 2594 | converter = '_Py_Dev_Converter' |
| 2595 | |
| 2596 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2597 | type = 'dev_t' |
| 2598 | conversion_fn = '_PyLong_FromDev' |
| 2599 | unsigned_cast = '(dev_t)' |
| 2600 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2601 | class FSConverter_converter(CConverter): |
| 2602 | type = 'PyObject *' |
| 2603 | converter = 'PyUnicode_FSConverter' |
| 2604 | def converter_init(self): |
| 2605 | if self.default is not unspecified: |
| 2606 | fail("FSConverter_converter does not support default values") |
| 2607 | self.c_default = 'NULL' |
| 2608 | |
| 2609 | def cleanup(self): |
| 2610 | return "Py_XDECREF(" + self.name + ");\n" |
| 2611 | |
| 2612 | class pid_t_converter(CConverter): |
| 2613 | type = 'pid_t' |
| 2614 | format_unit = '" _Py_PARSE_PID "' |
| 2615 | |
| 2616 | class idtype_t_converter(int_converter): |
| 2617 | type = 'idtype_t' |
| 2618 | |
| 2619 | class id_t_converter(CConverter): |
| 2620 | type = 'id_t' |
| 2621 | format_unit = '" _Py_PARSE_PID "' |
| 2622 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2623 | class intptr_t_converter(CConverter): |
| 2624 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2625 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2626 | |
| 2627 | class Py_off_t_converter(CConverter): |
| 2628 | type = 'Py_off_t' |
| 2629 | converter = 'Py_off_t_converter' |
| 2630 | |
| 2631 | class Py_off_t_return_converter(long_return_converter): |
| 2632 | type = 'Py_off_t' |
| 2633 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2634 | |
| 2635 | class path_confname_converter(CConverter): |
| 2636 | type="int" |
| 2637 | converter="conv_path_confname" |
| 2638 | |
| 2639 | class confstr_confname_converter(path_confname_converter): |
| 2640 | converter='conv_confstr_confname' |
| 2641 | |
| 2642 | class sysconf_confname_converter(path_confname_converter): |
| 2643 | converter="conv_sysconf_confname" |
| 2644 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2645 | [python start generated code]*/ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2646 | /*[python end generated code: output=da39a3ee5e6b4b0d input=f1c8ae8d744f6c8b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2647 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2648 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2649 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2650 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2651 | |
| 2652 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2653 | 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] | 2654 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2655 | |
| 2656 | * |
| 2657 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2658 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2659 | If not None, it should be a file descriptor open to a directory, |
| 2660 | and path should be a relative string; path will then be relative to |
| 2661 | that directory. |
| 2662 | |
| 2663 | follow_symlinks: bool = True |
| 2664 | If False, and the last element of the path is a symbolic link, |
| 2665 | stat will examine the symbolic link itself instead of the file |
| 2666 | the link points to. |
| 2667 | |
| 2668 | Perform a stat system call on the given path. |
| 2669 | |
| 2670 | dir_fd and follow_symlinks may not be implemented |
| 2671 | on your platform. If they are unavailable, using them will raise a |
| 2672 | NotImplementedError. |
| 2673 | |
| 2674 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2675 | an open file descriptor. |
| 2676 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2677 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2678 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2679 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2680 | 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] | 2681 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2682 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2683 | return posix_do_stat(module, "stat", path, dir_fd, follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2684 | } |
| 2685 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2686 | |
| 2687 | /*[clinic input] |
| 2688 | os.lstat |
| 2689 | |
| 2690 | path : path_t |
| 2691 | |
| 2692 | * |
| 2693 | |
| 2694 | dir_fd : dir_fd(requires='fstatat') = None |
| 2695 | |
| 2696 | Perform a stat system call on the given path, without following symbolic links. |
| 2697 | |
| 2698 | Like stat(), but do not follow symbolic links. |
| 2699 | Equivalent to stat(path, follow_symlinks=False). |
| 2700 | [clinic start generated code]*/ |
| 2701 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2702 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2703 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2704 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2705 | { |
| 2706 | int follow_symlinks = 0; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2707 | return posix_do_stat(module, "lstat", path, dir_fd, follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2708 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2709 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2710 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2711 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2712 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2713 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2714 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2715 | 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] | 2716 | |
| 2717 | mode: int |
| 2718 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2719 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2720 | |
| 2721 | * |
| 2722 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2723 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2724 | If not None, it should be a file descriptor open to a directory, |
| 2725 | and path should be relative; path will then be relative to that |
| 2726 | directory. |
| 2727 | |
| 2728 | effective_ids: bool = False |
| 2729 | If True, access will use the effective uid/gid instead of |
| 2730 | the real uid/gid. |
| 2731 | |
| 2732 | follow_symlinks: bool = True |
| 2733 | If False, and the last element of the path is a symbolic link, |
| 2734 | access will examine the symbolic link itself instead of the file |
| 2735 | the link points to. |
| 2736 | |
| 2737 | Use the real uid/gid to test for access to a path. |
| 2738 | |
| 2739 | {parameters} |
| 2740 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2741 | on your platform. If they are unavailable, using them will raise a |
| 2742 | NotImplementedError. |
| 2743 | |
| 2744 | Note that most operations will use the effective uid/gid, therefore this |
| 2745 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2746 | has the specified access to the path. |
| 2747 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2748 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2749 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2750 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2751 | 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] | 2752 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2753 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2754 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2755 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2756 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2757 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2758 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2759 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2760 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2761 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2762 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2763 | #ifndef HAVE_FACCESSAT |
| 2764 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2765 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2766 | |
| 2767 | if (effective_ids) { |
| 2768 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2769 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2770 | } |
| 2771 | #endif |
| 2772 | |
| 2773 | #ifdef MS_WINDOWS |
| 2774 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2775 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2776 | Py_END_ALLOW_THREADS |
| 2777 | |
| 2778 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2779 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2780 | * * we didn't get a -1, and |
| 2781 | * * write access wasn't requested, |
| 2782 | * * or the file isn't read-only, |
| 2783 | * * or it's a directory. |
| 2784 | * (Directories cannot be read-only on Windows.) |
| 2785 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2786 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2787 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2788 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2789 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2790 | #else |
| 2791 | |
| 2792 | Py_BEGIN_ALLOW_THREADS |
| 2793 | #ifdef HAVE_FACCESSAT |
| 2794 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2795 | effective_ids || |
| 2796 | !follow_symlinks) { |
| 2797 | int flags = 0; |
| 2798 | if (!follow_symlinks) |
| 2799 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2800 | if (effective_ids) |
| 2801 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2802 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2803 | } |
| 2804 | else |
| 2805 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2806 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2807 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2808 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2809 | #endif |
| 2810 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2811 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2814 | #ifndef F_OK |
| 2815 | #define F_OK 0 |
| 2816 | #endif |
| 2817 | #ifndef R_OK |
| 2818 | #define R_OK 4 |
| 2819 | #endif |
| 2820 | #ifndef W_OK |
| 2821 | #define W_OK 2 |
| 2822 | #endif |
| 2823 | #ifndef X_OK |
| 2824 | #define X_OK 1 |
| 2825 | #endif |
| 2826 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2827 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2828 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2829 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2830 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2831 | |
| 2832 | fd: int |
| 2833 | Integer file descriptor handle. |
| 2834 | |
| 2835 | / |
| 2836 | |
| 2837 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2838 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2839 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2840 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2841 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2842 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2843 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2844 | |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2845 | long size = sysconf(_SC_TTY_NAME_MAX); |
| 2846 | if (size == -1) { |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2847 | return posix_error(); |
| 2848 | } |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2849 | char *buffer = (char *)PyMem_RawMalloc(size); |
| 2850 | if (buffer == NULL) { |
| 2851 | return PyErr_NoMemory(); |
| 2852 | } |
| 2853 | int ret = ttyname_r(fd, buffer, size); |
| 2854 | if (ret != 0) { |
| 2855 | PyMem_RawFree(buffer); |
| 2856 | errno = ret; |
| 2857 | return posix_error(); |
| 2858 | } |
| 2859 | PyObject *res = PyUnicode_DecodeFSDefault(buffer); |
| 2860 | PyMem_RawFree(buffer); |
| 2861 | return res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2862 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2863 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2864 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2865 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2866 | /*[clinic input] |
| 2867 | os.ctermid |
| 2868 | |
| 2869 | Return the name of the controlling terminal for this process. |
| 2870 | [clinic start generated code]*/ |
| 2871 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2872 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2873 | os_ctermid_impl(PyObject *module) |
| 2874 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2875 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2876 | char *ret; |
| 2877 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2878 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2879 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2880 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2881 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2882 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2883 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2884 | if (ret == NULL) |
| 2885 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2886 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2887 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2888 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2889 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2890 | |
| 2891 | /*[clinic input] |
| 2892 | os.chdir |
| 2893 | |
| 2894 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2895 | |
| 2896 | Change the current working directory to the specified path. |
| 2897 | |
| 2898 | path may always be specified as a string. |
| 2899 | On some platforms, path may also be specified as an open file descriptor. |
| 2900 | If this functionality is unavailable, using it raises an exception. |
| 2901 | [clinic start generated code]*/ |
| 2902 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2903 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2904 | os_chdir_impl(PyObject *module, path_t *path) |
| 2905 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2906 | { |
| 2907 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2908 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 2909 | if (PySys_Audit("os.chdir", "(O)", path->object) < 0) { |
| 2910 | return NULL; |
| 2911 | } |
| 2912 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2913 | Py_BEGIN_ALLOW_THREADS |
| 2914 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2915 | /* on unix, success = 0, on windows, success = !0 */ |
| 2916 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2917 | #else |
| 2918 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2919 | if (path->fd != -1) |
| 2920 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2921 | else |
| 2922 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2923 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2924 | #endif |
| 2925 | Py_END_ALLOW_THREADS |
| 2926 | |
| 2927 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2928 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2929 | } |
| 2930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2931 | Py_RETURN_NONE; |
| 2932 | } |
| 2933 | |
| 2934 | |
| 2935 | #ifdef HAVE_FCHDIR |
| 2936 | /*[clinic input] |
| 2937 | os.fchdir |
| 2938 | |
| 2939 | fd: fildes |
| 2940 | |
| 2941 | Change to the directory of the given file descriptor. |
| 2942 | |
| 2943 | fd must be opened on a directory, not a file. |
| 2944 | Equivalent to os.chdir(fd). |
| 2945 | |
| 2946 | [clinic start generated code]*/ |
| 2947 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2948 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2949 | os_fchdir_impl(PyObject *module, int fd) |
| 2950 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2951 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 2952 | if (PySys_Audit("os.chdir", "(i)", fd) < 0) { |
| 2953 | return NULL; |
| 2954 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2955 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2956 | } |
| 2957 | #endif /* HAVE_FCHDIR */ |
| 2958 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2959 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2960 | /*[clinic input] |
| 2961 | os.chmod |
| 2962 | |
| 2963 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2964 | 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] | 2965 | On some platforms, path may also be specified as an open file descriptor. |
| 2966 | If this functionality is unavailable, using it raises an exception. |
| 2967 | |
| 2968 | mode: int |
| 2969 | Operating-system mode bitfield. |
| 2970 | |
| 2971 | * |
| 2972 | |
| 2973 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2974 | If not None, it should be a file descriptor open to a directory, |
| 2975 | and path should be relative; path will then be relative to that |
| 2976 | directory. |
| 2977 | |
| 2978 | follow_symlinks: bool = True |
| 2979 | If False, and the last element of the path is a symbolic link, |
| 2980 | chmod will modify the symbolic link itself instead of the file |
| 2981 | the link points to. |
| 2982 | |
| 2983 | Change the access permissions of a file. |
| 2984 | |
| 2985 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2986 | an open file descriptor. |
| 2987 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 2988 | If they are unavailable, using them will raise a NotImplementedError. |
| 2989 | |
| 2990 | [clinic start generated code]*/ |
| 2991 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2992 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2993 | 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] | 2994 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2995 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2996 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2997 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2998 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2999 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3000 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3001 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3002 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3003 | #ifdef HAVE_FCHMODAT |
| 3004 | int fchmodat_nofollow_unsupported = 0; |
| 3005 | #endif |
| 3006 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3007 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 3008 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3009 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3010 | #endif |
| 3011 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3012 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, |
| 3013 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3014 | return NULL; |
| 3015 | } |
| 3016 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3017 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3018 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3019 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 3020 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3021 | result = 0; |
| 3022 | else { |
| 3023 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3024 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 3025 | else |
| 3026 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3027 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3028 | } |
| 3029 | Py_END_ALLOW_THREADS |
| 3030 | |
| 3031 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3032 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3033 | } |
| 3034 | #else /* MS_WINDOWS */ |
| 3035 | Py_BEGIN_ALLOW_THREADS |
| 3036 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3037 | if (path->fd != -1) |
| 3038 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3039 | else |
| 3040 | #endif |
| 3041 | #ifdef HAVE_LCHMOD |
| 3042 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3043 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3044 | else |
| 3045 | #endif |
| 3046 | #ifdef HAVE_FCHMODAT |
| 3047 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 3048 | /* |
| 3049 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 3050 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3051 | * and then says it isn't implemented yet. |
| 3052 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3053 | * |
| 3054 | * Once it is supported, os.chmod will automatically |
| 3055 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 3056 | * Until then, we need to be careful what exception we raise. |
| 3057 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3058 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3059 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3060 | /* |
| 3061 | * But wait! We can't throw the exception without allowing threads, |
| 3062 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 3063 | */ |
| 3064 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3065 | result && |
| 3066 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 3067 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3068 | } |
| 3069 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3070 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3071 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3072 | Py_END_ALLOW_THREADS |
| 3073 | |
| 3074 | if (result) { |
| 3075 | #ifdef HAVE_FCHMODAT |
| 3076 | if (fchmodat_nofollow_unsupported) { |
| 3077 | if (dir_fd != DEFAULT_DIR_FD) |
| 3078 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3079 | dir_fd, follow_symlinks); |
| 3080 | else |
| 3081 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 3082 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3083 | } |
| 3084 | else |
| 3085 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3086 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3087 | } |
| 3088 | #endif |
| 3089 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3090 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3093 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3094 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3095 | /*[clinic input] |
| 3096 | os.fchmod |
| 3097 | |
| 3098 | fd: int |
| 3099 | mode: int |
| 3100 | |
| 3101 | Change the access permissions of the file given by file descriptor fd. |
| 3102 | |
| 3103 | Equivalent to os.chmod(fd, mode). |
| 3104 | [clinic start generated code]*/ |
| 3105 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3106 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3107 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 3108 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3109 | { |
| 3110 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3111 | int async_err = 0; |
| 3112 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3113 | if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) { |
| 3114 | return NULL; |
| 3115 | } |
| 3116 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3117 | do { |
| 3118 | Py_BEGIN_ALLOW_THREADS |
| 3119 | res = fchmod(fd, mode); |
| 3120 | Py_END_ALLOW_THREADS |
| 3121 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3122 | if (res != 0) |
| 3123 | return (!async_err) ? posix_error() : NULL; |
| 3124 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3125 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3126 | } |
| 3127 | #endif /* HAVE_FCHMOD */ |
| 3128 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3129 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3130 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3131 | /*[clinic input] |
| 3132 | os.lchmod |
| 3133 | |
| 3134 | path: path_t |
| 3135 | mode: int |
| 3136 | |
| 3137 | Change the access permissions of a file, without following symbolic links. |
| 3138 | |
| 3139 | If path is a symlink, this affects the link itself rather than the target. |
| 3140 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 3141 | [clinic start generated code]*/ |
| 3142 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3143 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3144 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 3145 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3146 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3147 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3148 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) { |
| 3149 | return NULL; |
| 3150 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3151 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3152 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3153 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3154 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3155 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3156 | return NULL; |
| 3157 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3158 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3159 | } |
| 3160 | #endif /* HAVE_LCHMOD */ |
| 3161 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3162 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3163 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3164 | /*[clinic input] |
| 3165 | os.chflags |
| 3166 | |
| 3167 | path: path_t |
| 3168 | flags: unsigned_long(bitwise=True) |
| 3169 | follow_symlinks: bool=True |
| 3170 | |
| 3171 | Set file flags. |
| 3172 | |
| 3173 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3174 | link, chflags will change flags on the symbolic link itself instead of the |
| 3175 | file the link points to. |
| 3176 | follow_symlinks may not be implemented on your platform. If it is |
| 3177 | unavailable, using it will raise a NotImplementedError. |
| 3178 | |
| 3179 | [clinic start generated code]*/ |
| 3180 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3181 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3182 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3183 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3184 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3185 | { |
| 3186 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3187 | |
| 3188 | #ifndef HAVE_LCHFLAGS |
| 3189 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3190 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3191 | #endif |
| 3192 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3193 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3194 | return NULL; |
| 3195 | } |
| 3196 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3197 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3198 | #ifdef HAVE_LCHFLAGS |
| 3199 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3200 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3201 | else |
| 3202 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3203 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3204 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3205 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3206 | if (result) |
| 3207 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3208 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3209 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3210 | } |
| 3211 | #endif /* HAVE_CHFLAGS */ |
| 3212 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3213 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3214 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3215 | /*[clinic input] |
| 3216 | os.lchflags |
| 3217 | |
| 3218 | path: path_t |
| 3219 | flags: unsigned_long(bitwise=True) |
| 3220 | |
| 3221 | Set file flags. |
| 3222 | |
| 3223 | This function will not follow symbolic links. |
| 3224 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3225 | [clinic start generated code]*/ |
| 3226 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3227 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3228 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3229 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3230 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3231 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3232 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3233 | return NULL; |
| 3234 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3235 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3236 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3237 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3238 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3239 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3240 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3241 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3242 | } |
| 3243 | #endif /* HAVE_LCHFLAGS */ |
| 3244 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3245 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3246 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3247 | /*[clinic input] |
| 3248 | os.chroot |
| 3249 | path: path_t |
| 3250 | |
| 3251 | Change root directory to path. |
| 3252 | |
| 3253 | [clinic start generated code]*/ |
| 3254 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3255 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3256 | os_chroot_impl(PyObject *module, path_t *path) |
| 3257 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3258 | { |
| 3259 | int res; |
| 3260 | Py_BEGIN_ALLOW_THREADS |
| 3261 | res = chroot(path->narrow); |
| 3262 | Py_END_ALLOW_THREADS |
| 3263 | if (res < 0) |
| 3264 | return path_error(path); |
| 3265 | Py_RETURN_NONE; |
| 3266 | } |
| 3267 | #endif /* HAVE_CHROOT */ |
| 3268 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3269 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3270 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3271 | /*[clinic input] |
| 3272 | os.fsync |
| 3273 | |
| 3274 | fd: fildes |
| 3275 | |
| 3276 | Force write of fd to disk. |
| 3277 | [clinic start generated code]*/ |
| 3278 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3279 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3280 | os_fsync_impl(PyObject *module, int fd) |
| 3281 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3282 | { |
| 3283 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3284 | } |
| 3285 | #endif /* HAVE_FSYNC */ |
| 3286 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3287 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3288 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3289 | /*[clinic input] |
| 3290 | os.sync |
| 3291 | |
| 3292 | Force write of everything to disk. |
| 3293 | [clinic start generated code]*/ |
| 3294 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3295 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3296 | os_sync_impl(PyObject *module) |
| 3297 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3298 | { |
| 3299 | Py_BEGIN_ALLOW_THREADS |
| 3300 | sync(); |
| 3301 | Py_END_ALLOW_THREADS |
| 3302 | Py_RETURN_NONE; |
| 3303 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3304 | #endif /* HAVE_SYNC */ |
| 3305 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3306 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3307 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3308 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3309 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3310 | #endif |
| 3311 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3312 | /*[clinic input] |
| 3313 | os.fdatasync |
| 3314 | |
| 3315 | fd: fildes |
| 3316 | |
| 3317 | Force write of fd to disk without forcing update of metadata. |
| 3318 | [clinic start generated code]*/ |
| 3319 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3320 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3321 | os_fdatasync_impl(PyObject *module, int fd) |
| 3322 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3323 | { |
| 3324 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3325 | } |
| 3326 | #endif /* HAVE_FDATASYNC */ |
| 3327 | |
| 3328 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3329 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3330 | /*[clinic input] |
| 3331 | os.chown |
| 3332 | |
| 3333 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3334 | 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] | 3335 | |
| 3336 | uid: uid_t |
| 3337 | |
| 3338 | gid: gid_t |
| 3339 | |
| 3340 | * |
| 3341 | |
| 3342 | dir_fd : dir_fd(requires='fchownat') = None |
| 3343 | If not None, it should be a file descriptor open to a directory, |
| 3344 | and path should be relative; path will then be relative to that |
| 3345 | directory. |
| 3346 | |
| 3347 | follow_symlinks: bool = True |
| 3348 | If False, and the last element of the path is a symbolic link, |
| 3349 | stat will examine the symbolic link itself instead of the file |
| 3350 | the link points to. |
| 3351 | |
| 3352 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3353 | |
| 3354 | path may always be specified as a string. |
| 3355 | On some platforms, path may also be specified as an open file descriptor. |
| 3356 | If this functionality is unavailable, using it raises an exception. |
| 3357 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3358 | and path should be relative; path will then be relative to that directory. |
| 3359 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3360 | link, chown will modify the symbolic link itself instead of the file the |
| 3361 | link points to. |
| 3362 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3363 | an open file descriptor. |
| 3364 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3365 | If they are unavailable, using them will raise a NotImplementedError. |
| 3366 | |
| 3367 | [clinic start generated code]*/ |
| 3368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3369 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3370 | os_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] | 3371 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3372 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3373 | { |
| 3374 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3375 | |
| 3376 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3377 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3378 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3379 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3380 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3381 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3382 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3383 | |
| 3384 | #ifdef __APPLE__ |
| 3385 | /* |
| 3386 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3387 | * (But we still have an lchown symbol because of weak-linking.) |
| 3388 | * It doesn't have fchownat either. So there's no possibility |
| 3389 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3390 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3391 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3392 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3393 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3394 | } |
| 3395 | #endif |
| 3396 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3397 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, |
| 3398 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3399 | return NULL; |
| 3400 | } |
| 3401 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3402 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3403 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3404 | if (path->fd != -1) |
| 3405 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3406 | else |
| 3407 | #endif |
| 3408 | #ifdef HAVE_LCHOWN |
| 3409 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3410 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3411 | else |
| 3412 | #endif |
| 3413 | #ifdef HAVE_FCHOWNAT |
| 3414 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3415 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3416 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3417 | else |
| 3418 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3419 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3420 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3421 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3422 | if (result) |
| 3423 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3424 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3425 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3426 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3427 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3428 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3429 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3430 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3431 | /*[clinic input] |
| 3432 | os.fchown |
| 3433 | |
| 3434 | fd: int |
| 3435 | uid: uid_t |
| 3436 | gid: gid_t |
| 3437 | |
| 3438 | Change the owner and group id of the file specified by file descriptor. |
| 3439 | |
| 3440 | Equivalent to os.chown(fd, uid, gid). |
| 3441 | |
| 3442 | [clinic start generated code]*/ |
| 3443 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3444 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3445 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3446 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3447 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3448 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3449 | int async_err = 0; |
| 3450 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3451 | if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) { |
| 3452 | return NULL; |
| 3453 | } |
| 3454 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3455 | do { |
| 3456 | Py_BEGIN_ALLOW_THREADS |
| 3457 | res = fchown(fd, uid, gid); |
| 3458 | Py_END_ALLOW_THREADS |
| 3459 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3460 | if (res != 0) |
| 3461 | return (!async_err) ? posix_error() : NULL; |
| 3462 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3463 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3464 | } |
| 3465 | #endif /* HAVE_FCHOWN */ |
| 3466 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3467 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3468 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3469 | /*[clinic input] |
| 3470 | os.lchown |
| 3471 | |
| 3472 | path : path_t |
| 3473 | uid: uid_t |
| 3474 | gid: gid_t |
| 3475 | |
| 3476 | Change the owner and group id of path to the numeric uid and gid. |
| 3477 | |
| 3478 | This function will not follow symbolic links. |
| 3479 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3480 | [clinic start generated code]*/ |
| 3481 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3482 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3483 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3484 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3485 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3486 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3487 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) { |
| 3488 | return NULL; |
| 3489 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3490 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3491 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3492 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3493 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3494 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3495 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3496 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3497 | } |
| 3498 | #endif /* HAVE_LCHOWN */ |
| 3499 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3500 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3501 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3502 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3503 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3504 | #ifdef MS_WINDOWS |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3505 | wchar_t wbuf[MAXPATHLEN]; |
| 3506 | wchar_t *wbuf2 = wbuf; |
| 3507 | DWORD len; |
| 3508 | |
| 3509 | Py_BEGIN_ALLOW_THREADS |
| 3510 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
| 3511 | /* If the buffer is large enough, len does not include the |
| 3512 | terminating \0. If the buffer is too small, len includes |
| 3513 | the space needed for the terminator. */ |
| 3514 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | ec3e20a | 2019-06-28 18:01:59 +0200 | [diff] [blame] | 3515 | if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3516 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3517 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3518 | else { |
| 3519 | wbuf2 = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3520 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3521 | if (wbuf2) { |
| 3522 | len = GetCurrentDirectoryW(len, wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3523 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3524 | } |
| 3525 | Py_END_ALLOW_THREADS |
| 3526 | |
| 3527 | if (!wbuf2) { |
| 3528 | PyErr_NoMemory(); |
| 3529 | return NULL; |
| 3530 | } |
| 3531 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3532 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3533 | PyMem_RawFree(wbuf2); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3534 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3535 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3536 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3537 | PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3538 | if (wbuf2 != wbuf) { |
| 3539 | PyMem_RawFree(wbuf2); |
| 3540 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3541 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3542 | if (use_bytes) { |
| 3543 | if (resobj == NULL) { |
| 3544 | return NULL; |
| 3545 | } |
| 3546 | Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj)); |
| 3547 | } |
| 3548 | |
| 3549 | return resobj; |
| 3550 | #else |
| 3551 | const size_t chunk = 1024; |
| 3552 | |
| 3553 | char *buf = NULL; |
| 3554 | char *cwd = NULL; |
| 3555 | size_t buflen = 0; |
| 3556 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3557 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3558 | do { |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3559 | char *newbuf; |
| 3560 | if (buflen <= PY_SSIZE_T_MAX - chunk) { |
| 3561 | buflen += chunk; |
| 3562 | newbuf = PyMem_RawRealloc(buf, buflen); |
| 3563 | } |
| 3564 | else { |
| 3565 | newbuf = NULL; |
| 3566 | } |
| 3567 | if (newbuf == NULL) { |
| 3568 | PyMem_RawFree(buf); |
| 3569 | buf = NULL; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3570 | break; |
| 3571 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3572 | buf = newbuf; |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3573 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3574 | cwd = getcwd(buf, buflen); |
| 3575 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3576 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3577 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3578 | if (buf == NULL) { |
| 3579 | return PyErr_NoMemory(); |
| 3580 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3581 | if (cwd == NULL) { |
| 3582 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3583 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3584 | } |
| 3585 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3586 | PyObject *obj; |
| 3587 | if (use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3588 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3589 | } |
| 3590 | else { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3591 | obj = PyUnicode_DecodeFSDefault(buf); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3592 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3593 | PyMem_RawFree(buf); |
| 3594 | |
| 3595 | return obj; |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3596 | #endif /* !MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3597 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3599 | |
| 3600 | /*[clinic input] |
| 3601 | os.getcwd |
| 3602 | |
| 3603 | Return a unicode string representing the current working directory. |
| 3604 | [clinic start generated code]*/ |
| 3605 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3606 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3607 | os_getcwd_impl(PyObject *module) |
| 3608 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3609 | { |
| 3610 | return posix_getcwd(0); |
| 3611 | } |
| 3612 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3613 | |
| 3614 | /*[clinic input] |
| 3615 | os.getcwdb |
| 3616 | |
| 3617 | Return a bytes string representing the current working directory. |
| 3618 | [clinic start generated code]*/ |
| 3619 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3620 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3621 | os_getcwdb_impl(PyObject *module) |
| 3622 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3623 | { |
| 3624 | return posix_getcwd(1); |
| 3625 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3626 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3627 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3628 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3629 | #define HAVE_LINK 1 |
| 3630 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3631 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3632 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3633 | /*[clinic input] |
| 3634 | |
| 3635 | os.link |
| 3636 | |
| 3637 | src : path_t |
| 3638 | dst : path_t |
| 3639 | * |
| 3640 | src_dir_fd : dir_fd = None |
| 3641 | dst_dir_fd : dir_fd = None |
| 3642 | follow_symlinks: bool = True |
| 3643 | |
| 3644 | Create a hard link to a file. |
| 3645 | |
| 3646 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3647 | descriptor open to a directory, and the respective path string (src or dst) |
| 3648 | should be relative; the path will then be relative to that directory. |
| 3649 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3650 | link, link will create a link to the symbolic link itself instead of the |
| 3651 | file the link points to. |
| 3652 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3653 | platform. If they are unavailable, using them will raise a |
| 3654 | NotImplementedError. |
| 3655 | [clinic start generated code]*/ |
| 3656 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3657 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3658 | 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] | 3659 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3660 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3661 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3662 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3663 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3664 | #else |
| 3665 | int result; |
| 3666 | #endif |
| 3667 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3668 | #ifndef HAVE_LINKAT |
| 3669 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3670 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3671 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3672 | } |
| 3673 | #endif |
| 3674 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3675 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3676 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3677 | PyErr_SetString(PyExc_NotImplementedError, |
| 3678 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3679 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3680 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3681 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3682 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3683 | if (PySys_Audit("os.link", "OOii", src->object, dst->object, |
| 3684 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 3685 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 3686 | return NULL; |
| 3687 | } |
| 3688 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3689 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3690 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3691 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3692 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3693 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3694 | if (!result) |
| 3695 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3696 | #else |
| 3697 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3698 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3699 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3700 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3701 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3702 | result = linkat(src_dir_fd, src->narrow, |
| 3703 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3704 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3705 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3706 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3707 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3708 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3709 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3710 | if (result) |
| 3711 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3712 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3713 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3714 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3715 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3716 | #endif |
| 3717 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3718 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3719 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3720 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3721 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3722 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3723 | PyObject *v; |
| 3724 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3725 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3726 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3727 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3728 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3729 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3730 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3731 | WIN32_FIND_DATAW wFileData; |
| 3732 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3733 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3734 | if (!path->wide) { /* Default arg: "." */ |
| 3735 | po_wchars = L"."; |
| 3736 | len = 1; |
| 3737 | } else { |
| 3738 | po_wchars = path->wide; |
| 3739 | len = wcslen(path->wide); |
| 3740 | } |
| 3741 | /* The +5 is so we can append "\\*.*\0" */ |
| 3742 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3743 | if (!wnamebuf) { |
| 3744 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3745 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3746 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3747 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3748 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3749 | wchar_t wch = wnamebuf[len-1]; |
| 3750 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3751 | wnamebuf[len++] = SEP; |
| 3752 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3753 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3754 | if ((list = PyList_New(0)) == NULL) { |
| 3755 | goto exit; |
| 3756 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3757 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3758 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3759 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3760 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3761 | int error = GetLastError(); |
| 3762 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3763 | goto exit; |
| 3764 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3765 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3766 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3767 | } |
| 3768 | do { |
| 3769 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3770 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3771 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3772 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3773 | wcslen(wFileData.cFileName)); |
| 3774 | if (path->narrow && v) { |
| 3775 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3776 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3777 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3778 | Py_DECREF(list); |
| 3779 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3780 | break; |
| 3781 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3782 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3783 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3784 | Py_DECREF(list); |
| 3785 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3786 | break; |
| 3787 | } |
| 3788 | Py_DECREF(v); |
| 3789 | } |
| 3790 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3791 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3792 | Py_END_ALLOW_THREADS |
| 3793 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3794 | it got to the end of the directory. */ |
| 3795 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3796 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3797 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3798 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3799 | } |
| 3800 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3801 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3802 | exit: |
| 3803 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3804 | if (FindClose(hFindFile) == FALSE) { |
| 3805 | if (list != NULL) { |
| 3806 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3807 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3808 | } |
| 3809 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3810 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3811 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3812 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3813 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3814 | } /* end of _listdir_windows_no_opendir */ |
| 3815 | |
| 3816 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3817 | |
| 3818 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3819 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3820 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3821 | PyObject *v; |
| 3822 | DIR *dirp = NULL; |
| 3823 | struct dirent *ep; |
| 3824 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3825 | #ifdef HAVE_FDOPENDIR |
| 3826 | int fd = -1; |
| 3827 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3828 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3829 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3830 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3831 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3832 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3833 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3834 | if (fd == -1) |
| 3835 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3836 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3837 | return_str = 1; |
| 3838 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3839 | Py_BEGIN_ALLOW_THREADS |
| 3840 | dirp = fdopendir(fd); |
| 3841 | Py_END_ALLOW_THREADS |
| 3842 | } |
| 3843 | else |
| 3844 | #endif |
| 3845 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3846 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3847 | if (path->narrow) { |
| 3848 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3849 | /* only return bytes if they specified a bytes-like object */ |
| 3850 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3851 | } |
| 3852 | else { |
| 3853 | name = "."; |
| 3854 | return_str = 1; |
| 3855 | } |
| 3856 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3857 | Py_BEGIN_ALLOW_THREADS |
| 3858 | dirp = opendir(name); |
| 3859 | Py_END_ALLOW_THREADS |
| 3860 | } |
| 3861 | |
| 3862 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3863 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3864 | #ifdef HAVE_FDOPENDIR |
| 3865 | if (fd != -1) { |
| 3866 | Py_BEGIN_ALLOW_THREADS |
| 3867 | close(fd); |
| 3868 | Py_END_ALLOW_THREADS |
| 3869 | } |
| 3870 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3871 | goto exit; |
| 3872 | } |
| 3873 | if ((list = PyList_New(0)) == NULL) { |
| 3874 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3875 | } |
| 3876 | for (;;) { |
| 3877 | errno = 0; |
| 3878 | Py_BEGIN_ALLOW_THREADS |
| 3879 | ep = readdir(dirp); |
| 3880 | Py_END_ALLOW_THREADS |
| 3881 | if (ep == NULL) { |
| 3882 | if (errno == 0) { |
| 3883 | break; |
| 3884 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3885 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3886 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3887 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3888 | } |
| 3889 | } |
| 3890 | if (ep->d_name[0] == '.' && |
| 3891 | (NAMLEN(ep) == 1 || |
| 3892 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3893 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3894 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3895 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3896 | else |
| 3897 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3898 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3899 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3900 | break; |
| 3901 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3902 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3903 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3904 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3905 | break; |
| 3906 | } |
| 3907 | Py_DECREF(v); |
| 3908 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3909 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3910 | exit: |
| 3911 | if (dirp != NULL) { |
| 3912 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3913 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3914 | if (fd > -1) |
| 3915 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3916 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3917 | closedir(dirp); |
| 3918 | Py_END_ALLOW_THREADS |
| 3919 | } |
| 3920 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3921 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3922 | } /* end of _posix_listdir */ |
| 3923 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3924 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3925 | |
| 3926 | /*[clinic input] |
| 3927 | os.listdir |
| 3928 | |
| 3929 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3930 | |
| 3931 | Return a list containing the names of the files in the directory. |
| 3932 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3933 | 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] | 3934 | the filenames returned will also be bytes; in all other circumstances |
| 3935 | the filenames returned will be str. |
| 3936 | If path is None, uses the path='.'. |
| 3937 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3938 | the file descriptor must refer to a directory. |
| 3939 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3940 | |
| 3941 | The list is in arbitrary order. It does not include the special |
| 3942 | entries '.' and '..' even if they are present in the directory. |
| 3943 | |
| 3944 | |
| 3945 | [clinic start generated code]*/ |
| 3946 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3947 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3948 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3949 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3950 | { |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 3951 | if (PySys_Audit("os.listdir", "O", |
| 3952 | path->object ? path->object : Py_None) < 0) { |
| 3953 | return NULL; |
| 3954 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3955 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3956 | return _listdir_windows_no_opendir(path, NULL); |
| 3957 | #else |
| 3958 | return _posix_listdir(path, NULL); |
| 3959 | #endif |
| 3960 | } |
| 3961 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3962 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3963 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3964 | /*[clinic input] |
| 3965 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3966 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3967 | path: path_t |
| 3968 | / |
| 3969 | |
| 3970 | [clinic start generated code]*/ |
| 3971 | |
| 3972 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3973 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3974 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3975 | { |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3976 | wchar_t *abspath; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3977 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3978 | /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */ |
| 3979 | if (_Py_abspath(path->wide, &abspath) < 0) { |
| 3980 | return win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3981 | } |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3982 | if (abspath == NULL) { |
| 3983 | return PyErr_NoMemory(); |
| 3984 | } |
| 3985 | |
| 3986 | PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath)); |
| 3987 | PyMem_RawFree(abspath); |
| 3988 | if (str == NULL) { |
| 3989 | return NULL; |
| 3990 | } |
| 3991 | if (path->narrow) { |
| 3992 | Py_SETREF(str, PyUnicode_EncodeFSDefault(str)); |
| 3993 | } |
| 3994 | return str; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3995 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3996 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3997 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3998 | /*[clinic input] |
| 3999 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 4000 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4001 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4002 | / |
| 4003 | |
| 4004 | A helper function for samepath on windows. |
| 4005 | [clinic start generated code]*/ |
| 4006 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4007 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4008 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 4009 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4010 | { |
| 4011 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4012 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 4013 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4014 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4015 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4016 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4017 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4018 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4019 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4020 | 0, /* desired access */ |
| 4021 | 0, /* share mode */ |
| 4022 | NULL, /* security attributes */ |
| 4023 | OPEN_EXISTING, |
| 4024 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 4025 | FILE_FLAG_BACKUP_SEMANTICS, |
| 4026 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4027 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4028 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4029 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4030 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4031 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4032 | |
| 4033 | /* We have a good handle to the target, use it to determine the |
| 4034 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4035 | while (1) { |
| 4036 | Py_BEGIN_ALLOW_THREADS |
| 4037 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 4038 | buf_size, VOLUME_NAME_DOS); |
| 4039 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4040 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4041 | if (!result_length) { |
| 4042 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 4043 | path->object); |
| 4044 | goto cleanup; |
| 4045 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4046 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4047 | if (result_length < buf_size) { |
| 4048 | break; |
| 4049 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4050 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4051 | wchar_t *tmp; |
| 4052 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 4053 | result_length * sizeof(*tmp)); |
| 4054 | if (!tmp) { |
| 4055 | result = PyErr_NoMemory(); |
| 4056 | goto cleanup; |
| 4057 | } |
| 4058 | |
| 4059 | buf_size = result_length; |
| 4060 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4061 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4062 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4063 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4064 | if (result && path->narrow) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4065 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4066 | } |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4067 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4068 | cleanup: |
| 4069 | if (target_path != buf) { |
| 4070 | PyMem_Free(target_path); |
| 4071 | } |
| 4072 | CloseHandle(hFile); |
| 4073 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4074 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 4075 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4076 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4077 | /*[clinic input] |
| 4078 | os._getvolumepathname |
| 4079 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4080 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4081 | |
| 4082 | A helper function for ismount on Win32. |
| 4083 | [clinic start generated code]*/ |
| 4084 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4085 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4086 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 4087 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4088 | { |
| 4089 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4090 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4091 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4092 | BOOL ret; |
| 4093 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4094 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4095 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4096 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 4097 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4098 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4099 | return NULL; |
| 4100 | } |
| 4101 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4102 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4103 | if (mountpath == NULL) |
| 4104 | return PyErr_NoMemory(); |
| 4105 | |
| 4106 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4107 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4108 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4109 | Py_END_ALLOW_THREADS |
| 4110 | |
| 4111 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4112 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4113 | goto exit; |
| 4114 | } |
| 4115 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4116 | if (path->narrow) |
| 4117 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4118 | |
| 4119 | exit: |
| 4120 | PyMem_Free(mountpath); |
| 4121 | return result; |
| 4122 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4123 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4124 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4125 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4126 | |
| 4127 | /*[clinic input] |
| 4128 | os.mkdir |
| 4129 | |
| 4130 | path : path_t |
| 4131 | |
| 4132 | mode: int = 0o777 |
| 4133 | |
| 4134 | * |
| 4135 | |
| 4136 | dir_fd : dir_fd(requires='mkdirat') = None |
| 4137 | |
| 4138 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4139 | |
| 4140 | Create a directory. |
| 4141 | |
| 4142 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4143 | and path should be relative; path will then be relative to that directory. |
| 4144 | dir_fd may not be implemented on your platform. |
| 4145 | If it is unavailable, using it will raise a NotImplementedError. |
| 4146 | |
| 4147 | The mode argument is ignored on Windows. |
| 4148 | [clinic start generated code]*/ |
| 4149 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4150 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4151 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 4152 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4153 | { |
| 4154 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4155 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4156 | if (PySys_Audit("os.mkdir", "Oii", path->object, mode, |
| 4157 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4158 | return NULL; |
| 4159 | } |
| 4160 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4161 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4162 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4163 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4164 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4165 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4166 | if (!result) |
| 4167 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4168 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4169 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4170 | #if HAVE_MKDIRAT |
| 4171 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4172 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4173 | else |
| 4174 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4175 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4176 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4177 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4178 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4179 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4180 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4181 | if (result < 0) |
| 4182 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4183 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4184 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4187 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4188 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4189 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4190 | #include <sys/resource.h> |
| 4191 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4192 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4193 | |
| 4194 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4195 | /*[clinic input] |
| 4196 | os.nice |
| 4197 | |
| 4198 | increment: int |
| 4199 | / |
| 4200 | |
| 4201 | Add increment to the priority of process and return the new priority. |
| 4202 | [clinic start generated code]*/ |
| 4203 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4204 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4205 | os_nice_impl(PyObject *module, int increment) |
| 4206 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4207 | { |
| 4208 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4209 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4210 | /* There are two flavours of 'nice': one that returns the new |
| 4211 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4212 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4213 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4214 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4215 | If we are of the nice family that returns the new priority, we |
| 4216 | need to clear errno before the call, and check if errno is filled |
| 4217 | before calling posix_error() on a returnvalue of -1, because the |
| 4218 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4219 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4220 | errno = 0; |
| 4221 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4222 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4223 | if (value == 0) |
| 4224 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4225 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4226 | if (value == -1 && errno != 0) |
| 4227 | /* either nice() or getpriority() returned an error */ |
| 4228 | return posix_error(); |
| 4229 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4230 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4231 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4232 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4233 | |
| 4234 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4235 | /*[clinic input] |
| 4236 | os.getpriority |
| 4237 | |
| 4238 | which: int |
| 4239 | who: int |
| 4240 | |
| 4241 | Return program scheduling priority. |
| 4242 | [clinic start generated code]*/ |
| 4243 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4244 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4245 | os_getpriority_impl(PyObject *module, int which, int who) |
| 4246 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4247 | { |
| 4248 | int retval; |
| 4249 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4250 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4251 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4252 | if (errno != 0) |
| 4253 | return posix_error(); |
| 4254 | return PyLong_FromLong((long)retval); |
| 4255 | } |
| 4256 | #endif /* HAVE_GETPRIORITY */ |
| 4257 | |
| 4258 | |
| 4259 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4260 | /*[clinic input] |
| 4261 | os.setpriority |
| 4262 | |
| 4263 | which: int |
| 4264 | who: int |
| 4265 | priority: int |
| 4266 | |
| 4267 | Set program scheduling priority. |
| 4268 | [clinic start generated code]*/ |
| 4269 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4270 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4271 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4272 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4273 | { |
| 4274 | int retval; |
| 4275 | |
| 4276 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4277 | if (retval == -1) |
| 4278 | return posix_error(); |
| 4279 | Py_RETURN_NONE; |
| 4280 | } |
| 4281 | #endif /* HAVE_SETPRIORITY */ |
| 4282 | |
| 4283 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4284 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4285 | 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] | 4286 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4287 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4288 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4289 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4290 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4291 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4292 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4293 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4294 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4295 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4296 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4297 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4298 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4299 | #ifndef HAVE_RENAMEAT |
| 4300 | if (dir_fd_specified) { |
| 4301 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4302 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4303 | } |
| 4304 | #endif |
| 4305 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4306 | if (PySys_Audit("os.rename", "OOii", src->object, dst->object, |
| 4307 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 4308 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 4309 | return NULL; |
| 4310 | } |
| 4311 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4312 | #ifdef MS_WINDOWS |
| 4313 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4314 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4315 | Py_END_ALLOW_THREADS |
| 4316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4317 | if (!result) |
| 4318 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4319 | |
| 4320 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4321 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4322 | PyErr_Format(PyExc_ValueError, |
| 4323 | "%s: src and dst must be the same type", function_name); |
| 4324 | return NULL; |
| 4325 | } |
| 4326 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4327 | Py_BEGIN_ALLOW_THREADS |
| 4328 | #ifdef HAVE_RENAMEAT |
| 4329 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4330 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4331 | else |
| 4332 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4333 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4334 | Py_END_ALLOW_THREADS |
| 4335 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4336 | if (result) |
| 4337 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4338 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4339 | Py_RETURN_NONE; |
| 4340 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4341 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4342 | |
| 4343 | /*[clinic input] |
| 4344 | os.rename |
| 4345 | |
| 4346 | src : path_t |
| 4347 | dst : path_t |
| 4348 | * |
| 4349 | src_dir_fd : dir_fd = None |
| 4350 | dst_dir_fd : dir_fd = None |
| 4351 | |
| 4352 | Rename a file or directory. |
| 4353 | |
| 4354 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4355 | descriptor open to a directory, and the respective path string (src or dst) |
| 4356 | should be relative; the path will then be relative to that directory. |
| 4357 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4358 | If they are unavailable, using them will raise a NotImplementedError. |
| 4359 | [clinic start generated code]*/ |
| 4360 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4361 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4362 | 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] | 4363 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4364 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4365 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4366 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4367 | } |
| 4368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4369 | |
| 4370 | /*[clinic input] |
| 4371 | os.replace = os.rename |
| 4372 | |
| 4373 | Rename a file or directory, overwriting the destination. |
| 4374 | |
| 4375 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4376 | descriptor open to a directory, and the respective path string (src or dst) |
| 4377 | should be relative; the path will then be relative to that directory. |
| 4378 | 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] | 4379 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4380 | [clinic start generated code]*/ |
| 4381 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4382 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4383 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4384 | int dst_dir_fd) |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4385 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4386 | { |
| 4387 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4388 | } |
| 4389 | |
| 4390 | |
| 4391 | /*[clinic input] |
| 4392 | os.rmdir |
| 4393 | |
| 4394 | path: path_t |
| 4395 | * |
| 4396 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4397 | |
| 4398 | Remove a directory. |
| 4399 | |
| 4400 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4401 | and path should be relative; path will then be relative to that directory. |
| 4402 | dir_fd may not be implemented on your platform. |
| 4403 | If it is unavailable, using it will raise a NotImplementedError. |
| 4404 | [clinic start generated code]*/ |
| 4405 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4406 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4407 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4408 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4409 | { |
| 4410 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4411 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4412 | if (PySys_Audit("os.rmdir", "Oi", path->object, |
| 4413 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4414 | return NULL; |
| 4415 | } |
| 4416 | |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4417 | Py_BEGIN_ALLOW_THREADS |
| 4418 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4419 | /* Windows, success=1, UNIX, success=0 */ |
| 4420 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4421 | #else |
| 4422 | #ifdef HAVE_UNLINKAT |
| 4423 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4424 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4425 | else |
| 4426 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4427 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4428 | #endif |
| 4429 | Py_END_ALLOW_THREADS |
| 4430 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4431 | if (result) |
| 4432 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4433 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4434 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4437 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4438 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4439 | #ifdef MS_WINDOWS |
| 4440 | /*[clinic input] |
| 4441 | os.system -> long |
| 4442 | |
| 4443 | command: Py_UNICODE |
| 4444 | |
| 4445 | Execute the command in a subshell. |
| 4446 | [clinic start generated code]*/ |
| 4447 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4448 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4449 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4450 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4451 | { |
| 4452 | long result; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4453 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4454 | if (PySys_Audit("os.system", "(u)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4455 | return -1; |
| 4456 | } |
| 4457 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4458 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4459 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4460 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4461 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4462 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4463 | return result; |
| 4464 | } |
| 4465 | #else /* MS_WINDOWS */ |
| 4466 | /*[clinic input] |
| 4467 | os.system -> long |
| 4468 | |
| 4469 | command: FSConverter |
| 4470 | |
| 4471 | Execute the command in a subshell. |
| 4472 | [clinic start generated code]*/ |
| 4473 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4474 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4475 | os_system_impl(PyObject *module, PyObject *command) |
| 4476 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4477 | { |
| 4478 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4479 | const char *bytes = PyBytes_AsString(command); |
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", "(O)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4482 | return -1; |
| 4483 | } |
| 4484 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4485 | Py_BEGIN_ALLOW_THREADS |
| 4486 | result = system(bytes); |
| 4487 | Py_END_ALLOW_THREADS |
| 4488 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4489 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4490 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4491 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4492 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4493 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4494 | /*[clinic input] |
| 4495 | os.umask |
| 4496 | |
| 4497 | mask: int |
| 4498 | / |
| 4499 | |
| 4500 | Set the current numeric umask and return the previous umask. |
| 4501 | [clinic start generated code]*/ |
| 4502 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4503 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4504 | os_umask_impl(PyObject *module, int mask) |
| 4505 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4506 | { |
| 4507 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4508 | if (i < 0) |
| 4509 | return posix_error(); |
| 4510 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4511 | } |
| 4512 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4513 | #ifdef MS_WINDOWS |
| 4514 | |
| 4515 | /* override the default DeleteFileW behavior so that directory |
| 4516 | symlinks can be removed with this function, the same as with |
| 4517 | Unix symlinks */ |
| 4518 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4519 | { |
| 4520 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4521 | WIN32_FIND_DATAW find_data; |
| 4522 | HANDLE find_data_handle; |
| 4523 | int is_directory = 0; |
| 4524 | int is_link = 0; |
| 4525 | |
| 4526 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4527 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4528 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4529 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4530 | it is a symlink */ |
| 4531 | if(is_directory && |
| 4532 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4533 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4534 | |
| 4535 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4536 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4537 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4538 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4539 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4540 | FindClose(find_data_handle); |
| 4541 | } |
| 4542 | } |
| 4543 | } |
| 4544 | |
| 4545 | if (is_directory && is_link) |
| 4546 | return RemoveDirectoryW(lpFileName); |
| 4547 | |
| 4548 | return DeleteFileW(lpFileName); |
| 4549 | } |
| 4550 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4551 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4552 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4553 | /*[clinic input] |
| 4554 | os.unlink |
| 4555 | |
| 4556 | path: path_t |
| 4557 | * |
| 4558 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4559 | |
| 4560 | Remove a file (same as remove()). |
| 4561 | |
| 4562 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4563 | and path should be relative; path will then be relative to that directory. |
| 4564 | dir_fd may not be implemented on your platform. |
| 4565 | If it is unavailable, using it will raise a NotImplementedError. |
| 4566 | |
| 4567 | [clinic start generated code]*/ |
| 4568 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4569 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4570 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4571 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4572 | { |
| 4573 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4574 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4575 | if (PySys_Audit("os.remove", "Oi", path->object, |
| 4576 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4577 | return NULL; |
| 4578 | } |
| 4579 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4580 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4581 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4582 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4583 | /* Windows, success=1, UNIX, success=0 */ |
| 4584 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4585 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4586 | #ifdef HAVE_UNLINKAT |
| 4587 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4588 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4589 | else |
| 4590 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4591 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4592 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4593 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4594 | Py_END_ALLOW_THREADS |
| 4595 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4596 | if (result) |
| 4597 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4599 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4603 | /*[clinic input] |
| 4604 | os.remove = os.unlink |
| 4605 | |
| 4606 | Remove a file (same as unlink()). |
| 4607 | |
| 4608 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4609 | and path should be relative; path will then be relative to that directory. |
| 4610 | dir_fd may not be implemented on your platform. |
| 4611 | If it is unavailable, using it will raise a NotImplementedError. |
| 4612 | [clinic start generated code]*/ |
| 4613 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4614 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4615 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4616 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4617 | { |
| 4618 | return os_unlink_impl(module, path, dir_fd); |
| 4619 | } |
| 4620 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4621 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4622 | static PyStructSequence_Field uname_result_fields[] = { |
| 4623 | {"sysname", "operating system name"}, |
| 4624 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4625 | {"release", "operating system release"}, |
| 4626 | {"version", "operating system version"}, |
| 4627 | {"machine", "hardware identifier"}, |
| 4628 | {NULL} |
| 4629 | }; |
| 4630 | |
| 4631 | PyDoc_STRVAR(uname_result__doc__, |
| 4632 | "uname_result: Result from os.uname().\n\n\ |
| 4633 | This object may be accessed either as a tuple of\n\ |
| 4634 | (sysname, nodename, release, version, machine),\n\ |
| 4635 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4636 | \n\ |
| 4637 | See os.uname for more information."); |
| 4638 | |
| 4639 | static PyStructSequence_Desc uname_result_desc = { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4640 | MODNAME ".uname_result", /* name */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4641 | uname_result__doc__, /* doc */ |
| 4642 | uname_result_fields, |
| 4643 | 5 |
| 4644 | }; |
| 4645 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4646 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4647 | /*[clinic input] |
| 4648 | os.uname |
| 4649 | |
| 4650 | Return an object identifying the current operating system. |
| 4651 | |
| 4652 | The object behaves like a named tuple with the following fields: |
| 4653 | (sysname, nodename, release, version, machine) |
| 4654 | |
| 4655 | [clinic start generated code]*/ |
| 4656 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4657 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4658 | os_uname_impl(PyObject *module) |
| 4659 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4660 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4661 | struct utsname u; |
| 4662 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4663 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4664 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4665 | Py_BEGIN_ALLOW_THREADS |
| 4666 | res = uname(&u); |
| 4667 | Py_END_ALLOW_THREADS |
| 4668 | if (res < 0) |
| 4669 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4670 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 4671 | PyObject *UnameResultType = get_posix_state(module)->UnameResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4672 | value = PyStructSequence_New((PyTypeObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4673 | if (value == NULL) |
| 4674 | return NULL; |
| 4675 | |
| 4676 | #define SET(i, field) \ |
| 4677 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4678 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4679 | if (!o) { \ |
| 4680 | Py_DECREF(value); \ |
| 4681 | return NULL; \ |
| 4682 | } \ |
| 4683 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4684 | } \ |
| 4685 | |
| 4686 | SET(0, u.sysname); |
| 4687 | SET(1, u.nodename); |
| 4688 | SET(2, u.release); |
| 4689 | SET(3, u.version); |
| 4690 | SET(4, u.machine); |
| 4691 | |
| 4692 | #undef SET |
| 4693 | |
| 4694 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4695 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4696 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4697 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4698 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4699 | |
| 4700 | typedef struct { |
| 4701 | int now; |
| 4702 | time_t atime_s; |
| 4703 | long atime_ns; |
| 4704 | time_t mtime_s; |
| 4705 | long mtime_ns; |
| 4706 | } utime_t; |
| 4707 | |
| 4708 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4709 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4710 | * they also intentionally leak the declaration of a pointer named "time" |
| 4711 | */ |
| 4712 | #define UTIME_TO_TIMESPEC \ |
| 4713 | struct timespec ts[2]; \ |
| 4714 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4715 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4716 | time = NULL; \ |
| 4717 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4718 | ts[0].tv_sec = ut->atime_s; \ |
| 4719 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4720 | ts[1].tv_sec = ut->mtime_s; \ |
| 4721 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4722 | time = ts; \ |
| 4723 | } \ |
| 4724 | |
| 4725 | #define UTIME_TO_TIMEVAL \ |
| 4726 | struct timeval tv[2]; \ |
| 4727 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4728 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4729 | time = NULL; \ |
| 4730 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4731 | tv[0].tv_sec = ut->atime_s; \ |
| 4732 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4733 | tv[1].tv_sec = ut->mtime_s; \ |
| 4734 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4735 | time = tv; \ |
| 4736 | } \ |
| 4737 | |
| 4738 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4739 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4740 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4741 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4742 | time = NULL; \ |
| 4743 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4744 | u.actime = ut->atime_s; \ |
| 4745 | u.modtime = ut->mtime_s; \ |
| 4746 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | #define UTIME_TO_TIME_T \ |
| 4750 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4751 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4752 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4753 | time = NULL; \ |
| 4754 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4755 | timet[0] = ut->atime_s; \ |
| 4756 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4757 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4758 | } \ |
| 4759 | |
| 4760 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4761 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4762 | |
| 4763 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4764 | 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] | 4765 | { |
| 4766 | #ifdef HAVE_UTIMENSAT |
| 4767 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4768 | UTIME_TO_TIMESPEC; |
| 4769 | return utimensat(dir_fd, path, time, flags); |
| 4770 | #elif defined(HAVE_FUTIMESAT) |
| 4771 | UTIME_TO_TIMEVAL; |
| 4772 | /* |
| 4773 | * follow_symlinks will never be false here; |
| 4774 | * we only allow !follow_symlinks and dir_fd together |
| 4775 | * if we have utimensat() |
| 4776 | */ |
| 4777 | assert(follow_symlinks); |
| 4778 | return futimesat(dir_fd, path, time); |
| 4779 | #endif |
| 4780 | } |
| 4781 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4782 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4783 | #else |
| 4784 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4785 | #endif |
| 4786 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4787 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4788 | |
| 4789 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4790 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4791 | { |
| 4792 | #ifdef HAVE_FUTIMENS |
| 4793 | UTIME_TO_TIMESPEC; |
| 4794 | return futimens(fd, time); |
| 4795 | #else |
| 4796 | UTIME_TO_TIMEVAL; |
| 4797 | return futimes(fd, time); |
| 4798 | #endif |
| 4799 | } |
| 4800 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4801 | #define PATH_UTIME_HAVE_FD 1 |
| 4802 | #else |
| 4803 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4804 | #endif |
| 4805 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4806 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4807 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4808 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4809 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4810 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4811 | |
| 4812 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4813 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4814 | { |
| 4815 | #ifdef HAVE_UTIMENSAT |
| 4816 | UTIME_TO_TIMESPEC; |
| 4817 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4818 | #else |
| 4819 | UTIME_TO_TIMEVAL; |
| 4820 | return lutimes(path, time); |
| 4821 | #endif |
| 4822 | } |
| 4823 | |
| 4824 | #endif |
| 4825 | |
| 4826 | #ifndef MS_WINDOWS |
| 4827 | |
| 4828 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4829 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4830 | { |
| 4831 | #ifdef HAVE_UTIMENSAT |
| 4832 | UTIME_TO_TIMESPEC; |
| 4833 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4834 | #elif defined(HAVE_UTIMES) |
| 4835 | UTIME_TO_TIMEVAL; |
| 4836 | return utimes(path, time); |
| 4837 | #elif defined(HAVE_UTIME_H) |
| 4838 | UTIME_TO_UTIMBUF; |
| 4839 | return utime(path, time); |
| 4840 | #else |
| 4841 | UTIME_TO_TIME_T; |
| 4842 | return utime(path, time); |
| 4843 | #endif |
| 4844 | } |
| 4845 | |
| 4846 | #endif |
| 4847 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4848 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4849 | 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] | 4850 | { |
| 4851 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4852 | PyObject *divmod; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4853 | divmod = PyNumber_Divmod(py_long, get_posix_state(module)->billion); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4854 | if (!divmod) |
| 4855 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4856 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4857 | PyErr_Format(PyExc_TypeError, |
| 4858 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4859 | _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod))); |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4860 | goto exit; |
| 4861 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4862 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4863 | if ((*s == -1) && PyErr_Occurred()) |
| 4864 | goto exit; |
| 4865 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4866 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4867 | goto exit; |
| 4868 | |
| 4869 | result = 1; |
| 4870 | exit: |
| 4871 | Py_XDECREF(divmod); |
| 4872 | return result; |
| 4873 | } |
| 4874 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4875 | |
| 4876 | /*[clinic input] |
| 4877 | os.utime |
| 4878 | |
| 4879 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4880 | times: object = None |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4881 | * |
| 4882 | ns: object = NULL |
| 4883 | dir_fd: dir_fd(requires='futimensat') = None |
| 4884 | follow_symlinks: bool=True |
| 4885 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4886 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4887 | |
| 4888 | Set the access and modified time of path. |
| 4889 | |
| 4890 | path may always be specified as a string. |
| 4891 | On some platforms, path may also be specified as an open file descriptor. |
| 4892 | If this functionality is unavailable, using it raises an exception. |
| 4893 | |
| 4894 | If times is not None, it must be a tuple (atime, mtime); |
| 4895 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4896 | 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] | 4897 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4898 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4899 | 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] | 4900 | Specifying tuples for both times and ns is an error. |
| 4901 | |
| 4902 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4903 | and path should be relative; path will then be relative to that directory. |
| 4904 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4905 | link, utime will modify the symbolic link itself instead of the file the |
| 4906 | link points to. |
| 4907 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4908 | as an open file descriptor. |
| 4909 | dir_fd and follow_symlinks may not be available on your platform. |
| 4910 | If they are unavailable, using them will raise a NotImplementedError. |
| 4911 | |
| 4912 | [clinic start generated code]*/ |
| 4913 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4914 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4915 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4916 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4917 | /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4918 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4919 | #ifdef MS_WINDOWS |
| 4920 | HANDLE hFile; |
| 4921 | FILETIME atime, mtime; |
| 4922 | #else |
| 4923 | int result; |
| 4924 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4925 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4926 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4927 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4928 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4929 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4930 | if (times != Py_None && ns) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4931 | PyErr_SetString(PyExc_ValueError, |
| 4932 | "utime: you may specify either 'times'" |
| 4933 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4934 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4935 | } |
| 4936 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4937 | if (times != Py_None) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4938 | time_t a_sec, m_sec; |
| 4939 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4940 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4941 | PyErr_SetString(PyExc_TypeError, |
| 4942 | "utime: 'times' must be either" |
| 4943 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4944 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4945 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4946 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4947 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4948 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4949 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4950 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4951 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4952 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4953 | utime.atime_s = a_sec; |
| 4954 | utime.atime_ns = a_nsec; |
| 4955 | utime.mtime_s = m_sec; |
| 4956 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4957 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4958 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4959 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4960 | PyErr_SetString(PyExc_TypeError, |
| 4961 | "utime: 'ns' must be a tuple of two ints"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4962 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4963 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4964 | utime.now = 0; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4965 | 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] | 4966 | &utime.atime_s, &utime.atime_ns) || |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 4967 | !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] | 4968 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4969 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4970 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4971 | } |
| 4972 | else { |
| 4973 | /* times and ns are both None/unspecified. use "now". */ |
| 4974 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4975 | } |
| 4976 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4977 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4978 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4979 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4980 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4981 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4982 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4983 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4984 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4985 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4986 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4987 | #if !defined(HAVE_UTIMENSAT) |
| 4988 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4989 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4990 | "utime: cannot use dir_fd and follow_symlinks " |
| 4991 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4992 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4993 | } |
| 4994 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4995 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4996 | if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None, |
| 4997 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4998 | return NULL; |
| 4999 | } |
| 5000 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5001 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5002 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5003 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 5004 | NULL, OPEN_EXISTING, |
| 5005 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5006 | Py_END_ALLOW_THREADS |
| 5007 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5008 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5009 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 5010 | } |
| 5011 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5012 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 5013 | GetSystemTimeAsFileTime(&mtime); |
| 5014 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5015 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5016 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 5017 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 5018 | _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] | 5019 | } |
| 5020 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 5021 | /* Avoid putting the file name into the error here, |
| 5022 | as that may confuse the user into believing that |
| 5023 | something is wrong with the file, when it also |
| 5024 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 5025 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5026 | CloseHandle(hFile); |
| 5027 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5028 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5029 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5030 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5031 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 5032 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 5033 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5034 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5035 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5036 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 5037 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5038 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5039 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5040 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5041 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5042 | else |
| 5043 | #endif |
| 5044 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5045 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5046 | if (path->fd != -1) |
| 5047 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5048 | else |
| 5049 | #endif |
| 5050 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5051 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5052 | |
| 5053 | Py_END_ALLOW_THREADS |
| 5054 | |
| 5055 | if (result < 0) { |
| 5056 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5057 | posix_error(); |
| 5058 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5059 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5060 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5061 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5062 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5063 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5066 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5067 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5068 | |
| 5069 | /*[clinic input] |
| 5070 | os._exit |
| 5071 | |
| 5072 | status: int |
| 5073 | |
| 5074 | Exit to the system with specified status, without normal exit processing. |
| 5075 | [clinic start generated code]*/ |
| 5076 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5077 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5078 | os__exit_impl(PyObject *module, int status) |
| 5079 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5080 | { |
| 5081 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5082 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5085 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5086 | #define EXECV_CHAR wchar_t |
| 5087 | #else |
| 5088 | #define EXECV_CHAR char |
| 5089 | #endif |
| 5090 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5091 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5092 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5093 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5094 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5095 | Py_ssize_t i; |
| 5096 | for (i = 0; i < count; i++) |
| 5097 | PyMem_Free(array[i]); |
| 5098 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5099 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5100 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5101 | static int |
| 5102 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5103 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5104 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5105 | PyObject *ub; |
| 5106 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5107 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5108 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5109 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5110 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 5111 | if (*out) |
| 5112 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5113 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5114 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5115 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5116 | size = PyBytes_GET_SIZE(ub); |
| 5117 | *out = PyMem_Malloc(size + 1); |
| 5118 | if (*out) { |
| 5119 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 5120 | result = 1; |
| 5121 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5122 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5123 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5124 | Py_DECREF(ub); |
| 5125 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5126 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5127 | #endif |
| 5128 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5129 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5130 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5131 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5132 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5133 | Py_ssize_t i, pos, envc; |
| 5134 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5135 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5136 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5137 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5138 | i = PyMapping_Size(env); |
| 5139 | if (i < 0) |
| 5140 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5141 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5142 | if (envlist == NULL) { |
| 5143 | PyErr_NoMemory(); |
| 5144 | return NULL; |
| 5145 | } |
| 5146 | envc = 0; |
| 5147 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5148 | if (!keys) |
| 5149 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5150 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5151 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5152 | goto error; |
| 5153 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5154 | PyErr_Format(PyExc_TypeError, |
| 5155 | "env.keys() or env.values() is not a list"); |
| 5156 | goto error; |
| 5157 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5158 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5159 | for (pos = 0; pos < i; pos++) { |
| 5160 | key = PyList_GetItem(keys, pos); |
| 5161 | val = PyList_GetItem(vals, pos); |
| 5162 | if (!key || !val) |
| 5163 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5164 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5165 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5166 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 5167 | goto error; |
| 5168 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 5169 | Py_DECREF(key2); |
| 5170 | goto error; |
| 5171 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5172 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 5173 | defining hidden environment variables. */ |
| 5174 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 5175 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 5176 | { |
| 5177 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5178 | Py_DECREF(key2); |
| 5179 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5180 | goto error; |
| 5181 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5182 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 5183 | #else |
| 5184 | if (!PyUnicode_FSConverter(key, &key2)) |
| 5185 | goto error; |
| 5186 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 5187 | Py_DECREF(key2); |
| 5188 | goto error; |
| 5189 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5190 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 5191 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 5192 | { |
| 5193 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5194 | Py_DECREF(key2); |
| 5195 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5196 | goto error; |
| 5197 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5198 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 5199 | PyBytes_AS_STRING(val2)); |
| 5200 | #endif |
| 5201 | Py_DECREF(key2); |
| 5202 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5203 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5204 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5205 | |
| 5206 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 5207 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5208 | goto error; |
| 5209 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5210 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5211 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5212 | } |
| 5213 | Py_DECREF(vals); |
| 5214 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5215 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5216 | envlist[envc] = 0; |
| 5217 | *envc_ptr = envc; |
| 5218 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5219 | |
| 5220 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5221 | Py_XDECREF(keys); |
| 5222 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5223 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5224 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5225 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5226 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5227 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5228 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5229 | { |
| 5230 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5231 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5232 | if (argvlist == NULL) { |
| 5233 | PyErr_NoMemory(); |
| 5234 | return NULL; |
| 5235 | } |
| 5236 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5237 | PyObject* item = PySequence_ITEM(argv, i); |
| 5238 | if (item == NULL) |
| 5239 | goto fail; |
| 5240 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5241 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5242 | goto fail; |
| 5243 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5244 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5245 | } |
| 5246 | argvlist[*argc] = NULL; |
| 5247 | return argvlist; |
| 5248 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5249 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5250 | free_string_array(argvlist, *argc); |
| 5251 | return NULL; |
| 5252 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5253 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5254 | #endif |
| 5255 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5256 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5257 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5258 | /*[clinic input] |
| 5259 | os.execv |
| 5260 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5261 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5262 | Path of executable file. |
| 5263 | argv: object |
| 5264 | Tuple or list of strings. |
| 5265 | / |
| 5266 | |
| 5267 | Execute an executable path with arguments, replacing current process. |
| 5268 | [clinic start generated code]*/ |
| 5269 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5270 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5271 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5272 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5273 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5274 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5275 | Py_ssize_t argc; |
| 5276 | |
| 5277 | /* execv has two arguments: (path, argv), where |
| 5278 | argv is a list or tuple of strings. */ |
| 5279 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5280 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5281 | PyErr_SetString(PyExc_TypeError, |
| 5282 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5283 | return NULL; |
| 5284 | } |
| 5285 | argc = PySequence_Size(argv); |
| 5286 | if (argc < 1) { |
| 5287 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5288 | return NULL; |
| 5289 | } |
| 5290 | |
| 5291 | argvlist = parse_arglist(argv, &argc); |
| 5292 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5293 | return NULL; |
| 5294 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5295 | if (!argvlist[0][0]) { |
| 5296 | PyErr_SetString(PyExc_ValueError, |
| 5297 | "execv() arg 2 first element cannot be empty"); |
| 5298 | free_string_array(argvlist, argc); |
| 5299 | return NULL; |
| 5300 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5301 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5302 | if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5303 | free_string_array(argvlist, argc); |
| 5304 | return NULL; |
| 5305 | } |
| 5306 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5307 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5308 | #ifdef HAVE_WEXECV |
| 5309 | _wexecv(path->wide, argvlist); |
| 5310 | #else |
| 5311 | execv(path->narrow, argvlist); |
| 5312 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5313 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5314 | |
| 5315 | /* If we get here it's definitely an error */ |
| 5316 | |
| 5317 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5318 | return posix_error(); |
| 5319 | } |
| 5320 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5321 | |
| 5322 | /*[clinic input] |
| 5323 | os.execve |
| 5324 | |
| 5325 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5326 | Path of executable file. |
| 5327 | argv: object |
| 5328 | Tuple or list of strings. |
| 5329 | env: object |
| 5330 | Dictionary of strings mapping to strings. |
| 5331 | |
| 5332 | Execute an executable path with arguments, replacing current process. |
| 5333 | [clinic start generated code]*/ |
| 5334 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5335 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5336 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5337 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5338 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5339 | EXECV_CHAR **argvlist = NULL; |
| 5340 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5341 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5342 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5343 | /* execve has three arguments: (path, argv, env), where |
| 5344 | argv is a list or tuple of strings and env is a dictionary |
| 5345 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5346 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5347 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5348 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5349 | "execve: argv must be a tuple or list"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5350 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5351 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5352 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5353 | if (argc < 1) { |
| 5354 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5355 | return NULL; |
| 5356 | } |
| 5357 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5358 | if (!PyMapping_Check(env)) { |
| 5359 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5360 | "execve: environment must be a mapping object"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5361 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5362 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5363 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5364 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5365 | if (argvlist == NULL) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5366 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5367 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5368 | if (!argvlist[0][0]) { |
| 5369 | PyErr_SetString(PyExc_ValueError, |
| 5370 | "execve: argv first element cannot be empty"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5371 | goto fail_0; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5372 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5373 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5374 | envlist = parse_envlist(env, &envc); |
| 5375 | if (envlist == NULL) |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5376 | goto fail_0; |
| 5377 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5378 | if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5379 | goto fail_1; |
| 5380 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5381 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5382 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5383 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5384 | if (path->fd > -1) |
| 5385 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5386 | else |
| 5387 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5388 | #ifdef HAVE_WEXECV |
| 5389 | _wexecve(path->wide, argvlist, envlist); |
| 5390 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5391 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5392 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5393 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5394 | |
| 5395 | /* If we get here it's definitely an error */ |
| 5396 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5397 | posix_path_error(path); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5398 | fail_1: |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5399 | free_string_array(envlist, envc); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5400 | fail_0: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5401 | if (argvlist) |
| 5402 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5403 | return NULL; |
| 5404 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5405 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5406 | #endif /* HAVE_EXECV */ |
| 5407 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5408 | #ifdef HAVE_POSIX_SPAWN |
| 5409 | |
| 5410 | enum posix_spawn_file_actions_identifier { |
| 5411 | POSIX_SPAWN_OPEN, |
| 5412 | POSIX_SPAWN_CLOSE, |
| 5413 | POSIX_SPAWN_DUP2 |
| 5414 | }; |
| 5415 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5416 | #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] | 5417 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5418 | convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5419 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5420 | |
| 5421 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5422 | parse_posix_spawn_flags(PyObject *module, const char *func_name, PyObject *setpgroup, |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5423 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5424 | PyObject *setsigdef, PyObject *scheduler, |
| 5425 | posix_spawnattr_t *attrp) |
| 5426 | { |
| 5427 | long all_flags = 0; |
| 5428 | |
| 5429 | errno = posix_spawnattr_init(attrp); |
| 5430 | if (errno) { |
| 5431 | posix_error(); |
| 5432 | return -1; |
| 5433 | } |
| 5434 | |
| 5435 | if (setpgroup) { |
| 5436 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5437 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5438 | goto fail; |
| 5439 | } |
| 5440 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5441 | if (errno) { |
| 5442 | posix_error(); |
| 5443 | goto fail; |
| 5444 | } |
| 5445 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5446 | } |
| 5447 | |
| 5448 | if (resetids) { |
| 5449 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5450 | } |
| 5451 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5452 | if (setsid) { |
| 5453 | #ifdef POSIX_SPAWN_SETSID |
| 5454 | all_flags |= POSIX_SPAWN_SETSID; |
| 5455 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5456 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5457 | #else |
| 5458 | argument_unavailable_error(func_name, "setsid"); |
| 5459 | return -1; |
| 5460 | #endif |
| 5461 | } |
| 5462 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5463 | if (setsigmask) { |
| 5464 | sigset_t set; |
| 5465 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5466 | goto fail; |
| 5467 | } |
| 5468 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5469 | if (errno) { |
| 5470 | posix_error(); |
| 5471 | goto fail; |
| 5472 | } |
| 5473 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5474 | } |
| 5475 | |
| 5476 | if (setsigdef) { |
| 5477 | sigset_t set; |
| 5478 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5479 | goto fail; |
| 5480 | } |
| 5481 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5482 | if (errno) { |
| 5483 | posix_error(); |
| 5484 | goto fail; |
| 5485 | } |
| 5486 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5487 | } |
| 5488 | |
| 5489 | if (scheduler) { |
| 5490 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5491 | PyObject *py_schedpolicy; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5492 | PyObject *schedparam_obj; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5493 | struct sched_param schedparam; |
| 5494 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5495 | if (!PyArg_ParseTuple(scheduler, "OO" |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5496 | ";A scheduler tuple must have two elements", |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5497 | &py_schedpolicy, &schedparam_obj)) { |
| 5498 | goto fail; |
| 5499 | } |
| 5500 | if (!convert_sched_param(module, schedparam_obj, &schedparam)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5501 | goto fail; |
| 5502 | } |
| 5503 | if (py_schedpolicy != Py_None) { |
| 5504 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5505 | |
| 5506 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5507 | goto fail; |
| 5508 | } |
| 5509 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5510 | if (errno) { |
| 5511 | posix_error(); |
| 5512 | goto fail; |
| 5513 | } |
| 5514 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5515 | } |
| 5516 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5517 | if (errno) { |
| 5518 | posix_error(); |
| 5519 | goto fail; |
| 5520 | } |
| 5521 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5522 | #else |
| 5523 | PyErr_SetString(PyExc_NotImplementedError, |
| 5524 | "The scheduler option is not supported in this system."); |
| 5525 | goto fail; |
| 5526 | #endif |
| 5527 | } |
| 5528 | |
| 5529 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5530 | if (errno) { |
| 5531 | posix_error(); |
| 5532 | goto fail; |
| 5533 | } |
| 5534 | |
| 5535 | return 0; |
| 5536 | |
| 5537 | fail: |
| 5538 | (void)posix_spawnattr_destroy(attrp); |
| 5539 | return -1; |
| 5540 | } |
| 5541 | |
| 5542 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5543 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5544 | posix_spawn_file_actions_t *file_actionsp, |
| 5545 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5546 | { |
| 5547 | PyObject *seq; |
| 5548 | PyObject *file_action = NULL; |
| 5549 | PyObject *tag_obj; |
| 5550 | |
| 5551 | seq = PySequence_Fast(file_actions, |
| 5552 | "file_actions must be a sequence or None"); |
| 5553 | if (seq == NULL) { |
| 5554 | return -1; |
| 5555 | } |
| 5556 | |
| 5557 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5558 | if (errno) { |
| 5559 | posix_error(); |
| 5560 | Py_DECREF(seq); |
| 5561 | return -1; |
| 5562 | } |
| 5563 | |
Zackery Spytz | d52a83a | 2019-06-26 14:54:20 -0600 | [diff] [blame] | 5564 | 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] | 5565 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5566 | Py_INCREF(file_action); |
| 5567 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5568 | PyErr_SetString(PyExc_TypeError, |
| 5569 | "Each file_actions element must be a non-empty tuple"); |
| 5570 | goto fail; |
| 5571 | } |
| 5572 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5573 | if (tag == -1 && PyErr_Occurred()) { |
| 5574 | goto fail; |
| 5575 | } |
| 5576 | |
| 5577 | /* Populate the file_actions object */ |
| 5578 | switch (tag) { |
| 5579 | case POSIX_SPAWN_OPEN: { |
| 5580 | int fd, oflag; |
| 5581 | PyObject *path; |
| 5582 | unsigned long mode; |
| 5583 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5584 | ";A open file_action tuple must have 5 elements", |
| 5585 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5586 | &oflag, &mode)) |
| 5587 | { |
| 5588 | goto fail; |
| 5589 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5590 | if (PyList_Append(temp_buffer, path)) { |
| 5591 | Py_DECREF(path); |
| 5592 | goto fail; |
| 5593 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5594 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5595 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5596 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5597 | if (errno) { |
| 5598 | posix_error(); |
| 5599 | goto fail; |
| 5600 | } |
| 5601 | break; |
| 5602 | } |
| 5603 | case POSIX_SPAWN_CLOSE: { |
| 5604 | int fd; |
| 5605 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5606 | ";A close file_action tuple must have 2 elements", |
| 5607 | &tag_obj, &fd)) |
| 5608 | { |
| 5609 | goto fail; |
| 5610 | } |
| 5611 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5612 | if (errno) { |
| 5613 | posix_error(); |
| 5614 | goto fail; |
| 5615 | } |
| 5616 | break; |
| 5617 | } |
| 5618 | case POSIX_SPAWN_DUP2: { |
| 5619 | int fd1, fd2; |
| 5620 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5621 | ";A dup2 file_action tuple must have 3 elements", |
| 5622 | &tag_obj, &fd1, &fd2)) |
| 5623 | { |
| 5624 | goto fail; |
| 5625 | } |
| 5626 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5627 | fd1, fd2); |
| 5628 | if (errno) { |
| 5629 | posix_error(); |
| 5630 | goto fail; |
| 5631 | } |
| 5632 | break; |
| 5633 | } |
| 5634 | default: { |
| 5635 | PyErr_SetString(PyExc_TypeError, |
| 5636 | "Unknown file_actions identifier"); |
| 5637 | goto fail; |
| 5638 | } |
| 5639 | } |
| 5640 | Py_DECREF(file_action); |
| 5641 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5642 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5643 | Py_DECREF(seq); |
| 5644 | return 0; |
| 5645 | |
| 5646 | fail: |
| 5647 | Py_DECREF(seq); |
| 5648 | Py_DECREF(file_action); |
| 5649 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5650 | return -1; |
| 5651 | } |
| 5652 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5653 | |
| 5654 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5655 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 5656 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5657 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5658 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5659 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5660 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5661 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5662 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5663 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5664 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5665 | posix_spawnattr_t attr; |
| 5666 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5667 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5668 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5669 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5670 | pid_t pid; |
| 5671 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5672 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5673 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5674 | 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] | 5675 | like posix.environ. */ |
| 5676 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5677 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5678 | PyErr_Format(PyExc_TypeError, |
| 5679 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5680 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5681 | } |
| 5682 | argc = PySequence_Size(argv); |
| 5683 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5684 | PyErr_Format(PyExc_ValueError, |
| 5685 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5686 | return NULL; |
| 5687 | } |
| 5688 | |
| 5689 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5690 | PyErr_Format(PyExc_TypeError, |
| 5691 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5692 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | argvlist = parse_arglist(argv, &argc); |
| 5696 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5697 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5698 | } |
| 5699 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5700 | PyErr_Format(PyExc_ValueError, |
| 5701 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5702 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5706 | if (envlist == NULL) { |
| 5707 | goto exit; |
| 5708 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5709 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 5710 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5711 | /* There is a bug in old versions of glibc that makes some of the |
| 5712 | * helper functions for manipulating file actions not copy the provided |
| 5713 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5714 | * copy the value of path for some old versions of glibc (<2.20). |
| 5715 | * The use of temp_buffer here is a workaround that keeps the |
| 5716 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5717 | * Check https://bugs.python.org/issue33630 and |
| 5718 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5719 | temp_buffer = PyList_New(0); |
| 5720 | if (!temp_buffer) { |
| 5721 | goto exit; |
| 5722 | } |
| 5723 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
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 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5726 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5727 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5728 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5729 | if (parse_posix_spawn_flags(module, func_name, setpgroup, resetids, setsid, |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5730 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5731 | goto exit; |
| 5732 | } |
| 5733 | attrp = &attr; |
| 5734 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5735 | if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5736 | goto exit; |
| 5737 | } |
| 5738 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5739 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5740 | #ifdef HAVE_POSIX_SPAWNP |
| 5741 | if (use_posix_spawnp) { |
| 5742 | err_code = posix_spawnp(&pid, path->narrow, |
| 5743 | file_actionsp, attrp, argvlist, envlist); |
| 5744 | } |
| 5745 | else |
| 5746 | #endif /* HAVE_POSIX_SPAWNP */ |
| 5747 | { |
| 5748 | err_code = posix_spawn(&pid, path->narrow, |
| 5749 | file_actionsp, attrp, argvlist, envlist); |
| 5750 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5751 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5752 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5753 | if (err_code) { |
| 5754 | errno = err_code; |
| 5755 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5756 | goto exit; |
| 5757 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5758 | #ifdef _Py_MEMORY_SANITIZER |
| 5759 | __msan_unpoison(&pid, sizeof(pid)); |
| 5760 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5761 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5762 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5763 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5764 | if (file_actionsp) { |
| 5765 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5766 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5767 | if (attrp) { |
| 5768 | (void)posix_spawnattr_destroy(attrp); |
| 5769 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5770 | if (envlist) { |
| 5771 | free_string_array(envlist, envc); |
| 5772 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5773 | if (argvlist) { |
| 5774 | free_string_array(argvlist, argc); |
| 5775 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5776 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5777 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5778 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5779 | |
| 5780 | |
| 5781 | /*[clinic input] |
| 5782 | |
| 5783 | os.posix_spawn |
| 5784 | path: path_t |
| 5785 | Path of executable file. |
| 5786 | argv: object |
| 5787 | Tuple or list of strings. |
| 5788 | env: object |
| 5789 | Dictionary of strings mapping to strings. |
| 5790 | / |
| 5791 | * |
| 5792 | file_actions: object(c_default='NULL') = () |
| 5793 | A sequence of file action tuples. |
| 5794 | setpgroup: object = NULL |
| 5795 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5796 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5797 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 5798 | setsid: bool(accept={int}) = False |
| 5799 | 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] | 5800 | setsigmask: object(c_default='NULL') = () |
| 5801 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5802 | setsigdef: object(c_default='NULL') = () |
| 5803 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5804 | scheduler: object = NULL |
| 5805 | A tuple with the scheduler policy (optional) and parameters. |
| 5806 | |
| 5807 | Execute the program specified by path in a new process. |
| 5808 | [clinic start generated code]*/ |
| 5809 | |
| 5810 | static PyObject * |
| 5811 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5812 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5813 | PyObject *setpgroup, int resetids, int setsid, |
| 5814 | PyObject *setsigmask, PyObject *setsigdef, |
| 5815 | PyObject *scheduler) |
| 5816 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5817 | { |
| 5818 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5819 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5820 | scheduler); |
| 5821 | } |
| 5822 | #endif /* HAVE_POSIX_SPAWN */ |
| 5823 | |
| 5824 | |
| 5825 | |
| 5826 | #ifdef HAVE_POSIX_SPAWNP |
| 5827 | /*[clinic input] |
| 5828 | |
| 5829 | os.posix_spawnp |
| 5830 | path: path_t |
| 5831 | Path of executable file. |
| 5832 | argv: object |
| 5833 | Tuple or list of strings. |
| 5834 | env: object |
| 5835 | Dictionary of strings mapping to strings. |
| 5836 | / |
| 5837 | * |
| 5838 | file_actions: object(c_default='NULL') = () |
| 5839 | A sequence of file action tuples. |
| 5840 | setpgroup: object = NULL |
| 5841 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5842 | resetids: bool(accept={int}) = False |
| 5843 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5844 | setsid: bool(accept={int}) = False |
| 5845 | 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] | 5846 | setsigmask: object(c_default='NULL') = () |
| 5847 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5848 | setsigdef: object(c_default='NULL') = () |
| 5849 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5850 | scheduler: object = NULL |
| 5851 | A tuple with the scheduler policy (optional) and parameters. |
| 5852 | |
| 5853 | Execute the program specified by path in a new process. |
| 5854 | [clinic start generated code]*/ |
| 5855 | |
| 5856 | static PyObject * |
| 5857 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5858 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5859 | PyObject *setpgroup, int resetids, int setsid, |
| 5860 | PyObject *setsigmask, PyObject *setsigdef, |
| 5861 | PyObject *scheduler) |
| 5862 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5863 | { |
| 5864 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5865 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5866 | scheduler); |
| 5867 | } |
| 5868 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5869 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5870 | #ifdef HAVE_RTPSPAWN |
| 5871 | static intptr_t |
| 5872 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], |
| 5873 | const char *envp[]) |
| 5874 | { |
| 5875 | RTP_ID rtpid; |
| 5876 | int status; |
| 5877 | pid_t res; |
| 5878 | int async_err = 0; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5879 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5880 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
| 5881 | uStackSize=0 cannot be used, the default stack size is too small for |
| 5882 | Python. */ |
| 5883 | if (envp) { |
| 5884 | rtpid = rtpSpawn(rtpFileName, argv, envp, |
| 5885 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5886 | } |
| 5887 | else { |
| 5888 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, |
| 5889 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5890 | } |
| 5891 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { |
| 5892 | do { |
| 5893 | res = waitpid((pid_t)rtpid, &status, 0); |
| 5894 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 5895 | |
| 5896 | if (res < 0) |
| 5897 | return RTP_ID_ERROR; |
| 5898 | return ((intptr_t)status); |
| 5899 | } |
| 5900 | return ((intptr_t)rtpid); |
| 5901 | } |
| 5902 | #endif |
| 5903 | |
| 5904 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5905 | /*[clinic input] |
| 5906 | os.spawnv |
| 5907 | |
| 5908 | mode: int |
| 5909 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5910 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5911 | Path of executable file. |
| 5912 | argv: object |
| 5913 | Tuple or list of strings. |
| 5914 | / |
| 5915 | |
| 5916 | Execute the program specified by path in a new process. |
| 5917 | [clinic start generated code]*/ |
| 5918 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5919 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5920 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5921 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5922 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5923 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5924 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5925 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5926 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5927 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5928 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5929 | /* spawnv has three arguments: (mode, path, argv), where |
| 5930 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5931 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5932 | if (PyList_Check(argv)) { |
| 5933 | argc = PyList_Size(argv); |
| 5934 | getitem = PyList_GetItem; |
| 5935 | } |
| 5936 | else if (PyTuple_Check(argv)) { |
| 5937 | argc = PyTuple_Size(argv); |
| 5938 | getitem = PyTuple_GetItem; |
| 5939 | } |
| 5940 | else { |
| 5941 | PyErr_SetString(PyExc_TypeError, |
| 5942 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5943 | return NULL; |
| 5944 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5945 | if (argc == 0) { |
| 5946 | PyErr_SetString(PyExc_ValueError, |
| 5947 | "spawnv() arg 2 cannot be empty"); |
| 5948 | return NULL; |
| 5949 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5950 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5951 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5952 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5953 | return PyErr_NoMemory(); |
| 5954 | } |
| 5955 | for (i = 0; i < argc; i++) { |
| 5956 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5957 | &argvlist[i])) { |
| 5958 | free_string_array(argvlist, i); |
| 5959 | PyErr_SetString( |
| 5960 | PyExc_TypeError, |
| 5961 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5962 | return NULL; |
| 5963 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5964 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5965 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5966 | PyErr_SetString( |
| 5967 | PyExc_ValueError, |
| 5968 | "spawnv() arg 2 first element cannot be empty"); |
| 5969 | return NULL; |
| 5970 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5971 | } |
| 5972 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5973 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5974 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5975 | if (mode == _OLD_P_OVERLAY) |
| 5976 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5977 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5978 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5979 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5980 | Py_None) < 0) { |
| 5981 | free_string_array(argvlist, argc); |
| 5982 | return NULL; |
| 5983 | } |
| 5984 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5985 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5986 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5987 | #ifdef HAVE_WSPAWNV |
| 5988 | spawnval = _wspawnv(mode, path->wide, argvlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5989 | #elif defined(HAVE_RTPSPAWN) |
| 5990 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5991 | #else |
| 5992 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5993 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5994 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5995 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5996 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5997 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5998 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5999 | if (spawnval == -1) |
| 6000 | return posix_error(); |
| 6001 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6002 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6003 | } |
| 6004 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6005 | /*[clinic input] |
| 6006 | os.spawnve |
| 6007 | |
| 6008 | mode: int |
| 6009 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6010 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6011 | Path of executable file. |
| 6012 | argv: object |
| 6013 | Tuple or list of strings. |
| 6014 | env: object |
| 6015 | Dictionary of strings mapping to strings. |
| 6016 | / |
| 6017 | |
| 6018 | Execute the program specified by path in a new process. |
| 6019 | [clinic start generated code]*/ |
| 6020 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6021 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6022 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6023 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6024 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6025 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6026 | EXECV_CHAR **argvlist; |
| 6027 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6028 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 6029 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6030 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6031 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6032 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6034 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 6035 | argv is a list or tuple of strings and env is a dictionary |
| 6036 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6037 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6038 | if (PyList_Check(argv)) { |
| 6039 | argc = PyList_Size(argv); |
| 6040 | getitem = PyList_GetItem; |
| 6041 | } |
| 6042 | else if (PyTuple_Check(argv)) { |
| 6043 | argc = PyTuple_Size(argv); |
| 6044 | getitem = PyTuple_GetItem; |
| 6045 | } |
| 6046 | else { |
| 6047 | PyErr_SetString(PyExc_TypeError, |
| 6048 | "spawnve() arg 2 must be a tuple or list"); |
| 6049 | goto fail_0; |
| 6050 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 6051 | if (argc == 0) { |
| 6052 | PyErr_SetString(PyExc_ValueError, |
| 6053 | "spawnve() arg 2 cannot be empty"); |
| 6054 | goto fail_0; |
| 6055 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6056 | if (!PyMapping_Check(env)) { |
| 6057 | PyErr_SetString(PyExc_TypeError, |
| 6058 | "spawnve() arg 3 must be a mapping object"); |
| 6059 | goto fail_0; |
| 6060 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6061 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6062 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6063 | if (argvlist == NULL) { |
| 6064 | PyErr_NoMemory(); |
| 6065 | goto fail_0; |
| 6066 | } |
| 6067 | for (i = 0; i < argc; i++) { |
| 6068 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 6069 | &argvlist[i])) |
| 6070 | { |
| 6071 | lastarg = i; |
| 6072 | goto fail_1; |
| 6073 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6074 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6075 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6076 | PyErr_SetString( |
| 6077 | PyExc_ValueError, |
| 6078 | "spawnv() arg 2 first element cannot be empty"); |
| 6079 | goto fail_1; |
| 6080 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6081 | } |
| 6082 | lastarg = argc; |
| 6083 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6084 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6085 | envlist = parse_envlist(env, &envc); |
| 6086 | if (envlist == NULL) |
| 6087 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6088 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6089 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6090 | if (mode == _OLD_P_OVERLAY) |
| 6091 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6092 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6093 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6094 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6095 | goto fail_2; |
| 6096 | } |
| 6097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6098 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6099 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6100 | #ifdef HAVE_WSPAWNV |
| 6101 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6102 | #elif defined(HAVE_RTPSPAWN) |
| 6103 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, |
| 6104 | (const char **)envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6105 | #else |
| 6106 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 6107 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6108 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6109 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6110 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6111 | if (spawnval == -1) |
| 6112 | (void) posix_error(); |
| 6113 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6114 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6115 | |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6116 | fail_2: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6117 | while (--envc >= 0) |
| 6118 | PyMem_DEL(envlist[envc]); |
| 6119 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 6120 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6121 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6122 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6123 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6124 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6125 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6126 | #endif /* HAVE_SPAWNV */ |
| 6127 | |
| 6128 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6129 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6130 | |
| 6131 | /* Helper function to validate arguments. |
| 6132 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 6133 | If obj is non-NULL it must be callable. */ |
| 6134 | static int |
| 6135 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 6136 | { |
| 6137 | if (obj && !PyCallable_Check(obj)) { |
| 6138 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6139 | obj_name, _PyType_Name(Py_TYPE(obj))); |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6140 | return -1; |
| 6141 | } |
| 6142 | return 0; |
| 6143 | } |
| 6144 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6145 | /*[clinic input] |
| 6146 | os.register_at_fork |
| 6147 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6148 | * |
| 6149 | before: object=NULL |
| 6150 | A callable to be called in the parent before the fork() syscall. |
| 6151 | after_in_child: object=NULL |
| 6152 | A callable to be called in the child after fork(). |
| 6153 | after_in_parent: object=NULL |
| 6154 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6155 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6156 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6157 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6158 | 'before' callbacks are called in reverse order. |
| 6159 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6160 | |
| 6161 | [clinic start generated code]*/ |
| 6162 | |
| 6163 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6164 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 6165 | PyObject *after_in_child, PyObject *after_in_parent) |
| 6166 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6167 | { |
| 6168 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6169 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6170 | if (!before && !after_in_child && !after_in_parent) { |
| 6171 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 6172 | return NULL; |
| 6173 | } |
| 6174 | if (check_null_or_callable(before, "before") || |
| 6175 | check_null_or_callable(after_in_child, "after_in_child") || |
| 6176 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6177 | return NULL; |
| 6178 | } |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6179 | interp = _PyInterpreterState_GET(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6180 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6181 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6182 | return NULL; |
| 6183 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6184 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6185 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6186 | } |
| 6187 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 6188 | return NULL; |
| 6189 | } |
| 6190 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6191 | } |
| 6192 | #endif /* HAVE_FORK */ |
| 6193 | |
| 6194 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6195 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6196 | /*[clinic input] |
| 6197 | os.fork1 |
| 6198 | |
| 6199 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 6200 | |
| 6201 | Return 0 to child process and PID of child to parent process. |
| 6202 | [clinic start generated code]*/ |
| 6203 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6204 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6205 | os_fork1_impl(PyObject *module) |
| 6206 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6207 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6208 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6209 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6210 | if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6211 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6212 | return NULL; |
| 6213 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6214 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6215 | pid = fork1(); |
| 6216 | if (pid == 0) { |
| 6217 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6218 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6219 | } else { |
| 6220 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6221 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6222 | } |
| 6223 | if (pid == -1) |
| 6224 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6225 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6226 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6227 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6228 | |
| 6229 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6230 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6231 | /*[clinic input] |
| 6232 | os.fork |
| 6233 | |
| 6234 | Fork a child process. |
| 6235 | |
| 6236 | Return 0 to child process and PID of child to parent process. |
| 6237 | [clinic start generated code]*/ |
| 6238 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6239 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6240 | os_fork_impl(PyObject *module) |
| 6241 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6242 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6243 | pid_t pid; |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 6244 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 6245 | if (interp->config._isolated_interpreter) { |
| 6246 | PyErr_SetString(PyExc_RuntimeError, |
| 6247 | "fork not supported for isolated subinterpreters"); |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6248 | return NULL; |
| 6249 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6250 | if (PySys_Audit("os.fork", NULL) < 0) { |
| 6251 | return NULL; |
| 6252 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6253 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6254 | pid = fork(); |
| 6255 | if (pid == 0) { |
| 6256 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6257 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6258 | } else { |
| 6259 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6260 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6261 | } |
| 6262 | if (pid == -1) |
| 6263 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6264 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6265 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6266 | #endif /* HAVE_FORK */ |
| 6267 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6268 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6269 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6270 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6271 | /*[clinic input] |
| 6272 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6273 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6274 | policy: int |
| 6275 | |
| 6276 | Get the maximum scheduling priority for policy. |
| 6277 | [clinic start generated code]*/ |
| 6278 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6279 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6280 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 6281 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6282 | { |
| 6283 | int max; |
| 6284 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6285 | max = sched_get_priority_max(policy); |
| 6286 | if (max < 0) |
| 6287 | return posix_error(); |
| 6288 | return PyLong_FromLong(max); |
| 6289 | } |
| 6290 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6291 | |
| 6292 | /*[clinic input] |
| 6293 | os.sched_get_priority_min |
| 6294 | |
| 6295 | policy: int |
| 6296 | |
| 6297 | Get the minimum scheduling priority for policy. |
| 6298 | [clinic start generated code]*/ |
| 6299 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6300 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6301 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 6302 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6303 | { |
| 6304 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6305 | if (min < 0) |
| 6306 | return posix_error(); |
| 6307 | return PyLong_FromLong(min); |
| 6308 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6309 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 6310 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6311 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6312 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 6313 | /*[clinic input] |
| 6314 | os.sched_getscheduler |
| 6315 | pid: pid_t |
| 6316 | / |
| 6317 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6318 | Get the scheduling policy for the process identified by pid. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6319 | |
| 6320 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6321 | [clinic start generated code]*/ |
| 6322 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6323 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6324 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6325 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6326 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6327 | int policy; |
| 6328 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6329 | policy = sched_getscheduler(pid); |
| 6330 | if (policy < 0) |
| 6331 | return posix_error(); |
| 6332 | return PyLong_FromLong(policy); |
| 6333 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6334 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6335 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6336 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6337 | #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] | 6338 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6339 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6340 | |
| 6341 | @classmethod |
| 6342 | os.sched_param.__new__ |
| 6343 | |
| 6344 | sched_priority: object |
| 6345 | A scheduling parameter. |
| 6346 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6347 | Currently has only one field: sched_priority |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6348 | [clinic start generated code]*/ |
| 6349 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6350 | static PyObject * |
| 6351 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6352 | /*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6353 | { |
| 6354 | PyObject *res; |
| 6355 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6356 | res = PyStructSequence_New(type); |
| 6357 | if (!res) |
| 6358 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6359 | Py_INCREF(sched_priority); |
| 6360 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6361 | return res; |
| 6362 | } |
| 6363 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6364 | PyDoc_VAR(os_sched_param__doc__); |
| 6365 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6366 | static PyStructSequence_Field sched_param_fields[] = { |
| 6367 | {"sched_priority", "the scheduling priority"}, |
| 6368 | {0} |
| 6369 | }; |
| 6370 | |
| 6371 | static PyStructSequence_Desc sched_param_desc = { |
| 6372 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6373 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6374 | sched_param_fields, |
| 6375 | 1 |
| 6376 | }; |
| 6377 | |
| 6378 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6379 | convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6380 | { |
| 6381 | long priority; |
| 6382 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6383 | if (!Py_IS_TYPE(param, (PyTypeObject *)get_posix_state(module)->SchedParamType)) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6384 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6385 | return 0; |
| 6386 | } |
| 6387 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6388 | if (priority == -1 && PyErr_Occurred()) |
| 6389 | return 0; |
| 6390 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6391 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6392 | return 0; |
| 6393 | } |
| 6394 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6395 | return 1; |
| 6396 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6397 | #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] | 6398 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6399 | |
| 6400 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6401 | /*[clinic input] |
| 6402 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6403 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6404 | pid: pid_t |
| 6405 | policy: int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6406 | param as param_obj: object |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6407 | / |
| 6408 | |
| 6409 | Set the scheduling policy for the process identified by pid. |
| 6410 | |
| 6411 | If pid is 0, the calling process is changed. |
| 6412 | param is an instance of sched_param. |
| 6413 | [clinic start generated code]*/ |
| 6414 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6415 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6416 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6417 | PyObject *param_obj) |
| 6418 | /*[clinic end generated code: output=cde27faa55dc993e input=73013d731bd8fbe9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6419 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6420 | struct sched_param param; |
| 6421 | if (!convert_sched_param(module, param_obj, ¶m)) { |
| 6422 | return NULL; |
| 6423 | } |
| 6424 | |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6425 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6426 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6427 | ** scheduling policy under Solaris/Illumos, and others. |
| 6428 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6429 | */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6430 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6431 | return posix_error(); |
| 6432 | Py_RETURN_NONE; |
| 6433 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6434 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6435 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6436 | |
| 6437 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6438 | /*[clinic input] |
| 6439 | os.sched_getparam |
| 6440 | pid: pid_t |
| 6441 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6442 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6443 | Returns scheduling parameters for the process identified by pid. |
| 6444 | |
| 6445 | If pid is 0, returns parameters for the calling process. |
| 6446 | Return value is an instance of sched_param. |
| 6447 | [clinic start generated code]*/ |
| 6448 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6449 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6450 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6451 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6452 | { |
| 6453 | struct sched_param param; |
| 6454 | PyObject *result; |
| 6455 | PyObject *priority; |
| 6456 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6457 | if (sched_getparam(pid, ¶m)) |
| 6458 | return posix_error(); |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6459 | PyObject *SchedParamType = get_posix_state(module)->SchedParamType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6460 | result = PyStructSequence_New((PyTypeObject *)SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6461 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6462 | return NULL; |
| 6463 | priority = PyLong_FromLong(param.sched_priority); |
| 6464 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6465 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6466 | return NULL; |
| 6467 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6468 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6469 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6470 | } |
| 6471 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6472 | |
| 6473 | /*[clinic input] |
| 6474 | os.sched_setparam |
| 6475 | pid: pid_t |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6476 | param as param_obj: object |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6477 | / |
| 6478 | |
| 6479 | Set scheduling parameters for the process identified by pid. |
| 6480 | |
| 6481 | If pid is 0, sets parameters for the calling process. |
| 6482 | param should be an instance of sched_param. |
| 6483 | [clinic start generated code]*/ |
| 6484 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6485 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6486 | os_sched_setparam_impl(PyObject *module, pid_t pid, PyObject *param_obj) |
| 6487 | /*[clinic end generated code: output=f19fe020a53741c1 input=27b98337c8b2dcc7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6488 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6489 | struct sched_param param; |
| 6490 | if (!convert_sched_param(module, param_obj, ¶m)) { |
| 6491 | return NULL; |
| 6492 | } |
| 6493 | |
| 6494 | if (sched_setparam(pid, ¶m)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6495 | return posix_error(); |
| 6496 | Py_RETURN_NONE; |
| 6497 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6498 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6499 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6500 | |
| 6501 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6502 | /*[clinic input] |
| 6503 | os.sched_rr_get_interval -> double |
| 6504 | pid: pid_t |
| 6505 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6506 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6507 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6508 | |
| 6509 | Value returned is a float. |
| 6510 | [clinic start generated code]*/ |
| 6511 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6512 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6513 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6514 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6515 | { |
| 6516 | struct timespec interval; |
| 6517 | if (sched_rr_get_interval(pid, &interval)) { |
| 6518 | posix_error(); |
| 6519 | return -1.0; |
| 6520 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6521 | #ifdef _Py_MEMORY_SANITIZER |
| 6522 | __msan_unpoison(&interval, sizeof(interval)); |
| 6523 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6524 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6525 | } |
| 6526 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6528 | |
| 6529 | /*[clinic input] |
| 6530 | os.sched_yield |
| 6531 | |
| 6532 | Voluntarily relinquish the CPU. |
| 6533 | [clinic start generated code]*/ |
| 6534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6535 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6536 | os_sched_yield_impl(PyObject *module) |
| 6537 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6538 | { |
| 6539 | if (sched_yield()) |
| 6540 | return posix_error(); |
| 6541 | Py_RETURN_NONE; |
| 6542 | } |
| 6543 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6544 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6545 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6546 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6547 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6548 | /*[clinic input] |
| 6549 | os.sched_setaffinity |
| 6550 | pid: pid_t |
| 6551 | mask : object |
| 6552 | / |
| 6553 | |
| 6554 | Set the CPU affinity of the process identified by pid to mask. |
| 6555 | |
| 6556 | mask should be an iterable of integers identifying CPUs. |
| 6557 | [clinic start generated code]*/ |
| 6558 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6559 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6560 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6561 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6562 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6563 | int ncpus; |
| 6564 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6565 | cpu_set_t *cpu_set = NULL; |
| 6566 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6567 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6568 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6569 | if (iterator == NULL) |
| 6570 | return NULL; |
| 6571 | |
| 6572 | ncpus = NCPUS_START; |
| 6573 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6574 | cpu_set = CPU_ALLOC(ncpus); |
| 6575 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6576 | PyErr_NoMemory(); |
| 6577 | goto error; |
| 6578 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6579 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6580 | |
| 6581 | while ((item = PyIter_Next(iterator))) { |
| 6582 | long cpu; |
| 6583 | if (!PyLong_Check(item)) { |
| 6584 | PyErr_Format(PyExc_TypeError, |
| 6585 | "expected an iterator of ints, " |
| 6586 | "but iterator yielded %R", |
| 6587 | Py_TYPE(item)); |
| 6588 | Py_DECREF(item); |
| 6589 | goto error; |
| 6590 | } |
| 6591 | cpu = PyLong_AsLong(item); |
| 6592 | Py_DECREF(item); |
| 6593 | if (cpu < 0) { |
| 6594 | if (!PyErr_Occurred()) |
| 6595 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6596 | goto error; |
| 6597 | } |
| 6598 | if (cpu > INT_MAX - 1) { |
| 6599 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6600 | goto error; |
| 6601 | } |
| 6602 | if (cpu >= ncpus) { |
| 6603 | /* Grow CPU mask to fit the CPU number */ |
| 6604 | int newncpus = ncpus; |
| 6605 | cpu_set_t *newmask; |
| 6606 | size_t newsetsize; |
| 6607 | while (newncpus <= cpu) { |
| 6608 | if (newncpus > INT_MAX / 2) |
| 6609 | newncpus = cpu + 1; |
| 6610 | else |
| 6611 | newncpus = newncpus * 2; |
| 6612 | } |
| 6613 | newmask = CPU_ALLOC(newncpus); |
| 6614 | if (newmask == NULL) { |
| 6615 | PyErr_NoMemory(); |
| 6616 | goto error; |
| 6617 | } |
| 6618 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6619 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6620 | memcpy(newmask, cpu_set, setsize); |
| 6621 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6622 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6623 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6624 | ncpus = newncpus; |
| 6625 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6626 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6627 | } |
Brandt Bucher | 45a30af | 2019-06-27 09:10:57 -0700 | [diff] [blame] | 6628 | if (PyErr_Occurred()) { |
| 6629 | goto error; |
| 6630 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6631 | Py_CLEAR(iterator); |
| 6632 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6633 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6634 | posix_error(); |
| 6635 | goto error; |
| 6636 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6637 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6638 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6639 | |
| 6640 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6641 | if (cpu_set) |
| 6642 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6643 | Py_XDECREF(iterator); |
| 6644 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6645 | } |
| 6646 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6647 | |
| 6648 | /*[clinic input] |
| 6649 | os.sched_getaffinity |
| 6650 | pid: pid_t |
| 6651 | / |
| 6652 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6653 | 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] | 6654 | |
| 6655 | The affinity is returned as a set of CPU identifiers. |
| 6656 | [clinic start generated code]*/ |
| 6657 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6658 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6659 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6660 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6661 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6662 | int cpu, ncpus, count; |
| 6663 | size_t setsize; |
| 6664 | cpu_set_t *mask = NULL; |
| 6665 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6666 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6667 | ncpus = NCPUS_START; |
| 6668 | while (1) { |
| 6669 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6670 | mask = CPU_ALLOC(ncpus); |
| 6671 | if (mask == NULL) |
| 6672 | return PyErr_NoMemory(); |
| 6673 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6674 | break; |
| 6675 | CPU_FREE(mask); |
| 6676 | if (errno != EINVAL) |
| 6677 | return posix_error(); |
| 6678 | if (ncpus > INT_MAX / 2) { |
| 6679 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6680 | "a large enough CPU set"); |
| 6681 | return NULL; |
| 6682 | } |
| 6683 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6684 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6685 | |
| 6686 | res = PySet_New(NULL); |
| 6687 | if (res == NULL) |
| 6688 | goto error; |
| 6689 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6690 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6691 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6692 | --count; |
| 6693 | if (cpu_num == NULL) |
| 6694 | goto error; |
| 6695 | if (PySet_Add(res, cpu_num)) { |
| 6696 | Py_DECREF(cpu_num); |
| 6697 | goto error; |
| 6698 | } |
| 6699 | Py_DECREF(cpu_num); |
| 6700 | } |
| 6701 | } |
| 6702 | CPU_FREE(mask); |
| 6703 | return res; |
| 6704 | |
| 6705 | error: |
| 6706 | if (mask) |
| 6707 | CPU_FREE(mask); |
| 6708 | Py_XDECREF(res); |
| 6709 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6710 | } |
| 6711 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6712 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6713 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6714 | #endif /* HAVE_SCHED_H */ |
| 6715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6716 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6717 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6718 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6719 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6720 | #define DEV_PTY_FILE "/dev/ptc" |
| 6721 | #define HAVE_DEV_PTMX |
| 6722 | #else |
| 6723 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6724 | #endif |
| 6725 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6726 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6727 | #ifdef HAVE_PTY_H |
| 6728 | #include <pty.h> |
| 6729 | #else |
| 6730 | #ifdef HAVE_LIBUTIL_H |
| 6731 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6732 | #else |
| 6733 | #ifdef HAVE_UTIL_H |
| 6734 | #include <util.h> |
| 6735 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6736 | #endif /* HAVE_LIBUTIL_H */ |
| 6737 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6738 | #ifdef HAVE_STROPTS_H |
| 6739 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6740 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6741 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6742 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6743 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6744 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6745 | /*[clinic input] |
| 6746 | os.openpty |
| 6747 | |
| 6748 | Open a pseudo-terminal. |
| 6749 | |
| 6750 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6751 | for both the master and slave ends. |
| 6752 | [clinic start generated code]*/ |
| 6753 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6754 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6755 | os_openpty_impl(PyObject *module) |
| 6756 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6757 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6758 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6759 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6760 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6761 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6762 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6763 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6764 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6765 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6766 | #endif |
| 6767 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6768 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6769 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6770 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6771 | goto posix_error; |
| 6772 | |
| 6773 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6774 | goto error; |
| 6775 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6776 | goto error; |
| 6777 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6778 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6779 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6780 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6781 | goto posix_error; |
| 6782 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6783 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6784 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6785 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6786 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6787 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6788 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6789 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6790 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6791 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6792 | goto posix_error; |
| 6793 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6794 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6795 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6796 | /* change permission of slave */ |
| 6797 | if (grantpt(master_fd) < 0) { |
| 6798 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6799 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6800 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6801 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6802 | /* unlock slave */ |
| 6803 | if (unlockpt(master_fd) < 0) { |
| 6804 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6805 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6806 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6807 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6808 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6809 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6810 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6811 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6812 | goto posix_error; |
| 6813 | |
| 6814 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6815 | if (slave_fd == -1) |
| 6816 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6817 | |
| 6818 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6819 | goto posix_error; |
| 6820 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6821 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6822 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6823 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6824 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6825 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6826 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6827 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6828 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6829 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6830 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6831 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6832 | posix_error: |
| 6833 | posix_error(); |
| 6834 | error: |
| 6835 | if (master_fd != -1) |
| 6836 | close(master_fd); |
| 6837 | if (slave_fd != -1) |
| 6838 | close(slave_fd); |
| 6839 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6840 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6841 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6842 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6843 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6844 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6845 | /*[clinic input] |
| 6846 | os.forkpty |
| 6847 | |
| 6848 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6849 | |
| 6850 | Returns a tuple of (pid, master_fd). |
| 6851 | Like fork(), return pid of 0 to the child process, |
| 6852 | and pid of child to the parent process. |
| 6853 | To both, return fd of newly opened pseudo-terminal. |
| 6854 | [clinic start generated code]*/ |
| 6855 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6856 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6857 | os_forkpty_impl(PyObject *module) |
| 6858 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6859 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6860 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6861 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6862 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6863 | if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6864 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6865 | return NULL; |
| 6866 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6867 | if (PySys_Audit("os.forkpty", NULL) < 0) { |
| 6868 | return NULL; |
| 6869 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6870 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6871 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6872 | if (pid == 0) { |
| 6873 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6874 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6875 | } else { |
| 6876 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6877 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6878 | } |
| 6879 | if (pid == -1) |
| 6880 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6881 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6882 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6883 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6884 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6885 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6886 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6887 | /*[clinic input] |
| 6888 | os.getegid |
| 6889 | |
| 6890 | Return the current process's effective group id. |
| 6891 | [clinic start generated code]*/ |
| 6892 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6893 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6894 | os_getegid_impl(PyObject *module) |
| 6895 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6896 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6897 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6898 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6899 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6900 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6901 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6902 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6903 | /*[clinic input] |
| 6904 | os.geteuid |
| 6905 | |
| 6906 | Return the current process's effective user id. |
| 6907 | [clinic start generated code]*/ |
| 6908 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6909 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6910 | os_geteuid_impl(PyObject *module) |
| 6911 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6912 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6913 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6914 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6915 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6916 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6917 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6918 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6919 | /*[clinic input] |
| 6920 | os.getgid |
| 6921 | |
| 6922 | Return the current process's group id. |
| 6923 | [clinic start generated code]*/ |
| 6924 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6925 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6926 | os_getgid_impl(PyObject *module) |
| 6927 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6928 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6929 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6930 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6931 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6932 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6933 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6934 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6935 | /*[clinic input] |
| 6936 | os.getpid |
| 6937 | |
| 6938 | Return the current process id. |
| 6939 | [clinic start generated code]*/ |
| 6940 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6941 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6942 | os_getpid_impl(PyObject *module) |
| 6943 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6944 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6945 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6946 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6947 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6948 | |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6949 | #ifdef NGROUPS_MAX |
| 6950 | #define MAX_GROUPS NGROUPS_MAX |
| 6951 | #else |
| 6952 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6953 | #define MAX_GROUPS 64 |
| 6954 | #endif |
| 6955 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6956 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6957 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 6958 | #ifdef __APPLE__ |
| 6959 | /*[clinic input] |
| 6960 | os.getgrouplist |
| 6961 | |
| 6962 | user: str |
| 6963 | username to lookup |
| 6964 | group as basegid: int |
| 6965 | base group id of the user |
| 6966 | / |
| 6967 | |
| 6968 | Returns a list of groups to which a user belongs. |
| 6969 | [clinic start generated code]*/ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6970 | |
| 6971 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 6972 | os_getgrouplist_impl(PyObject *module, const char *user, int basegid) |
| 6973 | /*[clinic end generated code: output=6e734697b8c26de0 input=f8d870374b09a490]*/ |
| 6974 | #else |
| 6975 | /*[clinic input] |
| 6976 | os.getgrouplist |
| 6977 | |
| 6978 | user: str |
| 6979 | username to lookup |
| 6980 | group as basegid: gid_t |
| 6981 | base group id of the user |
| 6982 | / |
| 6983 | |
| 6984 | Returns a list of groups to which a user belongs. |
| 6985 | [clinic start generated code]*/ |
| 6986 | |
| 6987 | static PyObject * |
| 6988 | os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid) |
| 6989 | /*[clinic end generated code: output=0ebd7fb70115575b input=cc61d5c20b08958d]*/ |
| 6990 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6991 | { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6992 | int i, ngroups; |
| 6993 | PyObject *list; |
| 6994 | #ifdef __APPLE__ |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 6995 | int *groups; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6996 | #else |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 6997 | gid_t *groups; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6998 | #endif |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6999 | |
| 7000 | /* |
| 7001 | * NGROUPS_MAX is defined by POSIX.1 as the maximum |
| 7002 | * number of supplimental groups a users can belong to. |
| 7003 | * We have to increment it by one because |
| 7004 | * getgrouplist() returns both the supplemental groups |
| 7005 | * and the primary group, i.e. all of the groups the |
| 7006 | * user belongs to. |
| 7007 | */ |
| 7008 | ngroups = 1 + MAX_GROUPS; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7009 | |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7010 | while (1) { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7011 | #ifdef __APPLE__ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7012 | groups = PyMem_New(int, ngroups); |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7013 | #else |
| 7014 | groups = PyMem_New(gid_t, ngroups); |
| 7015 | #endif |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7016 | if (groups == NULL) { |
| 7017 | return PyErr_NoMemory(); |
| 7018 | } |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7019 | |
| 7020 | int old_ngroups = ngroups; |
| 7021 | if (getgrouplist(user, basegid, groups, &ngroups) != -1) { |
| 7022 | /* Success */ |
| 7023 | break; |
| 7024 | } |
| 7025 | |
| 7026 | /* getgrouplist() fails if the group list is too small */ |
| 7027 | PyMem_Free(groups); |
| 7028 | |
| 7029 | if (ngroups > old_ngroups) { |
| 7030 | /* If the group list is too small, the glibc implementation of |
| 7031 | getgrouplist() sets ngroups to the total number of groups and |
| 7032 | returns -1. */ |
| 7033 | } |
| 7034 | else { |
| 7035 | /* Double the group list size */ |
| 7036 | if (ngroups > INT_MAX / 2) { |
| 7037 | return PyErr_NoMemory(); |
| 7038 | } |
| 7039 | ngroups *= 2; |
| 7040 | } |
| 7041 | |
| 7042 | /* Retry getgrouplist() with a larger group list */ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7043 | } |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7044 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 7045 | #ifdef _Py_MEMORY_SANITIZER |
| 7046 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 7047 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 7048 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 7049 | #endif |
| 7050 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7051 | list = PyList_New(ngroups); |
| 7052 | if (list == NULL) { |
| 7053 | PyMem_Del(groups); |
| 7054 | return NULL; |
| 7055 | } |
| 7056 | |
| 7057 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7058 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7059 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7060 | #else |
| 7061 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 7062 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7063 | if (o == NULL) { |
| 7064 | Py_DECREF(list); |
| 7065 | PyMem_Del(groups); |
| 7066 | return NULL; |
| 7067 | } |
| 7068 | PyList_SET_ITEM(list, i, o); |
| 7069 | } |
| 7070 | |
| 7071 | PyMem_Del(groups); |
| 7072 | |
| 7073 | return list; |
| 7074 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7075 | #endif /* HAVE_GETGROUPLIST */ |
| 7076 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7077 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7078 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7079 | /*[clinic input] |
| 7080 | os.getgroups |
| 7081 | |
| 7082 | Return list of supplemental group IDs for the process. |
| 7083 | [clinic start generated code]*/ |
| 7084 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7085 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7086 | os_getgroups_impl(PyObject *module) |
| 7087 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7088 | { |
| 7089 | PyObject *result = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7090 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7091 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7092 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7093 | * This is a helper variable to store the intermediate result when |
| 7094 | * that happens. |
| 7095 | * |
| 7096 | * To keep the code readable the OSX behaviour is unconditional, |
| 7097 | * according to the POSIX spec this should be safe on all unix-y |
| 7098 | * systems. |
| 7099 | */ |
| 7100 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7101 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7102 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7103 | #ifdef __APPLE__ |
| 7104 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 7105 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 7106 | * always first call getgroups with length 0 to get the actual number |
| 7107 | * of groups. |
| 7108 | */ |
| 7109 | n = getgroups(0, NULL); |
| 7110 | if (n < 0) { |
| 7111 | return posix_error(); |
| 7112 | } else if (n <= MAX_GROUPS) { |
| 7113 | /* groups will fit in existing array */ |
| 7114 | alt_grouplist = grouplist; |
| 7115 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7116 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7117 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7118 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7119 | } |
| 7120 | } |
| 7121 | |
| 7122 | n = getgroups(n, alt_grouplist); |
| 7123 | if (n == -1) { |
| 7124 | if (alt_grouplist != grouplist) { |
| 7125 | PyMem_Free(alt_grouplist); |
| 7126 | } |
| 7127 | return posix_error(); |
| 7128 | } |
| 7129 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7130 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7131 | if (n < 0) { |
| 7132 | if (errno == EINVAL) { |
| 7133 | n = getgroups(0, NULL); |
| 7134 | if (n == -1) { |
| 7135 | return posix_error(); |
| 7136 | } |
| 7137 | if (n == 0) { |
| 7138 | /* Avoid malloc(0) */ |
| 7139 | alt_grouplist = grouplist; |
| 7140 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7141 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7142 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7143 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7144 | } |
| 7145 | n = getgroups(n, alt_grouplist); |
| 7146 | if (n == -1) { |
| 7147 | PyMem_Free(alt_grouplist); |
| 7148 | return posix_error(); |
| 7149 | } |
| 7150 | } |
| 7151 | } else { |
| 7152 | return posix_error(); |
| 7153 | } |
| 7154 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7155 | #endif |
| 7156 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7157 | result = PyList_New(n); |
| 7158 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7159 | int i; |
| 7160 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7161 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7162 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7163 | Py_DECREF(result); |
| 7164 | result = NULL; |
| 7165 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7166 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7167 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7168 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7169 | } |
| 7170 | |
| 7171 | if (alt_grouplist != grouplist) { |
| 7172 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7173 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7174 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7175 | return result; |
| 7176 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7177 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7178 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7179 | #ifdef HAVE_INITGROUPS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7180 | #ifdef __APPLE__ |
| 7181 | /*[clinic input] |
| 7182 | os.initgroups |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7183 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7184 | username as oname: FSConverter |
| 7185 | gid: int |
| 7186 | / |
| 7187 | |
| 7188 | Initialize the group access list. |
| 7189 | |
| 7190 | Call the system initgroups() to initialize the group access list with all of |
| 7191 | the groups of which the specified username is a member, plus the specified |
| 7192 | group id. |
| 7193 | [clinic start generated code]*/ |
| 7194 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7195 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7196 | os_initgroups_impl(PyObject *module, PyObject *oname, int gid) |
| 7197 | /*[clinic end generated code: output=7f074d30a425fd3a input=df3d54331b0af204]*/ |
| 7198 | #else |
| 7199 | /*[clinic input] |
| 7200 | os.initgroups |
| 7201 | |
| 7202 | username as oname: FSConverter |
| 7203 | gid: gid_t |
| 7204 | / |
| 7205 | |
| 7206 | Initialize the group access list. |
| 7207 | |
| 7208 | Call the system initgroups() to initialize the group access list with all of |
| 7209 | the groups of which the specified username is a member, plus the specified |
| 7210 | group id. |
| 7211 | [clinic start generated code]*/ |
| 7212 | |
| 7213 | static PyObject * |
| 7214 | os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid) |
| 7215 | /*[clinic end generated code: output=59341244521a9e3f input=0cb91bdc59a4c564]*/ |
| 7216 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7217 | { |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7218 | const char *username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7219 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7220 | if (initgroups(username, gid) == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7221 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7222 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7223 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7224 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7225 | #endif /* HAVE_INITGROUPS */ |
| 7226 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7227 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7228 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7229 | /*[clinic input] |
| 7230 | os.getpgid |
| 7231 | |
| 7232 | pid: pid_t |
| 7233 | |
| 7234 | Call the system call getpgid(), and return the result. |
| 7235 | [clinic start generated code]*/ |
| 7236 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7237 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7238 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 7239 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7240 | { |
| 7241 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7242 | if (pgid < 0) |
| 7243 | return posix_error(); |
| 7244 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7245 | } |
| 7246 | #endif /* HAVE_GETPGID */ |
| 7247 | |
| 7248 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7249 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7250 | /*[clinic input] |
| 7251 | os.getpgrp |
| 7252 | |
| 7253 | Return the current process group id. |
| 7254 | [clinic start generated code]*/ |
| 7255 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7256 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7257 | os_getpgrp_impl(PyObject *module) |
| 7258 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7259 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7260 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7261 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7262 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7263 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7264 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7265 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7266 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7267 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7268 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7269 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7270 | /*[clinic input] |
| 7271 | os.setpgrp |
| 7272 | |
| 7273 | Make the current process the leader of its process group. |
| 7274 | [clinic start generated code]*/ |
| 7275 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7276 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7277 | os_setpgrp_impl(PyObject *module) |
| 7278 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7279 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7280 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7281 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7282 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7283 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7284 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7285 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7286 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7287 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7288 | #endif /* HAVE_SETPGRP */ |
| 7289 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7290 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7291 | |
| 7292 | #ifdef MS_WINDOWS |
| 7293 | #include <tlhelp32.h> |
| 7294 | |
| 7295 | static PyObject* |
| 7296 | win32_getppid() |
| 7297 | { |
| 7298 | HANDLE snapshot; |
| 7299 | pid_t mypid; |
| 7300 | PyObject* result = NULL; |
| 7301 | BOOL have_record; |
| 7302 | PROCESSENTRY32 pe; |
| 7303 | |
| 7304 | mypid = getpid(); /* This function never fails */ |
| 7305 | |
| 7306 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 7307 | if (snapshot == INVALID_HANDLE_VALUE) |
| 7308 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 7309 | |
| 7310 | pe.dwSize = sizeof(pe); |
| 7311 | have_record = Process32First(snapshot, &pe); |
| 7312 | while (have_record) { |
| 7313 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 7314 | /* We could cache the ulong value in a static variable. */ |
| 7315 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 7316 | break; |
| 7317 | } |
| 7318 | |
| 7319 | have_record = Process32Next(snapshot, &pe); |
| 7320 | } |
| 7321 | |
| 7322 | /* If our loop exits and our pid was not found (result will be NULL) |
| 7323 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 7324 | * error anyway, so let's raise it. */ |
| 7325 | if (!result) |
| 7326 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7327 | |
| 7328 | CloseHandle(snapshot); |
| 7329 | |
| 7330 | return result; |
| 7331 | } |
| 7332 | #endif /*MS_WINDOWS*/ |
| 7333 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7334 | |
| 7335 | /*[clinic input] |
| 7336 | os.getppid |
| 7337 | |
| 7338 | Return the parent's process id. |
| 7339 | |
| 7340 | If the parent process has already exited, Windows machines will still |
| 7341 | return its id; others systems will return the id of the 'init' process (1). |
| 7342 | [clinic start generated code]*/ |
| 7343 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7344 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7345 | os_getppid_impl(PyObject *module) |
| 7346 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7347 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7348 | #ifdef MS_WINDOWS |
| 7349 | return win32_getppid(); |
| 7350 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7351 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7352 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7353 | } |
| 7354 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7355 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7356 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7357 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7358 | /*[clinic input] |
| 7359 | os.getlogin |
| 7360 | |
| 7361 | Return the actual login name. |
| 7362 | [clinic start generated code]*/ |
| 7363 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7364 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7365 | os_getlogin_impl(PyObject *module) |
| 7366 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7367 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7368 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7369 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7370 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7371 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7372 | |
| 7373 | if (GetUserNameW(user_name, &num_chars)) { |
| 7374 | /* num_chars is the number of unicode chars plus null terminator */ |
| 7375 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7376 | } |
| 7377 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7378 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7379 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7380 | char *name; |
| 7381 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7382 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7383 | errno = 0; |
| 7384 | name = getlogin(); |
| 7385 | if (name == NULL) { |
| 7386 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7387 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7388 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7389 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7390 | } |
| 7391 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7392 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7393 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7394 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7395 | return result; |
| 7396 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7397 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7399 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7400 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7401 | /*[clinic input] |
| 7402 | os.getuid |
| 7403 | |
| 7404 | Return the current process's user id. |
| 7405 | [clinic start generated code]*/ |
| 7406 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7407 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7408 | os_getuid_impl(PyObject *module) |
| 7409 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7410 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7411 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7412 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7413 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7414 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7415 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7416 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7417 | #define HAVE_KILL |
| 7418 | #endif /* MS_WINDOWS */ |
| 7419 | |
| 7420 | #ifdef HAVE_KILL |
| 7421 | /*[clinic input] |
| 7422 | os.kill |
| 7423 | |
| 7424 | pid: pid_t |
| 7425 | signal: Py_ssize_t |
| 7426 | / |
| 7427 | |
| 7428 | Kill a process with a signal. |
| 7429 | [clinic start generated code]*/ |
| 7430 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7431 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7432 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7433 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7434 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7435 | if (PySys_Audit("os.kill", "in", pid, signal) < 0) { |
| 7436 | return NULL; |
| 7437 | } |
| 7438 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7439 | if (kill(pid, (int)signal) == -1) |
| 7440 | return posix_error(); |
| 7441 | Py_RETURN_NONE; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7442 | #else /* !MS_WINDOWS */ |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7443 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7444 | DWORD sig = (DWORD)signal; |
| 7445 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7446 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7447 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7448 | /* Console processes which share a common console can be sent CTRL+C or |
| 7449 | CTRL+BREAK events, provided they handle said events. */ |
| 7450 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7451 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7452 | err = GetLastError(); |
| 7453 | PyErr_SetFromWindowsErr(err); |
| 7454 | } |
| 7455 | else |
| 7456 | Py_RETURN_NONE; |
| 7457 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7458 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7459 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7460 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7461 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7462 | if (handle == NULL) { |
| 7463 | err = GetLastError(); |
| 7464 | return PyErr_SetFromWindowsErr(err); |
| 7465 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7466 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7467 | if (TerminateProcess(handle, sig) == 0) { |
| 7468 | err = GetLastError(); |
| 7469 | result = PyErr_SetFromWindowsErr(err); |
| 7470 | } else { |
| 7471 | Py_INCREF(Py_None); |
| 7472 | result = Py_None; |
| 7473 | } |
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 | CloseHandle(handle); |
| 7476 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7477 | #endif /* !MS_WINDOWS */ |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7478 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7479 | #endif /* HAVE_KILL */ |
| 7480 | |
| 7481 | |
| 7482 | #ifdef HAVE_KILLPG |
| 7483 | /*[clinic input] |
| 7484 | os.killpg |
| 7485 | |
| 7486 | pgid: pid_t |
| 7487 | signal: int |
| 7488 | / |
| 7489 | |
| 7490 | Kill a process group with a signal. |
| 7491 | [clinic start generated code]*/ |
| 7492 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7493 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7494 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7495 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7496 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7497 | if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) { |
| 7498 | return NULL; |
| 7499 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7500 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7501 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7502 | take the same type. Moreover, pid_t is always at least as wide as |
| 7503 | int (else compilation of this module fails), which is safe. */ |
| 7504 | if (killpg(pgid, signal) == -1) |
| 7505 | return posix_error(); |
| 7506 | Py_RETURN_NONE; |
| 7507 | } |
| 7508 | #endif /* HAVE_KILLPG */ |
| 7509 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7510 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7511 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7512 | #ifdef HAVE_SYS_LOCK_H |
| 7513 | #include <sys/lock.h> |
| 7514 | #endif |
| 7515 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7516 | /*[clinic input] |
| 7517 | os.plock |
| 7518 | op: int |
| 7519 | / |
| 7520 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7521 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7522 | [clinic start generated code]*/ |
| 7523 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7524 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7525 | os_plock_impl(PyObject *module, int op) |
| 7526 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7527 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7528 | if (plock(op) == -1) |
| 7529 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7530 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7531 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7532 | #endif /* HAVE_PLOCK */ |
| 7533 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7534 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7535 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7536 | /*[clinic input] |
| 7537 | os.setuid |
| 7538 | |
| 7539 | uid: uid_t |
| 7540 | / |
| 7541 | |
| 7542 | Set the current process's user id. |
| 7543 | [clinic start generated code]*/ |
| 7544 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7545 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7546 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7547 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7548 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7549 | if (setuid(uid) < 0) |
| 7550 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7551 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7552 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7553 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7554 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7555 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7556 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7557 | /*[clinic input] |
| 7558 | os.seteuid |
| 7559 | |
| 7560 | euid: uid_t |
| 7561 | / |
| 7562 | |
| 7563 | Set the current process's effective user id. |
| 7564 | [clinic start generated code]*/ |
| 7565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7566 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7567 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7568 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7569 | { |
| 7570 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7571 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7572 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7573 | } |
| 7574 | #endif /* HAVE_SETEUID */ |
| 7575 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7576 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7577 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7578 | /*[clinic input] |
| 7579 | os.setegid |
| 7580 | |
| 7581 | egid: gid_t |
| 7582 | / |
| 7583 | |
| 7584 | Set the current process's effective group id. |
| 7585 | [clinic start generated code]*/ |
| 7586 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7587 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7588 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7589 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7590 | { |
| 7591 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7592 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7593 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7594 | } |
| 7595 | #endif /* HAVE_SETEGID */ |
| 7596 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7597 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7598 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7599 | /*[clinic input] |
| 7600 | os.setreuid |
| 7601 | |
| 7602 | ruid: uid_t |
| 7603 | euid: uid_t |
| 7604 | / |
| 7605 | |
| 7606 | Set the current process's real and effective user ids. |
| 7607 | [clinic start generated code]*/ |
| 7608 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7609 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7610 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7611 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7612 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7613 | if (setreuid(ruid, euid) < 0) { |
| 7614 | return posix_error(); |
| 7615 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7616 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7617 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7618 | } |
| 7619 | #endif /* HAVE_SETREUID */ |
| 7620 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7621 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7622 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7623 | /*[clinic input] |
| 7624 | os.setregid |
| 7625 | |
| 7626 | rgid: gid_t |
| 7627 | egid: gid_t |
| 7628 | / |
| 7629 | |
| 7630 | Set the current process's real and effective group ids. |
| 7631 | [clinic start generated code]*/ |
| 7632 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7633 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7634 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7635 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7636 | { |
| 7637 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7638 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7639 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7640 | } |
| 7641 | #endif /* HAVE_SETREGID */ |
| 7642 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7643 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7644 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7645 | /*[clinic input] |
| 7646 | os.setgid |
| 7647 | gid: gid_t |
| 7648 | / |
| 7649 | |
| 7650 | Set the current process's group id. |
| 7651 | [clinic start generated code]*/ |
| 7652 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7653 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7654 | os_setgid_impl(PyObject *module, gid_t gid) |
| 7655 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7656 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7657 | if (setgid(gid) < 0) |
| 7658 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7659 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7660 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7661 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7662 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7663 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7664 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7665 | /*[clinic input] |
| 7666 | os.setgroups |
| 7667 | |
| 7668 | groups: object |
| 7669 | / |
| 7670 | |
| 7671 | Set the groups of the current process to list. |
| 7672 | [clinic start generated code]*/ |
| 7673 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7674 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7675 | os_setgroups(PyObject *module, PyObject *groups) |
| 7676 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7677 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7678 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7679 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7680 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7681 | if (!PySequence_Check(groups)) { |
| 7682 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7683 | return NULL; |
| 7684 | } |
| 7685 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7686 | if (len < 0) { |
| 7687 | return NULL; |
| 7688 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7689 | if (len > MAX_GROUPS) { |
| 7690 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7691 | return NULL; |
| 7692 | } |
| 7693 | for(i = 0; i < len; i++) { |
| 7694 | PyObject *elem; |
| 7695 | elem = PySequence_GetItem(groups, i); |
| 7696 | if (!elem) |
| 7697 | return NULL; |
| 7698 | if (!PyLong_Check(elem)) { |
| 7699 | PyErr_SetString(PyExc_TypeError, |
| 7700 | "groups must be integers"); |
| 7701 | Py_DECREF(elem); |
| 7702 | return NULL; |
| 7703 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7704 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7705 | Py_DECREF(elem); |
| 7706 | return NULL; |
| 7707 | } |
| 7708 | } |
| 7709 | Py_DECREF(elem); |
| 7710 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7711 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7712 | if (setgroups(len, grouplist) < 0) |
| 7713 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7714 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7715 | } |
| 7716 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7717 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7718 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7719 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7720 | wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7721 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7722 | PyObject *result; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7723 | PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7724 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7725 | if (pid == -1) |
| 7726 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7727 | |
Zackery Spytz | 682107c | 2019-09-09 09:48:32 -0600 | [diff] [blame] | 7728 | // If wait succeeded but no child was ready to report status, ru will not |
| 7729 | // have been populated. |
| 7730 | if (pid == 0) { |
| 7731 | memset(ru, 0, sizeof(*ru)); |
| 7732 | } |
| 7733 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7734 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7735 | if (m == NULL) |
| 7736 | return NULL; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7737 | struct_rusage = PyObject_GetAttr(m, get_posix_state(module)->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7738 | Py_DECREF(m); |
| 7739 | if (struct_rusage == NULL) |
| 7740 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7741 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7742 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7743 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
Eddie Elizondo | e4db1f0 | 2019-11-25 19:07:37 -0800 | [diff] [blame] | 7744 | Py_DECREF(struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7745 | if (!result) |
| 7746 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7747 | |
| 7748 | #ifndef doubletime |
| 7749 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7750 | #endif |
| 7751 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7752 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7753 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7754 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7755 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7756 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7757 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7758 | SET_INT(result, 2, ru->ru_maxrss); |
| 7759 | SET_INT(result, 3, ru->ru_ixrss); |
| 7760 | SET_INT(result, 4, ru->ru_idrss); |
| 7761 | SET_INT(result, 5, ru->ru_isrss); |
| 7762 | SET_INT(result, 6, ru->ru_minflt); |
| 7763 | SET_INT(result, 7, ru->ru_majflt); |
| 7764 | SET_INT(result, 8, ru->ru_nswap); |
| 7765 | SET_INT(result, 9, ru->ru_inblock); |
| 7766 | SET_INT(result, 10, ru->ru_oublock); |
| 7767 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7768 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7769 | SET_INT(result, 13, ru->ru_nsignals); |
| 7770 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7771 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7772 | #undef SET_INT |
| 7773 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7774 | if (PyErr_Occurred()) { |
| 7775 | Py_DECREF(result); |
| 7776 | return NULL; |
| 7777 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7778 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7779 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7780 | } |
| 7781 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7782 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7783 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7784 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7785 | /*[clinic input] |
| 7786 | os.wait3 |
| 7787 | |
| 7788 | options: int |
| 7789 | Wait for completion of a child process. |
| 7790 | |
| 7791 | Returns a tuple of information about the child process: |
| 7792 | (pid, status, rusage) |
| 7793 | [clinic start generated code]*/ |
| 7794 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7795 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7796 | os_wait3_impl(PyObject *module, int options) |
| 7797 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7798 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7799 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7800 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7801 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7802 | WAIT_TYPE status; |
| 7803 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7804 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7805 | do { |
| 7806 | Py_BEGIN_ALLOW_THREADS |
| 7807 | pid = wait3(&status, options, &ru); |
| 7808 | Py_END_ALLOW_THREADS |
| 7809 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7810 | if (pid < 0) |
| 7811 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7812 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7813 | return wait_helper(module, pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7814 | } |
| 7815 | #endif /* HAVE_WAIT3 */ |
| 7816 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7817 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7818 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7819 | /*[clinic input] |
| 7820 | |
| 7821 | os.wait4 |
| 7822 | |
| 7823 | pid: pid_t |
| 7824 | options: int |
| 7825 | |
| 7826 | Wait for completion of a specific child process. |
| 7827 | |
| 7828 | Returns a tuple of information about the child process: |
| 7829 | (pid, status, rusage) |
| 7830 | [clinic start generated code]*/ |
| 7831 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7832 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7833 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7834 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7835 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7836 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7837 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7838 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7839 | WAIT_TYPE status; |
| 7840 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7841 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7842 | do { |
| 7843 | Py_BEGIN_ALLOW_THREADS |
| 7844 | res = wait4(pid, &status, options, &ru); |
| 7845 | Py_END_ALLOW_THREADS |
| 7846 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7847 | if (res < 0) |
| 7848 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7849 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 7850 | return wait_helper(module, res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7851 | } |
| 7852 | #endif /* HAVE_WAIT4 */ |
| 7853 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7854 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7855 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7856 | /*[clinic input] |
| 7857 | os.waitid |
| 7858 | |
| 7859 | idtype: idtype_t |
| 7860 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7861 | id: id_t |
| 7862 | The id to wait on. |
| 7863 | options: int |
| 7864 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7865 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7866 | / |
| 7867 | |
| 7868 | Returns the result of waiting for a process or processes. |
| 7869 | |
| 7870 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7871 | no children in a waitable state. |
| 7872 | [clinic start generated code]*/ |
| 7873 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7874 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7875 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7876 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7877 | { |
| 7878 | PyObject *result; |
| 7879 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7880 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7881 | siginfo_t si; |
| 7882 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7883 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7884 | do { |
| 7885 | Py_BEGIN_ALLOW_THREADS |
| 7886 | res = waitid(idtype, id, &si, options); |
| 7887 | Py_END_ALLOW_THREADS |
| 7888 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7889 | if (res < 0) |
| 7890 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7891 | |
| 7892 | if (si.si_pid == 0) |
| 7893 | Py_RETURN_NONE; |
| 7894 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 7895 | PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7896 | result = PyStructSequence_New((PyTypeObject *)WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7897 | if (!result) |
| 7898 | return NULL; |
| 7899 | |
| 7900 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7901 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7902 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7903 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7904 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7905 | if (PyErr_Occurred()) { |
| 7906 | Py_DECREF(result); |
| 7907 | return NULL; |
| 7908 | } |
| 7909 | |
| 7910 | return result; |
| 7911 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7912 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7913 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7914 | |
| 7915 | #if defined(HAVE_WAITPID) |
| 7916 | /*[clinic input] |
| 7917 | os.waitpid |
| 7918 | pid: pid_t |
| 7919 | options: int |
| 7920 | / |
| 7921 | |
| 7922 | Wait for completion of a given child process. |
| 7923 | |
| 7924 | Returns a tuple of information regarding the child process: |
| 7925 | (pid, status) |
| 7926 | |
| 7927 | The options argument is ignored on Windows. |
| 7928 | [clinic start generated code]*/ |
| 7929 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7930 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7931 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7932 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7933 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7934 | pid_t res; |
| 7935 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7936 | WAIT_TYPE status; |
| 7937 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7938 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7939 | do { |
| 7940 | Py_BEGIN_ALLOW_THREADS |
| 7941 | res = waitpid(pid, &status, options); |
| 7942 | Py_END_ALLOW_THREADS |
| 7943 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7944 | if (res < 0) |
| 7945 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7946 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7947 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7948 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7949 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7950 | /* 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] | 7951 | /*[clinic input] |
| 7952 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7953 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7954 | options: int |
| 7955 | / |
| 7956 | |
| 7957 | Wait for completion of a given process. |
| 7958 | |
| 7959 | Returns a tuple of information regarding the process: |
| 7960 | (pid, status << 8) |
| 7961 | |
| 7962 | The options argument is ignored on Windows. |
| 7963 | [clinic start generated code]*/ |
| 7964 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7965 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7966 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7967 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7968 | { |
| 7969 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7970 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7971 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7972 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7973 | do { |
| 7974 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7975 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7976 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7977 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7978 | Py_END_ALLOW_THREADS |
| 7979 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7980 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7981 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7982 | |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 7983 | unsigned long long ustatus = (unsigned int)status; |
| 7984 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7985 | /* 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] | 7986 | return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7987 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7988 | #endif |
| 7989 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7990 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7991 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7992 | /*[clinic input] |
| 7993 | os.wait |
| 7994 | |
| 7995 | Wait for completion of a child process. |
| 7996 | |
| 7997 | Returns a tuple of information about the child process: |
| 7998 | (pid, status) |
| 7999 | [clinic start generated code]*/ |
| 8000 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8001 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8002 | os_wait_impl(PyObject *module) |
| 8003 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 8004 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8005 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8006 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8007 | WAIT_TYPE status; |
| 8008 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8009 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8010 | do { |
| 8011 | Py_BEGIN_ALLOW_THREADS |
| 8012 | pid = wait(&status); |
| 8013 | Py_END_ALLOW_THREADS |
| 8014 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8015 | if (pid < 0) |
| 8016 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8017 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8018 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8019 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8020 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8021 | |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 8022 | #if defined(__linux__) && defined(__NR_pidfd_open) |
| 8023 | /*[clinic input] |
| 8024 | os.pidfd_open |
| 8025 | pid: pid_t |
| 8026 | flags: unsigned_int = 0 |
| 8027 | |
| 8028 | Return a file descriptor referring to the process *pid*. |
| 8029 | |
| 8030 | The descriptor can be used to perform process management without races and |
| 8031 | signals. |
| 8032 | [clinic start generated code]*/ |
| 8033 | |
| 8034 | static PyObject * |
| 8035 | os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags) |
| 8036 | /*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/ |
| 8037 | { |
| 8038 | int fd = syscall(__NR_pidfd_open, pid, flags); |
| 8039 | if (fd < 0) { |
| 8040 | return posix_error(); |
| 8041 | } |
| 8042 | return PyLong_FromLong(fd); |
| 8043 | } |
| 8044 | #endif |
| 8045 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8046 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8047 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8048 | /*[clinic input] |
| 8049 | os.readlink |
| 8050 | |
| 8051 | path: path_t |
| 8052 | * |
| 8053 | dir_fd: dir_fd(requires='readlinkat') = None |
| 8054 | |
| 8055 | Return a string representing the path to which the symbolic link points. |
| 8056 | |
| 8057 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8058 | and path should be relative; path will then be relative to that directory. |
| 8059 | |
| 8060 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 8061 | using it will raise a NotImplementedError. |
| 8062 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8063 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8064 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8065 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 8066 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8067 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8068 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 8069 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8070 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8071 | |
| 8072 | Py_BEGIN_ALLOW_THREADS |
| 8073 | #ifdef HAVE_READLINKAT |
| 8074 | if (dir_fd != DEFAULT_DIR_FD) |
| 8075 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 8076 | else |
| 8077 | #endif |
| 8078 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 8079 | Py_END_ALLOW_THREADS |
| 8080 | |
| 8081 | if (length < 0) { |
| 8082 | return path_error(path); |
| 8083 | } |
| 8084 | buffer[length] = '\0'; |
| 8085 | |
| 8086 | if (PyUnicode_Check(path->object)) |
| 8087 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 8088 | else |
| 8089 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8090 | #elif defined(MS_WINDOWS) |
| 8091 | DWORD n_bytes_returned; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8092 | DWORD io_result = 0; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8093 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8094 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 8095 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8096 | PyObject *result = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8098 | /* First get a handle to the reparse point */ |
| 8099 | Py_BEGIN_ALLOW_THREADS |
| 8100 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8101 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8102 | 0, |
| 8103 | 0, |
| 8104 | 0, |
| 8105 | OPEN_EXISTING, |
| 8106 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 8107 | 0); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8108 | if (reparse_point_handle != INVALID_HANDLE_VALUE) { |
| 8109 | /* New call DeviceIoControl to read the reparse point */ |
| 8110 | io_result = DeviceIoControl( |
| 8111 | reparse_point_handle, |
| 8112 | FSCTL_GET_REPARSE_POINT, |
| 8113 | 0, 0, /* in buffer */ |
| 8114 | target_buffer, sizeof(target_buffer), |
| 8115 | &n_bytes_returned, |
| 8116 | 0 /* we're not using OVERLAPPED_IO */ |
| 8117 | ); |
| 8118 | CloseHandle(reparse_point_handle); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8119 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8120 | Py_END_ALLOW_THREADS |
| 8121 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8122 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8123 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8124 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8125 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8126 | wchar_t *name = NULL; |
| 8127 | Py_ssize_t nameLen = 0; |
| 8128 | if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8129 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8130 | name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 8131 | rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 8132 | nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8133 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8134 | else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) |
| 8135 | { |
| 8136 | name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer + |
| 8137 | rdb->MountPointReparseBuffer.SubstituteNameOffset); |
| 8138 | nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
| 8139 | } |
| 8140 | else |
| 8141 | { |
| 8142 | PyErr_SetString(PyExc_ValueError, "not a symbolic link"); |
| 8143 | } |
| 8144 | if (name) { |
| 8145 | if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) { |
| 8146 | /* Our buffer is mutable, so this is okay */ |
| 8147 | name[1] = L'\\'; |
| 8148 | } |
| 8149 | result = PyUnicode_FromWideChar(name, nameLen); |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8150 | if (result && path->narrow) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8151 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
| 8152 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8153 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8154 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8155 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8156 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8157 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8158 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8159 | #if defined(MS_WINDOWS) |
| 8160 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8161 | /* Remove the last portion of the path - return 0 on success */ |
| 8162 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8163 | _dirnameW(WCHAR *path) |
| 8164 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8165 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8166 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 8167 | if (length == MAX_PATH) { |
| 8168 | return -1; |
| 8169 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8170 | |
| 8171 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8172 | for(ptr = path + length; ptr != path; ptr--) { |
| 8173 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8174 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8175 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8176 | } |
| 8177 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8178 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8179 | } |
| 8180 | |
Minmin Gong | 7f21c9a | 2020-05-18 09:17:19 -0700 | [diff] [blame] | 8181 | #endif |
| 8182 | |
| 8183 | #ifdef HAVE_SYMLINK |
| 8184 | |
| 8185 | #if defined(MS_WINDOWS) |
| 8186 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8187 | /* Is this path absolute? */ |
| 8188 | static int |
| 8189 | _is_absW(const WCHAR *path) |
| 8190 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8191 | return path[0] == L'\\' || path[0] == L'/' || |
| 8192 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8193 | } |
| 8194 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8195 | /* join root and rest with a backslash - return 0 on success */ |
| 8196 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8197 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 8198 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8199 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8200 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8201 | } |
| 8202 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8203 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 8204 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8205 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8206 | |
| 8207 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 8208 | return -1; |
| 8209 | } |
| 8210 | |
| 8211 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8212 | } |
| 8213 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8214 | /* Return True if the path at src relative to dest is a directory */ |
| 8215 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8216 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8217 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8218 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 8219 | WCHAR dest_parent[MAX_PATH]; |
| 8220 | WCHAR src_resolved[MAX_PATH] = L""; |
| 8221 | |
| 8222 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8223 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 8224 | _dirnameW(dest_parent)) { |
| 8225 | return 0; |
| 8226 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8227 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8228 | if (_joinW(src_resolved, dest_parent, src)) { |
| 8229 | return 0; |
| 8230 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8231 | return ( |
| 8232 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 8233 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 8234 | ); |
| 8235 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8236 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8237 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8238 | |
| 8239 | /*[clinic input] |
| 8240 | os.symlink |
| 8241 | src: path_t |
| 8242 | dst: path_t |
| 8243 | target_is_directory: bool = False |
| 8244 | * |
| 8245 | dir_fd: dir_fd(requires='symlinkat')=None |
| 8246 | |
| 8247 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 8248 | |
| 8249 | Create a symbolic link pointing to src named dst. |
| 8250 | |
| 8251 | target_is_directory is required on Windows if the target is to be |
| 8252 | interpreted as a directory. (On Windows, symlink requires |
| 8253 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 8254 | target_is_directory is ignored on non-Windows platforms. |
| 8255 | |
| 8256 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8257 | and path should be relative; path will then be relative to that directory. |
| 8258 | dir_fd may not be implemented on your platform. |
| 8259 | If it is unavailable, using it will raise a NotImplementedError. |
| 8260 | |
| 8261 | [clinic start generated code]*/ |
| 8262 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8263 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8264 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8265 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8266 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8267 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8268 | #ifdef MS_WINDOWS |
| 8269 | DWORD result; |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8270 | DWORD flags = 0; |
| 8271 | |
| 8272 | /* Assumed true, set to false if detected to not be available. */ |
| 8273 | static int windows_has_symlink_unprivileged_flag = TRUE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8274 | #else |
| 8275 | int result; |
| 8276 | #endif |
| 8277 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 8278 | if (PySys_Audit("os.symlink", "OOi", src->object, dst->object, |
| 8279 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 8280 | return NULL; |
| 8281 | } |
| 8282 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8283 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8284 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8285 | if (windows_has_symlink_unprivileged_flag) { |
| 8286 | /* Allow non-admin symlinks if system allows it. */ |
| 8287 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 8288 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8289 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8290 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8291 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8292 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
| 8293 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { |
| 8294 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 8295 | } |
| 8296 | |
| 8297 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8298 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8299 | Py_END_ALLOW_THREADS |
| 8300 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8301 | if (windows_has_symlink_unprivileged_flag && !result && |
| 8302 | ERROR_INVALID_PARAMETER == GetLastError()) { |
| 8303 | |
| 8304 | Py_BEGIN_ALLOW_THREADS |
| 8305 | _Py_BEGIN_SUPPRESS_IPH |
| 8306 | /* This error might be caused by |
| 8307 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. |
| 8308 | Try again, and update windows_has_symlink_unprivileged_flag if we |
| 8309 | are successful this time. |
| 8310 | |
| 8311 | NOTE: There is a risk of a race condition here if there are other |
| 8312 | conditions than the flag causing ERROR_INVALID_PARAMETER, and |
| 8313 | another process (or thread) changes that condition in between our |
| 8314 | calls to CreateSymbolicLink. |
| 8315 | */ |
| 8316 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 8317 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
| 8318 | _Py_END_SUPPRESS_IPH |
| 8319 | Py_END_ALLOW_THREADS |
| 8320 | |
| 8321 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { |
| 8322 | windows_has_symlink_unprivileged_flag = FALSE; |
| 8323 | } |
| 8324 | } |
| 8325 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8326 | if (!result) |
| 8327 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8328 | |
| 8329 | #else |
| 8330 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8331 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 8332 | PyErr_SetString(PyExc_ValueError, |
| 8333 | "symlink: src and dst must be the same type"); |
| 8334 | return NULL; |
| 8335 | } |
| 8336 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8337 | Py_BEGIN_ALLOW_THREADS |
| 8338 | #if HAVE_SYMLINKAT |
| 8339 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8340 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8341 | else |
| 8342 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8343 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8344 | Py_END_ALLOW_THREADS |
| 8345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8346 | if (result) |
| 8347 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8348 | #endif |
| 8349 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8350 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8351 | } |
| 8352 | #endif /* HAVE_SYMLINK */ |
| 8353 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8354 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8355 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8356 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8357 | static PyStructSequence_Field times_result_fields[] = { |
| 8358 | {"user", "user time"}, |
| 8359 | {"system", "system time"}, |
| 8360 | {"children_user", "user time of children"}, |
| 8361 | {"children_system", "system time of children"}, |
| 8362 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 8363 | {NULL} |
| 8364 | }; |
| 8365 | |
| 8366 | PyDoc_STRVAR(times_result__doc__, |
| 8367 | "times_result: Result from os.times().\n\n\ |
| 8368 | This object may be accessed either as a tuple of\n\ |
| 8369 | (user, system, children_user, children_system, elapsed),\n\ |
| 8370 | or via the attributes user, system, children_user, children_system,\n\ |
| 8371 | and elapsed.\n\ |
| 8372 | \n\ |
| 8373 | See os.times for more information."); |
| 8374 | |
| 8375 | static PyStructSequence_Desc times_result_desc = { |
| 8376 | "times_result", /* name */ |
| 8377 | times_result__doc__, /* doc */ |
| 8378 | times_result_fields, |
| 8379 | 5 |
| 8380 | }; |
| 8381 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8382 | #ifdef MS_WINDOWS |
| 8383 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 8384 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8385 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8386 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8387 | |
| 8388 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8389 | build_times_result(PyObject *module, double user, double system, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8390 | double children_user, double children_system, |
| 8391 | double elapsed) |
| 8392 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8393 | PyObject *TimesResultType = get_posix_state(module)->TimesResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8394 | PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8395 | if (value == NULL) |
| 8396 | return NULL; |
| 8397 | |
| 8398 | #define SET(i, field) \ |
| 8399 | { \ |
| 8400 | PyObject *o = PyFloat_FromDouble(field); \ |
| 8401 | if (!o) { \ |
| 8402 | Py_DECREF(value); \ |
| 8403 | return NULL; \ |
| 8404 | } \ |
| 8405 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 8406 | } \ |
| 8407 | |
| 8408 | SET(0, user); |
| 8409 | SET(1, system); |
| 8410 | SET(2, children_user); |
| 8411 | SET(3, children_system); |
| 8412 | SET(4, elapsed); |
| 8413 | |
| 8414 | #undef SET |
| 8415 | |
| 8416 | return value; |
| 8417 | } |
| 8418 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8419 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8420 | #ifndef MS_WINDOWS |
| 8421 | #define NEED_TICKS_PER_SECOND |
| 8422 | static long ticks_per_second = -1; |
| 8423 | #endif /* MS_WINDOWS */ |
| 8424 | |
| 8425 | /*[clinic input] |
| 8426 | os.times |
| 8427 | |
| 8428 | Return a collection containing process timing information. |
| 8429 | |
| 8430 | The object returned behaves like a named tuple with these fields: |
| 8431 | (utime, stime, cutime, cstime, elapsed_time) |
| 8432 | All fields are floating point numbers. |
| 8433 | [clinic start generated code]*/ |
| 8434 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8435 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8436 | os_times_impl(PyObject *module) |
| 8437 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8438 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8439 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8440 | FILETIME create, exit, kernel, user; |
| 8441 | HANDLE hProc; |
| 8442 | hProc = GetCurrentProcess(); |
| 8443 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8444 | /* The fields of a FILETIME structure are the hi and lo part |
| 8445 | of a 64-bit value expressed in 100 nanosecond units. |
| 8446 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8447 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8448 | */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8449 | return build_times_result(module, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8450 | (double)(user.dwHighDateTime*429.4967296 + |
| 8451 | user.dwLowDateTime*1e-7), |
| 8452 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8453 | kernel.dwLowDateTime*1e-7), |
| 8454 | (double)0, |
| 8455 | (double)0, |
| 8456 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8457 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8458 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8459 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8460 | |
| 8461 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8462 | struct tms t; |
| 8463 | clock_t c; |
| 8464 | errno = 0; |
| 8465 | c = times(&t); |
| 8466 | if (c == (clock_t) -1) |
| 8467 | return posix_error(); |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8468 | return build_times_result(module, |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8469 | (double)t.tms_utime / ticks_per_second, |
| 8470 | (double)t.tms_stime / ticks_per_second, |
| 8471 | (double)t.tms_cutime / ticks_per_second, |
| 8472 | (double)t.tms_cstime / ticks_per_second, |
| 8473 | (double)c / ticks_per_second); |
| 8474 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8475 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8476 | #endif /* HAVE_TIMES */ |
| 8477 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8478 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8479 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8480 | /*[clinic input] |
| 8481 | os.getsid |
| 8482 | |
| 8483 | pid: pid_t |
| 8484 | / |
| 8485 | |
| 8486 | Call the system call getsid(pid) and return the result. |
| 8487 | [clinic start generated code]*/ |
| 8488 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8489 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8490 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8491 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8492 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8493 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8494 | sid = getsid(pid); |
| 8495 | if (sid < 0) |
| 8496 | return posix_error(); |
| 8497 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8498 | } |
| 8499 | #endif /* HAVE_GETSID */ |
| 8500 | |
| 8501 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8502 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8503 | /*[clinic input] |
| 8504 | os.setsid |
| 8505 | |
| 8506 | Call the system call setsid(). |
| 8507 | [clinic start generated code]*/ |
| 8508 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8509 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8510 | os_setsid_impl(PyObject *module) |
| 8511 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8512 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8513 | if (setsid() < 0) |
| 8514 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8515 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8516 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8517 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8518 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8519 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8520 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8521 | /*[clinic input] |
| 8522 | os.setpgid |
| 8523 | |
| 8524 | pid: pid_t |
| 8525 | pgrp: pid_t |
| 8526 | / |
| 8527 | |
| 8528 | Call the system call setpgid(pid, pgrp). |
| 8529 | [clinic start generated code]*/ |
| 8530 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8531 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8532 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8533 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8534 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8535 | if (setpgid(pid, pgrp) < 0) |
| 8536 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8537 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8538 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8539 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8540 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8541 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8542 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8543 | /*[clinic input] |
| 8544 | os.tcgetpgrp |
| 8545 | |
| 8546 | fd: int |
| 8547 | / |
| 8548 | |
| 8549 | Return the process group associated with the terminal specified by fd. |
| 8550 | [clinic start generated code]*/ |
| 8551 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8552 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8553 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8554 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8555 | { |
| 8556 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8557 | if (pgid < 0) |
| 8558 | return posix_error(); |
| 8559 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8560 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8561 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8562 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8563 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8564 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8565 | /*[clinic input] |
| 8566 | os.tcsetpgrp |
| 8567 | |
| 8568 | fd: int |
| 8569 | pgid: pid_t |
| 8570 | / |
| 8571 | |
| 8572 | Set the process group associated with the terminal specified by fd. |
| 8573 | [clinic start generated code]*/ |
| 8574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8575 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8576 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8577 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8578 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8579 | if (tcsetpgrp(fd, pgid) < 0) |
| 8580 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8581 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8582 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8583 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8584 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8585 | /* Functions acting on file descriptors */ |
| 8586 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8587 | #ifdef O_CLOEXEC |
| 8588 | extern int _Py_open_cloexec_works; |
| 8589 | #endif |
| 8590 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8591 | |
| 8592 | /*[clinic input] |
| 8593 | os.open -> int |
| 8594 | path: path_t |
| 8595 | flags: int |
| 8596 | mode: int = 0o777 |
| 8597 | * |
| 8598 | dir_fd: dir_fd(requires='openat') = None |
| 8599 | |
| 8600 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8601 | |
| 8602 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8603 | |
| 8604 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8605 | and path should be relative; path will then be relative to that directory. |
| 8606 | dir_fd may not be implemented on your platform. |
| 8607 | If it is unavailable, using it will raise a NotImplementedError. |
| 8608 | [clinic start generated code]*/ |
| 8609 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8610 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8611 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8612 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8613 | { |
| 8614 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8615 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8616 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8617 | #ifdef O_CLOEXEC |
| 8618 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8619 | #elif !defined(MS_WINDOWS) |
| 8620 | int *atomic_flag_works = NULL; |
| 8621 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8622 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8623 | #ifdef MS_WINDOWS |
| 8624 | flags |= O_NOINHERIT; |
| 8625 | #elif defined(O_CLOEXEC) |
| 8626 | flags |= O_CLOEXEC; |
| 8627 | #endif |
| 8628 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 8629 | if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) { |
| 8630 | return -1; |
| 8631 | } |
| 8632 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8633 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8634 | do { |
| 8635 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8636 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8637 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8638 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8639 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8640 | if (dir_fd != DEFAULT_DIR_FD) |
| 8641 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8642 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8643 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8644 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8645 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8646 | Py_END_ALLOW_THREADS |
| 8647 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8648 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8649 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8650 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8651 | if (!async_err) |
| 8652 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8653 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8654 | } |
| 8655 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8656 | #ifndef MS_WINDOWS |
| 8657 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8658 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8659 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8660 | } |
| 8661 | #endif |
| 8662 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8663 | return fd; |
| 8664 | } |
| 8665 | |
| 8666 | |
| 8667 | /*[clinic input] |
| 8668 | os.close |
| 8669 | |
| 8670 | fd: int |
| 8671 | |
| 8672 | Close a file descriptor. |
| 8673 | [clinic start generated code]*/ |
| 8674 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8675 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8676 | os_close_impl(PyObject *module, int fd) |
| 8677 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8678 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8679 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8680 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8681 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8682 | * for more details. |
| 8683 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8684 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8685 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8686 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8687 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8688 | Py_END_ALLOW_THREADS |
| 8689 | if (res < 0) |
| 8690 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8691 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8692 | } |
| 8693 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8694 | |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8695 | #ifdef HAVE_FDWALK |
| 8696 | static int |
| 8697 | _fdwalk_close_func(void *lohi, int fd) |
| 8698 | { |
| 8699 | int lo = ((int *)lohi)[0]; |
| 8700 | int hi = ((int *)lohi)[1]; |
| 8701 | |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8702 | if (fd >= hi) { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8703 | return 1; |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8704 | } |
| 8705 | else if (fd >= lo) { |
| 8706 | /* Ignore errors */ |
| 8707 | (void)close(fd); |
| 8708 | } |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8709 | return 0; |
| 8710 | } |
| 8711 | #endif /* HAVE_FDWALK */ |
| 8712 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8713 | /*[clinic input] |
| 8714 | os.closerange |
| 8715 | |
| 8716 | fd_low: int |
| 8717 | fd_high: int |
| 8718 | / |
| 8719 | |
| 8720 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8721 | [clinic start generated code]*/ |
| 8722 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8723 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8724 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8725 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8726 | { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8727 | #ifdef HAVE_FDWALK |
| 8728 | int lohi[2]; |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8729 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8730 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8731 | _Py_BEGIN_SUPPRESS_IPH |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8732 | #ifdef HAVE_FDWALK |
| 8733 | lohi[0] = Py_MAX(fd_low, 0); |
| 8734 | lohi[1] = fd_high; |
| 8735 | fdwalk(_fdwalk_close_func, lohi); |
| 8736 | #else |
Victor Stinner | 162c567 | 2020-04-24 12:00:51 +0200 | [diff] [blame] | 8737 | fd_low = Py_MAX(fd_low, 0); |
| 8738 | #ifdef __FreeBSD__ |
| 8739 | if (fd_high >= sysconf(_SC_OPEN_MAX)) { |
| 8740 | /* Any errors encountered while closing file descriptors are ignored */ |
| 8741 | closefrom(fd_low); |
| 8742 | } |
| 8743 | else |
| 8744 | #endif |
| 8745 | { |
| 8746 | for (int i = fd_low; i < fd_high; i++) { |
| 8747 | /* Ignore errors */ |
| 8748 | (void)close(i); |
| 8749 | } |
| 8750 | } |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8751 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8752 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8753 | Py_END_ALLOW_THREADS |
| 8754 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8755 | } |
| 8756 | |
| 8757 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8758 | /*[clinic input] |
| 8759 | os.dup -> int |
| 8760 | |
| 8761 | fd: int |
| 8762 | / |
| 8763 | |
| 8764 | Return a duplicate of a file descriptor. |
| 8765 | [clinic start generated code]*/ |
| 8766 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8767 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8768 | os_dup_impl(PyObject *module, int fd) |
| 8769 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8770 | { |
| 8771 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8772 | } |
| 8773 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8774 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8775 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8776 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8777 | fd: int |
| 8778 | fd2: int |
| 8779 | inheritable: bool=True |
| 8780 | |
| 8781 | Duplicate file descriptor. |
| 8782 | [clinic start generated code]*/ |
| 8783 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8784 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8785 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8786 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8787 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8788 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8789 | #if defined(HAVE_DUP3) && \ |
| 8790 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8791 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8792 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8793 | #endif |
| 8794 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8795 | if (fd < 0 || fd2 < 0) { |
| 8796 | posix_error(); |
| 8797 | return -1; |
| 8798 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8799 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8800 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8801 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8802 | * upon close(), and therefore below. |
| 8803 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8804 | #ifdef MS_WINDOWS |
| 8805 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8806 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8807 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8808 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8809 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8810 | if (res < 0) { |
| 8811 | posix_error(); |
| 8812 | return -1; |
| 8813 | } |
| 8814 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8815 | |
| 8816 | /* Character files like console cannot be make non-inheritable */ |
| 8817 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8818 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8819 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8820 | } |
| 8821 | |
| 8822 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8823 | Py_BEGIN_ALLOW_THREADS |
| 8824 | if (!inheritable) |
| 8825 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8826 | else |
| 8827 | res = dup2(fd, fd2); |
| 8828 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8829 | if (res < 0) { |
| 8830 | posix_error(); |
| 8831 | return -1; |
| 8832 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8833 | |
| 8834 | #else |
| 8835 | |
| 8836 | #ifdef HAVE_DUP3 |
| 8837 | if (!inheritable && dup3_works != 0) { |
| 8838 | Py_BEGIN_ALLOW_THREADS |
| 8839 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8840 | Py_END_ALLOW_THREADS |
| 8841 | if (res < 0) { |
| 8842 | if (dup3_works == -1) |
| 8843 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8844 | if (dup3_works) { |
| 8845 | posix_error(); |
| 8846 | return -1; |
| 8847 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8848 | } |
| 8849 | } |
| 8850 | |
| 8851 | if (inheritable || dup3_works == 0) |
| 8852 | { |
| 8853 | #endif |
| 8854 | Py_BEGIN_ALLOW_THREADS |
| 8855 | res = dup2(fd, fd2); |
| 8856 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8857 | if (res < 0) { |
| 8858 | posix_error(); |
| 8859 | return -1; |
| 8860 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8861 | |
| 8862 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8863 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8864 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8865 | } |
| 8866 | #ifdef HAVE_DUP3 |
| 8867 | } |
| 8868 | #endif |
| 8869 | |
| 8870 | #endif |
| 8871 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8872 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8873 | } |
| 8874 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8875 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8876 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8877 | /*[clinic input] |
| 8878 | os.lockf |
| 8879 | |
| 8880 | fd: int |
| 8881 | An open file descriptor. |
| 8882 | command: int |
| 8883 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8884 | length: Py_off_t |
| 8885 | The number of bytes to lock, starting at the current position. |
| 8886 | / |
| 8887 | |
| 8888 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8889 | |
| 8890 | [clinic start generated code]*/ |
| 8891 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8892 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8893 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8894 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8895 | { |
| 8896 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8897 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 8898 | if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) { |
| 8899 | return NULL; |
| 8900 | } |
| 8901 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8902 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8903 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8904 | Py_END_ALLOW_THREADS |
| 8905 | |
| 8906 | if (res < 0) |
| 8907 | return posix_error(); |
| 8908 | |
| 8909 | Py_RETURN_NONE; |
| 8910 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8911 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8912 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8913 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8914 | /*[clinic input] |
| 8915 | os.lseek -> Py_off_t |
| 8916 | |
| 8917 | fd: int |
| 8918 | position: Py_off_t |
| 8919 | how: int |
| 8920 | / |
| 8921 | |
| 8922 | Set the position of a file descriptor. Return the new position. |
| 8923 | |
| 8924 | Return the new cursor position in number of bytes |
| 8925 | relative to the beginning of the file. |
| 8926 | [clinic start generated code]*/ |
| 8927 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8928 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8929 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8930 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8931 | { |
| 8932 | Py_off_t result; |
| 8933 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8934 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8935 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8936 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8937 | case 0: how = SEEK_SET; break; |
| 8938 | case 1: how = SEEK_CUR; break; |
| 8939 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8940 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8941 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8942 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8943 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8944 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8945 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8946 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8947 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8948 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8949 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8950 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8951 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8952 | if (result < 0) |
| 8953 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8954 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8955 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8956 | } |
| 8957 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8958 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8959 | /*[clinic input] |
| 8960 | os.read |
| 8961 | fd: int |
| 8962 | length: Py_ssize_t |
| 8963 | / |
| 8964 | |
| 8965 | Read from a file descriptor. Returns a bytes object. |
| 8966 | [clinic start generated code]*/ |
| 8967 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8968 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8969 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 8970 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8971 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8972 | Py_ssize_t n; |
| 8973 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8974 | |
| 8975 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8976 | errno = EINVAL; |
| 8977 | return posix_error(); |
| 8978 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8979 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 8980 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8981 | |
| 8982 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8983 | if (buffer == NULL) |
| 8984 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8985 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8986 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8987 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8988 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8989 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8990 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8991 | |
| 8992 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8993 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8994 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8995 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8996 | } |
| 8997 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8998 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8999 | || defined(__APPLE__))) \ |
| 9000 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 9001 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9002 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9003 | 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] | 9004 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9005 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9006 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9007 | *iov = PyMem_New(struct iovec, cnt); |
| 9008 | if (*iov == NULL) { |
| 9009 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9010 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9011 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9012 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9013 | *buf = PyMem_New(Py_buffer, cnt); |
| 9014 | if (*buf == NULL) { |
| 9015 | PyMem_Del(*iov); |
| 9016 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9017 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9018 | } |
| 9019 | |
| 9020 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9021 | PyObject *item = PySequence_GetItem(seq, i); |
| 9022 | if (item == NULL) |
| 9023 | goto fail; |
| 9024 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 9025 | Py_DECREF(item); |
| 9026 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9027 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9028 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9029 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9030 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9031 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9032 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9033 | |
| 9034 | fail: |
| 9035 | PyMem_Del(*iov); |
| 9036 | for (j = 0; j < i; j++) { |
| 9037 | PyBuffer_Release(&(*buf)[j]); |
| 9038 | } |
| 9039 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9040 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9041 | } |
| 9042 | |
| 9043 | static void |
| 9044 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 9045 | { |
| 9046 | int i; |
| 9047 | PyMem_Del(iov); |
| 9048 | for (i = 0; i < cnt; i++) { |
| 9049 | PyBuffer_Release(&buf[i]); |
| 9050 | } |
| 9051 | PyMem_Del(buf); |
| 9052 | } |
| 9053 | #endif |
| 9054 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9055 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9056 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9057 | /*[clinic input] |
| 9058 | os.readv -> Py_ssize_t |
| 9059 | |
| 9060 | fd: int |
| 9061 | buffers: object |
| 9062 | / |
| 9063 | |
| 9064 | Read from a file descriptor fd into an iterable of buffers. |
| 9065 | |
| 9066 | The buffers should be mutable buffers accepting bytes. |
| 9067 | readv will transfer data into each buffer until it is full |
| 9068 | and then move on to the next buffer in the sequence to hold |
| 9069 | the rest of the data. |
| 9070 | |
| 9071 | readv returns the total number of bytes read, |
| 9072 | which may be less than the total capacity of all the buffers. |
| 9073 | [clinic start generated code]*/ |
| 9074 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9075 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9076 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 9077 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9078 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9079 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9080 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9081 | struct iovec *iov; |
| 9082 | Py_buffer *buf; |
| 9083 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9084 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9085 | PyErr_SetString(PyExc_TypeError, |
| 9086 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9087 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9088 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9089 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9090 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9091 | if (cnt < 0) |
| 9092 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9093 | |
| 9094 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 9095 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9096 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9097 | do { |
| 9098 | Py_BEGIN_ALLOW_THREADS |
| 9099 | n = readv(fd, iov, cnt); |
| 9100 | Py_END_ALLOW_THREADS |
| 9101 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9102 | |
| 9103 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9104 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9105 | if (!async_err) |
| 9106 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9107 | return -1; |
| 9108 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9109 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9110 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9111 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9112 | #endif /* HAVE_READV */ |
| 9113 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9114 | |
| 9115 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9116 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9117 | os.pread |
| 9118 | |
| 9119 | fd: int |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9120 | length: Py_ssize_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9121 | offset: Py_off_t |
| 9122 | / |
| 9123 | |
| 9124 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 9125 | |
| 9126 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 9127 | the beginning of the file. The file offset remains unchanged. |
| 9128 | [clinic start generated code]*/ |
| 9129 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9130 | static PyObject * |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9131 | os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset) |
| 9132 | /*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9133 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9134 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9135 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9136 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9137 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9138 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9139 | errno = EINVAL; |
| 9140 | return posix_error(); |
| 9141 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9142 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9143 | if (buffer == NULL) |
| 9144 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9145 | |
| 9146 | do { |
| 9147 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9148 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9149 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9150 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9151 | Py_END_ALLOW_THREADS |
| 9152 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9153 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9154 | if (n < 0) { |
| 9155 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9156 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9157 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9158 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9159 | _PyBytes_Resize(&buffer, n); |
| 9160 | return buffer; |
| 9161 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9162 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9163 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9164 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 9165 | /*[clinic input] |
| 9166 | os.preadv -> Py_ssize_t |
| 9167 | |
| 9168 | fd: int |
| 9169 | buffers: object |
| 9170 | offset: Py_off_t |
| 9171 | flags: int = 0 |
| 9172 | / |
| 9173 | |
| 9174 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 9175 | |
| 9176 | Combines the functionality of readv() and pread(). As readv(), it will |
| 9177 | transfer data into each buffer until it is full and then move on to the next |
| 9178 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 9179 | specifies the file offset at which the input operation is to be performed. It |
| 9180 | will return the total number of bytes read (which can be less than the total |
| 9181 | capacity of all the objects). |
| 9182 | |
| 9183 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9184 | |
| 9185 | - RWF_HIPRI |
| 9186 | - RWF_NOWAIT |
| 9187 | |
| 9188 | Using non-zero flags requires Linux 4.6 or newer. |
| 9189 | [clinic start generated code]*/ |
| 9190 | |
| 9191 | static Py_ssize_t |
| 9192 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9193 | int flags) |
| 9194 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 9195 | { |
| 9196 | Py_ssize_t cnt, n; |
| 9197 | int async_err = 0; |
| 9198 | struct iovec *iov; |
| 9199 | Py_buffer *buf; |
| 9200 | |
| 9201 | if (!PySequence_Check(buffers)) { |
| 9202 | PyErr_SetString(PyExc_TypeError, |
| 9203 | "preadv2() arg 2 must be a sequence"); |
| 9204 | return -1; |
| 9205 | } |
| 9206 | |
| 9207 | cnt = PySequence_Size(buffers); |
| 9208 | if (cnt < 0) { |
| 9209 | return -1; |
| 9210 | } |
| 9211 | |
| 9212 | #ifndef HAVE_PREADV2 |
| 9213 | if(flags != 0) { |
| 9214 | argument_unavailable_error("preadv2", "flags"); |
| 9215 | return -1; |
| 9216 | } |
| 9217 | #endif |
| 9218 | |
| 9219 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 9220 | return -1; |
| 9221 | } |
| 9222 | #ifdef HAVE_PREADV2 |
| 9223 | do { |
| 9224 | Py_BEGIN_ALLOW_THREADS |
| 9225 | _Py_BEGIN_SUPPRESS_IPH |
| 9226 | n = preadv2(fd, iov, cnt, offset, flags); |
| 9227 | _Py_END_SUPPRESS_IPH |
| 9228 | Py_END_ALLOW_THREADS |
| 9229 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9230 | #else |
| 9231 | do { |
| 9232 | Py_BEGIN_ALLOW_THREADS |
| 9233 | _Py_BEGIN_SUPPRESS_IPH |
| 9234 | n = preadv(fd, iov, cnt, offset); |
| 9235 | _Py_END_SUPPRESS_IPH |
| 9236 | Py_END_ALLOW_THREADS |
| 9237 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9238 | #endif |
| 9239 | |
| 9240 | iov_cleanup(iov, buf, cnt); |
| 9241 | if (n < 0) { |
| 9242 | if (!async_err) { |
| 9243 | posix_error(); |
| 9244 | } |
| 9245 | return -1; |
| 9246 | } |
| 9247 | |
| 9248 | return n; |
| 9249 | } |
| 9250 | #endif /* HAVE_PREADV */ |
| 9251 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9252 | |
| 9253 | /*[clinic input] |
| 9254 | os.write -> Py_ssize_t |
| 9255 | |
| 9256 | fd: int |
| 9257 | data: Py_buffer |
| 9258 | / |
| 9259 | |
| 9260 | Write a bytes object to a file descriptor. |
| 9261 | [clinic start generated code]*/ |
| 9262 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9263 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9264 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 9265 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9266 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9267 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9268 | } |
| 9269 | |
| 9270 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9271 | #ifdef __APPLE__ |
| 9272 | /*[clinic input] |
| 9273 | os.sendfile |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9274 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9275 | out_fd: int |
| 9276 | in_fd: int |
| 9277 | offset: Py_off_t |
| 9278 | count as sbytes: Py_off_t |
| 9279 | headers: object(c_default="NULL") = () |
| 9280 | trailers: object(c_default="NULL") = () |
| 9281 | flags: int = 0 |
| 9282 | |
| 9283 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9284 | [clinic start generated code]*/ |
| 9285 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9286 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9287 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, |
| 9288 | Py_off_t sbytes, PyObject *headers, PyObject *trailers, |
| 9289 | int flags) |
| 9290 | /*[clinic end generated code: output=81c4bcd143f5c82b input=b0d72579d4c69afa]*/ |
| 9291 | #elif defined(__FreeBSD__) || defined(__DragonFly__) |
| 9292 | /*[clinic input] |
| 9293 | os.sendfile |
| 9294 | |
| 9295 | out_fd: int |
| 9296 | in_fd: int |
| 9297 | offset: Py_off_t |
| 9298 | count: Py_ssize_t |
| 9299 | headers: object(c_default="NULL") = () |
| 9300 | trailers: object(c_default="NULL") = () |
| 9301 | flags: int = 0 |
| 9302 | |
| 9303 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9304 | [clinic start generated code]*/ |
| 9305 | |
| 9306 | static PyObject * |
| 9307 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, |
| 9308 | Py_ssize_t count, PyObject *headers, PyObject *trailers, |
| 9309 | int flags) |
| 9310 | /*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/ |
| 9311 | #else |
| 9312 | /*[clinic input] |
| 9313 | os.sendfile |
| 9314 | |
| 9315 | out_fd: int |
| 9316 | in_fd: int |
| 9317 | offset as offobj: object |
| 9318 | count: Py_ssize_t |
| 9319 | |
| 9320 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9321 | [clinic start generated code]*/ |
| 9322 | |
| 9323 | static PyObject * |
| 9324 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj, |
| 9325 | Py_ssize_t count) |
| 9326 | /*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/ |
| 9327 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9328 | { |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9329 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9330 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9331 | |
| 9332 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 9333 | #ifndef __APPLE__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9334 | off_t sbytes; |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9335 | #endif |
| 9336 | Py_buffer *hbuf, *tbuf; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9337 | struct sf_hdtr sf; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9338 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 9339 | sf.headers = NULL; |
| 9340 | sf.trailers = NULL; |
| 9341 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9342 | if (headers != NULL) { |
| 9343 | if (!PySequence_Check(headers)) { |
| 9344 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9345 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9346 | return NULL; |
| 9347 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9348 | Py_ssize_t i = PySequence_Size(headers); |
| 9349 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9350 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9351 | if (i > INT_MAX) { |
| 9352 | PyErr_SetString(PyExc_OverflowError, |
| 9353 | "sendfile() header is too large"); |
| 9354 | return NULL; |
| 9355 | } |
| 9356 | if (i > 0) { |
| 9357 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9358 | if (iov_setup(&(sf.headers), &hbuf, |
| 9359 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9360 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9361 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9362 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 9363 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 9364 | # define OFF_T_MAX 0x7fffffffffffffff |
| 9365 | if (sbytes >= OFF_T_MAX - blen) { |
| 9366 | PyErr_SetString(PyExc_OverflowError, |
| 9367 | "sendfile() header is too large"); |
| 9368 | return NULL; |
| 9369 | } |
| 9370 | sbytes += blen; |
| 9371 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9372 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9373 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9374 | } |
| 9375 | } |
| 9376 | if (trailers != NULL) { |
| 9377 | if (!PySequence_Check(trailers)) { |
| 9378 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9379 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9380 | return NULL; |
| 9381 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9382 | Py_ssize_t i = PySequence_Size(trailers); |
| 9383 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9384 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9385 | if (i > INT_MAX) { |
| 9386 | PyErr_SetString(PyExc_OverflowError, |
| 9387 | "sendfile() trailer is too large"); |
| 9388 | return NULL; |
| 9389 | } |
| 9390 | if (i > 0) { |
| 9391 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9392 | if (iov_setup(&(sf.trailers), &tbuf, |
| 9393 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9394 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9395 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9396 | } |
| 9397 | } |
| 9398 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9399 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9400 | do { |
| 9401 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9402 | #ifdef __APPLE__ |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9403 | ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9404 | #else |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9405 | ret = sendfile(in_fd, out_fd, offset, count, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9406 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9407 | Py_END_ALLOW_THREADS |
| 9408 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9409 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9410 | |
| 9411 | if (sf.headers != NULL) |
| 9412 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 9413 | if (sf.trailers != NULL) |
| 9414 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 9415 | |
| 9416 | if (ret < 0) { |
| 9417 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 9418 | if (sbytes != 0) { |
| 9419 | // some data has been sent |
| 9420 | goto done; |
| 9421 | } |
| 9422 | else { |
| 9423 | // no data has been sent; upper application is supposed |
| 9424 | // to retry on EAGAIN or EBUSY |
| 9425 | return posix_error(); |
| 9426 | } |
| 9427 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9428 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9429 | } |
| 9430 | goto done; |
| 9431 | |
| 9432 | done: |
| 9433 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 9434 | return Py_BuildValue("l", sbytes); |
| 9435 | #else |
| 9436 | return Py_BuildValue("L", sbytes); |
| 9437 | #endif |
| 9438 | |
| 9439 | #else |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9440 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9441 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9442 | do { |
| 9443 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9444 | ret = sendfile(out_fd, in_fd, NULL, count); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9445 | Py_END_ALLOW_THREADS |
| 9446 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9447 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9448 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9449 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9450 | } |
| 9451 | #endif |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9452 | off_t offset; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9453 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9454 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9455 | |
| 9456 | do { |
| 9457 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9458 | ret = sendfile(out_fd, in_fd, &offset, count); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9459 | Py_END_ALLOW_THREADS |
| 9460 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9461 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9462 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9463 | return Py_BuildValue("n", ret); |
| 9464 | #endif |
| 9465 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9466 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9467 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9468 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9469 | #if defined(__APPLE__) |
| 9470 | /*[clinic input] |
| 9471 | os._fcopyfile |
| 9472 | |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9473 | in_fd: int |
| 9474 | out_fd: int |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9475 | flags: int |
| 9476 | / |
| 9477 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9478 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9479 | [clinic start generated code]*/ |
| 9480 | |
| 9481 | static PyObject * |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9482 | os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags) |
| 9483 | /*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9484 | { |
| 9485 | int ret; |
| 9486 | |
| 9487 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9488 | ret = fcopyfile(in_fd, out_fd, NULL, flags); |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9489 | Py_END_ALLOW_THREADS |
| 9490 | if (ret < 0) |
| 9491 | return posix_error(); |
| 9492 | Py_RETURN_NONE; |
| 9493 | } |
| 9494 | #endif |
| 9495 | |
| 9496 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9497 | /*[clinic input] |
| 9498 | os.fstat |
| 9499 | |
| 9500 | fd : int |
| 9501 | |
| 9502 | Perform a stat system call on the given file descriptor. |
| 9503 | |
| 9504 | Like stat(), but for an open file descriptor. |
| 9505 | Equivalent to os.stat(fd). |
| 9506 | [clinic start generated code]*/ |
| 9507 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9508 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9509 | os_fstat_impl(PyObject *module, int fd) |
| 9510 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9511 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9512 | STRUCT_STAT st; |
| 9513 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9514 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9515 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9516 | do { |
| 9517 | Py_BEGIN_ALLOW_THREADS |
| 9518 | res = FSTAT(fd, &st); |
| 9519 | Py_END_ALLOW_THREADS |
| 9520 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9521 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9522 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9523 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9524 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9525 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9526 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9527 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9528 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 9529 | return _pystat_fromstructstat(module, &st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9530 | } |
| 9531 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9532 | |
| 9533 | /*[clinic input] |
| 9534 | os.isatty -> bool |
| 9535 | fd: int |
| 9536 | / |
| 9537 | |
| 9538 | Return True if the fd is connected to a terminal. |
| 9539 | |
| 9540 | Return True if the file descriptor is an open file descriptor |
| 9541 | connected to the slave end of a terminal. |
| 9542 | [clinic start generated code]*/ |
| 9543 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9544 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9545 | os_isatty_impl(PyObject *module, int fd) |
| 9546 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9547 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9548 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9549 | _Py_BEGIN_SUPPRESS_IPH |
| 9550 | return_value = isatty(fd); |
| 9551 | _Py_END_SUPPRESS_IPH |
| 9552 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9553 | } |
| 9554 | |
| 9555 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9556 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9557 | /*[clinic input] |
| 9558 | os.pipe |
| 9559 | |
| 9560 | Create a pipe. |
| 9561 | |
| 9562 | Returns a tuple of two file descriptors: |
| 9563 | (read_fd, write_fd) |
| 9564 | [clinic start generated code]*/ |
| 9565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9566 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9567 | os_pipe_impl(PyObject *module) |
| 9568 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9569 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9570 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9571 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9572 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9573 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9574 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9575 | #else |
| 9576 | int res; |
| 9577 | #endif |
| 9578 | |
| 9579 | #ifdef MS_WINDOWS |
| 9580 | attr.nLength = sizeof(attr); |
| 9581 | attr.lpSecurityDescriptor = NULL; |
| 9582 | attr.bInheritHandle = FALSE; |
| 9583 | |
| 9584 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9585 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9586 | ok = CreatePipe(&read, &write, &attr, 0); |
| 9587 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9588 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 9589 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9590 | if (fds[0] == -1 || fds[1] == -1) { |
| 9591 | CloseHandle(read); |
| 9592 | CloseHandle(write); |
| 9593 | ok = 0; |
| 9594 | } |
| 9595 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9596 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9597 | Py_END_ALLOW_THREADS |
| 9598 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9599 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9600 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9601 | #else |
| 9602 | |
| 9603 | #ifdef HAVE_PIPE2 |
| 9604 | Py_BEGIN_ALLOW_THREADS |
| 9605 | res = pipe2(fds, O_CLOEXEC); |
| 9606 | Py_END_ALLOW_THREADS |
| 9607 | |
| 9608 | if (res != 0 && errno == ENOSYS) |
| 9609 | { |
| 9610 | #endif |
| 9611 | Py_BEGIN_ALLOW_THREADS |
| 9612 | res = pipe(fds); |
| 9613 | Py_END_ALLOW_THREADS |
| 9614 | |
| 9615 | if (res == 0) { |
| 9616 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9617 | close(fds[0]); |
| 9618 | close(fds[1]); |
| 9619 | return NULL; |
| 9620 | } |
| 9621 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9622 | close(fds[0]); |
| 9623 | close(fds[1]); |
| 9624 | return NULL; |
| 9625 | } |
| 9626 | } |
| 9627 | #ifdef HAVE_PIPE2 |
| 9628 | } |
| 9629 | #endif |
| 9630 | |
| 9631 | if (res != 0) |
| 9632 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9633 | #endif /* !MS_WINDOWS */ |
| 9634 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9635 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9636 | #endif /* HAVE_PIPE */ |
| 9637 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9638 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9639 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9640 | /*[clinic input] |
| 9641 | os.pipe2 |
| 9642 | |
| 9643 | flags: int |
| 9644 | / |
| 9645 | |
| 9646 | Create a pipe with flags set atomically. |
| 9647 | |
| 9648 | Returns a tuple of two file descriptors: |
| 9649 | (read_fd, write_fd) |
| 9650 | |
| 9651 | flags can be constructed by ORing together one or more of these values: |
| 9652 | O_NONBLOCK, O_CLOEXEC. |
| 9653 | [clinic start generated code]*/ |
| 9654 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9655 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9656 | os_pipe2_impl(PyObject *module, int flags) |
| 9657 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9658 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9659 | int fds[2]; |
| 9660 | int res; |
| 9661 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9662 | res = pipe2(fds, flags); |
| 9663 | if (res != 0) |
| 9664 | return posix_error(); |
| 9665 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9666 | } |
| 9667 | #endif /* HAVE_PIPE2 */ |
| 9668 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9669 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9670 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9671 | /*[clinic input] |
| 9672 | os.writev -> Py_ssize_t |
| 9673 | fd: int |
| 9674 | buffers: object |
| 9675 | / |
| 9676 | |
| 9677 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9678 | |
| 9679 | Returns the total number of bytes written. |
| 9680 | buffers must be a sequence of bytes-like objects. |
| 9681 | [clinic start generated code]*/ |
| 9682 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9683 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9684 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9685 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9686 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9687 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9688 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9689 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9690 | struct iovec *iov; |
| 9691 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9692 | |
| 9693 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9694 | PyErr_SetString(PyExc_TypeError, |
| 9695 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9696 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9697 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9698 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9699 | if (cnt < 0) |
| 9700 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9701 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9702 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9703 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9704 | } |
| 9705 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9706 | do { |
| 9707 | Py_BEGIN_ALLOW_THREADS |
| 9708 | result = writev(fd, iov, cnt); |
| 9709 | Py_END_ALLOW_THREADS |
| 9710 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9711 | |
| 9712 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9713 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9714 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9715 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9716 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9717 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9718 | #endif /* HAVE_WRITEV */ |
| 9719 | |
| 9720 | |
| 9721 | #ifdef HAVE_PWRITE |
| 9722 | /*[clinic input] |
| 9723 | os.pwrite -> Py_ssize_t |
| 9724 | |
| 9725 | fd: int |
| 9726 | buffer: Py_buffer |
| 9727 | offset: Py_off_t |
| 9728 | / |
| 9729 | |
| 9730 | Write bytes to a file descriptor starting at a particular offset. |
| 9731 | |
| 9732 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9733 | the file. Returns the number of bytes writte. Does not change the |
| 9734 | current file offset. |
| 9735 | [clinic start generated code]*/ |
| 9736 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9737 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9738 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9739 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9740 | { |
| 9741 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9742 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9743 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9744 | do { |
| 9745 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9746 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9747 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9748 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9749 | Py_END_ALLOW_THREADS |
| 9750 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9751 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9752 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9753 | posix_error(); |
| 9754 | return size; |
| 9755 | } |
| 9756 | #endif /* HAVE_PWRITE */ |
| 9757 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9758 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9759 | /*[clinic input] |
| 9760 | os.pwritev -> Py_ssize_t |
| 9761 | |
| 9762 | fd: int |
| 9763 | buffers: object |
| 9764 | offset: Py_off_t |
| 9765 | flags: int = 0 |
| 9766 | / |
| 9767 | |
| 9768 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9769 | |
| 9770 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9771 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9772 | buffer is written before proceeding to second, and so on. The operating system may |
| 9773 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9774 | This function writes the contents of each object to the file descriptor and returns |
| 9775 | the total number of bytes written. |
| 9776 | |
| 9777 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9778 | |
| 9779 | - RWF_DSYNC |
| 9780 | - RWF_SYNC |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame^] | 9781 | - RWF_APPEND |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9782 | |
| 9783 | Using non-zero flags requires Linux 4.7 or newer. |
| 9784 | [clinic start generated code]*/ |
| 9785 | |
| 9786 | static Py_ssize_t |
| 9787 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9788 | int flags) |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame^] | 9789 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=35358c327e1a2a8e]*/ |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9790 | { |
| 9791 | Py_ssize_t cnt; |
| 9792 | Py_ssize_t result; |
| 9793 | int async_err = 0; |
| 9794 | struct iovec *iov; |
| 9795 | Py_buffer *buf; |
| 9796 | |
| 9797 | if (!PySequence_Check(buffers)) { |
| 9798 | PyErr_SetString(PyExc_TypeError, |
| 9799 | "pwritev() arg 2 must be a sequence"); |
| 9800 | return -1; |
| 9801 | } |
| 9802 | |
| 9803 | cnt = PySequence_Size(buffers); |
| 9804 | if (cnt < 0) { |
| 9805 | return -1; |
| 9806 | } |
| 9807 | |
| 9808 | #ifndef HAVE_PWRITEV2 |
| 9809 | if(flags != 0) { |
| 9810 | argument_unavailable_error("pwritev2", "flags"); |
| 9811 | return -1; |
| 9812 | } |
| 9813 | #endif |
| 9814 | |
| 9815 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9816 | return -1; |
| 9817 | } |
| 9818 | #ifdef HAVE_PWRITEV2 |
| 9819 | do { |
| 9820 | Py_BEGIN_ALLOW_THREADS |
| 9821 | _Py_BEGIN_SUPPRESS_IPH |
| 9822 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9823 | _Py_END_SUPPRESS_IPH |
| 9824 | Py_END_ALLOW_THREADS |
| 9825 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9826 | #else |
| 9827 | do { |
| 9828 | Py_BEGIN_ALLOW_THREADS |
| 9829 | _Py_BEGIN_SUPPRESS_IPH |
| 9830 | result = pwritev(fd, iov, cnt, offset); |
| 9831 | _Py_END_SUPPRESS_IPH |
| 9832 | Py_END_ALLOW_THREADS |
| 9833 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9834 | #endif |
| 9835 | |
| 9836 | iov_cleanup(iov, buf, cnt); |
| 9837 | if (result < 0) { |
| 9838 | if (!async_err) { |
| 9839 | posix_error(); |
| 9840 | } |
| 9841 | return -1; |
| 9842 | } |
| 9843 | |
| 9844 | return result; |
| 9845 | } |
| 9846 | #endif /* HAVE_PWRITEV */ |
| 9847 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9848 | #ifdef HAVE_COPY_FILE_RANGE |
| 9849 | /*[clinic input] |
| 9850 | |
| 9851 | os.copy_file_range |
| 9852 | src: int |
| 9853 | Source file descriptor. |
| 9854 | dst: int |
| 9855 | Destination file descriptor. |
| 9856 | count: Py_ssize_t |
| 9857 | Number of bytes to copy. |
| 9858 | offset_src: object = None |
| 9859 | Starting offset in src. |
| 9860 | offset_dst: object = None |
| 9861 | Starting offset in dst. |
| 9862 | |
| 9863 | Copy count bytes from one file descriptor to another. |
| 9864 | |
| 9865 | If offset_src is None, then src is read from the current position; |
| 9866 | respectively for offset_dst. |
| 9867 | [clinic start generated code]*/ |
| 9868 | |
| 9869 | static PyObject * |
| 9870 | os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, |
| 9871 | PyObject *offset_src, PyObject *offset_dst) |
| 9872 | /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ |
| 9873 | { |
| 9874 | off_t offset_src_val, offset_dst_val; |
| 9875 | off_t *p_offset_src = NULL; |
| 9876 | off_t *p_offset_dst = NULL; |
| 9877 | Py_ssize_t ret; |
| 9878 | int async_err = 0; |
| 9879 | /* The flags argument is provided to allow |
| 9880 | * for future extensions and currently must be to 0. */ |
| 9881 | int flags = 0; |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9882 | |
| 9883 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9884 | if (count < 0) { |
| 9885 | PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); |
| 9886 | return NULL; |
| 9887 | } |
| 9888 | |
| 9889 | if (offset_src != Py_None) { |
| 9890 | if (!Py_off_t_converter(offset_src, &offset_src_val)) { |
| 9891 | return NULL; |
| 9892 | } |
| 9893 | p_offset_src = &offset_src_val; |
| 9894 | } |
| 9895 | |
| 9896 | if (offset_dst != Py_None) { |
| 9897 | if (!Py_off_t_converter(offset_dst, &offset_dst_val)) { |
| 9898 | return NULL; |
| 9899 | } |
| 9900 | p_offset_dst = &offset_dst_val; |
| 9901 | } |
| 9902 | |
| 9903 | do { |
| 9904 | Py_BEGIN_ALLOW_THREADS |
| 9905 | ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags); |
| 9906 | Py_END_ALLOW_THREADS |
| 9907 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9908 | |
| 9909 | if (ret < 0) { |
| 9910 | return (!async_err) ? posix_error() : NULL; |
| 9911 | } |
| 9912 | |
| 9913 | return PyLong_FromSsize_t(ret); |
| 9914 | } |
| 9915 | #endif /* HAVE_COPY_FILE_RANGE*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9916 | |
| 9917 | #ifdef HAVE_MKFIFO |
| 9918 | /*[clinic input] |
| 9919 | os.mkfifo |
| 9920 | |
| 9921 | path: path_t |
| 9922 | mode: int=0o666 |
| 9923 | * |
| 9924 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9925 | |
| 9926 | Create a "fifo" (a POSIX named pipe). |
| 9927 | |
| 9928 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9929 | and path should be relative; path will then be relative to that directory. |
| 9930 | dir_fd may not be implemented on your platform. |
| 9931 | If it is unavailable, using it will raise a NotImplementedError. |
| 9932 | [clinic start generated code]*/ |
| 9933 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9934 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9935 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9936 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9937 | { |
| 9938 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9939 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9940 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9941 | do { |
| 9942 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9943 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9944 | if (dir_fd != DEFAULT_DIR_FD) |
| 9945 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9946 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9947 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9948 | result = mkfifo(path->narrow, mode); |
| 9949 | Py_END_ALLOW_THREADS |
| 9950 | } while (result != 0 && errno == EINTR && |
| 9951 | !(async_err = PyErr_CheckSignals())); |
| 9952 | if (result != 0) |
| 9953 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9954 | |
| 9955 | Py_RETURN_NONE; |
| 9956 | } |
| 9957 | #endif /* HAVE_MKFIFO */ |
| 9958 | |
| 9959 | |
| 9960 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9961 | /*[clinic input] |
| 9962 | os.mknod |
| 9963 | |
| 9964 | path: path_t |
| 9965 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9966 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9967 | * |
| 9968 | dir_fd: dir_fd(requires='mknodat')=None |
| 9969 | |
| 9970 | Create a node in the file system. |
| 9971 | |
| 9972 | Create a node in the file system (file, device special file or named pipe) |
| 9973 | at path. mode specifies both the permissions to use and the |
| 9974 | type of node to be created, being combined (bitwise OR) with one of |
| 9975 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 9976 | device defines the newly created device special file (probably using |
| 9977 | os.makedev()). Otherwise device is ignored. |
| 9978 | |
| 9979 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9980 | and path should be relative; path will then be relative to that directory. |
| 9981 | dir_fd may not be implemented on your platform. |
| 9982 | If it is unavailable, using it will raise a NotImplementedError. |
| 9983 | [clinic start generated code]*/ |
| 9984 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9985 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9986 | 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] | 9987 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9988 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9989 | { |
| 9990 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9991 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9992 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9993 | do { |
| 9994 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9995 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9996 | if (dir_fd != DEFAULT_DIR_FD) |
| 9997 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 9998 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9999 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10000 | result = mknod(path->narrow, mode, device); |
| 10001 | Py_END_ALLOW_THREADS |
| 10002 | } while (result != 0 && errno == EINTR && |
| 10003 | !(async_err = PyErr_CheckSignals())); |
| 10004 | if (result != 0) |
| 10005 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10006 | |
| 10007 | Py_RETURN_NONE; |
| 10008 | } |
| 10009 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 10010 | |
| 10011 | |
| 10012 | #ifdef HAVE_DEVICE_MACROS |
| 10013 | /*[clinic input] |
| 10014 | os.major -> unsigned_int |
| 10015 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10016 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10017 | / |
| 10018 | |
| 10019 | Extracts a device major number from a raw device number. |
| 10020 | [clinic start generated code]*/ |
| 10021 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10022 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10023 | os_major_impl(PyObject *module, dev_t device) |
| 10024 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10025 | { |
| 10026 | return major(device); |
| 10027 | } |
| 10028 | |
| 10029 | |
| 10030 | /*[clinic input] |
| 10031 | os.minor -> unsigned_int |
| 10032 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10033 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10034 | / |
| 10035 | |
| 10036 | Extracts a device minor number from a raw device number. |
| 10037 | [clinic start generated code]*/ |
| 10038 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10039 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10040 | os_minor_impl(PyObject *module, dev_t device) |
| 10041 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10042 | { |
| 10043 | return minor(device); |
| 10044 | } |
| 10045 | |
| 10046 | |
| 10047 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10048 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10049 | |
| 10050 | major: int |
| 10051 | minor: int |
| 10052 | / |
| 10053 | |
| 10054 | Composes a raw device number from the major and minor device numbers. |
| 10055 | [clinic start generated code]*/ |
| 10056 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10057 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10058 | os_makedev_impl(PyObject *module, int major, int minor) |
| 10059 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10060 | { |
| 10061 | return makedev(major, minor); |
| 10062 | } |
| 10063 | #endif /* HAVE_DEVICE_MACROS */ |
| 10064 | |
| 10065 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10066 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10067 | /*[clinic input] |
| 10068 | os.ftruncate |
| 10069 | |
| 10070 | fd: int |
| 10071 | length: Py_off_t |
| 10072 | / |
| 10073 | |
| 10074 | Truncate a file, specified by file descriptor, to a specific length. |
| 10075 | [clinic start generated code]*/ |
| 10076 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10077 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10078 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 10079 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10080 | { |
| 10081 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10082 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10083 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10084 | if (PySys_Audit("os.truncate", "in", fd, length) < 0) { |
| 10085 | return NULL; |
| 10086 | } |
| 10087 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10088 | do { |
| 10089 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10090 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10091 | #ifdef MS_WINDOWS |
| 10092 | result = _chsize_s(fd, length); |
| 10093 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10094 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10095 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10096 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10097 | Py_END_ALLOW_THREADS |
| 10098 | } while (result != 0 && errno == EINTR && |
| 10099 | !(async_err = PyErr_CheckSignals())); |
| 10100 | if (result != 0) |
| 10101 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10102 | Py_RETURN_NONE; |
| 10103 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10104 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10105 | |
| 10106 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10107 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10108 | /*[clinic input] |
| 10109 | os.truncate |
| 10110 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 10111 | length: Py_off_t |
| 10112 | |
| 10113 | Truncate a file, specified by path, to a specific length. |
| 10114 | |
| 10115 | On some platforms, path may also be specified as an open file descriptor. |
| 10116 | If this functionality is unavailable, using it raises an exception. |
| 10117 | [clinic start generated code]*/ |
| 10118 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10119 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10120 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 10121 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10122 | { |
| 10123 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10124 | #ifdef MS_WINDOWS |
| 10125 | int fd; |
| 10126 | #endif |
| 10127 | |
| 10128 | if (path->fd != -1) |
| 10129 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10130 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10131 | if (PySys_Audit("os.truncate", "On", path->object, length) < 0) { |
| 10132 | return NULL; |
| 10133 | } |
| 10134 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10135 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10136 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10137 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10138 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 10139 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10140 | result = -1; |
| 10141 | else { |
| 10142 | result = _chsize_s(fd, length); |
| 10143 | close(fd); |
| 10144 | if (result < 0) |
| 10145 | errno = result; |
| 10146 | } |
| 10147 | #else |
| 10148 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10149 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10150 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10151 | Py_END_ALLOW_THREADS |
| 10152 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 10153 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10154 | |
| 10155 | Py_RETURN_NONE; |
| 10156 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10157 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10158 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10159 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10160 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 10161 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 10162 | defined, which is the case in Python on AIX. AIX bug report: |
| 10163 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 10164 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 10165 | # define POSIX_FADVISE_AIX_BUG |
| 10166 | #endif |
| 10167 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10168 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10169 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10170 | /*[clinic input] |
| 10171 | os.posix_fallocate |
| 10172 | |
| 10173 | fd: int |
| 10174 | offset: Py_off_t |
| 10175 | length: Py_off_t |
| 10176 | / |
| 10177 | |
| 10178 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 10179 | |
| 10180 | Ensure that the file specified by fd encompasses a range of bytes |
| 10181 | starting at offset bytes from the beginning and continuing for length bytes. |
| 10182 | [clinic start generated code]*/ |
| 10183 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10184 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10185 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10186 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10187 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10188 | { |
| 10189 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10190 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10191 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10192 | do { |
| 10193 | Py_BEGIN_ALLOW_THREADS |
| 10194 | result = posix_fallocate(fd, offset, length); |
| 10195 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10196 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10197 | |
| 10198 | if (result == 0) |
| 10199 | Py_RETURN_NONE; |
| 10200 | |
| 10201 | if (async_err) |
| 10202 | return NULL; |
| 10203 | |
| 10204 | errno = result; |
| 10205 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10206 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10207 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10208 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10209 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10210 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10211 | /*[clinic input] |
| 10212 | os.posix_fadvise |
| 10213 | |
| 10214 | fd: int |
| 10215 | offset: Py_off_t |
| 10216 | length: Py_off_t |
| 10217 | advice: int |
| 10218 | / |
| 10219 | |
| 10220 | Announce an intention to access data in a specific pattern. |
| 10221 | |
| 10222 | Announce an intention to access data in a specific pattern, thus allowing |
| 10223 | the kernel to make optimizations. |
| 10224 | The advice applies to the region of the file specified by fd starting at |
| 10225 | offset and continuing for length bytes. |
| 10226 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 10227 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 10228 | POSIX_FADV_DONTNEED. |
| 10229 | [clinic start generated code]*/ |
| 10230 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10231 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10232 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10233 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10234 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10235 | { |
| 10236 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10237 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10238 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10239 | do { |
| 10240 | Py_BEGIN_ALLOW_THREADS |
| 10241 | result = posix_fadvise(fd, offset, length, advice); |
| 10242 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10243 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10244 | |
| 10245 | if (result == 0) |
| 10246 | Py_RETURN_NONE; |
| 10247 | |
| 10248 | if (async_err) |
| 10249 | return NULL; |
| 10250 | |
| 10251 | errno = result; |
| 10252 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10253 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10254 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10255 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10256 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 10257 | #ifdef MS_WINDOWS |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10258 | static PyObject* |
| 10259 | win32_putenv(PyObject *name, PyObject *value) |
| 10260 | { |
| 10261 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 10262 | defining hidden environment variables. */ |
| 10263 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 10264 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 10265 | { |
| 10266 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10267 | return NULL; |
| 10268 | } |
| 10269 | PyObject *unicode; |
| 10270 | if (value != NULL) { |
| 10271 | unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 10272 | } |
| 10273 | else { |
| 10274 | unicode = PyUnicode_FromFormat("%U=", name); |
| 10275 | } |
| 10276 | if (unicode == NULL) { |
| 10277 | return NULL; |
| 10278 | } |
| 10279 | |
| 10280 | Py_ssize_t size; |
| 10281 | /* PyUnicode_AsWideCharString() rejects embedded null characters */ |
| 10282 | wchar_t *env = PyUnicode_AsWideCharString(unicode, &size); |
| 10283 | Py_DECREF(unicode); |
| 10284 | |
| 10285 | if (env == NULL) { |
| 10286 | return NULL; |
| 10287 | } |
| 10288 | if (size > _MAX_ENV) { |
| 10289 | PyErr_Format(PyExc_ValueError, |
| 10290 | "the environment variable is longer than %u characters", |
| 10291 | _MAX_ENV); |
| 10292 | PyMem_Free(env); |
| 10293 | return NULL; |
| 10294 | } |
| 10295 | |
| 10296 | /* _wputenv() and SetEnvironmentVariableW() update the environment in the |
| 10297 | Process Environment Block (PEB). _wputenv() also updates CRT 'environ' |
| 10298 | and '_wenviron' variables, whereas SetEnvironmentVariableW() does not. |
| 10299 | |
| 10300 | Prefer _wputenv() to be compatible with C libraries using CRT |
| 10301 | variables and CRT functions using these variables (ex: getenv()). */ |
| 10302 | int err = _wputenv(env); |
| 10303 | PyMem_Free(env); |
| 10304 | |
| 10305 | if (err) { |
| 10306 | posix_error(); |
| 10307 | return NULL; |
| 10308 | } |
| 10309 | |
| 10310 | Py_RETURN_NONE; |
| 10311 | } |
| 10312 | #endif |
| 10313 | |
| 10314 | |
| 10315 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10316 | /*[clinic input] |
| 10317 | os.putenv |
| 10318 | |
| 10319 | name: unicode |
| 10320 | value: unicode |
| 10321 | / |
| 10322 | |
| 10323 | Change or add an environment variable. |
| 10324 | [clinic start generated code]*/ |
| 10325 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10326 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10327 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10328 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10329 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10330 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10331 | return NULL; |
| 10332 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10333 | return win32_putenv(name, value); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10334 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10335 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10336 | /*[clinic input] |
| 10337 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10339 | name: FSConverter |
| 10340 | value: FSConverter |
| 10341 | / |
| 10342 | |
| 10343 | Change or add an environment variable. |
| 10344 | [clinic start generated code]*/ |
| 10345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10346 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10347 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10348 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10349 | { |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10350 | const char *name_string = PyBytes_AS_STRING(name); |
| 10351 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10352 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10353 | if (strchr(name_string, '=') != NULL) { |
| 10354 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10355 | return NULL; |
| 10356 | } |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10357 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10358 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10359 | return NULL; |
| 10360 | } |
| 10361 | |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10362 | if (setenv(name_string, value_string, 1)) { |
| 10363 | return posix_error(); |
| 10364 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10365 | Py_RETURN_NONE; |
| 10366 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10367 | #endif /* !defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10368 | |
| 10369 | |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10370 | #ifdef MS_WINDOWS |
| 10371 | /*[clinic input] |
| 10372 | os.unsetenv |
| 10373 | name: unicode |
| 10374 | / |
| 10375 | |
| 10376 | Delete an environment variable. |
| 10377 | [clinic start generated code]*/ |
| 10378 | |
| 10379 | static PyObject * |
| 10380 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10381 | /*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/ |
| 10382 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10383 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10384 | return NULL; |
| 10385 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10386 | return win32_putenv(name, NULL); |
| 10387 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10388 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10389 | /*[clinic input] |
| 10390 | os.unsetenv |
| 10391 | name: FSConverter |
| 10392 | / |
| 10393 | |
| 10394 | Delete an environment variable. |
| 10395 | [clinic start generated code]*/ |
| 10396 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10397 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10398 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10399 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10400 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10401 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10402 | return NULL; |
| 10403 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10404 | #ifdef HAVE_BROKEN_UNSETENV |
| 10405 | unsetenv(PyBytes_AS_STRING(name)); |
| 10406 | #else |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10407 | int err = unsetenv(PyBytes_AS_STRING(name)); |
| 10408 | if (err) { |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10409 | return posix_error(); |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10410 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10411 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10412 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10413 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10414 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10415 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10416 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10417 | |
| 10418 | /*[clinic input] |
| 10419 | os.strerror |
| 10420 | |
| 10421 | code: int |
| 10422 | / |
| 10423 | |
| 10424 | Translate an error code to a message string. |
| 10425 | [clinic start generated code]*/ |
| 10426 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10427 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10428 | os_strerror_impl(PyObject *module, int code) |
| 10429 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10430 | { |
| 10431 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10432 | if (message == NULL) { |
| 10433 | PyErr_SetString(PyExc_ValueError, |
| 10434 | "strerror() argument out of range"); |
| 10435 | return NULL; |
| 10436 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 10437 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10438 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10439 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10440 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10441 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10442 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10443 | /*[clinic input] |
| 10444 | os.WCOREDUMP -> bool |
| 10445 | |
| 10446 | status: int |
| 10447 | / |
| 10448 | |
| 10449 | Return True if the process returning status was dumped to a core file. |
| 10450 | [clinic start generated code]*/ |
| 10451 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10452 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10453 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 10454 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10455 | { |
| 10456 | WAIT_TYPE wait_status; |
| 10457 | WAIT_STATUS_INT(wait_status) = status; |
| 10458 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10459 | } |
| 10460 | #endif /* WCOREDUMP */ |
| 10461 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10462 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10463 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10464 | /*[clinic input] |
| 10465 | os.WIFCONTINUED -> bool |
| 10466 | |
| 10467 | status: int |
| 10468 | |
| 10469 | Return True if a particular process was continued from a job control stop. |
| 10470 | |
| 10471 | Return True if the process returning status was continued from a |
| 10472 | job control stop. |
| 10473 | [clinic start generated code]*/ |
| 10474 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10475 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10476 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 10477 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10478 | { |
| 10479 | WAIT_TYPE wait_status; |
| 10480 | WAIT_STATUS_INT(wait_status) = status; |
| 10481 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10482 | } |
| 10483 | #endif /* WIFCONTINUED */ |
| 10484 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10485 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10486 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10487 | /*[clinic input] |
| 10488 | os.WIFSTOPPED -> bool |
| 10489 | |
| 10490 | status: int |
| 10491 | |
| 10492 | Return True if the process returning status was stopped. |
| 10493 | [clinic start generated code]*/ |
| 10494 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10495 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10496 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 10497 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10498 | { |
| 10499 | WAIT_TYPE wait_status; |
| 10500 | WAIT_STATUS_INT(wait_status) = status; |
| 10501 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10502 | } |
| 10503 | #endif /* WIFSTOPPED */ |
| 10504 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10505 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10506 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10507 | /*[clinic input] |
| 10508 | os.WIFSIGNALED -> bool |
| 10509 | |
| 10510 | status: int |
| 10511 | |
| 10512 | Return True if the process returning status was terminated by a signal. |
| 10513 | [clinic start generated code]*/ |
| 10514 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10515 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10516 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 10517 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10518 | { |
| 10519 | WAIT_TYPE wait_status; |
| 10520 | WAIT_STATUS_INT(wait_status) = status; |
| 10521 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10522 | } |
| 10523 | #endif /* WIFSIGNALED */ |
| 10524 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10525 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10526 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10527 | /*[clinic input] |
| 10528 | os.WIFEXITED -> bool |
| 10529 | |
| 10530 | status: int |
| 10531 | |
| 10532 | Return True if the process returning status exited via the exit() system call. |
| 10533 | [clinic start generated code]*/ |
| 10534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10535 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10536 | os_WIFEXITED_impl(PyObject *module, int status) |
| 10537 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10538 | { |
| 10539 | WAIT_TYPE wait_status; |
| 10540 | WAIT_STATUS_INT(wait_status) = status; |
| 10541 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10542 | } |
| 10543 | #endif /* WIFEXITED */ |
| 10544 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10545 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10546 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10547 | /*[clinic input] |
| 10548 | os.WEXITSTATUS -> int |
| 10549 | |
| 10550 | status: int |
| 10551 | |
| 10552 | Return the process return code from status. |
| 10553 | [clinic start generated code]*/ |
| 10554 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10555 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10556 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 10557 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10558 | { |
| 10559 | WAIT_TYPE wait_status; |
| 10560 | WAIT_STATUS_INT(wait_status) = status; |
| 10561 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10562 | } |
| 10563 | #endif /* WEXITSTATUS */ |
| 10564 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10565 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10566 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10567 | /*[clinic input] |
| 10568 | os.WTERMSIG -> int |
| 10569 | |
| 10570 | status: int |
| 10571 | |
| 10572 | Return the signal that terminated the process that provided the status value. |
| 10573 | [clinic start generated code]*/ |
| 10574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10575 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10576 | os_WTERMSIG_impl(PyObject *module, int status) |
| 10577 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10578 | { |
| 10579 | WAIT_TYPE wait_status; |
| 10580 | WAIT_STATUS_INT(wait_status) = status; |
| 10581 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10582 | } |
| 10583 | #endif /* WTERMSIG */ |
| 10584 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10585 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10586 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10587 | /*[clinic input] |
| 10588 | os.WSTOPSIG -> int |
| 10589 | |
| 10590 | status: int |
| 10591 | |
| 10592 | Return the signal that stopped the process that provided the status value. |
| 10593 | [clinic start generated code]*/ |
| 10594 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10595 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10596 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 10597 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10598 | { |
| 10599 | WAIT_TYPE wait_status; |
| 10600 | WAIT_STATUS_INT(wait_status) = status; |
| 10601 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10602 | } |
| 10603 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10604 | #endif /* HAVE_SYS_WAIT_H */ |
| 10605 | |
| 10606 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10607 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10608 | #ifdef _SCO_DS |
| 10609 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 10610 | needed definitions in sys/statvfs.h */ |
| 10611 | #define _SVID3 |
| 10612 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10613 | #include <sys/statvfs.h> |
| 10614 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10615 | static PyObject* |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10616 | _pystatvfs_fromstructstatvfs(PyObject *module, struct statvfs st) { |
| 10617 | PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 10618 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10619 | if (v == NULL) |
| 10620 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10621 | |
| 10622 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10623 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10624 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10625 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 10626 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 10627 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 10628 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 10629 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 10630 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 10631 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10632 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10633 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10634 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10635 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10636 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10637 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10638 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10639 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10640 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10641 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10642 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10643 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10644 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10645 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10646 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10647 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10648 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10649 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10650 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10651 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 10652 | * (issue #32390). */ |
| 10653 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 10654 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 10655 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10656 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10657 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10658 | if (PyErr_Occurred()) { |
| 10659 | Py_DECREF(v); |
| 10660 | return NULL; |
| 10661 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10662 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10663 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10664 | } |
| 10665 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10666 | |
| 10667 | /*[clinic input] |
| 10668 | os.fstatvfs |
| 10669 | fd: int |
| 10670 | / |
| 10671 | |
| 10672 | Perform an fstatvfs system call on the given fd. |
| 10673 | |
| 10674 | Equivalent to statvfs(fd). |
| 10675 | [clinic start generated code]*/ |
| 10676 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10677 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10678 | os_fstatvfs_impl(PyObject *module, int fd) |
| 10679 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10680 | { |
| 10681 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10682 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10683 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10684 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10685 | do { |
| 10686 | Py_BEGIN_ALLOW_THREADS |
| 10687 | result = fstatvfs(fd, &st); |
| 10688 | Py_END_ALLOW_THREADS |
| 10689 | } while (result != 0 && errno == EINTR && |
| 10690 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10691 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10692 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10693 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10694 | return _pystatvfs_fromstructstatvfs(module, st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10695 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10696 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10697 | |
| 10698 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10699 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10700 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10701 | /*[clinic input] |
| 10702 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10703 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10704 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10705 | |
| 10706 | Perform a statvfs system call on the given path. |
| 10707 | |
| 10708 | path may always be specified as a string. |
| 10709 | On some platforms, path may also be specified as an open file descriptor. |
| 10710 | If this functionality is unavailable, using it raises an exception. |
| 10711 | [clinic start generated code]*/ |
| 10712 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10713 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10714 | os_statvfs_impl(PyObject *module, path_t *path) |
| 10715 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10716 | { |
| 10717 | int result; |
| 10718 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10719 | |
| 10720 | Py_BEGIN_ALLOW_THREADS |
| 10721 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10722 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10723 | #ifdef __APPLE__ |
| 10724 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10725 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10726 | fd_specified("statvfs", path->fd); |
| 10727 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10728 | } |
| 10729 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10730 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10731 | } |
| 10732 | else |
| 10733 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10734 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10735 | Py_END_ALLOW_THREADS |
| 10736 | |
| 10737 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10738 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10739 | } |
| 10740 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 10741 | return _pystatvfs_fromstructstatvfs(module, st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10742 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10743 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10744 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10745 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10746 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10747 | /*[clinic input] |
| 10748 | os._getdiskusage |
| 10749 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10750 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10751 | |
| 10752 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10753 | [clinic start generated code]*/ |
| 10754 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10755 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10756 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10757 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10758 | { |
| 10759 | BOOL retval; |
| 10760 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10761 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10762 | |
| 10763 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10764 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10765 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10766 | if (retval == 0) { |
| 10767 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10768 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10769 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10770 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10771 | if (dir_path == NULL) { |
| 10772 | return PyErr_NoMemory(); |
| 10773 | } |
| 10774 | |
| 10775 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10776 | |
| 10777 | if (_dirnameW(dir_path) != -1) { |
| 10778 | Py_BEGIN_ALLOW_THREADS |
| 10779 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10780 | Py_END_ALLOW_THREADS |
| 10781 | } |
| 10782 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10783 | err = GetLastError(); |
| 10784 | PyMem_Free(dir_path); |
| 10785 | if (retval) { |
| 10786 | goto success; |
| 10787 | } |
| 10788 | } |
| 10789 | return PyErr_SetFromWindowsErr(err); |
| 10790 | } |
| 10791 | |
| 10792 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10793 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10794 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10795 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10796 | |
| 10797 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10798 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10799 | * It maps strings representing configuration variable names to |
| 10800 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10801 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10802 | * rarely-used constants. There are three separate tables that use |
| 10803 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10804 | * |
| 10805 | * This code is always included, even if none of the interfaces that |
| 10806 | * need it are included. The #if hackery needed to avoid it would be |
| 10807 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10808 | */ |
| 10809 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10810 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10811 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10812 | }; |
| 10813 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10814 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10815 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10816 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10817 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10818 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10819 | int value = _PyLong_AsInt(arg); |
| 10820 | if (value == -1 && PyErr_Occurred()) |
| 10821 | return 0; |
| 10822 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10823 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10824 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10825 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10826 | /* look up the value in the table using a binary search */ |
| 10827 | size_t lo = 0; |
| 10828 | size_t mid; |
| 10829 | size_t hi = tablesize; |
| 10830 | int cmp; |
| 10831 | const char *confname; |
| 10832 | if (!PyUnicode_Check(arg)) { |
| 10833 | PyErr_SetString(PyExc_TypeError, |
| 10834 | "configuration names must be strings or integers"); |
| 10835 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10836 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10837 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10838 | if (confname == NULL) |
| 10839 | return 0; |
| 10840 | while (lo < hi) { |
| 10841 | mid = (lo + hi) / 2; |
| 10842 | cmp = strcmp(confname, table[mid].name); |
| 10843 | if (cmp < 0) |
| 10844 | hi = mid; |
| 10845 | else if (cmp > 0) |
| 10846 | lo = mid + 1; |
| 10847 | else { |
| 10848 | *valuep = table[mid].value; |
| 10849 | return 1; |
| 10850 | } |
| 10851 | } |
| 10852 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10853 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10854 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10855 | } |
| 10856 | |
| 10857 | |
| 10858 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10859 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10860 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10861 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10862 | #endif |
| 10863 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10864 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10865 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10866 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10867 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10868 | #endif |
| 10869 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10870 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10871 | #endif |
| 10872 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10873 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10874 | #endif |
| 10875 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10876 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10877 | #endif |
| 10878 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10879 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10880 | #endif |
| 10881 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10882 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10883 | #endif |
| 10884 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10885 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10886 | #endif |
| 10887 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10888 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10889 | #endif |
| 10890 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10891 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10892 | #endif |
| 10893 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10894 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10895 | #endif |
| 10896 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10897 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10898 | #endif |
| 10899 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10900 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10901 | #endif |
| 10902 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10903 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10904 | #endif |
| 10905 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10906 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10907 | #endif |
| 10908 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10909 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10910 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10911 | #ifdef _PC_ACL_ENABLED |
| 10912 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10913 | #endif |
| 10914 | #ifdef _PC_MIN_HOLE_SIZE |
| 10915 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10916 | #endif |
| 10917 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10918 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10919 | #endif |
| 10920 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10921 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10922 | #endif |
| 10923 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10924 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10925 | #endif |
| 10926 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10927 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10928 | #endif |
| 10929 | #ifdef _PC_REC_XFER_ALIGN |
| 10930 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10931 | #endif |
| 10932 | #ifdef _PC_SYMLINK_MAX |
| 10933 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10934 | #endif |
| 10935 | #ifdef _PC_XATTR_ENABLED |
| 10936 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10937 | #endif |
| 10938 | #ifdef _PC_XATTR_EXISTS |
| 10939 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10940 | #endif |
| 10941 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10942 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10943 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10944 | }; |
| 10945 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10946 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10947 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10948 | { |
| 10949 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10950 | sizeof(posix_constants_pathconf) |
| 10951 | / sizeof(struct constdef)); |
| 10952 | } |
| 10953 | #endif |
| 10954 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10955 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10956 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10957 | /*[clinic input] |
| 10958 | os.fpathconf -> long |
| 10959 | |
| 10960 | fd: int |
| 10961 | name: path_confname |
| 10962 | / |
| 10963 | |
| 10964 | Return the configuration limit name for the file descriptor fd. |
| 10965 | |
| 10966 | If there is no limit, return -1. |
| 10967 | [clinic start generated code]*/ |
| 10968 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10969 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10970 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 10971 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10972 | { |
| 10973 | long limit; |
| 10974 | |
| 10975 | errno = 0; |
| 10976 | limit = fpathconf(fd, name); |
| 10977 | if (limit == -1 && errno != 0) |
| 10978 | posix_error(); |
| 10979 | |
| 10980 | return limit; |
| 10981 | } |
| 10982 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10983 | |
| 10984 | |
| 10985 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10986 | /*[clinic input] |
| 10987 | os.pathconf -> long |
| 10988 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 10989 | name: path_confname |
| 10990 | |
| 10991 | Return the configuration limit name for the file or directory path. |
| 10992 | |
| 10993 | If there is no limit, return -1. |
| 10994 | On some platforms, path may also be specified as an open file descriptor. |
| 10995 | If this functionality is unavailable, using it raises an exception. |
| 10996 | [clinic start generated code]*/ |
| 10997 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10998 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10999 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 11000 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11001 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11002 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11003 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11004 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11005 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11006 | if (path->fd != -1) |
| 11007 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11008 | else |
| 11009 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11010 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11011 | if (limit == -1 && errno != 0) { |
| 11012 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 11013 | /* could be a path or name problem */ |
| 11014 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11015 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11016 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11017 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11018 | |
| 11019 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11020 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11021 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11022 | |
| 11023 | #ifdef HAVE_CONFSTR |
| 11024 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11025 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11026 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11027 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11028 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11029 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11030 | #endif |
| 11031 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11032 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11033 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11034 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11035 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11036 | #endif |
| 11037 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11038 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11039 | #endif |
| 11040 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11041 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11042 | #endif |
| 11043 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11044 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11045 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11046 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11047 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11048 | #endif |
| 11049 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11050 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11051 | #endif |
| 11052 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11053 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11054 | #endif |
| 11055 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11056 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11057 | #endif |
| 11058 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11059 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11060 | #endif |
| 11061 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11062 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11063 | #endif |
| 11064 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11065 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11066 | #endif |
| 11067 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11068 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11069 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11070 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11071 | {"CS_MACHINE", _CS_MACHINE}, |
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_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11074 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11075 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11076 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11077 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11078 | #endif |
| 11079 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11080 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11081 | #endif |
| 11082 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11083 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11084 | #endif |
| 11085 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11086 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11087 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11088 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11089 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11090 | #endif |
| 11091 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11092 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11093 | #endif |
| 11094 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11095 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11096 | #endif |
| 11097 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11098 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11099 | #endif |
| 11100 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11101 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11102 | #endif |
| 11103 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11104 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11105 | #endif |
| 11106 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11107 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11108 | #endif |
| 11109 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11110 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11111 | #endif |
| 11112 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11113 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11114 | #endif |
| 11115 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11116 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11117 | #endif |
| 11118 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11119 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11120 | #endif |
| 11121 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11122 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11123 | #endif |
| 11124 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11125 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11126 | #endif |
| 11127 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11128 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11129 | #endif |
| 11130 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11131 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11132 | #endif |
| 11133 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11134 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11135 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11136 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11137 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11138 | #endif |
| 11139 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11140 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11141 | #endif |
| 11142 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11143 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11144 | #endif |
| 11145 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11146 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11147 | #endif |
| 11148 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11149 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11150 | #endif |
| 11151 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11152 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11153 | #endif |
| 11154 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11155 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11156 | #endif |
| 11157 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11158 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11159 | #endif |
| 11160 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11161 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11162 | #endif |
| 11163 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11164 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11165 | #endif |
| 11166 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11167 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11168 | #endif |
| 11169 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11170 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11171 | #endif |
| 11172 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11173 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11174 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11175 | }; |
| 11176 | |
| 11177 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11178 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11179 | { |
| 11180 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 11181 | sizeof(posix_constants_confstr) |
| 11182 | / sizeof(struct constdef)); |
| 11183 | } |
| 11184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11185 | |
| 11186 | /*[clinic input] |
| 11187 | os.confstr |
| 11188 | |
| 11189 | name: confstr_confname |
| 11190 | / |
| 11191 | |
| 11192 | Return a string-valued system configuration variable. |
| 11193 | [clinic start generated code]*/ |
| 11194 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11195 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11196 | os_confstr_impl(PyObject *module, int name) |
| 11197 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11198 | { |
| 11199 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11200 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11201 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11202 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11203 | errno = 0; |
| 11204 | len = confstr(name, buffer, sizeof(buffer)); |
| 11205 | if (len == 0) { |
| 11206 | if (errno) { |
| 11207 | posix_error(); |
| 11208 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11209 | } |
| 11210 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11211 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11212 | } |
| 11213 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11214 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11215 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11216 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11217 | char *buf = PyMem_Malloc(len); |
| 11218 | if (buf == NULL) |
| 11219 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11220 | len2 = confstr(name, buf, len); |
| 11221 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 11222 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11223 | PyMem_Free(buf); |
| 11224 | } |
| 11225 | else |
| 11226 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11227 | return result; |
| 11228 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11229 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11230 | |
| 11231 | |
| 11232 | #ifdef HAVE_SYSCONF |
| 11233 | static struct constdef posix_constants_sysconf[] = { |
| 11234 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11235 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11236 | #endif |
| 11237 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11238 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11239 | #endif |
| 11240 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11241 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11242 | #endif |
| 11243 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11244 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11245 | #endif |
| 11246 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11247 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11248 | #endif |
| 11249 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11250 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11251 | #endif |
| 11252 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11253 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11254 | #endif |
| 11255 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11256 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11257 | #endif |
| 11258 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11259 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11260 | #endif |
| 11261 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11262 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11263 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11264 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11265 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11266 | #endif |
| 11267 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11268 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11269 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11270 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11271 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11272 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11273 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11274 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11275 | #endif |
| 11276 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11277 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11278 | #endif |
| 11279 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11280 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11281 | #endif |
| 11282 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11283 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11284 | #endif |
| 11285 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11286 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11287 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11288 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11289 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11290 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11291 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11292 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11293 | #endif |
| 11294 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11295 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11296 | #endif |
| 11297 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11298 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11299 | #endif |
| 11300 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11301 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11302 | #endif |
| 11303 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11304 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11305 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11306 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11307 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11308 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11309 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11310 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11311 | #endif |
| 11312 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11313 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11314 | #endif |
| 11315 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11316 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11317 | #endif |
| 11318 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11319 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11320 | #endif |
| 11321 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11322 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11323 | #endif |
| 11324 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11325 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11326 | #endif |
| 11327 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11328 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11329 | #endif |
| 11330 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11331 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11332 | #endif |
| 11333 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11334 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11335 | #endif |
| 11336 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11337 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11338 | #endif |
| 11339 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11340 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11341 | #endif |
| 11342 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11343 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11344 | #endif |
| 11345 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11346 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11347 | #endif |
| 11348 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11349 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11350 | #endif |
| 11351 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11352 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11353 | #endif |
| 11354 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11355 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11356 | #endif |
| 11357 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11358 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11359 | #endif |
| 11360 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11361 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11362 | #endif |
| 11363 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11364 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11365 | #endif |
| 11366 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11367 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11368 | #endif |
| 11369 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11370 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11371 | #endif |
| 11372 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11373 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11374 | #endif |
| 11375 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11376 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11377 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11378 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11379 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11380 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11381 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11382 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11383 | #endif |
| 11384 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11385 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11386 | #endif |
| 11387 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11388 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11389 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11390 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11391 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11392 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11393 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11394 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11395 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11396 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11397 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11398 | #endif |
| 11399 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11400 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11401 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11402 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11403 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11404 | #endif |
| 11405 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11406 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11407 | #endif |
| 11408 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11409 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11410 | #endif |
| 11411 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11412 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11413 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11414 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11415 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11416 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11417 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11418 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11419 | #endif |
| 11420 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11421 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11422 | #endif |
| 11423 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11424 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11425 | #endif |
| 11426 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11427 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11428 | #endif |
| 11429 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11430 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11431 | #endif |
| 11432 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11433 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11434 | #endif |
| 11435 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11436 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11437 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11438 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11439 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11440 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11441 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11442 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11443 | #endif |
| 11444 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11445 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11446 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11447 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11448 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11449 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11450 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11451 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11452 | #endif |
| 11453 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11454 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11455 | #endif |
| 11456 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11457 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11458 | #endif |
| 11459 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11460 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11461 | #endif |
| 11462 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11463 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11464 | #endif |
| 11465 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11466 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11467 | #endif |
| 11468 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11469 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11470 | #endif |
| 11471 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11472 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11473 | #endif |
| 11474 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11475 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11476 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11477 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11478 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11479 | #endif |
| 11480 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11481 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11482 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11483 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11484 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11485 | #endif |
| 11486 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11487 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11488 | #endif |
| 11489 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11490 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11491 | #endif |
| 11492 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11493 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11494 | #endif |
Batuhan Taşkaya | 909f4a3 | 2020-04-05 03:40:49 +0300 | [diff] [blame] | 11495 | #ifdef _SC_AIX_REALMEM |
| 11496 | {"SC_AIX_REALMEM", _SC_AIX_REALMEM}, |
| 11497 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11498 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11499 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11500 | #endif |
| 11501 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11502 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11503 | #endif |
| 11504 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11505 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11506 | #endif |
| 11507 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11508 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11509 | #endif |
| 11510 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11511 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11512 | #endif |
| 11513 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11514 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11515 | #endif |
| 11516 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11517 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11518 | #endif |
| 11519 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11520 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11521 | #endif |
| 11522 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11523 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11524 | #endif |
| 11525 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11526 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11527 | #endif |
| 11528 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11529 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11530 | #endif |
| 11531 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11532 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11533 | #endif |
| 11534 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11535 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11536 | #endif |
| 11537 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11538 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11539 | #endif |
| 11540 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11541 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11542 | #endif |
| 11543 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11544 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11545 | #endif |
| 11546 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11547 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11548 | #endif |
| 11549 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11550 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11551 | #endif |
| 11552 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11553 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11554 | #endif |
| 11555 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11556 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11557 | #endif |
| 11558 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11559 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11560 | #endif |
| 11561 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11562 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11563 | #endif |
| 11564 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11565 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11566 | #endif |
| 11567 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11568 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11569 | #endif |
| 11570 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11571 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11572 | #endif |
| 11573 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11574 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11575 | #endif |
| 11576 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11577 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11578 | #endif |
| 11579 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11580 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11581 | #endif |
| 11582 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11583 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11584 | #endif |
| 11585 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11586 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11587 | #endif |
| 11588 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11589 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11590 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11591 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11592 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11593 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11594 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11595 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11596 | #endif |
| 11597 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11598 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11599 | #endif |
| 11600 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11601 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11602 | #endif |
| 11603 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11604 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11605 | #endif |
| 11606 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11607 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11608 | #endif |
| 11609 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11610 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11611 | #endif |
| 11612 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11613 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11614 | #endif |
| 11615 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11616 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11617 | #endif |
| 11618 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11619 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11620 | #endif |
| 11621 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11622 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11623 | #endif |
| 11624 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11625 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11626 | #endif |
| 11627 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11628 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11629 | #endif |
| 11630 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11631 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11632 | #endif |
| 11633 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11634 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11635 | #endif |
| 11636 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11637 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11638 | #endif |
| 11639 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11640 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11641 | #endif |
| 11642 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11643 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11644 | #endif |
| 11645 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11646 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11647 | #endif |
| 11648 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11649 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11650 | #endif |
| 11651 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11652 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11653 | #endif |
| 11654 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11655 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11656 | #endif |
| 11657 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11658 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11659 | #endif |
| 11660 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11661 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11662 | #endif |
| 11663 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11664 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11665 | #endif |
| 11666 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11667 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11668 | #endif |
| 11669 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11670 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11671 | #endif |
| 11672 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11673 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11674 | #endif |
| 11675 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11676 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11677 | #endif |
| 11678 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11679 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11680 | #endif |
| 11681 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11682 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11683 | #endif |
| 11684 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11685 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11686 | #endif |
| 11687 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11688 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11689 | #endif |
| 11690 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11691 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11692 | #endif |
| 11693 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11694 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11695 | #endif |
| 11696 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11697 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11698 | #endif |
| 11699 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11700 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11701 | #endif |
| 11702 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11703 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11704 | #endif |
| 11705 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11706 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11707 | #endif |
| 11708 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11709 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11710 | #endif |
| 11711 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11712 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11713 | #endif |
| 11714 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11715 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11716 | #endif |
| 11717 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11718 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11719 | #endif |
| 11720 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11721 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11722 | #endif |
| 11723 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11724 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11725 | #endif |
| 11726 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11727 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11728 | #endif |
| 11729 | }; |
| 11730 | |
| 11731 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11732 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11733 | { |
| 11734 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11735 | sizeof(posix_constants_sysconf) |
| 11736 | / sizeof(struct constdef)); |
| 11737 | } |
| 11738 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11739 | |
| 11740 | /*[clinic input] |
| 11741 | os.sysconf -> long |
| 11742 | name: sysconf_confname |
| 11743 | / |
| 11744 | |
| 11745 | Return an integer-valued system configuration variable. |
| 11746 | [clinic start generated code]*/ |
| 11747 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11748 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11749 | os_sysconf_impl(PyObject *module, int name) |
| 11750 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11751 | { |
| 11752 | long value; |
| 11753 | |
| 11754 | errno = 0; |
| 11755 | value = sysconf(name); |
| 11756 | if (value == -1 && errno != 0) |
| 11757 | posix_error(); |
| 11758 | return value; |
| 11759 | } |
| 11760 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11761 | |
| 11762 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11763 | /* 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] | 11764 | * 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] | 11765 | * the exported dictionaries that are used to publish information about the |
| 11766 | * names available on the host platform. |
| 11767 | * |
| 11768 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11769 | * when used, even for platforms we're not able to test on. It also makes |
| 11770 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11771 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11772 | |
| 11773 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11774 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11775 | { |
| 11776 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11777 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11778 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11779 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11780 | |
| 11781 | return strcmp(c1->name, c2->name); |
| 11782 | } |
| 11783 | |
| 11784 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11785 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11786 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11787 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11788 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11789 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11790 | |
| 11791 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11792 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11793 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11794 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11795 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11796 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11797 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11798 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11799 | Py_XDECREF(o); |
| 11800 | Py_DECREF(d); |
| 11801 | return -1; |
| 11802 | } |
| 11803 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11804 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11805 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11806 | } |
| 11807 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11808 | /* Return -1 on failure, 0 on success. */ |
| 11809 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11810 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11811 | { |
| 11812 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11813 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11814 | sizeof(posix_constants_pathconf) |
| 11815 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11816 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11817 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11818 | #endif |
| 11819 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11820 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11821 | sizeof(posix_constants_confstr) |
| 11822 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11823 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11824 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11825 | #endif |
| 11826 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11827 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11828 | sizeof(posix_constants_sysconf) |
| 11829 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11830 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11831 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11832 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11833 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11834 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11835 | |
| 11836 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11837 | /*[clinic input] |
| 11838 | os.abort |
| 11839 | |
| 11840 | Abort the interpreter immediately. |
| 11841 | |
| 11842 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11843 | on the hosting operating system. This function never returns. |
| 11844 | [clinic start generated code]*/ |
| 11845 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11846 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11847 | os_abort_impl(PyObject *module) |
| 11848 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11849 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11850 | abort(); |
| 11851 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11852 | #ifndef __clang__ |
| 11853 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11854 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11855 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11856 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11857 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11858 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11859 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11860 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11861 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11862 | /* Grab ShellExecute dynamically from shell32 */ |
| 11863 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11864 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11865 | LPCWSTR, INT); |
| 11866 | static int |
| 11867 | check_ShellExecute() |
| 11868 | { |
| 11869 | HINSTANCE hShell32; |
| 11870 | |
| 11871 | /* only recheck */ |
| 11872 | if (-1 == has_ShellExecute) { |
| 11873 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11874 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11875 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11876 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11877 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11878 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11879 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11880 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11881 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11882 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11883 | } else { |
| 11884 | has_ShellExecute = 0; |
| 11885 | } |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 11886 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11887 | } |
| 11888 | return has_ShellExecute; |
| 11889 | } |
| 11890 | |
| 11891 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11892 | /*[clinic input] |
| 11893 | os.startfile |
| 11894 | filepath: path_t |
| 11895 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11896 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11897 | Start a file with its associated application. |
| 11898 | |
| 11899 | When "operation" is not specified or "open", this acts like |
| 11900 | double-clicking the file in Explorer, or giving the file name as an |
| 11901 | argument to the DOS "start" command: the file is opened with whatever |
| 11902 | application (if any) its extension is associated. |
| 11903 | When another "operation" is given, it specifies what should be done with |
| 11904 | the file. A typical operation is "print". |
| 11905 | |
| 11906 | startfile returns as soon as the associated application is launched. |
| 11907 | There is no option to wait for the application to close, and no way |
| 11908 | to retrieve the application's exit status. |
| 11909 | |
| 11910 | The filepath is relative to the current directory. If you want to use |
| 11911 | an absolute path, make sure the first character is not a slash ("/"); |
| 11912 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11913 | [clinic start generated code]*/ |
| 11914 | |
| 11915 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11916 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11917 | const Py_UNICODE *operation) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 11918 | /*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11919 | { |
| 11920 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11921 | |
| 11922 | if(!check_ShellExecute()) { |
| 11923 | /* If the OS doesn't have ShellExecute, return a |
| 11924 | NotImplementedError. */ |
| 11925 | return PyErr_Format(PyExc_NotImplementedError, |
| 11926 | "startfile not available on this platform"); |
| 11927 | } |
| 11928 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 11929 | if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 11930 | return NULL; |
| 11931 | } |
| 11932 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11933 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11934 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11935 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11936 | Py_END_ALLOW_THREADS |
| 11937 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11938 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11939 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11940 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11941 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11942 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11943 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11944 | #endif /* MS_WINDOWS */ |
| 11945 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11946 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11947 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11948 | /*[clinic input] |
| 11949 | os.getloadavg |
| 11950 | |
| 11951 | Return average recent system load information. |
| 11952 | |
| 11953 | Return the number of processes in the system run queue averaged over |
| 11954 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11955 | Raises OSError if the load average was unobtainable. |
| 11956 | [clinic start generated code]*/ |
| 11957 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11958 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11959 | os_getloadavg_impl(PyObject *module) |
| 11960 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11961 | { |
| 11962 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11963 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11964 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11965 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11966 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11967 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11968 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11969 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11970 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11971 | |
| 11972 | /*[clinic input] |
| 11973 | os.device_encoding |
| 11974 | fd: int |
| 11975 | |
| 11976 | Return a string describing the encoding of a terminal's file descriptor. |
| 11977 | |
| 11978 | The file descriptor must be attached to a terminal. |
| 11979 | If the device is not a terminal, return None. |
| 11980 | [clinic start generated code]*/ |
| 11981 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11982 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11983 | os_device_encoding_impl(PyObject *module, int fd) |
| 11984 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11985 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11986 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 11987 | } |
| 11988 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11989 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11990 | #ifdef HAVE_SETRESUID |
| 11991 | /*[clinic input] |
| 11992 | os.setresuid |
| 11993 | |
| 11994 | ruid: uid_t |
| 11995 | euid: uid_t |
| 11996 | suid: uid_t |
| 11997 | / |
| 11998 | |
| 11999 | Set the current process's real, effective, and saved user ids. |
| 12000 | [clinic start generated code]*/ |
| 12001 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12002 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12003 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 12004 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12005 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12006 | if (setresuid(ruid, euid, suid) < 0) |
| 12007 | return posix_error(); |
| 12008 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12009 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12010 | #endif /* HAVE_SETRESUID */ |
| 12011 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12012 | |
| 12013 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12014 | /*[clinic input] |
| 12015 | os.setresgid |
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 | rgid: gid_t |
| 12018 | egid: gid_t |
| 12019 | sgid: gid_t |
| 12020 | / |
| 12021 | |
| 12022 | Set the current process's real, effective, and saved group ids. |
| 12023 | [clinic start generated code]*/ |
| 12024 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12025 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12026 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 12027 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12028 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12029 | if (setresgid(rgid, egid, sgid) < 0) |
| 12030 | return posix_error(); |
| 12031 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12032 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12033 | #endif /* HAVE_SETRESGID */ |
| 12034 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12035 | |
| 12036 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12037 | /*[clinic input] |
| 12038 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12039 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12040 | Return a tuple of the current process's real, effective, and saved user ids. |
| 12041 | [clinic start generated code]*/ |
| 12042 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12043 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12044 | os_getresuid_impl(PyObject *module) |
| 12045 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12046 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12047 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12048 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 12049 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 12050 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 12051 | _PyLong_FromUid(euid), |
| 12052 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12053 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12054 | #endif /* HAVE_GETRESUID */ |
| 12055 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12056 | |
| 12057 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12058 | /*[clinic input] |
| 12059 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12060 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12061 | Return a tuple of the current process's real, effective, and saved group ids. |
| 12062 | [clinic start generated code]*/ |
| 12063 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12064 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12065 | os_getresgid_impl(PyObject *module) |
| 12066 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12067 | { |
| 12068 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12069 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 12070 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 12071 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 12072 | _PyLong_FromGid(egid), |
| 12073 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12074 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12075 | #endif /* HAVE_GETRESGID */ |
| 12076 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12077 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12078 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12079 | /*[clinic input] |
| 12080 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12081 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12082 | path: path_t(allow_fd=True) |
| 12083 | attribute: path_t |
| 12084 | * |
| 12085 | follow_symlinks: bool = True |
| 12086 | |
| 12087 | Return the value of extended attribute attribute on path. |
| 12088 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12089 | 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] | 12090 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12091 | link, getxattr will examine the symbolic link itself instead of the file |
| 12092 | the link points to. |
| 12093 | |
| 12094 | [clinic start generated code]*/ |
| 12095 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12096 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12097 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12098 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12099 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12100 | { |
| 12101 | Py_ssize_t i; |
| 12102 | PyObject *buffer = NULL; |
| 12103 | |
| 12104 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 12105 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12106 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12107 | if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) { |
| 12108 | return NULL; |
| 12109 | } |
| 12110 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12111 | for (i = 0; ; i++) { |
| 12112 | void *ptr; |
| 12113 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12114 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12115 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12116 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12117 | path_error(path); |
| 12118 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12119 | } |
| 12120 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 12121 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12122 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12123 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12124 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12125 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12126 | if (path->fd >= 0) |
| 12127 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12128 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12129 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12130 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12131 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12132 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12133 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12134 | if (result < 0) { |
| 12135 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12136 | if (errno == ERANGE) |
| 12137 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12138 | path_error(path); |
| 12139 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12140 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12141 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12142 | if (result != buffer_size) { |
| 12143 | /* Can only shrink. */ |
| 12144 | _PyBytes_Resize(&buffer, result); |
| 12145 | } |
| 12146 | break; |
| 12147 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12148 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12149 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12150 | } |
| 12151 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12152 | |
| 12153 | /*[clinic input] |
| 12154 | os.setxattr |
| 12155 | |
| 12156 | path: path_t(allow_fd=True) |
| 12157 | attribute: path_t |
| 12158 | value: Py_buffer |
| 12159 | flags: int = 0 |
| 12160 | * |
| 12161 | follow_symlinks: bool = True |
| 12162 | |
| 12163 | Set extended attribute attribute on path to value. |
| 12164 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12165 | 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] | 12166 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12167 | link, setxattr will modify the symbolic link itself instead of the file |
| 12168 | the link points to. |
| 12169 | |
| 12170 | [clinic start generated code]*/ |
| 12171 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12172 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12173 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12174 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12175 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12176 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12177 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12178 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12179 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12180 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12181 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12182 | if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object, |
| 12183 | value->buf, value->len, flags) < 0) { |
| 12184 | return NULL; |
| 12185 | } |
| 12186 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12187 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12188 | if (path->fd > -1) |
| 12189 | result = fsetxattr(path->fd, attribute->narrow, |
| 12190 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12191 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12192 | result = setxattr(path->narrow, attribute->narrow, |
| 12193 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12194 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12195 | result = lsetxattr(path->narrow, attribute->narrow, |
| 12196 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12197 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12198 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12199 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12200 | path_error(path); |
| 12201 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12202 | } |
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 | Py_RETURN_NONE; |
| 12205 | } |
| 12206 | |
| 12207 | |
| 12208 | /*[clinic input] |
| 12209 | os.removexattr |
| 12210 | |
| 12211 | path: path_t(allow_fd=True) |
| 12212 | attribute: path_t |
| 12213 | * |
| 12214 | follow_symlinks: bool = True |
| 12215 | |
| 12216 | Remove extended attribute attribute on path. |
| 12217 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12218 | 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] | 12219 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12220 | link, removexattr will modify the symbolic link itself instead of the file |
| 12221 | the link points to. |
| 12222 | |
| 12223 | [clinic start generated code]*/ |
| 12224 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12225 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12226 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12227 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12228 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12229 | { |
| 12230 | ssize_t result; |
| 12231 | |
| 12232 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 12233 | return NULL; |
| 12234 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12235 | if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) { |
| 12236 | return NULL; |
| 12237 | } |
| 12238 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12239 | Py_BEGIN_ALLOW_THREADS; |
| 12240 | if (path->fd > -1) |
| 12241 | result = fremovexattr(path->fd, attribute->narrow); |
| 12242 | else if (follow_symlinks) |
| 12243 | result = removexattr(path->narrow, attribute->narrow); |
| 12244 | else |
| 12245 | result = lremovexattr(path->narrow, attribute->narrow); |
| 12246 | Py_END_ALLOW_THREADS; |
| 12247 | |
| 12248 | if (result) { |
| 12249 | return path_error(path); |
| 12250 | } |
| 12251 | |
| 12252 | Py_RETURN_NONE; |
| 12253 | } |
| 12254 | |
| 12255 | |
| 12256 | /*[clinic input] |
| 12257 | os.listxattr |
| 12258 | |
| 12259 | path: path_t(allow_fd=True, nullable=True) = None |
| 12260 | * |
| 12261 | follow_symlinks: bool = True |
| 12262 | |
| 12263 | Return a list of extended attributes on path. |
| 12264 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12265 | 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] | 12266 | if path is None, listxattr will examine the current directory. |
| 12267 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12268 | link, listxattr will examine the symbolic link itself instead of the file |
| 12269 | the link points to. |
| 12270 | [clinic start generated code]*/ |
| 12271 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12272 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12273 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12274 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12275 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12276 | Py_ssize_t i; |
| 12277 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12278 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12279 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12280 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12281 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12282 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12283 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12284 | if (PySys_Audit("os.listxattr", "(O)", |
| 12285 | path->object ? path->object : Py_None) < 0) { |
| 12286 | return NULL; |
| 12287 | } |
| 12288 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12289 | name = path->narrow ? path->narrow : "."; |
| 12290 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12291 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12292 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12293 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12294 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12295 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12296 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 12297 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12298 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12299 | break; |
| 12300 | } |
| 12301 | buffer = PyMem_MALLOC(buffer_size); |
| 12302 | if (!buffer) { |
| 12303 | PyErr_NoMemory(); |
| 12304 | break; |
| 12305 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12306 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12307 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12308 | if (path->fd > -1) |
| 12309 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12310 | else if (follow_symlinks) |
| 12311 | length = listxattr(name, buffer, buffer_size); |
| 12312 | else |
| 12313 | length = llistxattr(name, buffer, buffer_size); |
| 12314 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12315 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12316 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12317 | if (errno == ERANGE) { |
| 12318 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 12319 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12320 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12321 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12322 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12323 | break; |
| 12324 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12325 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12326 | result = PyList_New(0); |
| 12327 | if (!result) { |
| 12328 | goto exit; |
| 12329 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12330 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12331 | end = buffer + length; |
| 12332 | for (trace = start = buffer; trace != end; trace++) { |
| 12333 | if (!*trace) { |
| 12334 | int error; |
| 12335 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 12336 | trace - start); |
| 12337 | if (!attribute) { |
| 12338 | Py_DECREF(result); |
| 12339 | result = NULL; |
| 12340 | goto exit; |
| 12341 | } |
| 12342 | error = PyList_Append(result, attribute); |
| 12343 | Py_DECREF(attribute); |
| 12344 | if (error) { |
| 12345 | Py_DECREF(result); |
| 12346 | result = NULL; |
| 12347 | goto exit; |
| 12348 | } |
| 12349 | start = trace + 1; |
| 12350 | } |
| 12351 | } |
| 12352 | break; |
| 12353 | } |
| 12354 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12355 | if (buffer) |
| 12356 | PyMem_FREE(buffer); |
| 12357 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12358 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12359 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12360 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12361 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12362 | /*[clinic input] |
| 12363 | os.urandom |
| 12364 | |
| 12365 | size: Py_ssize_t |
| 12366 | / |
| 12367 | |
| 12368 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 12369 | [clinic start generated code]*/ |
| 12370 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12371 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12372 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 12373 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12374 | { |
| 12375 | PyObject *bytes; |
| 12376 | int result; |
| 12377 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12378 | if (size < 0) |
| 12379 | return PyErr_Format(PyExc_ValueError, |
| 12380 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12381 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 12382 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12383 | return NULL; |
| 12384 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 12385 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12386 | if (result == -1) { |
| 12387 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12388 | return NULL; |
| 12389 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12390 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12391 | } |
| 12392 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 12393 | #ifdef HAVE_MEMFD_CREATE |
| 12394 | /*[clinic input] |
| 12395 | os.memfd_create |
| 12396 | |
| 12397 | name: FSConverter |
| 12398 | flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC |
| 12399 | |
| 12400 | [clinic start generated code]*/ |
| 12401 | |
| 12402 | static PyObject * |
| 12403 | os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags) |
| 12404 | /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/ |
| 12405 | { |
| 12406 | int fd; |
| 12407 | const char *bytes = PyBytes_AS_STRING(name); |
| 12408 | Py_BEGIN_ALLOW_THREADS |
| 12409 | fd = memfd_create(bytes, flags); |
| 12410 | Py_END_ALLOW_THREADS |
| 12411 | if (fd == -1) { |
| 12412 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12413 | } |
| 12414 | return PyLong_FromLong(fd); |
| 12415 | } |
| 12416 | #endif |
| 12417 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12418 | /* Terminal size querying */ |
| 12419 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12420 | PyDoc_STRVAR(TerminalSize_docstring, |
| 12421 | "A tuple of (columns, lines) for holding terminal window size"); |
| 12422 | |
| 12423 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 12424 | {"columns", "width of the terminal window in characters"}, |
| 12425 | {"lines", "height of the terminal window in characters"}, |
| 12426 | {NULL, NULL} |
| 12427 | }; |
| 12428 | |
| 12429 | static PyStructSequence_Desc TerminalSize_desc = { |
| 12430 | "os.terminal_size", |
| 12431 | TerminalSize_docstring, |
| 12432 | TerminalSize_fields, |
| 12433 | 2, |
| 12434 | }; |
| 12435 | |
| 12436 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12437 | /*[clinic input] |
| 12438 | os.get_terminal_size |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12439 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12440 | fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1 |
| 12441 | / |
| 12442 | |
| 12443 | Return the size of the terminal window as (columns, lines). |
| 12444 | |
| 12445 | The optional argument fd (default standard output) specifies |
| 12446 | which file descriptor should be queried. |
| 12447 | |
| 12448 | If the file descriptor is not connected to a terminal, an OSError |
| 12449 | is thrown. |
| 12450 | |
| 12451 | This function will only be defined if an implementation is |
| 12452 | available for this system. |
| 12453 | |
| 12454 | shutil.get_terminal_size is the high-level function which should |
| 12455 | normally be used, os.get_terminal_size is the low-level implementation. |
| 12456 | [clinic start generated code]*/ |
| 12457 | |
| 12458 | static PyObject * |
| 12459 | os_get_terminal_size_impl(PyObject *module, int fd) |
| 12460 | /*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12461 | { |
| 12462 | int columns, lines; |
| 12463 | PyObject *termsize; |
| 12464 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12465 | /* Under some conditions stdout may not be connected and |
| 12466 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 12467 | * GUI apps don't have valid standard streams by default. |
| 12468 | * |
| 12469 | * If this happens, and the optional fd argument is not present, |
| 12470 | * the ioctl below will fail returning EBADF. This is what we want. |
| 12471 | */ |
| 12472 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12473 | #ifdef TERMSIZE_USE_IOCTL |
| 12474 | { |
| 12475 | struct winsize w; |
| 12476 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 12477 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12478 | columns = w.ws_col; |
| 12479 | lines = w.ws_row; |
| 12480 | } |
| 12481 | #endif /* TERMSIZE_USE_IOCTL */ |
| 12482 | |
| 12483 | #ifdef TERMSIZE_USE_CONIO |
| 12484 | { |
| 12485 | DWORD nhandle; |
| 12486 | HANDLE handle; |
| 12487 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 12488 | switch (fd) { |
| 12489 | case 0: nhandle = STD_INPUT_HANDLE; |
| 12490 | break; |
| 12491 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 12492 | break; |
| 12493 | case 2: nhandle = STD_ERROR_HANDLE; |
| 12494 | break; |
| 12495 | default: |
| 12496 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 12497 | } |
| 12498 | handle = GetStdHandle(nhandle); |
| 12499 | if (handle == NULL) |
| 12500 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 12501 | if (handle == INVALID_HANDLE_VALUE) |
| 12502 | return PyErr_SetFromWindowsErr(0); |
| 12503 | |
| 12504 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 12505 | return PyErr_SetFromWindowsErr(0); |
| 12506 | |
| 12507 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 12508 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 12509 | } |
| 12510 | #endif /* TERMSIZE_USE_CONIO */ |
| 12511 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12512 | PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12513 | termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12514 | if (termsize == NULL) |
| 12515 | return NULL; |
| 12516 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 12517 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 12518 | if (PyErr_Occurred()) { |
| 12519 | Py_DECREF(termsize); |
| 12520 | return NULL; |
| 12521 | } |
| 12522 | return termsize; |
| 12523 | } |
| 12524 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 12525 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12526 | |
| 12527 | /*[clinic input] |
| 12528 | os.cpu_count |
| 12529 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 12530 | Return the number of CPUs in the system; return None if indeterminable. |
| 12531 | |
| 12532 | This number is not equivalent to the number of CPUs the current process can |
| 12533 | use. The number of usable CPUs can be obtained with |
| 12534 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12535 | [clinic start generated code]*/ |
| 12536 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12537 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12538 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12539 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12540 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12541 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12542 | #ifdef MS_WINDOWS |
Steve Dower | aa92927 | 2019-09-11 16:15:39 +0100 | [diff] [blame] | 12543 | ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12544 | #elif defined(__hpux) |
| 12545 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 12546 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 12547 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12548 | #elif defined(__DragonFly__) || \ |
| 12549 | defined(__OpenBSD__) || \ |
| 12550 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12551 | defined(__NetBSD__) || \ |
| 12552 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12553 | int mib[2]; |
| 12554 | size_t len = sizeof(ncpu); |
| 12555 | mib[0] = CTL_HW; |
| 12556 | mib[1] = HW_NCPU; |
| 12557 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 12558 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12559 | #endif |
| 12560 | if (ncpu >= 1) |
| 12561 | return PyLong_FromLong(ncpu); |
| 12562 | else |
| 12563 | Py_RETURN_NONE; |
| 12564 | } |
| 12565 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12566 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12567 | /*[clinic input] |
| 12568 | os.get_inheritable -> bool |
| 12569 | |
| 12570 | fd: int |
| 12571 | / |
| 12572 | |
| 12573 | Get the close-on-exe flag of the specified file descriptor. |
| 12574 | [clinic start generated code]*/ |
| 12575 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12576 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12577 | os_get_inheritable_impl(PyObject *module, int fd) |
| 12578 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12579 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12580 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12581 | _Py_BEGIN_SUPPRESS_IPH |
| 12582 | return_value = _Py_get_inheritable(fd); |
| 12583 | _Py_END_SUPPRESS_IPH |
| 12584 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12585 | } |
| 12586 | |
| 12587 | |
| 12588 | /*[clinic input] |
| 12589 | os.set_inheritable |
| 12590 | fd: int |
| 12591 | inheritable: int |
| 12592 | / |
| 12593 | |
| 12594 | Set the inheritable flag of the specified file descriptor. |
| 12595 | [clinic start generated code]*/ |
| 12596 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12597 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12598 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 12599 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12600 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12601 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12602 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12603 | _Py_BEGIN_SUPPRESS_IPH |
| 12604 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 12605 | _Py_END_SUPPRESS_IPH |
| 12606 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12607 | return NULL; |
| 12608 | Py_RETURN_NONE; |
| 12609 | } |
| 12610 | |
| 12611 | |
| 12612 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12613 | /*[clinic input] |
| 12614 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12615 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12616 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12617 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12618 | Get the close-on-exe flag of the specified file descriptor. |
| 12619 | [clinic start generated code]*/ |
| 12620 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12621 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12622 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12623 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12624 | { |
| 12625 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12626 | |
| 12627 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 12628 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12629 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12630 | } |
| 12631 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12632 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12633 | } |
| 12634 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12635 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12636 | /*[clinic input] |
| 12637 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12638 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12639 | inheritable: bool |
| 12640 | / |
| 12641 | |
| 12642 | Set the inheritable flag of the specified handle. |
| 12643 | [clinic start generated code]*/ |
| 12644 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12645 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12646 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12647 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12648 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12649 | { |
| 12650 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12651 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 12652 | PyErr_SetFromWindowsErr(0); |
| 12653 | return NULL; |
| 12654 | } |
| 12655 | Py_RETURN_NONE; |
| 12656 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12657 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12658 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12659 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12660 | /*[clinic input] |
| 12661 | os.get_blocking -> bool |
| 12662 | fd: int |
| 12663 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12664 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12665 | Get the blocking mode of the file descriptor. |
| 12666 | |
| 12667 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 12668 | [clinic start generated code]*/ |
| 12669 | |
| 12670 | static int |
| 12671 | os_get_blocking_impl(PyObject *module, int fd) |
| 12672 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12673 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12674 | int blocking; |
| 12675 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12676 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12677 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12678 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12679 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12680 | } |
| 12681 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12682 | /*[clinic input] |
| 12683 | os.set_blocking |
| 12684 | fd: int |
| 12685 | blocking: bool(accept={int}) |
| 12686 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12687 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12688 | Set the blocking mode of the specified file descriptor. |
| 12689 | |
| 12690 | Set the O_NONBLOCK flag if blocking is False, |
| 12691 | clear the O_NONBLOCK flag otherwise. |
| 12692 | [clinic start generated code]*/ |
| 12693 | |
| 12694 | static PyObject * |
| 12695 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 12696 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12697 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12698 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12699 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12700 | _Py_BEGIN_SUPPRESS_IPH |
| 12701 | result = _Py_set_blocking(fd, blocking); |
| 12702 | _Py_END_SUPPRESS_IPH |
| 12703 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12704 | return NULL; |
| 12705 | Py_RETURN_NONE; |
| 12706 | } |
| 12707 | #endif /* !MS_WINDOWS */ |
| 12708 | |
| 12709 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12710 | /*[clinic input] |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12711 | class os.DirEntry "DirEntry *" "DirEntryType" |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12712 | [clinic start generated code]*/ |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12713 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12714 | |
| 12715 | typedef struct { |
| 12716 | PyObject_HEAD |
| 12717 | PyObject *name; |
| 12718 | PyObject *path; |
| 12719 | PyObject *stat; |
| 12720 | PyObject *lstat; |
| 12721 | #ifdef MS_WINDOWS |
| 12722 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12723 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12724 | int got_file_index; |
| 12725 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12726 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12727 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12728 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12729 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12730 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12731 | #endif |
| 12732 | } DirEntry; |
| 12733 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12734 | static PyObject * |
| 12735 | _disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 12736 | { |
| 12737 | PyErr_Format(PyExc_TypeError, |
| 12738 | "cannot create '%.100s' instances", _PyType_Name(type)); |
| 12739 | return NULL; |
| 12740 | } |
| 12741 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12742 | static void |
| 12743 | DirEntry_dealloc(DirEntry *entry) |
| 12744 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12745 | PyTypeObject *tp = Py_TYPE(entry); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12746 | Py_XDECREF(entry->name); |
| 12747 | Py_XDECREF(entry->path); |
| 12748 | Py_XDECREF(entry->stat); |
| 12749 | Py_XDECREF(entry->lstat); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12750 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 12751 | free_func(entry); |
| 12752 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12753 | } |
| 12754 | |
| 12755 | /* Forward reference */ |
| 12756 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12757 | DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self, |
| 12758 | int follow_symlinks, unsigned short mode_bits); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12759 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12760 | /*[clinic input] |
| 12761 | os.DirEntry.is_symlink -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12762 | defining_class: defining_class |
| 12763 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12764 | |
| 12765 | Return True if the entry is a symbolic link; cached per entry. |
| 12766 | [clinic start generated code]*/ |
| 12767 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12768 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12769 | os_DirEntry_is_symlink_impl(DirEntry *self, PyTypeObject *defining_class) |
| 12770 | /*[clinic end generated code: output=293096d589b6d47c input=e9acc5ee4d511113]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12771 | { |
| 12772 | #ifdef MS_WINDOWS |
| 12773 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12774 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12775 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12776 | if (self->d_type != DT_UNKNOWN) |
| 12777 | return self->d_type == DT_LNK; |
| 12778 | else |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12779 | return DirEntry_test_mode(defining_class, self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12780 | #else |
| 12781 | /* POSIX without d_type */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12782 | return DirEntry_test_mode(defining_class, self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12783 | #endif |
| 12784 | } |
| 12785 | |
| 12786 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12787 | DirEntry_fetch_stat(PyObject *module, DirEntry *self, int follow_symlinks) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12788 | { |
| 12789 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12790 | STRUCT_STAT st; |
| 12791 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12792 | |
| 12793 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12794 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12795 | return NULL; |
| 12796 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12797 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12798 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12799 | return NULL; |
| 12800 | const char *path = PyBytes_AS_STRING(ub); |
| 12801 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12802 | #ifdef HAVE_FSTATAT |
| 12803 | result = fstatat(self->dir_fd, path, &st, |
| 12804 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12805 | #else |
| 12806 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12807 | return NULL; |
| 12808 | #endif /* HAVE_FSTATAT */ |
| 12809 | } |
| 12810 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12811 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12812 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12813 | if (follow_symlinks) |
| 12814 | result = STAT(path, &st); |
| 12815 | else |
| 12816 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12817 | } |
| 12818 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12819 | |
| 12820 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12821 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12822 | |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12823 | return _pystat_fromstructstat(module, &st); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12824 | } |
| 12825 | |
| 12826 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12827 | DirEntry_get_lstat(PyTypeObject *defining_class, DirEntry *self) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12828 | { |
| 12829 | if (!self->lstat) { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12830 | PyObject *module = PyType_GetModule(defining_class); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12831 | #ifdef MS_WINDOWS |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12832 | self->lstat = _pystat_fromstructstat(module, &self->win32_lstat); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12833 | #else /* POSIX */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12834 | self->lstat = DirEntry_fetch_stat(module, self, 0); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12835 | #endif |
| 12836 | } |
| 12837 | Py_XINCREF(self->lstat); |
| 12838 | return self->lstat; |
| 12839 | } |
| 12840 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12841 | /*[clinic input] |
| 12842 | os.DirEntry.stat |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12843 | defining_class: defining_class |
| 12844 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12845 | * |
| 12846 | follow_symlinks: bool = True |
| 12847 | |
| 12848 | Return stat_result object for the entry; cached per entry. |
| 12849 | [clinic start generated code]*/ |
| 12850 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12851 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12852 | os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class, |
| 12853 | int follow_symlinks) |
| 12854 | /*[clinic end generated code: output=23f803e19c3e780e input=e816273c4e67ee98]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12855 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12856 | if (!follow_symlinks) { |
| 12857 | return DirEntry_get_lstat(defining_class, self); |
| 12858 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12859 | |
| 12860 | if (!self->stat) { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12861 | int result = os_DirEntry_is_symlink_impl(self, defining_class); |
| 12862 | if (result == -1) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12863 | return NULL; |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12864 | } |
| 12865 | if (result) { |
| 12866 | PyObject *module = PyType_GetModule(defining_class); |
| 12867 | self->stat = DirEntry_fetch_stat(module, self, 1); |
| 12868 | } |
| 12869 | else { |
| 12870 | self->stat = DirEntry_get_lstat(defining_class, self); |
| 12871 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12872 | } |
| 12873 | |
| 12874 | Py_XINCREF(self->stat); |
| 12875 | return self->stat; |
| 12876 | } |
| 12877 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12878 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12879 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12880 | DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self, |
| 12881 | int follow_symlinks, unsigned short mode_bits) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12882 | { |
| 12883 | PyObject *stat = NULL; |
| 12884 | PyObject *st_mode = NULL; |
| 12885 | long mode; |
| 12886 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12887 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12888 | int is_symlink; |
| 12889 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12890 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12891 | #ifdef MS_WINDOWS |
| 12892 | unsigned long dir_bits; |
| 12893 | #endif |
| 12894 | |
| 12895 | #ifdef MS_WINDOWS |
| 12896 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12897 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12898 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12899 | is_symlink = self->d_type == DT_LNK; |
| 12900 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12901 | #endif |
| 12902 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12903 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12904 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12905 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12906 | stat = os_DirEntry_stat_impl(self, defining_class, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12907 | if (!stat) { |
| 12908 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12909 | /* If file doesn't exist (anymore), then return False |
| 12910 | (i.e., say it's not a file/directory) */ |
| 12911 | PyErr_Clear(); |
| 12912 | return 0; |
| 12913 | } |
| 12914 | goto error; |
| 12915 | } |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12916 | _posixstate* state = get_posix_state(PyType_GetModule(defining_class)); |
| 12917 | st_mode = PyObject_GetAttr(stat, state->st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12918 | if (!st_mode) |
| 12919 | goto error; |
| 12920 | |
| 12921 | mode = PyLong_AsLong(st_mode); |
| 12922 | if (mode == -1 && PyErr_Occurred()) |
| 12923 | goto error; |
| 12924 | Py_CLEAR(st_mode); |
| 12925 | Py_CLEAR(stat); |
| 12926 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12927 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12928 | } |
| 12929 | else if (is_symlink) { |
| 12930 | assert(mode_bits != S_IFLNK); |
| 12931 | result = 0; |
| 12932 | } |
| 12933 | else { |
| 12934 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 12935 | #ifdef MS_WINDOWS |
| 12936 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 12937 | if (mode_bits == S_IFDIR) |
| 12938 | result = dir_bits != 0; |
| 12939 | else |
| 12940 | result = dir_bits == 0; |
| 12941 | #else /* POSIX */ |
| 12942 | if (mode_bits == S_IFDIR) |
| 12943 | result = self->d_type == DT_DIR; |
| 12944 | else |
| 12945 | result = self->d_type == DT_REG; |
| 12946 | #endif |
| 12947 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12948 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12949 | |
| 12950 | return result; |
| 12951 | |
| 12952 | error: |
| 12953 | Py_XDECREF(st_mode); |
| 12954 | Py_XDECREF(stat); |
| 12955 | return -1; |
| 12956 | } |
| 12957 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12958 | /*[clinic input] |
| 12959 | os.DirEntry.is_dir -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12960 | defining_class: defining_class |
| 12961 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12962 | * |
| 12963 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12964 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12965 | Return True if the entry is a directory; cached per entry. |
| 12966 | [clinic start generated code]*/ |
| 12967 | |
| 12968 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12969 | os_DirEntry_is_dir_impl(DirEntry *self, PyTypeObject *defining_class, |
| 12970 | int follow_symlinks) |
| 12971 | /*[clinic end generated code: output=0cd453b9c0987fdf input=1a4ffd6dec9920cb]*/ |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12972 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12973 | return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12974 | } |
| 12975 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12976 | /*[clinic input] |
| 12977 | os.DirEntry.is_file -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12978 | defining_class: defining_class |
| 12979 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12980 | * |
| 12981 | follow_symlinks: bool = True |
| 12982 | |
| 12983 | Return True if the entry is a file; cached per entry. |
| 12984 | [clinic start generated code]*/ |
| 12985 | |
| 12986 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12987 | os_DirEntry_is_file_impl(DirEntry *self, PyTypeObject *defining_class, |
| 12988 | int follow_symlinks) |
| 12989 | /*[clinic end generated code: output=f7c277ab5ba80908 input=0a64c5a12e802e3b]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12990 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 12991 | return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12992 | } |
| 12993 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12994 | /*[clinic input] |
| 12995 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12996 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12997 | Return inode of the entry; cached per entry. |
| 12998 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12999 | |
| 13000 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13001 | os_DirEntry_inode_impl(DirEntry *self) |
| 13002 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13003 | { |
| 13004 | #ifdef MS_WINDOWS |
| 13005 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13006 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 13007 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13008 | STRUCT_STAT stat; |
| 13009 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13010 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13011 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13012 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13013 | path = PyUnicode_AsUnicode(unicode); |
| 13014 | result = LSTAT(path, &stat); |
| 13015 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13016 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13017 | if (result != 0) |
| 13018 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13019 | |
| 13020 | self->win32_file_index = stat.st_ino; |
| 13021 | self->got_file_index = 1; |
| 13022 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 13023 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 13024 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13025 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 13026 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 13027 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13028 | #endif |
| 13029 | } |
| 13030 | |
| 13031 | static PyObject * |
| 13032 | DirEntry_repr(DirEntry *self) |
| 13033 | { |
| 13034 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 13035 | } |
| 13036 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13037 | /*[clinic input] |
| 13038 | os.DirEntry.__fspath__ |
| 13039 | |
| 13040 | Returns the path for the entry. |
| 13041 | [clinic start generated code]*/ |
| 13042 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 13043 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13044 | os_DirEntry___fspath___impl(DirEntry *self) |
| 13045 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 13046 | { |
| 13047 | Py_INCREF(self->path); |
| 13048 | return self->path; |
| 13049 | } |
| 13050 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13051 | static PyMemberDef DirEntry_members[] = { |
| 13052 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 13053 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 13054 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 13055 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 13056 | {NULL} |
| 13057 | }; |
| 13058 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13059 | #include "clinic/posixmodule.c.h" |
| 13060 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13061 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13062 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 13063 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 13064 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 13065 | OS_DIRENTRY_STAT_METHODDEF |
| 13066 | OS_DIRENTRY_INODE_METHODDEF |
| 13067 | OS_DIRENTRY___FSPATH___METHODDEF |
Batuhan Taşkaya | f9dd51e | 2020-04-08 00:37:19 +0300 | [diff] [blame] | 13068 | {"__class_getitem__", (PyCFunction)Py_GenericAlias, |
| 13069 | METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13070 | {NULL} |
| 13071 | }; |
| 13072 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13073 | static PyType_Slot DirEntryType_slots[] = { |
| 13074 | {Py_tp_new, _disabled_new}, |
| 13075 | {Py_tp_dealloc, DirEntry_dealloc}, |
| 13076 | {Py_tp_repr, DirEntry_repr}, |
| 13077 | {Py_tp_methods, DirEntry_methods}, |
| 13078 | {Py_tp_members, DirEntry_members}, |
| 13079 | {0, 0}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13080 | }; |
| 13081 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13082 | static PyType_Spec DirEntryType_spec = { |
| 13083 | MODNAME ".DirEntry", |
| 13084 | sizeof(DirEntry), |
| 13085 | 0, |
| 13086 | Py_TPFLAGS_DEFAULT, |
| 13087 | DirEntryType_slots |
| 13088 | }; |
| 13089 | |
| 13090 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13091 | #ifdef MS_WINDOWS |
| 13092 | |
| 13093 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 13094 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13095 | { |
| 13096 | Py_ssize_t path_len; |
| 13097 | Py_ssize_t size; |
| 13098 | wchar_t *result; |
| 13099 | wchar_t ch; |
| 13100 | |
| 13101 | if (!path_wide) { /* Default arg: "." */ |
| 13102 | path_wide = L"."; |
| 13103 | path_len = 1; |
| 13104 | } |
| 13105 | else { |
| 13106 | path_len = wcslen(path_wide); |
| 13107 | } |
| 13108 | |
| 13109 | /* The +1's are for the path separator and the NUL */ |
| 13110 | size = path_len + 1 + wcslen(filename) + 1; |
| 13111 | result = PyMem_New(wchar_t, size); |
| 13112 | if (!result) { |
| 13113 | PyErr_NoMemory(); |
| 13114 | return NULL; |
| 13115 | } |
| 13116 | wcscpy(result, path_wide); |
| 13117 | if (path_len > 0) { |
| 13118 | ch = result[path_len - 1]; |
| 13119 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 13120 | result[path_len++] = SEP; |
| 13121 | wcscpy(result + path_len, filename); |
| 13122 | } |
| 13123 | return result; |
| 13124 | } |
| 13125 | |
| 13126 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13127 | 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] | 13128 | { |
| 13129 | DirEntry *entry; |
| 13130 | BY_HANDLE_FILE_INFORMATION file_info; |
| 13131 | ULONG reparse_tag; |
| 13132 | wchar_t *joined_path; |
| 13133 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13134 | PyObject *DirEntryType = get_posix_state(module)->DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13135 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13136 | if (!entry) |
| 13137 | return NULL; |
| 13138 | entry->name = NULL; |
| 13139 | entry->path = NULL; |
| 13140 | entry->stat = NULL; |
| 13141 | entry->lstat = NULL; |
| 13142 | entry->got_file_index = 0; |
| 13143 | |
| 13144 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 13145 | if (!entry->name) |
| 13146 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13147 | if (path->narrow) { |
| 13148 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 13149 | if (!entry->name) |
| 13150 | goto error; |
| 13151 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13152 | |
| 13153 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 13154 | if (!joined_path) |
| 13155 | goto error; |
| 13156 | |
| 13157 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 13158 | PyMem_Free(joined_path); |
| 13159 | if (!entry->path) |
| 13160 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13161 | if (path->narrow) { |
| 13162 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 13163 | if (!entry->path) |
| 13164 | goto error; |
| 13165 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13166 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13167 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13168 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 13169 | |
| 13170 | return (PyObject *)entry; |
| 13171 | |
| 13172 | error: |
| 13173 | Py_DECREF(entry); |
| 13174 | return NULL; |
| 13175 | } |
| 13176 | |
| 13177 | #else /* POSIX */ |
| 13178 | |
| 13179 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 13180 | 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] | 13181 | { |
| 13182 | Py_ssize_t path_len; |
| 13183 | Py_ssize_t size; |
| 13184 | char *result; |
| 13185 | |
| 13186 | if (!path_narrow) { /* Default arg: "." */ |
| 13187 | path_narrow = "."; |
| 13188 | path_len = 1; |
| 13189 | } |
| 13190 | else { |
| 13191 | path_len = strlen(path_narrow); |
| 13192 | } |
| 13193 | |
| 13194 | if (filename_len == -1) |
| 13195 | filename_len = strlen(filename); |
| 13196 | |
| 13197 | /* The +1's are for the path separator and the NUL */ |
| 13198 | size = path_len + 1 + filename_len + 1; |
| 13199 | result = PyMem_New(char, size); |
| 13200 | if (!result) { |
| 13201 | PyErr_NoMemory(); |
| 13202 | return NULL; |
| 13203 | } |
| 13204 | strcpy(result, path_narrow); |
| 13205 | if (path_len > 0 && result[path_len - 1] != '/') |
| 13206 | result[path_len++] = '/'; |
| 13207 | strcpy(result + path_len, filename); |
| 13208 | return result; |
| 13209 | } |
| 13210 | |
| 13211 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13212 | DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name, |
| 13213 | Py_ssize_t name_len, ino_t d_ino |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13214 | #ifdef HAVE_DIRENT_D_TYPE |
| 13215 | , unsigned char d_type |
| 13216 | #endif |
| 13217 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13218 | { |
| 13219 | DirEntry *entry; |
| 13220 | char *joined_path; |
| 13221 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13222 | PyObject *DirEntryType = get_posix_state(module)->DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13223 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13224 | if (!entry) |
| 13225 | return NULL; |
| 13226 | entry->name = NULL; |
| 13227 | entry->path = NULL; |
| 13228 | entry->stat = NULL; |
| 13229 | entry->lstat = NULL; |
| 13230 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13231 | if (path->fd != -1) { |
| 13232 | entry->dir_fd = path->fd; |
| 13233 | joined_path = NULL; |
| 13234 | } |
| 13235 | else { |
| 13236 | entry->dir_fd = DEFAULT_DIR_FD; |
| 13237 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 13238 | if (!joined_path) |
| 13239 | goto error; |
| 13240 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13241 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 13242 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13243 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13244 | if (joined_path) |
| 13245 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13246 | } |
| 13247 | else { |
| 13248 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13249 | if (joined_path) |
| 13250 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13251 | } |
| 13252 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13253 | if (!entry->name) |
| 13254 | goto error; |
| 13255 | |
| 13256 | if (path->fd != -1) { |
| 13257 | entry->path = entry->name; |
| 13258 | Py_INCREF(entry->path); |
| 13259 | } |
| 13260 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13261 | goto error; |
| 13262 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13263 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13264 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13265 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13266 | entry->d_ino = d_ino; |
| 13267 | |
| 13268 | return (PyObject *)entry; |
| 13269 | |
| 13270 | error: |
| 13271 | Py_XDECREF(entry); |
| 13272 | return NULL; |
| 13273 | } |
| 13274 | |
| 13275 | #endif |
| 13276 | |
| 13277 | |
| 13278 | typedef struct { |
| 13279 | PyObject_HEAD |
| 13280 | path_t path; |
| 13281 | #ifdef MS_WINDOWS |
| 13282 | HANDLE handle; |
| 13283 | WIN32_FIND_DATAW file_data; |
| 13284 | int first_time; |
| 13285 | #else /* POSIX */ |
| 13286 | DIR *dirp; |
| 13287 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13288 | #ifdef HAVE_FDOPENDIR |
| 13289 | int fd; |
| 13290 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13291 | } ScandirIterator; |
| 13292 | |
| 13293 | #ifdef MS_WINDOWS |
| 13294 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13295 | static int |
| 13296 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13297 | { |
| 13298 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 13299 | } |
| 13300 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13301 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13302 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13303 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13304 | HANDLE handle = iterator->handle; |
| 13305 | |
| 13306 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13307 | return; |
| 13308 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13309 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13310 | Py_BEGIN_ALLOW_THREADS |
| 13311 | FindClose(handle); |
| 13312 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13313 | } |
| 13314 | |
| 13315 | static PyObject * |
| 13316 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13317 | { |
| 13318 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 13319 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13320 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13321 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13322 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13323 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13324 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13325 | |
| 13326 | while (1) { |
| 13327 | if (!iterator->first_time) { |
| 13328 | Py_BEGIN_ALLOW_THREADS |
| 13329 | success = FindNextFileW(iterator->handle, file_data); |
| 13330 | Py_END_ALLOW_THREADS |
| 13331 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13332 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13333 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13334 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13335 | break; |
| 13336 | } |
| 13337 | } |
| 13338 | iterator->first_time = 0; |
| 13339 | |
| 13340 | /* Skip over . and .. */ |
| 13341 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13342 | wcscmp(file_data->cFileName, L"..") != 0) |
| 13343 | { |
| 13344 | PyObject *module = PyType_GetModule(Py_TYPE(iterator)); |
| 13345 | entry = DirEntry_from_find_data(module, &iterator->path, file_data); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13346 | if (!entry) |
| 13347 | break; |
| 13348 | return entry; |
| 13349 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13350 | |
| 13351 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13352 | } |
| 13353 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13354 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13355 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13356 | return NULL; |
| 13357 | } |
| 13358 | |
| 13359 | #else /* POSIX */ |
| 13360 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13361 | static int |
| 13362 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13363 | { |
| 13364 | return !iterator->dirp; |
| 13365 | } |
| 13366 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13367 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13368 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13369 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13370 | DIR *dirp = iterator->dirp; |
| 13371 | |
| 13372 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13373 | return; |
| 13374 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13375 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13376 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13377 | #ifdef HAVE_FDOPENDIR |
| 13378 | if (iterator->path.fd != -1) |
| 13379 | rewinddir(dirp); |
| 13380 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13381 | closedir(dirp); |
| 13382 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13383 | return; |
| 13384 | } |
| 13385 | |
| 13386 | static PyObject * |
| 13387 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13388 | { |
| 13389 | struct dirent *direntp; |
| 13390 | Py_ssize_t name_len; |
| 13391 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13392 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13393 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13394 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13395 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13396 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13397 | |
| 13398 | while (1) { |
| 13399 | errno = 0; |
| 13400 | Py_BEGIN_ALLOW_THREADS |
| 13401 | direntp = readdir(iterator->dirp); |
| 13402 | Py_END_ALLOW_THREADS |
| 13403 | |
| 13404 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13405 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13406 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13407 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13408 | break; |
| 13409 | } |
| 13410 | |
| 13411 | /* Skip over . and .. */ |
| 13412 | name_len = NAMLEN(direntp); |
| 13413 | is_dot = direntp->d_name[0] == '.' && |
| 13414 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 13415 | if (!is_dot) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13416 | PyObject *module = PyType_GetModule(Py_TYPE(iterator)); |
| 13417 | entry = DirEntry_from_posix_info(module, |
| 13418 | &iterator->path, direntp->d_name, |
| 13419 | name_len, direntp->d_ino |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13420 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13421 | , direntp->d_type |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13422 | #endif |
| 13423 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13424 | if (!entry) |
| 13425 | break; |
| 13426 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13427 | } |
| 13428 | |
| 13429 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13430 | } |
| 13431 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13432 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13433 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13434 | return NULL; |
| 13435 | } |
| 13436 | |
| 13437 | #endif |
| 13438 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13439 | static PyObject * |
| 13440 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 13441 | { |
| 13442 | ScandirIterator_closedir(self); |
| 13443 | Py_RETURN_NONE; |
| 13444 | } |
| 13445 | |
| 13446 | static PyObject * |
| 13447 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 13448 | { |
| 13449 | Py_INCREF(self); |
| 13450 | return self; |
| 13451 | } |
| 13452 | |
| 13453 | static PyObject * |
| 13454 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 13455 | { |
| 13456 | ScandirIterator_closedir(self); |
| 13457 | Py_RETURN_NONE; |
| 13458 | } |
| 13459 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13460 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13461 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 13462 | { |
| 13463 | PyObject *error_type, *error_value, *error_traceback; |
| 13464 | |
| 13465 | /* Save the current exception, if any. */ |
| 13466 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 13467 | |
| 13468 | if (!ScandirIterator_is_closed(iterator)) { |
| 13469 | ScandirIterator_closedir(iterator); |
| 13470 | |
| 13471 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 13472 | "unclosed scandir iterator %R", iterator)) { |
| 13473 | /* Spurious errors can appear at shutdown */ |
| 13474 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 13475 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 13476 | } |
| 13477 | } |
| 13478 | } |
| 13479 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13480 | path_cleanup(&iterator->path); |
| 13481 | |
| 13482 | /* Restore the saved exception. */ |
| 13483 | PyErr_Restore(error_type, error_value, error_traceback); |
| 13484 | } |
| 13485 | |
| 13486 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13487 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 13488 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13489 | PyTypeObject *tp = Py_TYPE(iterator); |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13490 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 13491 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13492 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13493 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 13494 | free_func(iterator); |
| 13495 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13496 | } |
| 13497 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13498 | static PyMethodDef ScandirIterator_methods[] = { |
| 13499 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 13500 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 13501 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 13502 | {NULL} |
| 13503 | }; |
| 13504 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13505 | static PyType_Slot ScandirIteratorType_slots[] = { |
| 13506 | {Py_tp_new, _disabled_new}, |
| 13507 | {Py_tp_dealloc, ScandirIterator_dealloc}, |
| 13508 | {Py_tp_finalize, ScandirIterator_finalize}, |
| 13509 | {Py_tp_iter, PyObject_SelfIter}, |
| 13510 | {Py_tp_iternext, ScandirIterator_iternext}, |
| 13511 | {Py_tp_methods, ScandirIterator_methods}, |
| 13512 | {0, 0}, |
| 13513 | }; |
| 13514 | |
| 13515 | static PyType_Spec ScandirIteratorType_spec = { |
| 13516 | MODNAME ".ScandirIterator", |
| 13517 | sizeof(ScandirIterator), |
| 13518 | 0, |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13519 | // bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since |
| 13520 | // PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance. |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13521 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 13522 | ScandirIteratorType_slots |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13523 | }; |
| 13524 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13525 | /*[clinic input] |
| 13526 | os.scandir |
| 13527 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13528 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13529 | |
| 13530 | Return an iterator of DirEntry objects for given path. |
| 13531 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13532 | 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] | 13533 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 13534 | all other circumstances they will be str. |
| 13535 | |
| 13536 | If path is None, uses the path='.'. |
| 13537 | [clinic start generated code]*/ |
| 13538 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13539 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13540 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13541 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13542 | { |
| 13543 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13544 | #ifdef MS_WINDOWS |
| 13545 | wchar_t *path_strW; |
| 13546 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13547 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13548 | #ifdef HAVE_FDOPENDIR |
| 13549 | int fd = -1; |
| 13550 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13551 | #endif |
| 13552 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 13553 | if (PySys_Audit("os.scandir", "O", |
| 13554 | path->object ? path->object : Py_None) < 0) { |
| 13555 | return NULL; |
| 13556 | } |
| 13557 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 13558 | PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13559 | iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13560 | if (!iterator) |
| 13561 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13562 | |
| 13563 | #ifdef MS_WINDOWS |
| 13564 | iterator->handle = INVALID_HANDLE_VALUE; |
| 13565 | #else |
| 13566 | iterator->dirp = NULL; |
| 13567 | #endif |
| 13568 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13569 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 13570 | /* Move the ownership to iterator->path */ |
| 13571 | path->object = NULL; |
| 13572 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13573 | |
| 13574 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13575 | iterator->first_time = 1; |
| 13576 | |
| 13577 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 13578 | if (!path_strW) |
| 13579 | goto error; |
| 13580 | |
| 13581 | Py_BEGIN_ALLOW_THREADS |
| 13582 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 13583 | Py_END_ALLOW_THREADS |
| 13584 | |
| 13585 | PyMem_Free(path_strW); |
| 13586 | |
| 13587 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 13588 | path_error(&iterator->path); |
| 13589 | goto error; |
| 13590 | } |
| 13591 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13592 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13593 | #ifdef HAVE_FDOPENDIR |
| 13594 | if (path->fd != -1) { |
| 13595 | /* closedir() closes the FD, so we duplicate it */ |
| 13596 | fd = _Py_dup(path->fd); |
| 13597 | if (fd == -1) |
| 13598 | goto error; |
| 13599 | |
| 13600 | Py_BEGIN_ALLOW_THREADS |
| 13601 | iterator->dirp = fdopendir(fd); |
| 13602 | Py_END_ALLOW_THREADS |
| 13603 | } |
| 13604 | else |
| 13605 | #endif |
| 13606 | { |
| 13607 | if (iterator->path.narrow) |
| 13608 | path_str = iterator->path.narrow; |
| 13609 | else |
| 13610 | path_str = "."; |
| 13611 | |
| 13612 | Py_BEGIN_ALLOW_THREADS |
| 13613 | iterator->dirp = opendir(path_str); |
| 13614 | Py_END_ALLOW_THREADS |
| 13615 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13616 | |
| 13617 | if (!iterator->dirp) { |
| 13618 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13619 | #ifdef HAVE_FDOPENDIR |
| 13620 | if (fd != -1) { |
| 13621 | Py_BEGIN_ALLOW_THREADS |
| 13622 | close(fd); |
| 13623 | Py_END_ALLOW_THREADS |
| 13624 | } |
| 13625 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13626 | goto error; |
| 13627 | } |
| 13628 | #endif |
| 13629 | |
| 13630 | return (PyObject *)iterator; |
| 13631 | |
| 13632 | error: |
| 13633 | Py_DECREF(iterator); |
| 13634 | return NULL; |
| 13635 | } |
| 13636 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13637 | /* |
| 13638 | Return the file system path representation of the object. |
| 13639 | |
| 13640 | If the object is str or bytes, then allow it to pass through with |
| 13641 | an incremented refcount. If the object defines __fspath__(), then |
| 13642 | return the result of that method. All other types raise a TypeError. |
| 13643 | */ |
| 13644 | PyObject * |
| 13645 | PyOS_FSPath(PyObject *path) |
| 13646 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13647 | /* For error message reasons, this function is manually inlined in |
| 13648 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13649 | PyObject *func = NULL; |
| 13650 | PyObject *path_repr = NULL; |
| 13651 | |
| 13652 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 13653 | Py_INCREF(path); |
| 13654 | return path; |
| 13655 | } |
| 13656 | |
| 13657 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 13658 | if (NULL == func) { |
| 13659 | return PyErr_Format(PyExc_TypeError, |
| 13660 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13661 | "not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13662 | _PyType_Name(Py_TYPE(path))); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13663 | } |
| 13664 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13665 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13666 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13667 | if (NULL == path_repr) { |
| 13668 | return NULL; |
| 13669 | } |
| 13670 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13671 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 13672 | PyErr_Format(PyExc_TypeError, |
| 13673 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13674 | "not %.200s", _PyType_Name(Py_TYPE(path)), |
| 13675 | _PyType_Name(Py_TYPE(path_repr))); |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13676 | Py_DECREF(path_repr); |
| 13677 | return NULL; |
| 13678 | } |
| 13679 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13680 | return path_repr; |
| 13681 | } |
| 13682 | |
| 13683 | /*[clinic input] |
| 13684 | os.fspath |
| 13685 | |
| 13686 | path: object |
| 13687 | |
| 13688 | Return the file system path representation of the object. |
| 13689 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13690 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 13691 | object defines __fspath__(), then return the result of that method. All other |
| 13692 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13693 | [clinic start generated code]*/ |
| 13694 | |
| 13695 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13696 | os_fspath_impl(PyObject *module, PyObject *path) |
| 13697 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13698 | { |
| 13699 | return PyOS_FSPath(path); |
| 13700 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13701 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13702 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13703 | /*[clinic input] |
| 13704 | os.getrandom |
| 13705 | |
| 13706 | size: Py_ssize_t |
| 13707 | flags: int=0 |
| 13708 | |
| 13709 | Obtain a series of random bytes. |
| 13710 | [clinic start generated code]*/ |
| 13711 | |
| 13712 | static PyObject * |
| 13713 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 13714 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 13715 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13716 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13717 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13718 | |
| 13719 | if (size < 0) { |
| 13720 | errno = EINVAL; |
| 13721 | return posix_error(); |
| 13722 | } |
| 13723 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13724 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13725 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13726 | PyErr_NoMemory(); |
| 13727 | return NULL; |
| 13728 | } |
| 13729 | |
| 13730 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13731 | n = syscall(SYS_getrandom, |
| 13732 | PyBytes_AS_STRING(bytes), |
| 13733 | PyBytes_GET_SIZE(bytes), |
| 13734 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13735 | if (n < 0 && errno == EINTR) { |
| 13736 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13737 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13738 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13739 | |
| 13740 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13741 | continue; |
| 13742 | } |
| 13743 | break; |
| 13744 | } |
| 13745 | |
| 13746 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13747 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13748 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13749 | } |
| 13750 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13751 | if (n != size) { |
| 13752 | _PyBytes_Resize(&bytes, n); |
| 13753 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13754 | |
| 13755 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13756 | |
| 13757 | error: |
| 13758 | Py_DECREF(bytes); |
| 13759 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13760 | } |
| 13761 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13762 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13763 | #ifdef MS_WINDOWS |
| 13764 | /* bpo-36085: Helper functions for managing DLL search directories |
| 13765 | * on win32 |
| 13766 | */ |
| 13767 | |
| 13768 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); |
| 13769 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); |
| 13770 | |
| 13771 | /*[clinic input] |
| 13772 | os._add_dll_directory |
| 13773 | |
| 13774 | path: path_t |
| 13775 | |
| 13776 | Add a path to the DLL search path. |
| 13777 | |
| 13778 | This search path is used when resolving dependencies for imported |
| 13779 | extension modules (the module itself is resolved through sys.path), |
| 13780 | and also by ctypes. |
| 13781 | |
| 13782 | Returns an opaque value that may be passed to os.remove_dll_directory |
| 13783 | to remove this directory from the search path. |
| 13784 | [clinic start generated code]*/ |
| 13785 | |
| 13786 | static PyObject * |
| 13787 | os__add_dll_directory_impl(PyObject *module, path_t *path) |
| 13788 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ |
| 13789 | { |
| 13790 | HMODULE hKernel32; |
| 13791 | PAddDllDirectory AddDllDirectory; |
| 13792 | DLL_DIRECTORY_COOKIE cookie = 0; |
| 13793 | DWORD err = 0; |
| 13794 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 13795 | if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) { |
| 13796 | return NULL; |
| 13797 | } |
| 13798 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13799 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13800 | infrequent operation, just do it each time. Kernel32 is always |
| 13801 | loaded. */ |
| 13802 | Py_BEGIN_ALLOW_THREADS |
| 13803 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13804 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( |
| 13805 | hKernel32, "AddDllDirectory")) || |
| 13806 | !(cookie = (*AddDllDirectory)(path->wide))) { |
| 13807 | err = GetLastError(); |
| 13808 | } |
| 13809 | Py_END_ALLOW_THREADS |
| 13810 | |
| 13811 | if (err) { |
| 13812 | return win32_error_object_err("add_dll_directory", |
| 13813 | path->object, err); |
| 13814 | } |
| 13815 | |
| 13816 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); |
| 13817 | } |
| 13818 | |
| 13819 | /*[clinic input] |
| 13820 | os._remove_dll_directory |
| 13821 | |
| 13822 | cookie: object |
| 13823 | |
| 13824 | Removes a path from the DLL search path. |
| 13825 | |
| 13826 | The parameter is an opaque value that was returned from |
| 13827 | os.add_dll_directory. You can only remove directories that you added |
| 13828 | yourself. |
| 13829 | [clinic start generated code]*/ |
| 13830 | |
| 13831 | static PyObject * |
| 13832 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) |
| 13833 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ |
| 13834 | { |
| 13835 | HMODULE hKernel32; |
| 13836 | PRemoveDllDirectory RemoveDllDirectory; |
| 13837 | DLL_DIRECTORY_COOKIE cookieValue; |
| 13838 | DWORD err = 0; |
| 13839 | |
| 13840 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { |
| 13841 | PyErr_SetString(PyExc_TypeError, |
| 13842 | "Provided cookie was not returned from os.add_dll_directory"); |
| 13843 | return NULL; |
| 13844 | } |
| 13845 | |
| 13846 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( |
| 13847 | cookie, "DLL directory cookie"); |
| 13848 | |
| 13849 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13850 | infrequent operation, just do it each time. Kernel32 is always |
| 13851 | loaded. */ |
| 13852 | Py_BEGIN_ALLOW_THREADS |
| 13853 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13854 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( |
| 13855 | hKernel32, "RemoveDllDirectory")) || |
| 13856 | !(*RemoveDllDirectory)(cookieValue)) { |
| 13857 | err = GetLastError(); |
| 13858 | } |
| 13859 | Py_END_ALLOW_THREADS |
| 13860 | |
| 13861 | if (err) { |
| 13862 | return win32_error_object_err("remove_dll_directory", |
| 13863 | NULL, err); |
| 13864 | } |
| 13865 | |
| 13866 | if (PyCapsule_SetName(cookie, NULL)) { |
| 13867 | return NULL; |
| 13868 | } |
| 13869 | |
| 13870 | Py_RETURN_NONE; |
| 13871 | } |
| 13872 | |
| 13873 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13874 | |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13875 | |
| 13876 | /* Only check if WIFEXITED is available: expect that it comes |
| 13877 | with WEXITSTATUS, WIFSIGNALED, etc. |
| 13878 | |
| 13879 | os.waitstatus_to_exitcode() is implemented in C and not in Python, so |
| 13880 | subprocess can safely call it during late Python finalization without |
| 13881 | risking that used os attributes were set to None by _PyImport_Cleanup(). */ |
| 13882 | #if defined(WIFEXITED) || defined(MS_WINDOWS) |
| 13883 | /*[clinic input] |
| 13884 | os.waitstatus_to_exitcode |
| 13885 | |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13886 | status as status_obj: object |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13887 | |
| 13888 | Convert a wait status to an exit code. |
| 13889 | |
| 13890 | On Unix: |
| 13891 | |
| 13892 | * If WIFEXITED(status) is true, return WEXITSTATUS(status). |
| 13893 | * If WIFSIGNALED(status) is true, return -WTERMSIG(status). |
| 13894 | * Otherwise, raise a ValueError. |
| 13895 | |
| 13896 | On Windows, return status shifted right by 8 bits. |
| 13897 | |
| 13898 | On Unix, if the process is being traced or if waitpid() was called with |
| 13899 | WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true. |
| 13900 | This function must not be called if WIFSTOPPED(status) is true. |
| 13901 | [clinic start generated code]*/ |
| 13902 | |
| 13903 | static PyObject * |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13904 | os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj) |
| 13905 | /*[clinic end generated code: output=db50b1b0ba3c7153 input=7fe2d7fdaea3db42]*/ |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13906 | { |
| 13907 | #ifndef MS_WINDOWS |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13908 | int status = _PyLong_AsInt(status_obj); |
| 13909 | if (status == -1 && PyErr_Occurred()) { |
| 13910 | return NULL; |
| 13911 | } |
| 13912 | |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13913 | WAIT_TYPE wait_status; |
| 13914 | WAIT_STATUS_INT(wait_status) = status; |
| 13915 | int exitcode; |
| 13916 | if (WIFEXITED(wait_status)) { |
| 13917 | exitcode = WEXITSTATUS(wait_status); |
| 13918 | /* Sanity check to provide warranty on the function behavior. |
| 13919 | It should not occur in practice */ |
| 13920 | if (exitcode < 0) { |
| 13921 | PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode); |
| 13922 | return NULL; |
| 13923 | } |
| 13924 | } |
| 13925 | else if (WIFSIGNALED(wait_status)) { |
| 13926 | int signum = WTERMSIG(wait_status); |
| 13927 | /* Sanity check to provide warranty on the function behavior. |
| 13928 | It should not occurs in practice */ |
| 13929 | if (signum <= 0) { |
| 13930 | PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum); |
| 13931 | return NULL; |
| 13932 | } |
| 13933 | exitcode = -signum; |
| 13934 | } else if (WIFSTOPPED(wait_status)) { |
| 13935 | /* Status only received if the process is being traced |
| 13936 | or if waitpid() was called with WUNTRACED option. */ |
| 13937 | int signum = WSTOPSIG(wait_status); |
| 13938 | PyErr_Format(PyExc_ValueError, |
| 13939 | "process stopped by delivery of signal %i", |
| 13940 | signum); |
| 13941 | return NULL; |
| 13942 | } |
| 13943 | else { |
| 13944 | PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status); |
| 13945 | return NULL; |
| 13946 | } |
| 13947 | return PyLong_FromLong(exitcode); |
| 13948 | #else |
| 13949 | /* Windows implementation: see os.waitpid() implementation |
| 13950 | which uses _cwait(). */ |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 13951 | unsigned long long status = PyLong_AsUnsignedLongLong(status_obj); |
| 13952 | if (status == (unsigned long long)-1 && PyErr_Occurred()) { |
| 13953 | return NULL; |
| 13954 | } |
| 13955 | |
| 13956 | unsigned long long exitcode = (status >> 8); |
| 13957 | /* ExitProcess() accepts an UINT type: |
| 13958 | reject exit code which doesn't fit in an UINT */ |
| 13959 | if (exitcode > UINT_MAX) { |
| 13960 | PyErr_Format(PyExc_ValueError, "invalid exit code: %llu", exitcode); |
| 13961 | return NULL; |
| 13962 | } |
| 13963 | return PyLong_FromUnsignedLong((unsigned long)exitcode); |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13964 | #endif |
| 13965 | } |
| 13966 | #endif |
| 13967 | |
| 13968 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13969 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13970 | |
| 13971 | OS_STAT_METHODDEF |
| 13972 | OS_ACCESS_METHODDEF |
| 13973 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13974 | OS_CHDIR_METHODDEF |
| 13975 | OS_CHFLAGS_METHODDEF |
| 13976 | OS_CHMOD_METHODDEF |
| 13977 | OS_FCHMOD_METHODDEF |
| 13978 | OS_LCHMOD_METHODDEF |
| 13979 | OS_CHOWN_METHODDEF |
| 13980 | OS_FCHOWN_METHODDEF |
| 13981 | OS_LCHOWN_METHODDEF |
| 13982 | OS_LCHFLAGS_METHODDEF |
| 13983 | OS_CHROOT_METHODDEF |
| 13984 | OS_CTERMID_METHODDEF |
| 13985 | OS_GETCWD_METHODDEF |
| 13986 | OS_GETCWDB_METHODDEF |
| 13987 | OS_LINK_METHODDEF |
| 13988 | OS_LISTDIR_METHODDEF |
| 13989 | OS_LSTAT_METHODDEF |
| 13990 | OS_MKDIR_METHODDEF |
| 13991 | OS_NICE_METHODDEF |
| 13992 | OS_GETPRIORITY_METHODDEF |
| 13993 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13994 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 13995 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13996 | OS_READLINK_METHODDEF |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 13997 | OS_COPY_FILE_RANGE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13998 | OS_RENAME_METHODDEF |
| 13999 | OS_REPLACE_METHODDEF |
| 14000 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14001 | OS_SYMLINK_METHODDEF |
| 14002 | OS_SYSTEM_METHODDEF |
| 14003 | OS_UMASK_METHODDEF |
| 14004 | OS_UNAME_METHODDEF |
| 14005 | OS_UNLINK_METHODDEF |
| 14006 | OS_REMOVE_METHODDEF |
| 14007 | OS_UTIME_METHODDEF |
| 14008 | OS_TIMES_METHODDEF |
| 14009 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14010 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14011 | OS_EXECV_METHODDEF |
| 14012 | OS_EXECVE_METHODDEF |
| 14013 | OS_SPAWNV_METHODDEF |
| 14014 | OS_SPAWNVE_METHODDEF |
| 14015 | OS_FORK1_METHODDEF |
| 14016 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 14017 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14018 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 14019 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 14020 | OS_SCHED_GETPARAM_METHODDEF |
| 14021 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 14022 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 14023 | OS_SCHED_SETPARAM_METHODDEF |
| 14024 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 14025 | OS_SCHED_YIELD_METHODDEF |
| 14026 | OS_SCHED_SETAFFINITY_METHODDEF |
| 14027 | OS_SCHED_GETAFFINITY_METHODDEF |
| 14028 | OS_OPENPTY_METHODDEF |
| 14029 | OS_FORKPTY_METHODDEF |
| 14030 | OS_GETEGID_METHODDEF |
| 14031 | OS_GETEUID_METHODDEF |
| 14032 | OS_GETGID_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14033 | OS_GETGROUPLIST_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14034 | OS_GETGROUPS_METHODDEF |
| 14035 | OS_GETPID_METHODDEF |
| 14036 | OS_GETPGRP_METHODDEF |
| 14037 | OS_GETPPID_METHODDEF |
| 14038 | OS_GETUID_METHODDEF |
| 14039 | OS_GETLOGIN_METHODDEF |
| 14040 | OS_KILL_METHODDEF |
| 14041 | OS_KILLPG_METHODDEF |
| 14042 | OS_PLOCK_METHODDEF |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 14043 | OS_STARTFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14044 | OS_SETUID_METHODDEF |
| 14045 | OS_SETEUID_METHODDEF |
| 14046 | OS_SETREUID_METHODDEF |
| 14047 | OS_SETGID_METHODDEF |
| 14048 | OS_SETEGID_METHODDEF |
| 14049 | OS_SETREGID_METHODDEF |
| 14050 | OS_SETGROUPS_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14051 | OS_INITGROUPS_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14052 | OS_GETPGID_METHODDEF |
| 14053 | OS_SETPGRP_METHODDEF |
| 14054 | OS_WAIT_METHODDEF |
| 14055 | OS_WAIT3_METHODDEF |
| 14056 | OS_WAIT4_METHODDEF |
| 14057 | OS_WAITID_METHODDEF |
| 14058 | OS_WAITPID_METHODDEF |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 14059 | OS_PIDFD_OPEN_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14060 | OS_GETSID_METHODDEF |
| 14061 | OS_SETSID_METHODDEF |
| 14062 | OS_SETPGID_METHODDEF |
| 14063 | OS_TCGETPGRP_METHODDEF |
| 14064 | OS_TCSETPGRP_METHODDEF |
| 14065 | OS_OPEN_METHODDEF |
| 14066 | OS_CLOSE_METHODDEF |
| 14067 | OS_CLOSERANGE_METHODDEF |
| 14068 | OS_DEVICE_ENCODING_METHODDEF |
| 14069 | OS_DUP_METHODDEF |
| 14070 | OS_DUP2_METHODDEF |
| 14071 | OS_LOCKF_METHODDEF |
| 14072 | OS_LSEEK_METHODDEF |
| 14073 | OS_READ_METHODDEF |
| 14074 | OS_READV_METHODDEF |
| 14075 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14076 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14077 | OS_WRITE_METHODDEF |
| 14078 | OS_WRITEV_METHODDEF |
| 14079 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14080 | OS_PWRITEV_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14081 | OS_SENDFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14082 | OS_FSTAT_METHODDEF |
| 14083 | OS_ISATTY_METHODDEF |
| 14084 | OS_PIPE_METHODDEF |
| 14085 | OS_PIPE2_METHODDEF |
| 14086 | OS_MKFIFO_METHODDEF |
| 14087 | OS_MKNOD_METHODDEF |
| 14088 | OS_MAJOR_METHODDEF |
| 14089 | OS_MINOR_METHODDEF |
| 14090 | OS_MAKEDEV_METHODDEF |
| 14091 | OS_FTRUNCATE_METHODDEF |
| 14092 | OS_TRUNCATE_METHODDEF |
| 14093 | OS_POSIX_FALLOCATE_METHODDEF |
| 14094 | OS_POSIX_FADVISE_METHODDEF |
| 14095 | OS_PUTENV_METHODDEF |
| 14096 | OS_UNSETENV_METHODDEF |
| 14097 | OS_STRERROR_METHODDEF |
| 14098 | OS_FCHDIR_METHODDEF |
| 14099 | OS_FSYNC_METHODDEF |
| 14100 | OS_SYNC_METHODDEF |
| 14101 | OS_FDATASYNC_METHODDEF |
| 14102 | OS_WCOREDUMP_METHODDEF |
| 14103 | OS_WIFCONTINUED_METHODDEF |
| 14104 | OS_WIFSTOPPED_METHODDEF |
| 14105 | OS_WIFSIGNALED_METHODDEF |
| 14106 | OS_WIFEXITED_METHODDEF |
| 14107 | OS_WEXITSTATUS_METHODDEF |
| 14108 | OS_WTERMSIG_METHODDEF |
| 14109 | OS_WSTOPSIG_METHODDEF |
| 14110 | OS_FSTATVFS_METHODDEF |
| 14111 | OS_STATVFS_METHODDEF |
| 14112 | OS_CONFSTR_METHODDEF |
| 14113 | OS_SYSCONF_METHODDEF |
| 14114 | OS_FPATHCONF_METHODDEF |
| 14115 | OS_PATHCONF_METHODDEF |
| 14116 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 14117 | OS__GETFULLPATHNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14118 | OS__GETDISKUSAGE_METHODDEF |
| 14119 | OS__GETFINALPATHNAME_METHODDEF |
| 14120 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 14121 | OS_GETLOADAVG_METHODDEF |
| 14122 | OS_URANDOM_METHODDEF |
| 14123 | OS_SETRESUID_METHODDEF |
| 14124 | OS_SETRESGID_METHODDEF |
| 14125 | OS_GETRESUID_METHODDEF |
| 14126 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 14127 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14128 | OS_GETXATTR_METHODDEF |
| 14129 | OS_SETXATTR_METHODDEF |
| 14130 | OS_REMOVEXATTR_METHODDEF |
| 14131 | OS_LISTXATTR_METHODDEF |
| 14132 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14133 | OS_GET_TERMINAL_SIZE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14134 | OS_CPU_COUNT_METHODDEF |
| 14135 | OS_GET_INHERITABLE_METHODDEF |
| 14136 | OS_SET_INHERITABLE_METHODDEF |
| 14137 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 14138 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 14139 | OS_GET_BLOCKING_METHODDEF |
| 14140 | OS_SET_BLOCKING_METHODDEF |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 14141 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14142 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14143 | OS_GETRANDOM_METHODDEF |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14144 | OS_MEMFD_CREATE_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14145 | OS__ADD_DLL_DIRECTORY_METHODDEF |
| 14146 | OS__REMOVE_DLL_DIRECTORY_METHODDEF |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14147 | OS_WAITSTATUS_TO_EXITCODE_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14148 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14149 | }; |
| 14150 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14151 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14152 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14153 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14154 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14155 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14156 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14157 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14158 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14159 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14160 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14161 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14162 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14163 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14164 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14165 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14166 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14167 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14168 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14169 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14170 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14171 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14172 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14173 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14174 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14175 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14176 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14177 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14178 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14179 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14180 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14181 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14182 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14183 | #endif |
| 14184 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14185 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14186 | #endif |
| 14187 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14188 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14189 | #endif |
| 14190 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14191 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14192 | #endif |
| 14193 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14194 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14195 | #endif |
| 14196 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14197 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14198 | #endif |
| 14199 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14200 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14201 | #endif |
| 14202 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14203 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14204 | #endif |
| 14205 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14206 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14207 | #endif |
| 14208 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14209 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14210 | #endif |
| 14211 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14212 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14213 | #endif |
| 14214 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14215 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14216 | #endif |
| 14217 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14218 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14219 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14220 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14221 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14222 | #endif |
| 14223 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14224 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14225 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14226 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14227 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14228 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14229 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14230 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14231 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14232 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14233 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14234 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14235 | #endif |
| 14236 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14237 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14238 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14239 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14240 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14241 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14242 | #endif |
| 14243 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14244 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14245 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14246 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14247 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14248 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14249 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14250 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14251 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 14252 | #ifdef O_TMPFILE |
| 14253 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 14254 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14255 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14256 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14257 | #endif |
| 14258 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14259 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14260 | #endif |
| 14261 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14262 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14263 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14264 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14265 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14266 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14267 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14268 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14269 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14270 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14271 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14272 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14273 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14274 | #endif |
| 14275 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14276 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14277 | #endif |
| 14278 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14279 | /* MS Windows */ |
| 14280 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14281 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14282 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14283 | #endif |
| 14284 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14285 | /* Optimize for short life (keep in memory). */ |
| 14286 | /* 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] | 14287 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14288 | #endif |
| 14289 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14290 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14291 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14292 | #endif |
| 14293 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14294 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14295 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14296 | #endif |
| 14297 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14298 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14299 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14300 | #endif |
| 14301 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14302 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14303 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14304 | /* Send a SIGIO signal whenever input or output |
| 14305 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14306 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14307 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14308 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14309 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14310 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14311 | #endif |
| 14312 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14313 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14314 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14315 | #endif |
| 14316 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14317 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14318 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14319 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14320 | #ifdef O_NOLINKS |
| 14321 | /* 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] | 14322 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14323 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14324 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14325 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14326 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14327 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 14328 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14329 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14330 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14331 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14332 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14333 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14334 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14335 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14336 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14337 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14338 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14339 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14340 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14341 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14342 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14343 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14344 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14345 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14346 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14347 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14348 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14349 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14350 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14351 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14352 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14353 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14354 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14355 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14356 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14357 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14358 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14359 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14360 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14361 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14362 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14363 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14364 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14365 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14366 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14367 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14368 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14369 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14370 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14371 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14372 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14373 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14374 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14375 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14376 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14377 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14378 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14379 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14380 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14381 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 14382 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14383 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14384 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14385 | #endif /* ST_RDONLY */ |
| 14386 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14387 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14388 | #endif /* ST_NOSUID */ |
| 14389 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 14390 | /* GNU extensions */ |
| 14391 | #ifdef ST_NODEV |
| 14392 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 14393 | #endif /* ST_NODEV */ |
| 14394 | #ifdef ST_NOEXEC |
| 14395 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 14396 | #endif /* ST_NOEXEC */ |
| 14397 | #ifdef ST_SYNCHRONOUS |
| 14398 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 14399 | #endif /* ST_SYNCHRONOUS */ |
| 14400 | #ifdef ST_MANDLOCK |
| 14401 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 14402 | #endif /* ST_MANDLOCK */ |
| 14403 | #ifdef ST_WRITE |
| 14404 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 14405 | #endif /* ST_WRITE */ |
| 14406 | #ifdef ST_APPEND |
| 14407 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 14408 | #endif /* ST_APPEND */ |
| 14409 | #ifdef ST_NOATIME |
| 14410 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 14411 | #endif /* ST_NOATIME */ |
| 14412 | #ifdef ST_NODIRATIME |
| 14413 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 14414 | #endif /* ST_NODIRATIME */ |
| 14415 | #ifdef ST_RELATIME |
| 14416 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 14417 | #endif /* ST_RELATIME */ |
| 14418 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14419 | /* FreeBSD sendfile() constants */ |
| 14420 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14421 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14422 | #endif |
| 14423 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14424 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14425 | #endif |
| 14426 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14427 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14428 | #endif |
| 14429 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14430 | /* constants for posix_fadvise */ |
| 14431 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14432 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14433 | #endif |
| 14434 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14435 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14436 | #endif |
| 14437 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14438 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14439 | #endif |
| 14440 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14441 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14442 | #endif |
| 14443 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14444 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14445 | #endif |
| 14446 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14447 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14448 | #endif |
| 14449 | |
| 14450 | /* constants for waitid */ |
| 14451 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14452 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 14453 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 14454 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 14455 | #ifdef P_PIDFD |
| 14456 | if (PyModule_AddIntMacro(m, P_PIDFD)) return -1; |
| 14457 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14458 | #endif |
| 14459 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14460 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14461 | #endif |
| 14462 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14463 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14464 | #endif |
| 14465 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14466 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14467 | #endif |
| 14468 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14469 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14470 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14471 | #ifdef CLD_KILLED |
| 14472 | if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1; |
| 14473 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14474 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14475 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14476 | #endif |
| 14477 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14478 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14479 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14480 | #ifdef CLD_STOPPED |
| 14481 | if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1; |
| 14482 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14483 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14484 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14485 | #endif |
| 14486 | |
| 14487 | /* constants for lockf */ |
| 14488 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14489 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14490 | #endif |
| 14491 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14492 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14493 | #endif |
| 14494 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14495 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14496 | #endif |
| 14497 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14498 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14499 | #endif |
| 14500 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14501 | #ifdef RWF_DSYNC |
| 14502 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 14503 | #endif |
| 14504 | #ifdef RWF_HIPRI |
| 14505 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 14506 | #endif |
| 14507 | #ifdef RWF_SYNC |
| 14508 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 14509 | #endif |
| 14510 | #ifdef RWF_NOWAIT |
| 14511 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 14512 | #endif |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame^] | 14513 | #ifdef RWF_APPEND |
| 14514 | if (PyModule_AddIntConstant(m, "RWF_APPEND", RWF_APPEND)) return -1; |
| 14515 | #endif |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14516 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14517 | /* constants for posix_spawn */ |
| 14518 | #ifdef HAVE_POSIX_SPAWN |
| 14519 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 14520 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 14521 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 14522 | #endif |
| 14523 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14524 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14525 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 14526 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14527 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14528 | #endif |
| 14529 | #ifdef HAVE_SPAWNV |
| 14530 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14531 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 14532 | #endif |
| 14533 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14534 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14535 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14536 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14537 | #endif |
| 14538 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14539 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14540 | #endif |
| 14541 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14542 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14543 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14544 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 14545 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14546 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14547 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14548 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14549 | #endif |
| 14550 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14551 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14552 | #endif |
| 14553 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14554 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14555 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14556 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14557 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14558 | #endif |
| 14559 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14560 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14561 | #endif |
| 14562 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14563 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14564 | #endif |
| 14565 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14566 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14567 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14568 | #endif |
| 14569 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 14570 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14571 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 14572 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 14573 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 14574 | #endif |
| 14575 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14576 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14577 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14578 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14579 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14580 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14581 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14582 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14583 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14584 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14585 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14586 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14587 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14588 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14589 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14590 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14591 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14592 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14593 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14594 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14595 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14596 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 14597 | #if HAVE_DECL_RTLD_MEMBER |
| 14598 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 14599 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14600 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14601 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 14602 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 14603 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 14604 | #endif |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14605 | #ifdef HAVE_MEMFD_CREATE |
| 14606 | if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1; |
| 14607 | if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1; |
| 14608 | #ifdef MFD_HUGETLB |
| 14609 | if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14610 | #endif |
| 14611 | #ifdef MFD_HUGE_SHIFT |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14612 | if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14613 | #endif |
| 14614 | #ifdef MFD_HUGE_MASK |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14615 | if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14616 | #endif |
| 14617 | #ifdef MFD_HUGE_64KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14618 | if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14619 | #endif |
| 14620 | #ifdef MFD_HUGE_512KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14621 | if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14622 | #endif |
| 14623 | #ifdef MFD_HUGE_1MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14624 | if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14625 | #endif |
| 14626 | #ifdef MFD_HUGE_2MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14627 | if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14628 | #endif |
| 14629 | #ifdef MFD_HUGE_8MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14630 | if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14631 | #endif |
| 14632 | #ifdef MFD_HUGE_16MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14633 | if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14634 | #endif |
| 14635 | #ifdef MFD_HUGE_32MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14636 | if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14637 | #endif |
| 14638 | #ifdef MFD_HUGE_256MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14639 | if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14640 | #endif |
| 14641 | #ifdef MFD_HUGE_512MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14642 | if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14643 | #endif |
| 14644 | #ifdef MFD_HUGE_1GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14645 | if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14646 | #endif |
| 14647 | #ifdef MFD_HUGE_2GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14648 | if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14649 | #endif |
| 14650 | #ifdef MFD_HUGE_16GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14651 | if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1; |
| 14652 | #endif |
| 14653 | #endif |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14654 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14655 | #if defined(__APPLE__) |
| 14656 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 14657 | #endif |
| 14658 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14659 | #ifdef MS_WINDOWS |
| 14660 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; |
| 14661 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; |
| 14662 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; |
| 14663 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; |
| 14664 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; |
| 14665 | #endif |
| 14666 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14667 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14668 | } |
| 14669 | |
| 14670 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14671 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14672 | |
| 14673 | #ifdef HAVE_FACCESSAT |
| 14674 | "HAVE_FACCESSAT", |
| 14675 | #endif |
| 14676 | |
| 14677 | #ifdef HAVE_FCHDIR |
| 14678 | "HAVE_FCHDIR", |
| 14679 | #endif |
| 14680 | |
| 14681 | #ifdef HAVE_FCHMOD |
| 14682 | "HAVE_FCHMOD", |
| 14683 | #endif |
| 14684 | |
| 14685 | #ifdef HAVE_FCHMODAT |
| 14686 | "HAVE_FCHMODAT", |
| 14687 | #endif |
| 14688 | |
| 14689 | #ifdef HAVE_FCHOWN |
| 14690 | "HAVE_FCHOWN", |
| 14691 | #endif |
| 14692 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 14693 | #ifdef HAVE_FCHOWNAT |
| 14694 | "HAVE_FCHOWNAT", |
| 14695 | #endif |
| 14696 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14697 | #ifdef HAVE_FEXECVE |
| 14698 | "HAVE_FEXECVE", |
| 14699 | #endif |
| 14700 | |
| 14701 | #ifdef HAVE_FDOPENDIR |
| 14702 | "HAVE_FDOPENDIR", |
| 14703 | #endif |
| 14704 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14705 | #ifdef HAVE_FPATHCONF |
| 14706 | "HAVE_FPATHCONF", |
| 14707 | #endif |
| 14708 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14709 | #ifdef HAVE_FSTATAT |
| 14710 | "HAVE_FSTATAT", |
| 14711 | #endif |
| 14712 | |
| 14713 | #ifdef HAVE_FSTATVFS |
| 14714 | "HAVE_FSTATVFS", |
| 14715 | #endif |
| 14716 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 14717 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14718 | "HAVE_FTRUNCATE", |
| 14719 | #endif |
| 14720 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14721 | #ifdef HAVE_FUTIMENS |
| 14722 | "HAVE_FUTIMENS", |
| 14723 | #endif |
| 14724 | |
| 14725 | #ifdef HAVE_FUTIMES |
| 14726 | "HAVE_FUTIMES", |
| 14727 | #endif |
| 14728 | |
| 14729 | #ifdef HAVE_FUTIMESAT |
| 14730 | "HAVE_FUTIMESAT", |
| 14731 | #endif |
| 14732 | |
| 14733 | #ifdef HAVE_LINKAT |
| 14734 | "HAVE_LINKAT", |
| 14735 | #endif |
| 14736 | |
| 14737 | #ifdef HAVE_LCHFLAGS |
| 14738 | "HAVE_LCHFLAGS", |
| 14739 | #endif |
| 14740 | |
| 14741 | #ifdef HAVE_LCHMOD |
| 14742 | "HAVE_LCHMOD", |
| 14743 | #endif |
| 14744 | |
| 14745 | #ifdef HAVE_LCHOWN |
| 14746 | "HAVE_LCHOWN", |
| 14747 | #endif |
| 14748 | |
| 14749 | #ifdef HAVE_LSTAT |
| 14750 | "HAVE_LSTAT", |
| 14751 | #endif |
| 14752 | |
| 14753 | #ifdef HAVE_LUTIMES |
| 14754 | "HAVE_LUTIMES", |
| 14755 | #endif |
| 14756 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14757 | #ifdef HAVE_MEMFD_CREATE |
| 14758 | "HAVE_MEMFD_CREATE", |
| 14759 | #endif |
| 14760 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14761 | #ifdef HAVE_MKDIRAT |
| 14762 | "HAVE_MKDIRAT", |
| 14763 | #endif |
| 14764 | |
| 14765 | #ifdef HAVE_MKFIFOAT |
| 14766 | "HAVE_MKFIFOAT", |
| 14767 | #endif |
| 14768 | |
| 14769 | #ifdef HAVE_MKNODAT |
| 14770 | "HAVE_MKNODAT", |
| 14771 | #endif |
| 14772 | |
| 14773 | #ifdef HAVE_OPENAT |
| 14774 | "HAVE_OPENAT", |
| 14775 | #endif |
| 14776 | |
| 14777 | #ifdef HAVE_READLINKAT |
| 14778 | "HAVE_READLINKAT", |
| 14779 | #endif |
| 14780 | |
| 14781 | #ifdef HAVE_RENAMEAT |
| 14782 | "HAVE_RENAMEAT", |
| 14783 | #endif |
| 14784 | |
| 14785 | #ifdef HAVE_SYMLINKAT |
| 14786 | "HAVE_SYMLINKAT", |
| 14787 | #endif |
| 14788 | |
| 14789 | #ifdef HAVE_UNLINKAT |
| 14790 | "HAVE_UNLINKAT", |
| 14791 | #endif |
| 14792 | |
| 14793 | #ifdef HAVE_UTIMENSAT |
| 14794 | "HAVE_UTIMENSAT", |
| 14795 | #endif |
| 14796 | |
| 14797 | #ifdef MS_WINDOWS |
| 14798 | "MS_WINDOWS", |
| 14799 | #endif |
| 14800 | |
| 14801 | NULL |
| 14802 | }; |
| 14803 | |
| 14804 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14805 | static int |
| 14806 | posixmodule_exec(PyObject *m) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14807 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14808 | _posixstate *state = get_posix_state(m); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14809 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14810 | /* Initialize environ dictionary */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14811 | PyObject *v = convertenviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14812 | Py_XINCREF(v); |
| 14813 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14814 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14815 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14816 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14817 | if (all_ins(m)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14818 | return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14819 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14820 | if (setup_confname_tables(m)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14821 | return -1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14822 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14823 | Py_INCREF(PyExc_OSError); |
| 14824 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 14825 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14826 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14827 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 14828 | PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc); |
| 14829 | if (WaitidResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14830 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14831 | } |
| 14832 | Py_INCREF(WaitidResultType); |
| 14833 | PyModule_AddObject(m, "waitid_result", WaitidResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14834 | state->WaitidResultType = WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14835 | #endif |
| 14836 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14837 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
| 14838 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 14839 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 14840 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 14841 | PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc); |
| 14842 | if (StatResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14843 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14844 | } |
| 14845 | Py_INCREF(StatResultType); |
| 14846 | PyModule_AddObject(m, "stat_result", StatResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14847 | state->StatResultType = StatResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14848 | structseq_new = ((PyTypeObject *)StatResultType)->tp_new; |
| 14849 | ((PyTypeObject *)StatResultType)->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14850 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14851 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
| 14852 | PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc); |
| 14853 | if (StatVFSResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14854 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14855 | } |
| 14856 | Py_INCREF(StatVFSResultType); |
| 14857 | PyModule_AddObject(m, "statvfs_result", StatVFSResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14858 | state->StatVFSResultType = StatVFSResultType; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14859 | #ifdef NEED_TICKS_PER_SECOND |
| 14860 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14861 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14862 | # elif defined(HZ) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14863 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14864 | # else |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14865 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14866 | # endif |
| 14867 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14868 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14869 | #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] | 14870 | sched_param_desc.name = MODNAME ".sched_param"; |
| 14871 | PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc); |
| 14872 | if (SchedParamType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14873 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14874 | } |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14875 | Py_INCREF(SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14876 | PyModule_AddObject(m, "sched_param", SchedParamType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14877 | state->SchedParamType = SchedParamType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14878 | ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param; |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14879 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14880 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14881 | /* initialize TerminalSize_info */ |
| 14882 | PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc); |
| 14883 | if (TerminalSizeType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14884 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14885 | } |
| 14886 | Py_INCREF(TerminalSizeType); |
| 14887 | PyModule_AddObject(m, "terminal_size", TerminalSizeType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14888 | state->TerminalSizeType = TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14889 | |
| 14890 | /* initialize scandir types */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14891 | PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14892 | if (ScandirIteratorType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14893 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14894 | } |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14895 | state->ScandirIteratorType = ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14896 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14897 | PyObject *DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14898 | if (DirEntryType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14899 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14900 | } |
| 14901 | Py_INCREF(DirEntryType); |
| 14902 | PyModule_AddObject(m, "DirEntry", DirEntryType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14903 | state->DirEntryType = DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14904 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14905 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14906 | PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(×_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14907 | if (TimesResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14908 | return -1; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14909 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14910 | Py_INCREF(TimesResultType); |
| 14911 | PyModule_AddObject(m, "times_result", TimesResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14912 | state->TimesResultType = TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14913 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14914 | PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14915 | if (UnameResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14916 | return -1; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14917 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14918 | Py_INCREF(UnameResultType); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14919 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14920 | state->UnameResultType = (PyObject *)UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14921 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14922 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14923 | /* |
| 14924 | * Step 2 of weak-linking support on Mac OS X. |
| 14925 | * |
| 14926 | * The code below removes functions that are not available on the |
| 14927 | * currently active platform. |
| 14928 | * |
| 14929 | * 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] | 14930 | * 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] | 14931 | * OSX 10.4. |
| 14932 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14933 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14934 | if (fstatvfs == NULL) { |
| 14935 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14936 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14937 | } |
| 14938 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14939 | #endif /* HAVE_FSTATVFS */ |
| 14940 | |
| 14941 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14942 | if (statvfs == NULL) { |
| 14943 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14944 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14945 | } |
| 14946 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14947 | #endif /* HAVE_STATVFS */ |
| 14948 | |
| 14949 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14950 | if (lchown == NULL) { |
| 14951 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14952 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14953 | } |
| 14954 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14955 | #endif /* HAVE_LCHOWN */ |
| 14956 | |
| 14957 | |
| 14958 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14959 | |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14960 | if ((state->billion = PyLong_FromLong(1000000000)) == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14961 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14962 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14963 | state->struct_rusage = PyUnicode_InternFromString("struct_rusage"); |
| 14964 | if (state->struct_rusage == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14965 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14966 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14967 | state->st_mode = PyUnicode_InternFromString("st_mode"); |
| 14968 | if (state->st_mode == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14969 | return -1; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 14970 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14971 | /* suppress "function not used" warnings */ |
| 14972 | { |
| 14973 | int ignored; |
| 14974 | fd_specified("", -1); |
| 14975 | follow_symlinks_specified("", 1); |
| 14976 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 14977 | dir_fd_converter(Py_None, &ignored); |
| 14978 | dir_fd_unavailable(Py_None, &ignored); |
| 14979 | } |
| 14980 | |
| 14981 | /* |
| 14982 | * provide list of locally available functions |
| 14983 | * so os.py can populate support_* lists |
| 14984 | */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14985 | PyObject *list = PyList_New(0); |
| 14986 | if (!list) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14987 | return -1; |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 14988 | } |
| 14989 | for (const char * const *trace = have_functions; *trace; trace++) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14990 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 14991 | if (!unicode) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14992 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14993 | if (PyList_Append(list, unicode)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14994 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14995 | Py_DECREF(unicode); |
| 14996 | } |
| 14997 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 14998 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 14999 | return 0; |
| 15000 | } |
| 15001 | |
| 15002 | |
| 15003 | static PyModuleDef_Slot posixmodile_slots[] = { |
| 15004 | {Py_mod_exec, posixmodule_exec}, |
| 15005 | {0, NULL} |
| 15006 | }; |
| 15007 | |
| 15008 | static struct PyModuleDef posixmodule = { |
| 15009 | PyModuleDef_HEAD_INIT, |
| 15010 | .m_name = MODNAME, |
| 15011 | .m_doc = posix__doc__, |
| 15012 | .m_size = sizeof(_posixstate), |
| 15013 | .m_methods = posix_methods, |
| 15014 | .m_slots = posixmodile_slots, |
| 15015 | .m_traverse = _posix_traverse, |
| 15016 | .m_clear = _posix_clear, |
| 15017 | .m_free = _posix_free, |
| 15018 | }; |
| 15019 | |
| 15020 | PyMODINIT_FUNC |
| 15021 | INITFUNC(void) |
| 15022 | { |
| 15023 | return PyModuleDef_Init(&posixmodule); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 15024 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15025 | |
| 15026 | #ifdef __cplusplus |
| 15027 | } |
| 15028 | #endif |