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 | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 10 | #define PY_SSIZE_T_CLEAN |
| 11 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Kyle Evans | 7992579 | 2020-10-13 15:04:44 -0500 | [diff] [blame] | 13 | #include "pycore_fileutils.h" |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 14 | #ifdef MS_WINDOWS |
| 15 | /* include <windows.h> early to avoid conflict with pycore_condvar.h: |
| 16 | |
| 17 | #define WIN32_LEAN_AND_MEAN |
| 18 | #include <windows.h> |
| 19 | |
| 20 | FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */ |
| 21 | # include <windows.h> |
| 22 | #endif |
| 23 | |
pxinwr | 3405e05 | 2020-08-07 13:21:52 +0800 | [diff] [blame] | 24 | #ifdef __VXWORKS__ |
| 25 | # include "pycore_bitutils.h" // _Py_popcount32() |
| 26 | #endif |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 27 | #include "pycore_ceval.h" // _PyEval_ReInitThreads() |
| 28 | #include "pycore_import.h" // _PyImport_ReInitLock() |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 29 | #include "pycore_initconfig.h" // _PyStatus_EXCEPTION() |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 30 | #include "pycore_pystate.h" // _PyInterpreterState_GET() |
| 31 | #include "structmember.h" // PyMemberDef |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 32 | #ifndef MS_WINDOWS |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 33 | # include "posixmodule.h" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 34 | #else |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 35 | # include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 36 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 37 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 38 | /* On android API level 21, 'AT_EACCESS' is not declared although |
| 39 | * HAVE_FACCESSAT is defined. */ |
| 40 | #ifdef __ANDROID__ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 41 | # undef HAVE_FACCESSAT |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 42 | #endif |
| 43 | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | fa76eee | 2016-05-28 21:03:48 +0000 | [diff] [blame] | 44 | #include <stdio.h> /* needed for ctermid() */ |
| 45 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 46 | /* |
| 47 | * A number of APIs are available on macOS from a certain macOS version. |
| 48 | * To support building with a new SDK while deploying to older versions |
| 49 | * the availability test is split into two: |
| 50 | * - HAVE_<FUNCTION>: The configure check for compile time availability |
| 51 | * - HAVE_<FUNCTION>_RUNTIME: Runtime check for availability |
| 52 | * |
| 53 | * The latter is always true when not on macOS, or when using a compiler |
| 54 | * that does not support __has_builtin (older versions of Xcode). |
| 55 | * |
| 56 | * Due to compiler restrictions there is one valid use of HAVE_<FUNCTION>_RUNTIME: |
| 57 | * if (HAVE_<FUNCTION>_RUNTIME) { ... } |
| 58 | * |
| 59 | * In mixing the test with other tests or using negations will result in compile |
| 60 | * errors. |
| 61 | */ |
| 62 | #if defined(__APPLE__) |
| 63 | |
| 64 | #if defined(__has_builtin) && __has_builtin(__builtin_available) |
| 65 | # define HAVE_FSTATAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 66 | # define HAVE_FACCESSAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 67 | # define HAVE_FCHMODAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 68 | # define HAVE_FCHOWNAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 69 | # define HAVE_LINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 70 | # define HAVE_FDOPENDIR_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 71 | # define HAVE_MKDIRAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 72 | # define HAVE_RENAMEAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 73 | # define HAVE_UNLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 74 | # define HAVE_OPENAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 75 | # define HAVE_READLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 76 | # define HAVE_SYMLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *) |
| 77 | # define HAVE_FUTIMENS_RUNTIME __builtin_available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *) |
| 78 | # define HAVE_UTIMENSAT_RUNTIME __builtin_available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *) |
| 79 | # define HAVE_PWRITEV_RUNTIME __builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *) |
| 80 | |
| 81 | # define HAVE_POSIX_SPAWN_SETSID_RUNTIME __builtin_available(macOS 10.15, *) |
| 82 | |
| 83 | #else /* Xcode 8 or earlier */ |
| 84 | |
| 85 | /* __builtin_available is not present in these compilers, but |
| 86 | * some of the symbols might be weak linked (10.10 SDK or later |
| 87 | * deploying on 10.9. |
| 88 | * |
| 89 | * Fall back to the older style of availability checking for |
| 90 | * symbols introduced in macOS 10.10. |
| 91 | */ |
| 92 | |
| 93 | # ifdef HAVE_FSTATAT |
| 94 | # define HAVE_FSTATAT_RUNTIME (fstatat != NULL) |
| 95 | # endif |
| 96 | |
| 97 | # ifdef HAVE_FACCESSAT |
| 98 | # define HAVE_FACCESSAT_RUNTIME (faccessat != NULL) |
| 99 | # endif |
| 100 | |
| 101 | # ifdef HAVE_FCHMODAT |
| 102 | # define HAVE_FCHMODAT_RUNTIME (fchmodat != NULL) |
| 103 | # endif |
| 104 | |
| 105 | # ifdef HAVE_FCHOWNAT |
| 106 | # define HAVE_FCHOWNAT_RUNTIME (fchownat != NULL) |
| 107 | # endif |
| 108 | |
| 109 | # ifdef HAVE_LINKAT |
| 110 | # define HAVE_LINKAT_RUNTIME (linkat != NULL) |
| 111 | # endif |
| 112 | |
| 113 | # ifdef HAVE_FDOPENDIR |
| 114 | # define HAVE_FDOPENDIR_RUNTIME (fdopendir != NULL) |
| 115 | # endif |
| 116 | |
| 117 | # ifdef HAVE_MKDIRAT |
| 118 | # define HAVE_MKDIRAT_RUNTIME (mkdirat != NULL) |
| 119 | # endif |
| 120 | |
| 121 | # ifdef HAVE_RENAMEAT |
| 122 | # define HAVE_RENAMEAT_RUNTIME (renameat != NULL) |
| 123 | # endif |
| 124 | |
| 125 | # ifdef HAVE_UNLINKAT |
| 126 | # define HAVE_UNLINKAT_RUNTIME (unlinkat != NULL) |
| 127 | # endif |
| 128 | |
| 129 | # ifdef HAVE_OPENAT |
| 130 | # define HAVE_OPENAT_RUNTIME (openat != NULL) |
| 131 | # endif |
| 132 | |
| 133 | # ifdef HAVE_READLINKAT |
| 134 | # define HAVE_READLINKAT_RUNTIME (readlinkat != NULL) |
| 135 | # endif |
| 136 | |
| 137 | # ifdef HAVE_SYMLINKAT |
| 138 | # define HAVE_SYMLINKAT_RUNTIME (symlinkat != NULL) |
| 139 | # endif |
| 140 | |
| 141 | #endif |
| 142 | |
| 143 | #ifdef HAVE_FUTIMESAT |
| 144 | /* Some of the logic for weak linking depends on this assertion */ |
| 145 | # error "HAVE_FUTIMESAT unexpectedly defined" |
| 146 | #endif |
| 147 | |
| 148 | #else |
| 149 | # define HAVE_FSTATAT_RUNTIME 1 |
| 150 | # define HAVE_FACCESSAT_RUNTIME 1 |
| 151 | # define HAVE_FCHMODAT_RUNTIME 1 |
| 152 | # define HAVE_FCHOWNAT_RUNTIME 1 |
| 153 | # define HAVE_LINKAT_RUNTIME 1 |
| 154 | # define HAVE_FDOPENDIR_RUNTIME 1 |
| 155 | # define HAVE_MKDIRAT_RUNTIME 1 |
| 156 | # define HAVE_RENAMEAT_RUNTIME 1 |
| 157 | # define HAVE_UNLINKAT_RUNTIME 1 |
| 158 | # define HAVE_OPENAT_RUNTIME 1 |
| 159 | # define HAVE_READLINKAT_RUNTIME 1 |
| 160 | # define HAVE_SYMLINKAT_RUNTIME 1 |
| 161 | # define HAVE_FUTIMENS_RUNTIME 1 |
| 162 | # define HAVE_UTIMENSAT_RUNTIME 1 |
| 163 | # define HAVE_PWRITEV_RUNTIME 1 |
| 164 | #endif |
| 165 | |
| 166 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 167 | #ifdef __cplusplus |
| 168 | extern "C" { |
| 169 | #endif |
| 170 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 171 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 172 | "This module provides access to operating system functionality that is\n\ |
| 173 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 174 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 175 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 176 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 177 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 178 | #ifdef HAVE_SYS_UIO_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 179 | # include <sys/uio.h> |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 180 | #endif |
| 181 | |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 182 | #ifdef HAVE_SYS_SYSMACROS_H |
| 183 | /* GNU C Library: major(), minor(), makedev() */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 184 | # include <sys/sysmacros.h> |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 185 | #endif |
| 186 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 187 | #ifdef HAVE_SYS_TYPES_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 188 | # include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 189 | #endif /* HAVE_SYS_TYPES_H */ |
| 190 | |
| 191 | #ifdef HAVE_SYS_STAT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 192 | # include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 193 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 195 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 196 | # include <sys/wait.h> // WNOHANG |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 197 | #endif |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 198 | #ifdef HAVE_LINUX_WAIT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 199 | # include <linux/wait.h> // P_PIDFD |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 200 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 201 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 202 | #ifdef HAVE_SIGNAL_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 203 | # include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 204 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 206 | #ifdef HAVE_FCNTL_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 207 | # include <fcntl.h> |
| 208 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 210 | #ifdef HAVE_GRP_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 211 | # include <grp.h> |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 212 | #endif |
| 213 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 214 | #ifdef HAVE_SYSEXITS_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 215 | # include <sysexits.h> |
| 216 | #endif |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 217 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 218 | #ifdef HAVE_SYS_LOADAVG_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 219 | # include <sys/loadavg.h> |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 220 | #endif |
| 221 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 222 | #ifdef HAVE_SYS_SENDFILE_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 223 | # include <sys/sendfile.h> |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 224 | #endif |
| 225 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 226 | #if defined(__APPLE__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 227 | # include <copyfile.h> |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 228 | #endif |
| 229 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 230 | #ifdef HAVE_SCHED_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 231 | # include <sched.h> |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 232 | #endif |
| 233 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 234 | #ifdef HAVE_COPY_FILE_RANGE |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 235 | # include <unistd.h> |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 236 | #endif |
| 237 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 238 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 239 | # undef HAVE_SCHED_SETAFFINITY |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 240 | #endif |
| 241 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 242 | #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] | 243 | # define USE_XATTRS |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 244 | #endif |
| 245 | |
| 246 | #ifdef USE_XATTRS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 247 | # include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 248 | #endif |
| 249 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 250 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 251 | # ifdef HAVE_SYS_SOCKET_H |
| 252 | # include <sys/socket.h> |
| 253 | # endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 254 | #endif |
| 255 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 256 | #ifdef HAVE_DLFCN_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 257 | # include <dlfcn.h> |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 258 | #endif |
| 259 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 260 | #ifdef __hpux |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 261 | # include <sys/mpctl.h> |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 262 | #endif |
| 263 | |
| 264 | #if defined(__DragonFly__) || \ |
| 265 | defined(__OpenBSD__) || \ |
| 266 | defined(__FreeBSD__) || \ |
| 267 | defined(__NetBSD__) || \ |
| 268 | defined(__APPLE__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 269 | # include <sys/sysctl.h> |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 270 | #endif |
| 271 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 272 | #ifdef HAVE_LINUX_RANDOM_H |
| 273 | # include <linux/random.h> |
| 274 | #endif |
| 275 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 276 | # include <sys/syscall.h> |
| 277 | #endif |
| 278 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 279 | #if defined(MS_WINDOWS) |
| 280 | # define TERMSIZE_USE_CONIO |
| 281 | #elif defined(HAVE_SYS_IOCTL_H) |
| 282 | # include <sys/ioctl.h> |
| 283 | # if defined(HAVE_TERMIOS_H) |
| 284 | # include <termios.h> |
| 285 | # endif |
| 286 | # if defined(TIOCGWINSZ) |
| 287 | # define TERMSIZE_USE_IOCTL |
| 288 | # endif |
| 289 | #endif /* MS_WINDOWS */ |
| 290 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 291 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 292 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 293 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 294 | # define HAVE_OPENDIR 1 |
| 295 | # define HAVE_SYSTEM 1 |
| 296 | # include <process.h> |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 297 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 298 | # ifdef _MSC_VER |
| 299 | /* Microsoft compiler */ |
| 300 | # define HAVE_GETPPID 1 |
| 301 | # define HAVE_GETLOGIN 1 |
| 302 | # define HAVE_SPAWNV 1 |
| 303 | # define HAVE_EXECV 1 |
| 304 | # define HAVE_WSPAWNV 1 |
| 305 | # define HAVE_WEXECV 1 |
| 306 | # define HAVE_PIPE 1 |
| 307 | # define HAVE_SYSTEM 1 |
| 308 | # define HAVE_CWAIT 1 |
| 309 | # define HAVE_FSYNC 1 |
| 310 | # define fsync _commit |
| 311 | # else |
| 312 | /* Unix functions that the configure script doesn't check for */ |
| 313 | # ifndef __VXWORKS__ |
| 314 | # define HAVE_EXECV 1 |
| 315 | # define HAVE_FORK 1 |
| 316 | # if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
| 317 | # define HAVE_FORK1 1 |
| 318 | # endif |
| 319 | # endif |
| 320 | # define HAVE_GETEGID 1 |
| 321 | # define HAVE_GETEUID 1 |
| 322 | # define HAVE_GETGID 1 |
| 323 | # define HAVE_GETPPID 1 |
| 324 | # define HAVE_GETUID 1 |
| 325 | # define HAVE_KILL 1 |
| 326 | # define HAVE_OPENDIR 1 |
| 327 | # define HAVE_PIPE 1 |
| 328 | # define HAVE_SYSTEM 1 |
| 329 | # define HAVE_WAIT 1 |
| 330 | # define HAVE_TTYNAME 1 |
| 331 | # endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 332 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 333 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 334 | _Py_IDENTIFIER(__fspath__); |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 335 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 336 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 337 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 338 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 339 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 340 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 341 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 342 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 343 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 344 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 345 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 346 | (default) */ |
| 347 | extern char *ctermid_r(char *); |
| 348 | #endif |
| 349 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 350 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 351 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 352 | #if defined(__VXWORKS__) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 353 | # include <vxCpuLib.h> |
| 354 | # include <rtpLib.h> |
| 355 | # include <wait.h> |
| 356 | # include <taskLib.h> |
| 357 | # ifndef _P_WAIT |
| 358 | # define _P_WAIT 0 |
| 359 | # define _P_NOWAIT 1 |
| 360 | # define _P_NOWAITO 1 |
| 361 | # endif |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 362 | #endif /* __VXWORKS__ */ |
| 363 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 364 | #ifdef HAVE_POSIX_SPAWN |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 365 | # include <spawn.h> |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 366 | #endif |
| 367 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 368 | #ifdef HAVE_UTIME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 369 | # include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 370 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 371 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 372 | #ifdef HAVE_SYS_UTIME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 373 | # include <sys/utime.h> |
| 374 | # 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] | 375 | #endif /* HAVE_SYS_UTIME_H */ |
| 376 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 377 | #ifdef HAVE_SYS_TIMES_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 378 | # include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 379 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 380 | |
| 381 | #ifdef HAVE_SYS_PARAM_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 382 | # include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 383 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 384 | |
| 385 | #ifdef HAVE_SYS_UTSNAME_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 386 | # include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 387 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 389 | #ifdef HAVE_DIRENT_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 390 | # include <dirent.h> |
| 391 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 392 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 393 | # if defined(__WATCOMC__) && !defined(__QNX__) |
| 394 | # include <direct.h> |
| 395 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
| 396 | # else |
| 397 | # define dirent direct |
| 398 | # define NAMLEN(dirent) (dirent)->d_namlen |
| 399 | # endif |
| 400 | # ifdef HAVE_SYS_NDIR_H |
| 401 | # include <sys/ndir.h> |
| 402 | # endif |
| 403 | # ifdef HAVE_SYS_DIR_H |
| 404 | # include <sys/dir.h> |
| 405 | # endif |
| 406 | # ifdef HAVE_NDIR_H |
| 407 | # include <ndir.h> |
| 408 | # endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 409 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 410 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 411 | #ifdef _MSC_VER |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 412 | # ifdef HAVE_DIRECT_H |
| 413 | # include <direct.h> |
| 414 | # endif |
| 415 | # ifdef HAVE_IO_H |
| 416 | # include <io.h> |
| 417 | # endif |
| 418 | # ifdef HAVE_PROCESS_H |
| 419 | # include <process.h> |
| 420 | # endif |
| 421 | # ifndef IO_REPARSE_TAG_SYMLINK |
| 422 | # define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
| 423 | # endif |
| 424 | # ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 425 | # define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 426 | # endif |
| 427 | # include "osdefs.h" // SEP |
| 428 | # include <malloc.h> |
| 429 | # include <windows.h> |
| 430 | # include <shellapi.h> // ShellExecute() |
| 431 | # include <lmcons.h> // UNLEN |
| 432 | # define HAVE_SYMLINK |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 433 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 434 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 435 | #ifndef MAXPATHLEN |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 436 | # if defined(PATH_MAX) && PATH_MAX > 1024 |
| 437 | # define MAXPATHLEN PATH_MAX |
| 438 | # else |
| 439 | # define MAXPATHLEN 1024 |
| 440 | # endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 441 | #endif /* MAXPATHLEN */ |
| 442 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 443 | #ifdef UNION_WAIT |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 444 | /* Emulate some macros on systems that have a union instead of macros */ |
| 445 | # ifndef WIFEXITED |
| 446 | # define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 447 | # endif |
| 448 | # ifndef WEXITSTATUS |
| 449 | # define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 450 | # endif |
| 451 | # ifndef WTERMSIG |
| 452 | # define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 453 | # endif |
| 454 | # define WAIT_TYPE union wait |
| 455 | # define WAIT_STATUS_INT(s) (s.w_status) |
| 456 | #else |
| 457 | /* !UNION_WAIT */ |
| 458 | # define WAIT_TYPE int |
| 459 | # define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 460 | #endif /* UNION_WAIT */ |
| 461 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 462 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 463 | prototype for it, at least on Solaris -- maybe others as well?). */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 464 | #if defined(HAVE_CTERMID_R) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 465 | # define USE_CTERMID_R |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 466 | #endif |
| 467 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 468 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 469 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 470 | #undef FSTAT |
| 471 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 472 | #ifdef MS_WINDOWS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 473 | # define STAT win32_stat |
| 474 | # define LSTAT win32_lstat |
| 475 | # define FSTAT _Py_fstat_noraise |
| 476 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 477 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 478 | # define STAT stat |
| 479 | # define LSTAT lstat |
| 480 | # define FSTAT fstat |
| 481 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 482 | #endif |
| 483 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 484 | #if defined(MAJOR_IN_MKDEV) |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 485 | # include <sys/mkdev.h> |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 486 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 487 | # if defined(MAJOR_IN_SYSMACROS) |
| 488 | # include <sys/sysmacros.h> |
| 489 | # endif |
| 490 | # if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 491 | # include <sys/mkdev.h> |
| 492 | # endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 493 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 494 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 495 | #ifdef MS_WINDOWS |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 496 | # define INITFUNC PyInit_nt |
| 497 | # define MODNAME "nt" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 498 | #else |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 499 | # define INITFUNC PyInit_posix |
| 500 | # define MODNAME "posix" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 501 | #endif |
| 502 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 503 | #if defined(__sun) |
| 504 | /* Something to implement in autoconf, not present in autoconf 2.69 */ |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 505 | # define HAVE_STRUCT_STAT_ST_FSTYPE 1 |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 506 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 507 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 508 | /* memfd_create is either defined in sys/mman.h or sys/memfd.h |
| 509 | * linux/memfd.h defines additional flags |
| 510 | */ |
| 511 | #ifdef HAVE_SYS_MMAN_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 512 | # include <sys/mman.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 513 | #endif |
| 514 | #ifdef HAVE_SYS_MEMFD_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 515 | # include <sys/memfd.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 516 | #endif |
| 517 | #ifdef HAVE_LINUX_MEMFD_H |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 518 | # include <linux/memfd.h> |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 519 | #endif |
| 520 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 521 | #ifdef _Py_MEMORY_SANITIZER |
Victor Stinner | 5eca75d | 2020-04-15 15:07:31 +0200 | [diff] [blame] | 522 | # include <sanitizer/msan_interface.h> |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 523 | #endif |
| 524 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 525 | #ifdef HAVE_FORK |
| 526 | static void |
| 527 | run_at_forkers(PyObject *lst, int reverse) |
| 528 | { |
| 529 | Py_ssize_t i; |
| 530 | PyObject *cpy; |
| 531 | |
| 532 | if (lst != NULL) { |
| 533 | assert(PyList_CheckExact(lst)); |
| 534 | |
| 535 | /* Use a list copy in case register_at_fork() is called from |
| 536 | * one of the callbacks. |
| 537 | */ |
| 538 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); |
| 539 | if (cpy == NULL) |
| 540 | PyErr_WriteUnraisable(lst); |
| 541 | else { |
| 542 | if (reverse) |
| 543 | PyList_Reverse(cpy); |
| 544 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { |
| 545 | PyObject *func, *res; |
| 546 | func = PyList_GET_ITEM(cpy, i); |
Jeroen Demeyer | 7f41c8e | 2019-07-04 12:35:31 +0200 | [diff] [blame] | 547 | res = _PyObject_CallNoArg(func); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 548 | if (res == NULL) |
| 549 | PyErr_WriteUnraisable(func); |
| 550 | else |
| 551 | Py_DECREF(res); |
| 552 | } |
| 553 | Py_DECREF(cpy); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | void |
| 559 | PyOS_BeforeFork(void) |
| 560 | { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 561 | run_at_forkers(_PyInterpreterState_GET()->before_forkers, 1); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 562 | |
| 563 | _PyImport_AcquireLock(); |
| 564 | } |
| 565 | |
| 566 | void |
| 567 | PyOS_AfterFork_Parent(void) |
| 568 | { |
| 569 | if (_PyImport_ReleaseLock() <= 0) |
| 570 | Py_FatalError("failed releasing import lock after fork"); |
| 571 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 572 | run_at_forkers(_PyInterpreterState_GET()->after_forkers_parent, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void |
| 576 | PyOS_AfterFork_Child(void) |
| 577 | { |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 578 | PyStatus status; |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 579 | _PyRuntimeState *runtime = &_PyRuntime; |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 580 | |
| 581 | status = _PyGILState_Reinit(runtime); |
| 582 | if (_PyStatus_EXCEPTION(status)) { |
| 583 | goto fatal_error; |
| 584 | } |
| 585 | |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 586 | PyThreadState *tstate = _PyThreadState_GET(); |
| 587 | _Py_EnsureTstateNotNULL(tstate); |
| 588 | |
| 589 | status = _PyEval_ReInitThreads(tstate); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 590 | if (_PyStatus_EXCEPTION(status)) { |
| 591 | goto fatal_error; |
| 592 | } |
| 593 | |
| 594 | status = _PyImport_ReInitLock(); |
| 595 | if (_PyStatus_EXCEPTION(status)) { |
| 596 | goto fatal_error; |
| 597 | } |
| 598 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 599 | _PySignal_AfterFork(); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 600 | |
| 601 | status = _PyRuntimeState_ReInitThreads(runtime); |
| 602 | if (_PyStatus_EXCEPTION(status)) { |
| 603 | goto fatal_error; |
| 604 | } |
| 605 | |
| 606 | status = _PyInterpreterState_DeleteExceptMain(runtime); |
| 607 | if (_PyStatus_EXCEPTION(status)) { |
| 608 | goto fatal_error; |
| 609 | } |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 610 | assert(_PyThreadState_GET() == tstate); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 611 | |
Victor Stinner | 317bab0 | 2020-06-02 18:44:54 +0200 | [diff] [blame] | 612 | run_at_forkers(tstate->interp->after_forkers_child, 0); |
Victor Stinner | 26881c8 | 2020-06-02 15:51:37 +0200 | [diff] [blame] | 613 | return; |
| 614 | |
| 615 | fatal_error: |
| 616 | Py_ExitStatusException(status); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | static int |
| 620 | register_at_forker(PyObject **lst, PyObject *func) |
| 621 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 622 | if (func == NULL) /* nothing to register? do nothing. */ |
| 623 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 624 | if (*lst == NULL) { |
| 625 | *lst = PyList_New(0); |
| 626 | if (*lst == NULL) |
| 627 | return -1; |
| 628 | } |
| 629 | return PyList_Append(*lst, func); |
| 630 | } |
Victor Stinner | 87255be | 2020-04-07 23:11:49 +0200 | [diff] [blame] | 631 | #endif /* HAVE_FORK */ |
| 632 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 633 | |
| 634 | /* Legacy wrapper */ |
| 635 | void |
| 636 | PyOS_AfterFork(void) |
| 637 | { |
| 638 | #ifdef HAVE_FORK |
| 639 | PyOS_AfterFork_Child(); |
| 640 | #endif |
| 641 | } |
| 642 | |
| 643 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 644 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 645 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 646 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 647 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 648 | ULONG, struct _Py_stat_struct *); |
| 649 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 650 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 651 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 652 | #ifndef MS_WINDOWS |
| 653 | PyObject * |
| 654 | _PyLong_FromUid(uid_t uid) |
| 655 | { |
| 656 | if (uid == (uid_t)-1) |
| 657 | return PyLong_FromLong(-1); |
| 658 | return PyLong_FromUnsignedLong(uid); |
| 659 | } |
| 660 | |
| 661 | PyObject * |
| 662 | _PyLong_FromGid(gid_t gid) |
| 663 | { |
| 664 | if (gid == (gid_t)-1) |
| 665 | return PyLong_FromLong(-1); |
| 666 | return PyLong_FromUnsignedLong(gid); |
| 667 | } |
| 668 | |
| 669 | int |
| 670 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 671 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 672 | uid_t uid; |
| 673 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 674 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 675 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 676 | unsigned long uresult; |
| 677 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 678 | index = _PyNumber_Index(obj); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 679 | if (index == NULL) { |
| 680 | PyErr_Format(PyExc_TypeError, |
| 681 | "uid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 682 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 683 | return 0; |
| 684 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 685 | |
| 686 | /* |
| 687 | * Handling uid_t is complicated for two reasons: |
| 688 | * * Although uid_t is (always?) unsigned, it still |
| 689 | * accepts -1. |
| 690 | * * We don't know its size in advance--it may be |
| 691 | * bigger than an int, or it may be smaller than |
| 692 | * a long. |
| 693 | * |
| 694 | * So a bit of defensive programming is in order. |
| 695 | * Start with interpreting the value passed |
| 696 | * in as a signed long and see if it works. |
| 697 | */ |
| 698 | |
| 699 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 700 | |
| 701 | if (!overflow) { |
| 702 | uid = (uid_t)result; |
| 703 | |
| 704 | if (result == -1) { |
| 705 | if (PyErr_Occurred()) |
| 706 | goto fail; |
| 707 | /* It's a legitimate -1, we're done. */ |
| 708 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 709 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 710 | |
| 711 | /* Any other negative number is disallowed. */ |
| 712 | if (result < 0) |
| 713 | goto underflow; |
| 714 | |
| 715 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 716 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 717 | (long)uid != result) |
| 718 | goto underflow; |
| 719 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 720 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 721 | |
| 722 | if (overflow < 0) |
| 723 | goto underflow; |
| 724 | |
| 725 | /* |
| 726 | * Okay, the value overflowed a signed long. If it |
| 727 | * fits in an *unsigned* long, it may still be okay, |
| 728 | * as uid_t may be unsigned long on this platform. |
| 729 | */ |
| 730 | uresult = PyLong_AsUnsignedLong(index); |
| 731 | if (PyErr_Occurred()) { |
| 732 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 733 | goto overflow; |
| 734 | goto fail; |
| 735 | } |
| 736 | |
| 737 | uid = (uid_t)uresult; |
| 738 | |
| 739 | /* |
| 740 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 741 | * but this value would get interpreted as (uid_t)-1 by chown |
| 742 | * and its siblings. That's not what the user meant! So we |
| 743 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 744 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 745 | */ |
| 746 | if (uid == (uid_t)-1) |
| 747 | goto overflow; |
| 748 | |
| 749 | /* Ensure the value wasn't truncated. */ |
| 750 | if (sizeof(uid_t) < sizeof(long) && |
| 751 | (unsigned long)uid != uresult) |
| 752 | goto overflow; |
| 753 | /* fallthrough */ |
| 754 | |
| 755 | success: |
| 756 | Py_DECREF(index); |
| 757 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 758 | return 1; |
| 759 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 760 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 761 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 762 | "uid is less than minimum"); |
| 763 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 764 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 765 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 766 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 767 | "uid is greater than maximum"); |
| 768 | /* fallthrough */ |
| 769 | |
| 770 | fail: |
| 771 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | int |
| 776 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 777 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 778 | gid_t gid; |
| 779 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 780 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 781 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 782 | unsigned long uresult; |
| 783 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 784 | index = _PyNumber_Index(obj); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 785 | if (index == NULL) { |
| 786 | PyErr_Format(PyExc_TypeError, |
| 787 | "gid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 788 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 789 | return 0; |
| 790 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 791 | |
| 792 | /* |
| 793 | * Handling gid_t is complicated for two reasons: |
| 794 | * * Although gid_t is (always?) unsigned, it still |
| 795 | * accepts -1. |
| 796 | * * We don't know its size in advance--it may be |
| 797 | * bigger than an int, or it may be smaller than |
| 798 | * a long. |
| 799 | * |
| 800 | * So a bit of defensive programming is in order. |
| 801 | * Start with interpreting the value passed |
| 802 | * in as a signed long and see if it works. |
| 803 | */ |
| 804 | |
| 805 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 806 | |
| 807 | if (!overflow) { |
| 808 | gid = (gid_t)result; |
| 809 | |
| 810 | if (result == -1) { |
| 811 | if (PyErr_Occurred()) |
| 812 | goto fail; |
| 813 | /* It's a legitimate -1, we're done. */ |
| 814 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 815 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 816 | |
| 817 | /* Any other negative number is disallowed. */ |
| 818 | if (result < 0) { |
| 819 | goto underflow; |
| 820 | } |
| 821 | |
| 822 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 823 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 824 | (long)gid != result) |
| 825 | goto underflow; |
| 826 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 827 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 828 | |
| 829 | if (overflow < 0) |
| 830 | goto underflow; |
| 831 | |
| 832 | /* |
| 833 | * Okay, the value overflowed a signed long. If it |
| 834 | * fits in an *unsigned* long, it may still be okay, |
| 835 | * as gid_t may be unsigned long on this platform. |
| 836 | */ |
| 837 | uresult = PyLong_AsUnsignedLong(index); |
| 838 | if (PyErr_Occurred()) { |
| 839 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 840 | goto overflow; |
| 841 | goto fail; |
| 842 | } |
| 843 | |
| 844 | gid = (gid_t)uresult; |
| 845 | |
| 846 | /* |
| 847 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 848 | * but this value would get interpreted as (gid_t)-1 by chown |
| 849 | * and its siblings. That's not what the user meant! So we |
| 850 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 851 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 852 | */ |
| 853 | if (gid == (gid_t)-1) |
| 854 | goto overflow; |
| 855 | |
| 856 | /* Ensure the value wasn't truncated. */ |
| 857 | if (sizeof(gid_t) < sizeof(long) && |
| 858 | (unsigned long)gid != uresult) |
| 859 | goto overflow; |
| 860 | /* fallthrough */ |
| 861 | |
| 862 | success: |
| 863 | Py_DECREF(index); |
| 864 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 865 | return 1; |
| 866 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 867 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 868 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 869 | "gid is less than minimum"); |
| 870 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 871 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 872 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 873 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 874 | "gid is greater than maximum"); |
| 875 | /* fallthrough */ |
| 876 | |
| 877 | fail: |
| 878 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 879 | return 0; |
| 880 | } |
| 881 | #endif /* MS_WINDOWS */ |
| 882 | |
| 883 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 884 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 885 | |
| 886 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 887 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 888 | static int |
| 889 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 890 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 891 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 892 | if (PyErr_Occurred()) |
| 893 | return 0; |
| 894 | return 1; |
| 895 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 896 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 897 | |
| 898 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 899 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 900 | /* |
| 901 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 902 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 903 | * which doesn't play nicely with all the initializer lines in this file that |
| 904 | * look like this: |
| 905 | * int dir_fd = DEFAULT_DIR_FD; |
| 906 | */ |
| 907 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 908 | #else |
| 909 | #define DEFAULT_DIR_FD (-100) |
| 910 | #endif |
| 911 | |
| 912 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 913 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 914 | { |
| 915 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 916 | long long_value; |
| 917 | |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 918 | PyObject *index = _PyNumber_Index(o); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 919 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 920 | return 0; |
| 921 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 922 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 923 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 924 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 925 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 926 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 927 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 928 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 929 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 930 | return 0; |
| 931 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 932 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 933 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 934 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 935 | return 0; |
| 936 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 937 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 938 | *p = (int)long_value; |
| 939 | return 1; |
| 940 | } |
| 941 | |
| 942 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 943 | dir_fd_converter(PyObject *o, void *p) |
| 944 | { |
| 945 | if (o == Py_None) { |
| 946 | *(int *)p = DEFAULT_DIR_FD; |
| 947 | return 1; |
| 948 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 949 | else if (PyIndex_Check(o)) { |
| 950 | return _fd_converter(o, (int *)p); |
| 951 | } |
| 952 | else { |
| 953 | PyErr_Format(PyExc_TypeError, |
| 954 | "argument should be integer or None, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 955 | _PyType_Name(Py_TYPE(o))); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 956 | return 0; |
| 957 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 960 | typedef struct { |
| 961 | PyObject *billion; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 962 | PyObject *DirEntryType; |
| 963 | PyObject *ScandirIteratorType; |
| 964 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 965 | PyObject *SchedParamType; |
| 966 | #endif |
| 967 | PyObject *StatResultType; |
| 968 | PyObject *StatVFSResultType; |
| 969 | PyObject *TerminalSizeType; |
| 970 | PyObject *TimesResultType; |
| 971 | PyObject *UnameResultType; |
| 972 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 973 | PyObject *WaitidResultType; |
| 974 | #endif |
| 975 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 976 | PyObject *struct_rusage; |
| 977 | #endif |
| 978 | PyObject *st_mode; |
| 979 | } _posixstate; |
| 980 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 981 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 982 | static inline _posixstate* |
| 983 | get_posix_state(PyObject *module) |
| 984 | { |
| 985 | void *state = PyModule_GetState(module); |
| 986 | assert(state != NULL); |
| 987 | return (_posixstate *)state; |
| 988 | } |
| 989 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 990 | /* |
| 991 | * A PyArg_ParseTuple "converter" function |
| 992 | * that handles filesystem paths in the manner |
| 993 | * preferred by the os module. |
| 994 | * |
| 995 | * path_converter accepts (Unicode) strings and their |
| 996 | * subclasses, and bytes and their subclasses. What |
| 997 | * it does with the argument depends on the platform: |
| 998 | * |
| 999 | * * On Windows, if we get a (Unicode) string we |
| 1000 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1001 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1002 | * |
| 1003 | * * On all other platforms, strings are encoded |
| 1004 | * to bytes using PyUnicode_FSConverter, then we |
| 1005 | * extract the char * from the bytes object and |
| 1006 | * return that. |
| 1007 | * |
| 1008 | * path_converter also optionally accepts signed |
| 1009 | * integers (representing open file descriptors) instead |
| 1010 | * of path strings. |
| 1011 | * |
| 1012 | * Input fields: |
| 1013 | * path.nullable |
| 1014 | * If nonzero, the path is permitted to be None. |
| 1015 | * path.allow_fd |
| 1016 | * If nonzero, the path is permitted to be a file handle |
| 1017 | * (a signed int) instead of a string. |
| 1018 | * path.function_name |
| 1019 | * If non-NULL, path_converter will use that as the name |
| 1020 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1021 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1022 | * path.argument_name |
| 1023 | * If non-NULL, path_converter will use that as the name |
| 1024 | * of the parameter in error messages. |
| 1025 | * (If path.argument_name is NULL it uses "path".) |
| 1026 | * |
| 1027 | * Output fields: |
| 1028 | * path.wide |
| 1029 | * Points to the path if it was expressed as Unicode |
| 1030 | * and was not encoded. (Only used on Windows.) |
| 1031 | * path.narrow |
| 1032 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1033 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 1034 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1035 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1036 | * path.fd |
| 1037 | * Contains a file descriptor if path.accept_fd was true |
| 1038 | * and the caller provided a signed integer instead of any |
| 1039 | * sort of string. |
| 1040 | * |
| 1041 | * WARNING: if your "path" parameter is optional, and is |
| 1042 | * unspecified, path_converter will never get called. |
| 1043 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 1044 | * yourself! |
| 1045 | * path.length |
| 1046 | * The length of the path in characters, if specified as |
| 1047 | * a string. |
| 1048 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1049 | * The original object passed in (if get a PathLike object, |
| 1050 | * the result of PyOS_FSPath() is treated as the original object). |
| 1051 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1052 | * path.cleanup |
| 1053 | * For internal use only. May point to a temporary object. |
| 1054 | * (Pay no attention to the man behind the curtain.) |
| 1055 | * |
| 1056 | * At most one of path.wide or path.narrow will be non-NULL. |
| 1057 | * If path was None and path.nullable was set, |
| 1058 | * or if path was an integer and path.allow_fd was set, |
| 1059 | * both path.wide and path.narrow will be NULL |
| 1060 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 1061 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1062 | * path_converter takes care to not write to the path_t |
| 1063 | * unless it's successful. However it must reset the |
| 1064 | * "cleanup" field each time it's called. |
| 1065 | * |
| 1066 | * Use as follows: |
| 1067 | * path_t path; |
| 1068 | * memset(&path, 0, sizeof(path)); |
| 1069 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 1070 | * // ... use values from path ... |
| 1071 | * path_cleanup(&path); |
| 1072 | * |
| 1073 | * (Note that if PyArg_Parse fails you don't need to call |
| 1074 | * path_cleanup(). However it is safe to do so.) |
| 1075 | */ |
| 1076 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 1077 | const char *function_name; |
| 1078 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1079 | int nullable; |
| 1080 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1081 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1082 | #ifdef MS_WINDOWS |
| 1083 | BOOL narrow; |
| 1084 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1085 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1086 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1087 | int fd; |
| 1088 | Py_ssize_t length; |
| 1089 | PyObject *object; |
| 1090 | PyObject *cleanup; |
| 1091 | } path_t; |
| 1092 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1093 | #ifdef MS_WINDOWS |
| 1094 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 1095 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 1096 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1097 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 1098 | {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] | 1099 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1100 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1101 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1102 | path_cleanup(path_t *path) |
| 1103 | { |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1104 | #if !USE_UNICODE_WCHAR_CACHE |
| 1105 | wchar_t *wide = (wchar_t *)path->wide; |
| 1106 | path->wide = NULL; |
| 1107 | PyMem_Free(wide); |
| 1108 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1109 | Py_CLEAR(path->object); |
| 1110 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1114 | path_converter(PyObject *o, void *p) |
| 1115 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1116 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1117 | PyObject *bytes = NULL; |
| 1118 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1119 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1120 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1121 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1122 | PyObject *wo = NULL; |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1123 | wchar_t *wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1124 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1125 | |
| 1126 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 1127 | PyErr_Format(exc, "%s%s" fmt, \ |
| 1128 | path->function_name ? path->function_name : "", \ |
| 1129 | path->function_name ? ": " : "", \ |
| 1130 | path->argument_name ? path->argument_name : "path") |
| 1131 | |
| 1132 | /* Py_CLEANUP_SUPPORTED support */ |
| 1133 | if (o == NULL) { |
| 1134 | path_cleanup(path); |
| 1135 | return 1; |
| 1136 | } |
| 1137 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1138 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1139 | path->object = path->cleanup = NULL; |
| 1140 | /* path->object owns a reference to the original object */ |
| 1141 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1142 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1143 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1144 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1145 | #ifdef MS_WINDOWS |
| 1146 | path->narrow = FALSE; |
| 1147 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1148 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1149 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1150 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1151 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1154 | /* Only call this here so that we don't treat the return value of |
| 1155 | os.fspath() as an fd or buffer. */ |
| 1156 | is_index = path->allow_fd && PyIndex_Check(o); |
| 1157 | is_buffer = PyObject_CheckBuffer(o); |
| 1158 | is_bytes = PyBytes_Check(o); |
| 1159 | is_unicode = PyUnicode_Check(o); |
| 1160 | |
| 1161 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 1162 | /* Inline PyOS_FSPath() for better error messages. */ |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1163 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1164 | |
| 1165 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 1166 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1167 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1168 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1169 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1170 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1171 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1172 | goto error_exit; |
| 1173 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1174 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1175 | is_unicode = 1; |
| 1176 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1177 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1178 | is_bytes = 1; |
| 1179 | } |
| 1180 | else { |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1181 | PyErr_Format(PyExc_TypeError, |
| 1182 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1183 | "not %.200s", _PyType_Name(Py_TYPE(o)), |
| 1184 | _PyType_Name(Py_TYPE(res))); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1185 | Py_DECREF(res); |
| 1186 | goto error_exit; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1187 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1188 | |
| 1189 | /* still owns a reference to the original object */ |
| 1190 | Py_DECREF(o); |
| 1191 | o = res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1195 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1196 | #if USE_UNICODE_WCHAR_CACHE |
| 1197 | _Py_COMP_DIAG_PUSH |
| 1198 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1199 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1200 | _Py_COMP_DIAG_POP |
| 1201 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 1202 | wide = PyUnicode_AsWideCharString(o, &length); |
| 1203 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1204 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1205 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1206 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1207 | if (length > 32767) { |
| 1208 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1209 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1210 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1211 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1212 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1213 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1214 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1215 | |
| 1216 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1217 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1218 | path->fd = -1; |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1219 | #if !USE_UNICODE_WCHAR_CACHE |
| 1220 | wide = NULL; |
| 1221 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1222 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1223 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1224 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1225 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1226 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1227 | #endif |
| 1228 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1229 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1230 | bytes = o; |
| 1231 | Py_INCREF(bytes); |
| 1232 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1233 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1234 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1235 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1236 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1237 | "%s%s%s should be %s, not %.200s", |
| 1238 | path->function_name ? path->function_name : "", |
| 1239 | path->function_name ? ": " : "", |
| 1240 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1241 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1242 | "integer or None" : |
| 1243 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1244 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1245 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1246 | _PyType_Name(Py_TYPE(o)))) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1247 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1248 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1249 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1250 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1251 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1252 | } |
| 1253 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1254 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1255 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1256 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1257 | } |
| 1258 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1259 | #ifdef MS_WINDOWS |
| 1260 | path->narrow = FALSE; |
| 1261 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1262 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1263 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1264 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1265 | } |
| 1266 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1267 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1268 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1269 | path->function_name ? path->function_name : "", |
| 1270 | path->function_name ? ": " : "", |
| 1271 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1272 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1273 | "integer or None" : |
| 1274 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1275 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1276 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1277 | _PyType_Name(Py_TYPE(o))); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1278 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1281 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1282 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1283 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1284 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1285 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1288 | #ifdef MS_WINDOWS |
| 1289 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1290 | narrow, |
| 1291 | length |
| 1292 | ); |
| 1293 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1294 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1297 | #if USE_UNICODE_WCHAR_CACHE |
| 1298 | _Py_COMP_DIAG_PUSH |
| 1299 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1300 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1301 | _Py_COMP_DIAG_POP |
| 1302 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 1303 | wide = PyUnicode_AsWideCharString(wo, &length); |
| 1304 | Py_DECREF(wo); |
| 1305 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1306 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1307 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1308 | } |
| 1309 | if (length > 32767) { |
| 1310 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1311 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1312 | } |
| 1313 | if (wcslen(wide) != length) { |
| 1314 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1315 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1316 | } |
| 1317 | path->wide = wide; |
| 1318 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1319 | Py_DECREF(bytes); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1320 | #if USE_UNICODE_WCHAR_CACHE |
| 1321 | path->cleanup = wo; |
| 1322 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 1323 | wide = NULL; |
| 1324 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1325 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1326 | path->wide = NULL; |
| 1327 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1328 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1329 | /* Still a reference owned by path->object, don't have to |
| 1330 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1331 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1332 | } |
| 1333 | else { |
| 1334 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1335 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1336 | #endif |
| 1337 | path->fd = -1; |
| 1338 | |
| 1339 | success_exit: |
| 1340 | path->length = length; |
| 1341 | path->object = o; |
| 1342 | return Py_CLEANUP_SUPPORTED; |
| 1343 | |
| 1344 | error_exit: |
| 1345 | Py_XDECREF(o); |
| 1346 | Py_XDECREF(bytes); |
| 1347 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1348 | #if USE_UNICODE_WCHAR_CACHE |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1349 | Py_XDECREF(wo); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1350 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 1351 | PyMem_Free(wide); |
| 1352 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1353 | #endif |
| 1354 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1358 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1359 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1360 | PyErr_Format(PyExc_NotImplementedError, |
| 1361 | "%s%s%s unavailable on this platform", |
| 1362 | (function_name != NULL) ? function_name : "", |
| 1363 | (function_name != NULL) ? ": ": "", |
| 1364 | argument_name); |
| 1365 | } |
| 1366 | |
| 1367 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1368 | dir_fd_unavailable(PyObject *o, void *p) |
| 1369 | { |
| 1370 | int dir_fd; |
| 1371 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1372 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1373 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1374 | argument_unavailable_error(NULL, "dir_fd"); |
| 1375 | return 0; |
| 1376 | } |
| 1377 | *(int *)p = dir_fd; |
| 1378 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1382 | fd_specified(const char *function_name, int fd) |
| 1383 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1384 | if (fd == -1) |
| 1385 | return 0; |
| 1386 | |
| 1387 | argument_unavailable_error(function_name, "fd"); |
| 1388 | return 1; |
| 1389 | } |
| 1390 | |
| 1391 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1392 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1393 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1394 | if (follow_symlinks) |
| 1395 | return 0; |
| 1396 | |
| 1397 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1398 | return 1; |
| 1399 | } |
| 1400 | |
| 1401 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1402 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1403 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1404 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1405 | #ifndef MS_WINDOWS |
| 1406 | && !path->narrow |
| 1407 | #endif |
| 1408 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1409 | PyErr_Format(PyExc_ValueError, |
| 1410 | "%s: can't specify dir_fd without matching path", |
| 1411 | function_name); |
| 1412 | return 1; |
| 1413 | } |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1418 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1419 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1420 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1421 | PyErr_Format(PyExc_ValueError, |
| 1422 | "%s: can't specify both dir_fd and fd", |
| 1423 | function_name); |
| 1424 | return 1; |
| 1425 | } |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1430 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1431 | int follow_symlinks) |
| 1432 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1433 | if ((fd > 0) && (!follow_symlinks)) { |
| 1434 | PyErr_Format(PyExc_ValueError, |
| 1435 | "%s: cannot use fd and follow_symlinks together", |
| 1436 | function_name); |
| 1437 | return 1; |
| 1438 | } |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1443 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1444 | int follow_symlinks) |
| 1445 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1446 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1447 | PyErr_Format(PyExc_ValueError, |
| 1448 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1449 | function_name); |
| 1450 | return 1; |
| 1451 | } |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1455 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1456 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1457 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1458 | typedef off_t Py_off_t; |
| 1459 | #endif |
| 1460 | |
| 1461 | static int |
| 1462 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1463 | { |
| 1464 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1465 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1466 | #else |
| 1467 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1468 | #endif |
| 1469 | if (PyErr_Occurred()) |
| 1470 | return 0; |
| 1471 | return 1; |
| 1472 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1473 | |
| 1474 | static PyObject * |
| 1475 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1476 | { |
| 1477 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1478 | return PyLong_FromLongLong(offset); |
| 1479 | #else |
| 1480 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1481 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1482 | } |
| 1483 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1484 | #ifdef HAVE_SIGSET_T |
| 1485 | /* Convert an iterable of integers to a sigset. |
| 1486 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1487 | int |
| 1488 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1489 | { |
| 1490 | sigset_t *mask = (sigset_t *)addr; |
| 1491 | PyObject *iterator, *item; |
| 1492 | long signum; |
| 1493 | int overflow; |
| 1494 | |
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1495 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
| 1496 | if (sigemptyset(mask) < (0)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1497 | /* Probably only if mask == NULL. */ |
| 1498 | PyErr_SetFromErrno(PyExc_OSError); |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | iterator = PyObject_GetIter(obj); |
| 1503 | if (iterator == NULL) { |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1508 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1509 | Py_DECREF(item); |
| 1510 | if (signum <= 0 || signum >= NSIG) { |
| 1511 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1512 | PyErr_Format(PyExc_ValueError, |
| 1513 | "signal number %ld out of range", signum); |
| 1514 | } |
| 1515 | goto error; |
| 1516 | } |
| 1517 | if (sigaddset(mask, (int)signum)) { |
| 1518 | if (errno != EINVAL) { |
| 1519 | /* Probably impossible */ |
| 1520 | PyErr_SetFromErrno(PyExc_OSError); |
| 1521 | goto error; |
| 1522 | } |
| 1523 | /* For backwards compatibility, allow idioms such as |
| 1524 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1525 | */ |
| 1526 | const char msg[] = |
| 1527 | "invalid signal number %ld, please use valid_signals()"; |
| 1528 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1529 | goto error; |
| 1530 | } |
| 1531 | } |
| 1532 | } |
| 1533 | if (!PyErr_Occurred()) { |
| 1534 | Py_DECREF(iterator); |
| 1535 | return 1; |
| 1536 | } |
| 1537 | |
| 1538 | error: |
| 1539 | Py_DECREF(iterator); |
| 1540 | return 0; |
| 1541 | } |
| 1542 | #endif /* HAVE_SIGSET_T */ |
| 1543 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1544 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1545 | |
| 1546 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1547 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1548 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1549 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1550 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1551 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1552 | |
| 1553 | if (0 == DeviceIoControl( |
| 1554 | reparse_point_handle, |
| 1555 | FSCTL_GET_REPARSE_POINT, |
| 1556 | NULL, 0, /* in buffer */ |
| 1557 | target_buffer, sizeof(target_buffer), |
| 1558 | &n_bytes_returned, |
| 1559 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1560 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1561 | |
| 1562 | if (reparse_tag) |
| 1563 | *reparse_tag = rdb->ReparseTag; |
| 1564 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1565 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1566 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1567 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1568 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1569 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1570 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1571 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1572 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1573 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1574 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1575 | */ |
| 1576 | #include <crt_externs.h> |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1577 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1578 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1579 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1580 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1581 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1582 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1583 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1584 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1585 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1586 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1587 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1588 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1589 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1590 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1591 | d = PyDict_New(); |
| 1592 | if (d == NULL) |
| 1593 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1594 | #ifdef MS_WINDOWS |
| 1595 | /* _wenviron must be initialized in this way if the program is started |
| 1596 | through main() instead of wmain(). */ |
| 1597 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1598 | e = _wenviron; |
Benoit Hudson | 723f71a | 2019-12-06 14:15:03 -0500 | [diff] [blame] | 1599 | #elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
| 1600 | /* environ is not accessible as an extern in a shared object on OSX; use |
| 1601 | _NSGetEnviron to resolve it. The value changes if you add environment |
| 1602 | variables between calls to Py_Initialize, so don't cache the value. */ |
| 1603 | e = *_NSGetEnviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1604 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1605 | e = environ; |
| 1606 | #endif |
| 1607 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1608 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1609 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1610 | PyObject *k; |
| 1611 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1612 | #ifdef MS_WINDOWS |
| 1613 | const wchar_t *p = wcschr(*e, L'='); |
| 1614 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1615 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1616 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1617 | if (p == NULL) |
| 1618 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1619 | #ifdef MS_WINDOWS |
| 1620 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1621 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1622 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1623 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1624 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1625 | Py_DECREF(d); |
| 1626 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1627 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1628 | #ifdef MS_WINDOWS |
| 1629 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1630 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1631 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1632 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1633 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1634 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1635 | Py_DECREF(d); |
| 1636 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1637 | } |
Serhiy Storchaka | b510e10 | 2020-10-26 12:47:57 +0200 | [diff] [blame] | 1638 | if (PyDict_SetDefault(d, k, v) == NULL) { |
| 1639 | Py_DECREF(v); |
| 1640 | Py_DECREF(k); |
| 1641 | Py_DECREF(d); |
| 1642 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1643 | } |
| 1644 | Py_DECREF(k); |
| 1645 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1646 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1647 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1650 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1651 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1652 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1653 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1654 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1655 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1656 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1657 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1658 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1659 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1660 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1661 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1662 | /* XXX We should pass the function name along in the future. |
| 1663 | (winreg.c also wants to pass the function name.) |
| 1664 | This would however require an additional param to the |
| 1665 | Windows error object, which is non-trivial. |
| 1666 | */ |
| 1667 | errno = GetLastError(); |
| 1668 | if (filename) |
| 1669 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1670 | else |
| 1671 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1672 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1673 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1674 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1675 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1676 | { |
| 1677 | /* XXX - see win32_error for comments on 'function' */ |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1678 | if (filename) |
| 1679 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1680 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1681 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1682 | filename); |
| 1683 | else |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1684 | return PyErr_SetFromWindowsErr(err); |
| 1685 | } |
| 1686 | |
| 1687 | static PyObject * |
| 1688 | win32_error_object(const char* function, PyObject* filename) |
| 1689 | { |
| 1690 | errno = GetLastError(); |
| 1691 | return win32_error_object_err(function, filename, errno); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1692 | } |
| 1693 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1694 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1695 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1696 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1697 | posix_path_object_error(PyObject *path) |
| 1698 | { |
| 1699 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1700 | } |
| 1701 | |
| 1702 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1703 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1704 | { |
| 1705 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1706 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1707 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1708 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1709 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1710 | #endif |
| 1711 | } |
| 1712 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1713 | static PyObject * |
| 1714 | path_object_error2(PyObject *path, PyObject *path2) |
| 1715 | { |
| 1716 | #ifdef MS_WINDOWS |
| 1717 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1718 | PyExc_OSError, 0, path, path2); |
| 1719 | #else |
| 1720 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1721 | #endif |
| 1722 | } |
| 1723 | |
| 1724 | static PyObject * |
| 1725 | path_error(path_t *path) |
| 1726 | { |
| 1727 | return path_object_error(path->object); |
| 1728 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1729 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1730 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1731 | posix_path_error(path_t *path) |
| 1732 | { |
| 1733 | return posix_path_object_error(path->object); |
| 1734 | } |
| 1735 | |
| 1736 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1737 | path_error2(path_t *path, path_t *path2) |
| 1738 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1739 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1743 | /* POSIX generic methods */ |
| 1744 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1745 | static PyObject * |
| 1746 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1747 | { |
| 1748 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1749 | int async_err = 0; |
| 1750 | |
| 1751 | do { |
| 1752 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1753 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1754 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1755 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1756 | Py_END_ALLOW_THREADS |
| 1757 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1758 | if (res != 0) |
| 1759 | return (!async_err) ? posix_error() : NULL; |
| 1760 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1761 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1762 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1763 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1764 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1765 | /* This is a reimplementation of the C library's chdir function, |
| 1766 | but one that produces Win32 errors instead of DOS error codes. |
| 1767 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1768 | it also needs to set "magic" environment variables indicating |
| 1769 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1770 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1771 | win32_wchdir(LPCWSTR path) |
| 1772 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1773 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1774 | int result; |
| 1775 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1776 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1777 | if(!SetCurrentDirectoryW(path)) |
| 1778 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1779 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1780 | if (!result) |
| 1781 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1782 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1783 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1784 | if (!new_path) { |
| 1785 | SetLastError(ERROR_OUTOFMEMORY); |
| 1786 | return FALSE; |
| 1787 | } |
| 1788 | result = GetCurrentDirectoryW(result, new_path); |
| 1789 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1790 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1791 | return FALSE; |
| 1792 | } |
| 1793 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1794 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1795 | wcsncmp(new_path, L"//", 2) == 0); |
| 1796 | if (!is_unc_like_path) { |
| 1797 | env[1] = new_path[0]; |
| 1798 | result = SetEnvironmentVariableW(env, new_path); |
| 1799 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1800 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1801 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1802 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1803 | } |
| 1804 | #endif |
| 1805 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1806 | #ifdef MS_WINDOWS |
| 1807 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1808 | - time stamps are restricted to second resolution |
| 1809 | - file modification times suffer from forth-and-back conversions between |
| 1810 | UTC and local time |
| 1811 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1812 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1813 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1814 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1815 | #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1816 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1817 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1818 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1819 | BY_HANDLE_FILE_INFORMATION *info, |
| 1820 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1821 | { |
| 1822 | memset(info, 0, sizeof(*info)); |
| 1823 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1824 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1825 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1826 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1827 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1828 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1829 | /* info->nNumberOfLinks = 1; */ |
| 1830 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1831 | *reparse_tag = pFileData->dwReserved0; |
| 1832 | else |
| 1833 | *reparse_tag = 0; |
| 1834 | } |
| 1835 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1836 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1837 | 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] | 1838 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1839 | HANDLE hFindFile; |
| 1840 | WIN32_FIND_DATAW FileData; |
| 1841 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1842 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1843 | return FALSE; |
| 1844 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1845 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1846 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1849 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1850 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1851 | BOOL traverse) |
| 1852 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1853 | HANDLE hFile; |
| 1854 | BY_HANDLE_FILE_INFORMATION fileInfo; |
| 1855 | FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 }; |
| 1856 | DWORD fileType, error; |
| 1857 | BOOL isUnhandledTag = FALSE; |
| 1858 | int retval = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1859 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1860 | DWORD access = FILE_READ_ATTRIBUTES; |
| 1861 | DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */ |
| 1862 | if (!traverse) { |
| 1863 | flags |= FILE_FLAG_OPEN_REPARSE_POINT; |
| 1864 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1865 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1866 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1867 | if (hFile == INVALID_HANDLE_VALUE) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1868 | /* Either the path doesn't exist, or the caller lacks access. */ |
| 1869 | error = GetLastError(); |
| 1870 | switch (error) { |
| 1871 | case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */ |
| 1872 | case ERROR_SHARING_VIOLATION: /* It's a paging file. */ |
| 1873 | /* Try reading the parent directory. */ |
| 1874 | if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) { |
| 1875 | /* Cannot read the parent directory. */ |
| 1876 | SetLastError(error); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1877 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1878 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1879 | if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1880 | if (traverse || |
| 1881 | !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1882 | /* The stat call has to traverse but cannot, so fail. */ |
| 1883 | SetLastError(error); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1884 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1885 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1886 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1887 | break; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1888 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1889 | case ERROR_INVALID_PARAMETER: |
| 1890 | /* \\.\con requires read or write access. */ |
| 1891 | hFile = CreateFileW(path, access | GENERIC_READ, |
| 1892 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1893 | OPEN_EXISTING, flags, NULL); |
| 1894 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1895 | SetLastError(error); |
| 1896 | return -1; |
| 1897 | } |
| 1898 | break; |
| 1899 | |
| 1900 | case ERROR_CANT_ACCESS_FILE: |
| 1901 | /* bpo37834: open unhandled reparse points if traverse fails. */ |
| 1902 | if (traverse) { |
| 1903 | traverse = FALSE; |
| 1904 | isUnhandledTag = TRUE; |
| 1905 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, |
| 1906 | flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 1907 | } |
| 1908 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1909 | SetLastError(error); |
| 1910 | return -1; |
| 1911 | } |
| 1912 | break; |
| 1913 | |
| 1914 | default: |
| 1915 | return -1; |
| 1916 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1917 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1918 | |
| 1919 | if (hFile != INVALID_HANDLE_VALUE) { |
| 1920 | /* Handle types other than files on disk. */ |
| 1921 | fileType = GetFileType(hFile); |
| 1922 | if (fileType != FILE_TYPE_DISK) { |
| 1923 | if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) { |
| 1924 | retval = -1; |
| 1925 | goto cleanup; |
| 1926 | } |
| 1927 | DWORD fileAttributes = GetFileAttributesW(path); |
| 1928 | memset(result, 0, sizeof(*result)); |
| 1929 | if (fileAttributes != INVALID_FILE_ATTRIBUTES && |
| 1930 | fileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 1931 | /* \\.\pipe\ or \\.\mailslot\ */ |
| 1932 | result->st_mode = _S_IFDIR; |
| 1933 | } else if (fileType == FILE_TYPE_CHAR) { |
| 1934 | /* \\.\nul */ |
| 1935 | result->st_mode = _S_IFCHR; |
| 1936 | } else if (fileType == FILE_TYPE_PIPE) { |
| 1937 | /* \\.\pipe\spam */ |
| 1938 | result->st_mode = _S_IFIFO; |
| 1939 | } |
| 1940 | /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */ |
| 1941 | goto cleanup; |
| 1942 | } |
| 1943 | |
| 1944 | /* Query the reparse tag, and traverse a non-link. */ |
| 1945 | if (!traverse) { |
| 1946 | if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, |
| 1947 | &tagInfo, sizeof(tagInfo))) { |
| 1948 | /* Allow devices that do not support FileAttributeTagInfo. */ |
| 1949 | switch (GetLastError()) { |
| 1950 | case ERROR_INVALID_PARAMETER: |
| 1951 | case ERROR_INVALID_FUNCTION: |
| 1952 | case ERROR_NOT_SUPPORTED: |
| 1953 | tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL; |
| 1954 | tagInfo.ReparseTag = 0; |
| 1955 | break; |
| 1956 | default: |
| 1957 | retval = -1; |
| 1958 | goto cleanup; |
| 1959 | } |
| 1960 | } else if (tagInfo.FileAttributes & |
| 1961 | FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1962 | if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1963 | if (isUnhandledTag) { |
| 1964 | /* Traversing previously failed for either this link |
| 1965 | or its target. */ |
| 1966 | SetLastError(ERROR_CANT_ACCESS_FILE); |
| 1967 | retval = -1; |
| 1968 | goto cleanup; |
| 1969 | } |
| 1970 | /* Traverse a non-link, but not if traversing already failed |
| 1971 | for an unhandled tag. */ |
| 1972 | } else if (!isUnhandledTag) { |
| 1973 | CloseHandle(hFile); |
| 1974 | return win32_xstat_impl(path, result, TRUE); |
| 1975 | } |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | if (!GetFileInformationByHandle(hFile, &fileInfo)) { |
| 1980 | switch (GetLastError()) { |
| 1981 | case ERROR_INVALID_PARAMETER: |
| 1982 | case ERROR_INVALID_FUNCTION: |
| 1983 | case ERROR_NOT_SUPPORTED: |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1984 | /* Volumes and physical disks are block devices, e.g. |
| 1985 | \\.\C: and \\.\PhysicalDrive0. */ |
| 1986 | memset(result, 0, sizeof(*result)); |
| 1987 | result->st_mode = 0x6000; /* S_IFBLK */ |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1988 | goto cleanup; |
| 1989 | } |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1990 | retval = -1; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1991 | goto cleanup; |
| 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result); |
| 1996 | |
| 1997 | if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1998 | /* Fix the file execute permissions. This hack sets S_IEXEC if |
| 1999 | the filename has an extension that is commonly used by files |
| 2000 | that CreateProcessW can execute. A real implementation calls |
| 2001 | GetSecurityInfo, OpenThreadToken/OpenProcessToken, and |
| 2002 | AccessCheck to check for generic read, write, and execute |
| 2003 | access. */ |
| 2004 | const wchar_t *fileExtension = wcsrchr(path, '.'); |
| 2005 | if (fileExtension) { |
| 2006 | if (_wcsicmp(fileExtension, L".exe") == 0 || |
| 2007 | _wcsicmp(fileExtension, L".bat") == 0 || |
| 2008 | _wcsicmp(fileExtension, L".cmd") == 0 || |
| 2009 | _wcsicmp(fileExtension, L".com") == 0) { |
| 2010 | result->st_mode |= 0111; |
| 2011 | } |
| 2012 | } |
| 2013 | } |
| 2014 | |
| 2015 | cleanup: |
| 2016 | if (hFile != INVALID_HANDLE_VALUE) { |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 2017 | /* Preserve last error if we are failing */ |
| 2018 | error = retval ? GetLastError() : 0; |
| 2019 | if (!CloseHandle(hFile)) { |
| 2020 | retval = -1; |
| 2021 | } else if (retval) { |
| 2022 | /* Restore last error */ |
| 2023 | SetLastError(error); |
| 2024 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | return retval; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2031 | 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] | 2032 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 2033 | /* Protocol violation: we explicitly clear errno, instead of |
| 2034 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 2035 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 2036 | errno = 0; |
| 2037 | return code; |
| 2038 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 2039 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2040 | |
| 2041 | In Posix, stat automatically traverses symlinks and returns the stat |
| 2042 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 2043 | default does not traverse symlinks and instead returns attributes for |
| 2044 | the symlink. |
| 2045 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2046 | Instead, we will open the file (which *does* traverse symlinks by default) |
| 2047 | and GetFileInformationByHandle(). */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2048 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 2049 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2050 | 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] | 2051 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 2052 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2055 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2056 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2057 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 2058 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2061 | #endif /* MS_WINDOWS */ |
| 2062 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2063 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2064 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2065 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2066 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2067 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 2068 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2069 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 2070 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2071 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2072 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2073 | |
| 2074 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2075 | {"st_mode", "protection bits"}, |
| 2076 | {"st_ino", "inode"}, |
| 2077 | {"st_dev", "device"}, |
| 2078 | {"st_nlink", "number of hard links"}, |
| 2079 | {"st_uid", "user ID of owner"}, |
| 2080 | {"st_gid", "group ID of owner"}, |
| 2081 | {"st_size", "total size, in bytes"}, |
| 2082 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 2083 | {NULL, "integer time of last access"}, |
| 2084 | {NULL, "integer time of last modification"}, |
| 2085 | {NULL, "integer time of last change"}, |
| 2086 | {"st_atime", "time of last access"}, |
| 2087 | {"st_mtime", "time of last modification"}, |
| 2088 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2089 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 2090 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 2091 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2092 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2093 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2094 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2095 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2096 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2097 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2098 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2099 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2100 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2101 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2102 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2103 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2104 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2105 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2106 | #endif |
| 2107 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2108 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2109 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2110 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2111 | {"st_file_attributes", "Windows file attribute bits"}, |
| 2112 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2113 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2114 | {"st_fstype", "Type of filesystem"}, |
| 2115 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2116 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2117 | {"st_reparse_tag", "Windows reparse tag"}, |
| 2118 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2119 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2120 | }; |
| 2121 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2122 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2123 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2124 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2125 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2126 | #endif |
| 2127 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2128 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2129 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 2130 | #else |
| 2131 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 2132 | #endif |
| 2133 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2134 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2135 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 2136 | #else |
| 2137 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 2138 | #endif |
| 2139 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2140 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 2141 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 2142 | #else |
| 2143 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 2144 | #endif |
| 2145 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2146 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2147 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2148 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2149 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2150 | #endif |
| 2151 | |
| 2152 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 2153 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 2154 | #else |
| 2155 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 2156 | #endif |
| 2157 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2158 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2159 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 2160 | #else |
| 2161 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 2162 | #endif |
| 2163 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2164 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2165 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 2166 | #else |
| 2167 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 2168 | #endif |
| 2169 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2170 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2171 | #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1) |
| 2172 | #else |
| 2173 | #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX |
| 2174 | #endif |
| 2175 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2176 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2177 | "stat_result", /* name */ |
| 2178 | stat_result__doc__, /* doc */ |
| 2179 | stat_result_fields, |
| 2180 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2181 | }; |
| 2182 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2183 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2184 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2185 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2186 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2187 | 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] | 2188 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2189 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2190 | |
| 2191 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2192 | {"f_bsize", }, |
| 2193 | {"f_frsize", }, |
| 2194 | {"f_blocks", }, |
| 2195 | {"f_bfree", }, |
| 2196 | {"f_bavail", }, |
| 2197 | {"f_files", }, |
| 2198 | {"f_ffree", }, |
| 2199 | {"f_favail", }, |
| 2200 | {"f_flag", }, |
| 2201 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 2202 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2203 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2204 | }; |
| 2205 | |
| 2206 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2207 | "statvfs_result", /* name */ |
| 2208 | statvfs_result__doc__, /* doc */ |
| 2209 | statvfs_result_fields, |
| 2210 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2211 | }; |
| 2212 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2213 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2214 | PyDoc_STRVAR(waitid_result__doc__, |
| 2215 | "waitid_result: Result from waitid.\n\n\ |
| 2216 | This object may be accessed either as a tuple of\n\ |
| 2217 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2218 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2219 | \n\ |
| 2220 | See os.waitid for more information."); |
| 2221 | |
| 2222 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2223 | {"si_pid", }, |
| 2224 | {"si_uid", }, |
| 2225 | {"si_signo", }, |
| 2226 | {"si_status", }, |
| 2227 | {"si_code", }, |
| 2228 | {0} |
| 2229 | }; |
| 2230 | |
| 2231 | static PyStructSequence_Desc waitid_result_desc = { |
| 2232 | "waitid_result", /* name */ |
| 2233 | waitid_result__doc__, /* doc */ |
| 2234 | waitid_result_fields, |
| 2235 | 5 |
| 2236 | }; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2237 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2238 | static newfunc structseq_new; |
| 2239 | |
| 2240 | static PyObject * |
| 2241 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2242 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2243 | PyStructSequence *result; |
| 2244 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2245 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2246 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2247 | if (!result) |
| 2248 | return NULL; |
| 2249 | /* If we have been initialized from a tuple, |
| 2250 | st_?time might be set to None. Initialize it |
| 2251 | from the int slots. */ |
| 2252 | for (i = 7; i <= 9; i++) { |
| 2253 | if (result->ob_item[i+3] == Py_None) { |
| 2254 | Py_DECREF(Py_None); |
| 2255 | Py_INCREF(result->ob_item[i]); |
| 2256 | result->ob_item[i+3] = result->ob_item[i]; |
| 2257 | } |
| 2258 | } |
| 2259 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2262 | static int |
| 2263 | _posix_clear(PyObject *module) |
| 2264 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2265 | _posixstate *state = get_posix_state(module); |
| 2266 | Py_CLEAR(state->billion); |
| 2267 | Py_CLEAR(state->DirEntryType); |
| 2268 | Py_CLEAR(state->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2269 | #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] | 2270 | Py_CLEAR(state->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2271 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2272 | Py_CLEAR(state->StatResultType); |
| 2273 | Py_CLEAR(state->StatVFSResultType); |
| 2274 | Py_CLEAR(state->TerminalSizeType); |
| 2275 | Py_CLEAR(state->TimesResultType); |
| 2276 | Py_CLEAR(state->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2277 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2278 | Py_CLEAR(state->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2279 | #endif |
| 2280 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2281 | Py_CLEAR(state->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2282 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2283 | Py_CLEAR(state->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2284 | return 0; |
| 2285 | } |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2286 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2287 | static int |
| 2288 | _posix_traverse(PyObject *module, visitproc visit, void *arg) |
| 2289 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2290 | _posixstate *state = get_posix_state(module); |
| 2291 | Py_VISIT(state->billion); |
| 2292 | Py_VISIT(state->DirEntryType); |
| 2293 | Py_VISIT(state->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2294 | #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] | 2295 | Py_VISIT(state->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2296 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2297 | Py_VISIT(state->StatResultType); |
| 2298 | Py_VISIT(state->StatVFSResultType); |
| 2299 | Py_VISIT(state->TerminalSizeType); |
| 2300 | Py_VISIT(state->TimesResultType); |
| 2301 | Py_VISIT(state->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2302 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2303 | Py_VISIT(state->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2304 | #endif |
| 2305 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2306 | Py_VISIT(state->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2307 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 2308 | Py_VISIT(state->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2309 | return 0; |
| 2310 | } |
| 2311 | |
| 2312 | static void |
| 2313 | _posix_free(void *module) |
| 2314 | { |
| 2315 | _posix_clear((PyObject *)module); |
| 2316 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2317 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2318 | static void |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2319 | 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] | 2320 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2321 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2322 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2323 | PyObject *s_in_ns = NULL; |
| 2324 | PyObject *ns_total = NULL; |
| 2325 | PyObject *float_s = NULL; |
| 2326 | |
| 2327 | if (!(s && ns_fractional)) |
| 2328 | goto exit; |
| 2329 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2330 | s_in_ns = PyNumber_Multiply(s, get_posix_state(module)->billion); |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2331 | if (!s_in_ns) |
| 2332 | goto exit; |
| 2333 | |
| 2334 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2335 | if (!ns_total) |
| 2336 | goto exit; |
| 2337 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2338 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2339 | if (!float_s) { |
| 2340 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | PyStructSequence_SET_ITEM(v, index, s); |
| 2344 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2345 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2346 | s = NULL; |
| 2347 | float_s = NULL; |
| 2348 | ns_total = NULL; |
| 2349 | exit: |
| 2350 | Py_XDECREF(s); |
| 2351 | Py_XDECREF(ns_fractional); |
| 2352 | Py_XDECREF(s_in_ns); |
| 2353 | Py_XDECREF(ns_total); |
| 2354 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2357 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2358 | (used by posix_stat() and posix_fstat()) */ |
| 2359 | static PyObject* |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2360 | _pystat_fromstructstat(PyObject *module, STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2361 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2362 | unsigned long ansec, mnsec, cnsec; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2363 | PyObject *StatResultType = get_posix_state(module)->StatResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2364 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2365 | if (v == NULL) |
| 2366 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2367 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2368 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2369 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2370 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2371 | #ifdef MS_WINDOWS |
| 2372 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2373 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2374 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2375 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2376 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2377 | #if defined(MS_WINDOWS) |
| 2378 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2379 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2380 | #else |
| 2381 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2382 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2383 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2384 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2385 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2386 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2387 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2388 | ansec = st->st_atim.tv_nsec; |
| 2389 | mnsec = st->st_mtim.tv_nsec; |
| 2390 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2391 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2392 | ansec = st->st_atimespec.tv_nsec; |
| 2393 | mnsec = st->st_mtimespec.tv_nsec; |
| 2394 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2395 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2396 | ansec = st->st_atime_nsec; |
| 2397 | mnsec = st->st_mtime_nsec; |
| 2398 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2399 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2400 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2401 | #endif |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2402 | fill_time(module, v, 7, st->st_atime, ansec); |
| 2403 | fill_time(module, v, 8, st->st_mtime, mnsec); |
| 2404 | fill_time(module, v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2405 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2406 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2407 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2408 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2409 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2410 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2411 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2412 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2413 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2414 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2415 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2416 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2417 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2418 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2419 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2420 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2421 | #endif |
| 2422 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2423 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2424 | PyObject *val; |
| 2425 | unsigned long bsec,bnsec; |
| 2426 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2427 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2428 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2429 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2430 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2431 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2432 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2433 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2434 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2435 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2436 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2437 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2438 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2439 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2440 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2441 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2442 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2443 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2444 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2445 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2446 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2447 | PyUnicode_FromString(st->st_fstype)); |
| 2448 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2449 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2450 | PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX, |
| 2451 | PyLong_FromUnsignedLong(st->st_reparse_tag)); |
| 2452 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2453 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2454 | if (PyErr_Occurred()) { |
| 2455 | Py_DECREF(v); |
| 2456 | return NULL; |
| 2457 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2458 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2459 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2462 | /* POSIX methods */ |
| 2463 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2464 | |
| 2465 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2466 | posix_do_stat(PyObject *module, const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2467 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2468 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2469 | STRUCT_STAT st; |
| 2470 | int result; |
| 2471 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 2472 | #ifdef HAVE_FSTATAT |
| 2473 | int fstatat_unavailable = 0; |
| 2474 | #endif |
| 2475 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2476 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2477 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2478 | return NULL; |
| 2479 | #endif |
| 2480 | |
| 2481 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2482 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2483 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2484 | return NULL; |
| 2485 | |
| 2486 | Py_BEGIN_ALLOW_THREADS |
| 2487 | if (path->fd != -1) |
| 2488 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2489 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2490 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2491 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2492 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2493 | result = win32_lstat(path->wide, &st); |
| 2494 | #else |
| 2495 | else |
| 2496 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2497 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2498 | result = LSTAT(path->narrow, &st); |
| 2499 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2500 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2501 | #ifdef HAVE_FSTATAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 2502 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 2503 | if (HAVE_FSTATAT_RUNTIME) { |
| 2504 | result = fstatat(dir_fd, path->narrow, &st, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2505 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 2506 | |
| 2507 | } else { |
| 2508 | fstatat_unavailable = 1; |
| 2509 | } |
| 2510 | } else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2511 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2512 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2513 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2514 | Py_END_ALLOW_THREADS |
| 2515 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 2516 | #ifdef HAVE_FSTATAT |
| 2517 | if (fstatat_unavailable) { |
| 2518 | argument_unavailable_error("stat", "dir_fd"); |
| 2519 | return NULL; |
| 2520 | } |
| 2521 | #endif |
| 2522 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2523 | if (result != 0) { |
| 2524 | return path_error(path); |
| 2525 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2526 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2527 | return _pystat_fromstructstat(module, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2528 | } |
| 2529 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2530 | /*[python input] |
| 2531 | |
| 2532 | for s in """ |
| 2533 | |
| 2534 | FACCESSAT |
| 2535 | FCHMODAT |
| 2536 | FCHOWNAT |
| 2537 | FSTATAT |
| 2538 | LINKAT |
| 2539 | MKDIRAT |
| 2540 | MKFIFOAT |
| 2541 | MKNODAT |
| 2542 | OPENAT |
| 2543 | READLINKAT |
| 2544 | SYMLINKAT |
| 2545 | UNLINKAT |
| 2546 | |
| 2547 | """.strip().split(): |
| 2548 | s = s.strip() |
| 2549 | print(""" |
| 2550 | #ifdef HAVE_{s} |
| 2551 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2552 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2553 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2554 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2555 | """.rstrip().format(s=s)) |
| 2556 | |
| 2557 | for s in """ |
| 2558 | |
| 2559 | FCHDIR |
| 2560 | FCHMOD |
| 2561 | FCHOWN |
| 2562 | FDOPENDIR |
| 2563 | FEXECVE |
| 2564 | FPATHCONF |
| 2565 | FSTATVFS |
| 2566 | FTRUNCATE |
| 2567 | |
| 2568 | """.strip().split(): |
| 2569 | s = s.strip() |
| 2570 | print(""" |
| 2571 | #ifdef HAVE_{s} |
| 2572 | #define PATH_HAVE_{s} 1 |
| 2573 | #else |
| 2574 | #define PATH_HAVE_{s} 0 |
| 2575 | #endif |
| 2576 | |
| 2577 | """.rstrip().format(s=s)) |
| 2578 | [python start generated code]*/ |
| 2579 | |
| 2580 | #ifdef HAVE_FACCESSAT |
| 2581 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2582 | #else |
| 2583 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2584 | #endif |
| 2585 | |
| 2586 | #ifdef HAVE_FCHMODAT |
| 2587 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2588 | #else |
| 2589 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2590 | #endif |
| 2591 | |
| 2592 | #ifdef HAVE_FCHOWNAT |
| 2593 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2594 | #else |
| 2595 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2596 | #endif |
| 2597 | |
| 2598 | #ifdef HAVE_FSTATAT |
| 2599 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2600 | #else |
| 2601 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2602 | #endif |
| 2603 | |
| 2604 | #ifdef HAVE_LINKAT |
| 2605 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2606 | #else |
| 2607 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2608 | #endif |
| 2609 | |
| 2610 | #ifdef HAVE_MKDIRAT |
| 2611 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2612 | #else |
| 2613 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2614 | #endif |
| 2615 | |
| 2616 | #ifdef HAVE_MKFIFOAT |
| 2617 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2618 | #else |
| 2619 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2620 | #endif |
| 2621 | |
| 2622 | #ifdef HAVE_MKNODAT |
| 2623 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2624 | #else |
| 2625 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2626 | #endif |
| 2627 | |
| 2628 | #ifdef HAVE_OPENAT |
| 2629 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2630 | #else |
| 2631 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2632 | #endif |
| 2633 | |
| 2634 | #ifdef HAVE_READLINKAT |
| 2635 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2636 | #else |
| 2637 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2638 | #endif |
| 2639 | |
| 2640 | #ifdef HAVE_SYMLINKAT |
| 2641 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2642 | #else |
| 2643 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2644 | #endif |
| 2645 | |
| 2646 | #ifdef HAVE_UNLINKAT |
| 2647 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2648 | #else |
| 2649 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2650 | #endif |
| 2651 | |
| 2652 | #ifdef HAVE_FCHDIR |
| 2653 | #define PATH_HAVE_FCHDIR 1 |
| 2654 | #else |
| 2655 | #define PATH_HAVE_FCHDIR 0 |
| 2656 | #endif |
| 2657 | |
| 2658 | #ifdef HAVE_FCHMOD |
| 2659 | #define PATH_HAVE_FCHMOD 1 |
| 2660 | #else |
| 2661 | #define PATH_HAVE_FCHMOD 0 |
| 2662 | #endif |
| 2663 | |
| 2664 | #ifdef HAVE_FCHOWN |
| 2665 | #define PATH_HAVE_FCHOWN 1 |
| 2666 | #else |
| 2667 | #define PATH_HAVE_FCHOWN 0 |
| 2668 | #endif |
| 2669 | |
| 2670 | #ifdef HAVE_FDOPENDIR |
| 2671 | #define PATH_HAVE_FDOPENDIR 1 |
| 2672 | #else |
| 2673 | #define PATH_HAVE_FDOPENDIR 0 |
| 2674 | #endif |
| 2675 | |
| 2676 | #ifdef HAVE_FEXECVE |
| 2677 | #define PATH_HAVE_FEXECVE 1 |
| 2678 | #else |
| 2679 | #define PATH_HAVE_FEXECVE 0 |
| 2680 | #endif |
| 2681 | |
| 2682 | #ifdef HAVE_FPATHCONF |
| 2683 | #define PATH_HAVE_FPATHCONF 1 |
| 2684 | #else |
| 2685 | #define PATH_HAVE_FPATHCONF 0 |
| 2686 | #endif |
| 2687 | |
| 2688 | #ifdef HAVE_FSTATVFS |
| 2689 | #define PATH_HAVE_FSTATVFS 1 |
| 2690 | #else |
| 2691 | #define PATH_HAVE_FSTATVFS 0 |
| 2692 | #endif |
| 2693 | |
| 2694 | #ifdef HAVE_FTRUNCATE |
| 2695 | #define PATH_HAVE_FTRUNCATE 1 |
| 2696 | #else |
| 2697 | #define PATH_HAVE_FTRUNCATE 0 |
| 2698 | #endif |
| 2699 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2700 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2701 | #ifdef MS_WINDOWS |
| 2702 | #undef PATH_HAVE_FTRUNCATE |
| 2703 | #define PATH_HAVE_FTRUNCATE 1 |
| 2704 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2705 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2706 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2707 | |
| 2708 | class path_t_converter(CConverter): |
| 2709 | |
| 2710 | type = "path_t" |
| 2711 | impl_by_reference = True |
| 2712 | parse_by_reference = True |
| 2713 | |
| 2714 | converter = 'path_converter' |
| 2715 | |
| 2716 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2717 | # right now path_t doesn't support default values. |
| 2718 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2719 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2720 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2721 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2722 | if self.c_default not in (None, 'Py_None'): |
| 2723 | 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] | 2724 | |
| 2725 | self.nullable = nullable |
| 2726 | self.allow_fd = allow_fd |
| 2727 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2728 | def pre_render(self): |
| 2729 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2730 | if isinstance(value, str): |
| 2731 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2732 | return str(int(bool(value))) |
| 2733 | |
| 2734 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2735 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2736 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2737 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2738 | strify(self.nullable), |
| 2739 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2740 | ) |
| 2741 | |
| 2742 | def cleanup(self): |
| 2743 | return "path_cleanup(&" + self.name + ");\n" |
| 2744 | |
| 2745 | |
| 2746 | class dir_fd_converter(CConverter): |
| 2747 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2748 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2749 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2750 | if self.default in (unspecified, None): |
| 2751 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2752 | if isinstance(requires, str): |
| 2753 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2754 | else: |
| 2755 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2756 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2757 | class uid_t_converter(CConverter): |
| 2758 | type = "uid_t" |
| 2759 | converter = '_Py_Uid_Converter' |
| 2760 | |
| 2761 | class gid_t_converter(CConverter): |
| 2762 | type = "gid_t" |
| 2763 | converter = '_Py_Gid_Converter' |
| 2764 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2765 | class dev_t_converter(CConverter): |
| 2766 | type = 'dev_t' |
| 2767 | converter = '_Py_Dev_Converter' |
| 2768 | |
| 2769 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2770 | type = 'dev_t' |
| 2771 | conversion_fn = '_PyLong_FromDev' |
| 2772 | unsigned_cast = '(dev_t)' |
| 2773 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2774 | class FSConverter_converter(CConverter): |
| 2775 | type = 'PyObject *' |
| 2776 | converter = 'PyUnicode_FSConverter' |
| 2777 | def converter_init(self): |
| 2778 | if self.default is not unspecified: |
| 2779 | fail("FSConverter_converter does not support default values") |
| 2780 | self.c_default = 'NULL' |
| 2781 | |
| 2782 | def cleanup(self): |
| 2783 | return "Py_XDECREF(" + self.name + ");\n" |
| 2784 | |
| 2785 | class pid_t_converter(CConverter): |
| 2786 | type = 'pid_t' |
| 2787 | format_unit = '" _Py_PARSE_PID "' |
| 2788 | |
| 2789 | class idtype_t_converter(int_converter): |
| 2790 | type = 'idtype_t' |
| 2791 | |
| 2792 | class id_t_converter(CConverter): |
| 2793 | type = 'id_t' |
| 2794 | format_unit = '" _Py_PARSE_PID "' |
| 2795 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2796 | class intptr_t_converter(CConverter): |
| 2797 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2798 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2799 | |
| 2800 | class Py_off_t_converter(CConverter): |
| 2801 | type = 'Py_off_t' |
| 2802 | converter = 'Py_off_t_converter' |
| 2803 | |
| 2804 | class Py_off_t_return_converter(long_return_converter): |
| 2805 | type = 'Py_off_t' |
| 2806 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2807 | |
| 2808 | class path_confname_converter(CConverter): |
| 2809 | type="int" |
| 2810 | converter="conv_path_confname" |
| 2811 | |
| 2812 | class confstr_confname_converter(path_confname_converter): |
| 2813 | converter='conv_confstr_confname' |
| 2814 | |
| 2815 | class sysconf_confname_converter(path_confname_converter): |
| 2816 | converter="conv_sysconf_confname" |
| 2817 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2818 | [python start generated code]*/ |
Serhiy Storchaka | 9975cc5 | 2020-10-09 23:00:45 +0300 | [diff] [blame] | 2819 | /*[python end generated code: output=da39a3ee5e6b4b0d input=3338733161aa7879]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2820 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2821 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2822 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2823 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2824 | |
| 2825 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2826 | 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] | 2827 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2828 | |
| 2829 | * |
| 2830 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2831 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2832 | If not None, it should be a file descriptor open to a directory, |
| 2833 | and path should be a relative string; path will then be relative to |
| 2834 | that directory. |
| 2835 | |
| 2836 | follow_symlinks: bool = True |
| 2837 | If False, and the last element of the path is a symbolic link, |
| 2838 | stat will examine the symbolic link itself instead of the file |
| 2839 | the link points to. |
| 2840 | |
| 2841 | Perform a stat system call on the given path. |
| 2842 | |
| 2843 | dir_fd and follow_symlinks may not be implemented |
| 2844 | on your platform. If they are unavailable, using them will raise a |
| 2845 | NotImplementedError. |
| 2846 | |
| 2847 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2848 | an open file descriptor. |
| 2849 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2850 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2851 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2852 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2853 | 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] | 2854 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2855 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2856 | return posix_do_stat(module, "stat", path, dir_fd, follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2859 | |
| 2860 | /*[clinic input] |
| 2861 | os.lstat |
| 2862 | |
| 2863 | path : path_t |
| 2864 | |
| 2865 | * |
| 2866 | |
| 2867 | dir_fd : dir_fd(requires='fstatat') = None |
| 2868 | |
| 2869 | Perform a stat system call on the given path, without following symbolic links. |
| 2870 | |
| 2871 | Like stat(), but do not follow symbolic links. |
| 2872 | Equivalent to stat(path, follow_symlinks=False). |
| 2873 | [clinic start generated code]*/ |
| 2874 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2875 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2876 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2877 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2878 | { |
| 2879 | int follow_symlinks = 0; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 2880 | return posix_do_stat(module, "lstat", path, dir_fd, follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2881 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2882 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2883 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2884 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2885 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2886 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2887 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2888 | 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] | 2889 | |
| 2890 | mode: int |
| 2891 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2892 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2893 | |
| 2894 | * |
| 2895 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2896 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2897 | If not None, it should be a file descriptor open to a directory, |
| 2898 | and path should be relative; path will then be relative to that |
| 2899 | directory. |
| 2900 | |
| 2901 | effective_ids: bool = False |
| 2902 | If True, access will use the effective uid/gid instead of |
| 2903 | the real uid/gid. |
| 2904 | |
| 2905 | follow_symlinks: bool = True |
| 2906 | If False, and the last element of the path is a symbolic link, |
| 2907 | access will examine the symbolic link itself instead of the file |
| 2908 | the link points to. |
| 2909 | |
| 2910 | Use the real uid/gid to test for access to a path. |
| 2911 | |
| 2912 | {parameters} |
| 2913 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2914 | on your platform. If they are unavailable, using them will raise a |
| 2915 | NotImplementedError. |
| 2916 | |
| 2917 | Note that most operations will use the effective uid/gid, therefore this |
| 2918 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2919 | has the specified access to the path. |
| 2920 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2921 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2922 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2923 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2924 | 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] | 2925 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2926 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2927 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2928 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2929 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2930 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2931 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2932 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2933 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2934 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2935 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 2936 | #ifdef HAVE_FACCESSAT |
| 2937 | int faccessat_unavailable = 0; |
| 2938 | #endif |
| 2939 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2940 | #ifndef HAVE_FACCESSAT |
| 2941 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2942 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2943 | |
| 2944 | if (effective_ids) { |
| 2945 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2946 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2947 | } |
| 2948 | #endif |
| 2949 | |
| 2950 | #ifdef MS_WINDOWS |
| 2951 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2952 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2953 | Py_END_ALLOW_THREADS |
| 2954 | |
| 2955 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2956 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2957 | * * we didn't get a -1, and |
| 2958 | * * write access wasn't requested, |
| 2959 | * * or the file isn't read-only, |
| 2960 | * * or it's a directory. |
| 2961 | * (Directories cannot be read-only on Windows.) |
| 2962 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2963 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2964 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2965 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2966 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2967 | #else |
| 2968 | |
| 2969 | Py_BEGIN_ALLOW_THREADS |
| 2970 | #ifdef HAVE_FACCESSAT |
| 2971 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2972 | effective_ids || |
| 2973 | !follow_symlinks) { |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 2974 | |
| 2975 | if (HAVE_FACCESSAT_RUNTIME) { |
| 2976 | int flags = 0; |
| 2977 | if (!follow_symlinks) |
| 2978 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2979 | if (effective_ids) |
| 2980 | flags |= AT_EACCESS; |
| 2981 | result = faccessat(dir_fd, path->narrow, mode, flags); |
| 2982 | } else { |
| 2983 | faccessat_unavailable = 1; |
| 2984 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2985 | } |
| 2986 | else |
| 2987 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2988 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2989 | Py_END_ALLOW_THREADS |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 2990 | |
| 2991 | #ifdef HAVE_FACCESSAT |
| 2992 | if (faccessat_unavailable) { |
| 2993 | if (dir_fd != DEFAULT_DIR_FD) { |
| 2994 | argument_unavailable_error("access", "dir_fd"); |
| 2995 | return -1; |
| 2996 | } |
| 2997 | if (follow_symlinks_specified("access", follow_symlinks)) |
| 2998 | return -1; |
| 2999 | |
| 3000 | if (effective_ids) { |
| 3001 | argument_unavailable_error("access", "effective_ids"); |
| 3002 | return -1; |
| 3003 | } |
| 3004 | /* should be unreachable */ |
| 3005 | return -1; |
| 3006 | } |
| 3007 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3008 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3009 | #endif |
| 3010 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3011 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 3014 | #ifndef F_OK |
| 3015 | #define F_OK 0 |
| 3016 | #endif |
| 3017 | #ifndef R_OK |
| 3018 | #define R_OK 4 |
| 3019 | #endif |
| 3020 | #ifndef W_OK |
| 3021 | #define W_OK 2 |
| 3022 | #endif |
| 3023 | #ifndef X_OK |
| 3024 | #define X_OK 1 |
| 3025 | #endif |
| 3026 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3027 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 3028 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3029 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 3030 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3031 | |
| 3032 | fd: int |
| 3033 | Integer file descriptor handle. |
| 3034 | |
| 3035 | / |
| 3036 | |
| 3037 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3038 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3039 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 3040 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3041 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 3042 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3043 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3044 | |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 3045 | long size = sysconf(_SC_TTY_NAME_MAX); |
| 3046 | if (size == -1) { |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 3047 | return posix_error(); |
| 3048 | } |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 3049 | char *buffer = (char *)PyMem_RawMalloc(size); |
| 3050 | if (buffer == NULL) { |
| 3051 | return PyErr_NoMemory(); |
| 3052 | } |
| 3053 | int ret = ttyname_r(fd, buffer, size); |
| 3054 | if (ret != 0) { |
| 3055 | PyMem_RawFree(buffer); |
| 3056 | errno = ret; |
| 3057 | return posix_error(); |
| 3058 | } |
| 3059 | PyObject *res = PyUnicode_DecodeFSDefault(buffer); |
| 3060 | PyMem_RawFree(buffer); |
| 3061 | return res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3062 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 3063 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 3064 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3065 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3066 | /*[clinic input] |
| 3067 | os.ctermid |
| 3068 | |
| 3069 | Return the name of the controlling terminal for this process. |
| 3070 | [clinic start generated code]*/ |
| 3071 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3072 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3073 | os_ctermid_impl(PyObject *module) |
| 3074 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3075 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3076 | char *ret; |
| 3077 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3078 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 3079 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3080 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3081 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3082 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3083 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3084 | if (ret == NULL) |
| 3085 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 3086 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3087 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3088 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 3089 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3090 | |
| 3091 | /*[clinic input] |
| 3092 | os.chdir |
| 3093 | |
| 3094 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 3095 | |
| 3096 | Change the current working directory to the specified path. |
| 3097 | |
| 3098 | path may always be specified as a string. |
| 3099 | On some platforms, path may also be specified as an open file descriptor. |
| 3100 | If this functionality is unavailable, using it raises an exception. |
| 3101 | [clinic start generated code]*/ |
| 3102 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3103 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3104 | os_chdir_impl(PyObject *module, path_t *path) |
| 3105 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3106 | { |
| 3107 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3108 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3109 | if (PySys_Audit("os.chdir", "(O)", path->object) < 0) { |
| 3110 | return NULL; |
| 3111 | } |
| 3112 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3113 | Py_BEGIN_ALLOW_THREADS |
| 3114 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3115 | /* on unix, success = 0, on windows, success = !0 */ |
| 3116 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3117 | #else |
| 3118 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3119 | if (path->fd != -1) |
| 3120 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3121 | else |
| 3122 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3123 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3124 | #endif |
| 3125 | Py_END_ALLOW_THREADS |
| 3126 | |
| 3127 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3128 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3129 | } |
| 3130 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3131 | Py_RETURN_NONE; |
| 3132 | } |
| 3133 | |
| 3134 | |
| 3135 | #ifdef HAVE_FCHDIR |
| 3136 | /*[clinic input] |
| 3137 | os.fchdir |
| 3138 | |
| 3139 | fd: fildes |
| 3140 | |
| 3141 | Change to the directory of the given file descriptor. |
| 3142 | |
| 3143 | fd must be opened on a directory, not a file. |
| 3144 | Equivalent to os.chdir(fd). |
| 3145 | |
| 3146 | [clinic start generated code]*/ |
| 3147 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3148 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3149 | os_fchdir_impl(PyObject *module, int fd) |
| 3150 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3151 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3152 | if (PySys_Audit("os.chdir", "(i)", fd) < 0) { |
| 3153 | return NULL; |
| 3154 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3155 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3156 | } |
| 3157 | #endif /* HAVE_FCHDIR */ |
| 3158 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3159 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3160 | /*[clinic input] |
| 3161 | os.chmod |
| 3162 | |
| 3163 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3164 | 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] | 3165 | On some platforms, path may also be specified as an open file descriptor. |
| 3166 | If this functionality is unavailable, using it raises an exception. |
| 3167 | |
| 3168 | mode: int |
| 3169 | Operating-system mode bitfield. |
| 3170 | |
| 3171 | * |
| 3172 | |
| 3173 | dir_fd : dir_fd(requires='fchmodat') = None |
| 3174 | If not None, it should be a file descriptor open to a directory, |
| 3175 | and path should be relative; path will then be relative to that |
| 3176 | directory. |
| 3177 | |
| 3178 | follow_symlinks: bool = True |
| 3179 | If False, and the last element of the path is a symbolic link, |
| 3180 | chmod will modify the symbolic link itself instead of the file |
| 3181 | the link points to. |
| 3182 | |
| 3183 | Change the access permissions of a file. |
| 3184 | |
| 3185 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3186 | an open file descriptor. |
| 3187 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3188 | If they are unavailable, using them will raise a NotImplementedError. |
| 3189 | |
| 3190 | [clinic start generated code]*/ |
| 3191 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3192 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3193 | os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3194 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3195 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3196 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3197 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3198 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3199 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3200 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3201 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3202 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3203 | #ifdef HAVE_FCHMODAT |
| 3204 | int fchmodat_nofollow_unsupported = 0; |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3205 | int fchmodat_unsupported = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3206 | #endif |
| 3207 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3208 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 3209 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3210 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3211 | #endif |
| 3212 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3213 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, |
| 3214 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3215 | return NULL; |
| 3216 | } |
| 3217 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3218 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3219 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3220 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 3221 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3222 | result = 0; |
| 3223 | else { |
| 3224 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3225 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 3226 | else |
| 3227 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3228 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3229 | } |
| 3230 | Py_END_ALLOW_THREADS |
| 3231 | |
| 3232 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3233 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3234 | } |
| 3235 | #else /* MS_WINDOWS */ |
| 3236 | Py_BEGIN_ALLOW_THREADS |
| 3237 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3238 | if (path->fd != -1) |
| 3239 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3240 | else |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3241 | #endif /* HAVE_CHMOD */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3242 | #ifdef HAVE_LCHMOD |
| 3243 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3244 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3245 | else |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3246 | #endif /* HAVE_LCHMOD */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3247 | #ifdef HAVE_FCHMODAT |
| 3248 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3249 | if (HAVE_FCHMODAT_RUNTIME) { |
| 3250 | /* |
| 3251 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 3252 | * The documentation specifically shows how to use it, |
| 3253 | * and then says it isn't implemented yet. |
| 3254 | * (true on linux with glibc 2.15, and openindiana 3.x) |
| 3255 | * |
| 3256 | * Once it is supported, os.chmod will automatically |
| 3257 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 3258 | * Until then, we need to be careful what exception we raise. |
| 3259 | */ |
| 3260 | result = fchmodat(dir_fd, path->narrow, mode, |
| 3261 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3262 | /* |
| 3263 | * But wait! We can't throw the exception without allowing threads, |
| 3264 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 3265 | */ |
| 3266 | fchmodat_nofollow_unsupported = |
| 3267 | result && |
| 3268 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 3269 | !follow_symlinks; |
| 3270 | } else { |
| 3271 | fchmodat_unsupported = 1; |
| 3272 | fchmodat_nofollow_unsupported = 1; |
| 3273 | |
| 3274 | result = -1; |
| 3275 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3276 | } |
| 3277 | else |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3278 | #endif /* HAVE_FHCMODAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3279 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3280 | Py_END_ALLOW_THREADS |
| 3281 | |
| 3282 | if (result) { |
| 3283 | #ifdef HAVE_FCHMODAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3284 | if (fchmodat_unsupported) { |
| 3285 | if (dir_fd != DEFAULT_DIR_FD) { |
| 3286 | argument_unavailable_error("chmod", "dir_fd"); |
| 3287 | return NULL; |
| 3288 | } |
| 3289 | } |
| 3290 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3291 | if (fchmodat_nofollow_unsupported) { |
| 3292 | if (dir_fd != DEFAULT_DIR_FD) |
| 3293 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3294 | dir_fd, follow_symlinks); |
| 3295 | else |
| 3296 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 3297 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3298 | } |
| 3299 | else |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3300 | #endif /* HAVE_FCHMODAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3301 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3302 | } |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3303 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3304 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3305 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3308 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3309 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3310 | /*[clinic input] |
| 3311 | os.fchmod |
| 3312 | |
| 3313 | fd: int |
| 3314 | mode: int |
| 3315 | |
| 3316 | Change the access permissions of the file given by file descriptor fd. |
| 3317 | |
| 3318 | Equivalent to os.chmod(fd, mode). |
| 3319 | [clinic start generated code]*/ |
| 3320 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3321 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3322 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 3323 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3324 | { |
| 3325 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3326 | int async_err = 0; |
| 3327 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3328 | if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) { |
| 3329 | return NULL; |
| 3330 | } |
| 3331 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3332 | do { |
| 3333 | Py_BEGIN_ALLOW_THREADS |
| 3334 | res = fchmod(fd, mode); |
| 3335 | Py_END_ALLOW_THREADS |
| 3336 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3337 | if (res != 0) |
| 3338 | return (!async_err) ? posix_error() : NULL; |
| 3339 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3340 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3341 | } |
| 3342 | #endif /* HAVE_FCHMOD */ |
| 3343 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3344 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3345 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3346 | /*[clinic input] |
| 3347 | os.lchmod |
| 3348 | |
| 3349 | path: path_t |
| 3350 | mode: int |
| 3351 | |
| 3352 | Change the access permissions of a file, without following symbolic links. |
| 3353 | |
| 3354 | If path is a symlink, this affects the link itself rather than the target. |
| 3355 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 3356 | [clinic start generated code]*/ |
| 3357 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3358 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3359 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 3360 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3361 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3362 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3363 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) { |
| 3364 | return NULL; |
| 3365 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3366 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3367 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3368 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3369 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3370 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3371 | return NULL; |
| 3372 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3373 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3374 | } |
| 3375 | #endif /* HAVE_LCHMOD */ |
| 3376 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3377 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3378 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3379 | /*[clinic input] |
| 3380 | os.chflags |
| 3381 | |
| 3382 | path: path_t |
| 3383 | flags: unsigned_long(bitwise=True) |
| 3384 | follow_symlinks: bool=True |
| 3385 | |
| 3386 | Set file flags. |
| 3387 | |
| 3388 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3389 | link, chflags will change flags on the symbolic link itself instead of the |
| 3390 | file the link points to. |
| 3391 | follow_symlinks may not be implemented on your platform. If it is |
| 3392 | unavailable, using it will raise a NotImplementedError. |
| 3393 | |
| 3394 | [clinic start generated code]*/ |
| 3395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3396 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3397 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3398 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3399 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3400 | { |
| 3401 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3402 | |
| 3403 | #ifndef HAVE_LCHFLAGS |
| 3404 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3405 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3406 | #endif |
| 3407 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3408 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3409 | return NULL; |
| 3410 | } |
| 3411 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3412 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3413 | #ifdef HAVE_LCHFLAGS |
| 3414 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3415 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3416 | else |
| 3417 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3418 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3419 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3420 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3421 | if (result) |
| 3422 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3423 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3424 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3425 | } |
| 3426 | #endif /* HAVE_CHFLAGS */ |
| 3427 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3428 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3429 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3430 | /*[clinic input] |
| 3431 | os.lchflags |
| 3432 | |
| 3433 | path: path_t |
| 3434 | flags: unsigned_long(bitwise=True) |
| 3435 | |
| 3436 | Set file flags. |
| 3437 | |
| 3438 | This function will not follow symbolic links. |
| 3439 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3440 | [clinic start generated code]*/ |
| 3441 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3442 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3443 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3444 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3445 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3446 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3447 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3448 | return NULL; |
| 3449 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3450 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3451 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3452 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3453 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3454 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3455 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3456 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3457 | } |
| 3458 | #endif /* HAVE_LCHFLAGS */ |
| 3459 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3460 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3461 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3462 | /*[clinic input] |
| 3463 | os.chroot |
| 3464 | path: path_t |
| 3465 | |
| 3466 | Change root directory to path. |
| 3467 | |
| 3468 | [clinic start generated code]*/ |
| 3469 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3470 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3471 | os_chroot_impl(PyObject *module, path_t *path) |
| 3472 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3473 | { |
| 3474 | int res; |
| 3475 | Py_BEGIN_ALLOW_THREADS |
| 3476 | res = chroot(path->narrow); |
| 3477 | Py_END_ALLOW_THREADS |
| 3478 | if (res < 0) |
| 3479 | return path_error(path); |
| 3480 | Py_RETURN_NONE; |
| 3481 | } |
| 3482 | #endif /* HAVE_CHROOT */ |
| 3483 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3484 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3485 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3486 | /*[clinic input] |
| 3487 | os.fsync |
| 3488 | |
| 3489 | fd: fildes |
| 3490 | |
| 3491 | Force write of fd to disk. |
| 3492 | [clinic start generated code]*/ |
| 3493 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3494 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3495 | os_fsync_impl(PyObject *module, int fd) |
| 3496 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3497 | { |
| 3498 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3499 | } |
| 3500 | #endif /* HAVE_FSYNC */ |
| 3501 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3502 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3503 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3504 | /*[clinic input] |
| 3505 | os.sync |
| 3506 | |
| 3507 | Force write of everything to disk. |
| 3508 | [clinic start generated code]*/ |
| 3509 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3510 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3511 | os_sync_impl(PyObject *module) |
| 3512 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3513 | { |
| 3514 | Py_BEGIN_ALLOW_THREADS |
| 3515 | sync(); |
| 3516 | Py_END_ALLOW_THREADS |
| 3517 | Py_RETURN_NONE; |
| 3518 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3519 | #endif /* HAVE_SYNC */ |
| 3520 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3521 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3522 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3523 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3524 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3525 | #endif |
| 3526 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3527 | /*[clinic input] |
| 3528 | os.fdatasync |
| 3529 | |
| 3530 | fd: fildes |
| 3531 | |
| 3532 | Force write of fd to disk without forcing update of metadata. |
| 3533 | [clinic start generated code]*/ |
| 3534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3535 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3536 | os_fdatasync_impl(PyObject *module, int fd) |
| 3537 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3538 | { |
| 3539 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3540 | } |
| 3541 | #endif /* HAVE_FDATASYNC */ |
| 3542 | |
| 3543 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3544 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3545 | /*[clinic input] |
| 3546 | os.chown |
| 3547 | |
| 3548 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3549 | 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] | 3550 | |
| 3551 | uid: uid_t |
| 3552 | |
| 3553 | gid: gid_t |
| 3554 | |
| 3555 | * |
| 3556 | |
| 3557 | dir_fd : dir_fd(requires='fchownat') = None |
| 3558 | If not None, it should be a file descriptor open to a directory, |
| 3559 | and path should be relative; path will then be relative to that |
| 3560 | directory. |
| 3561 | |
| 3562 | follow_symlinks: bool = True |
| 3563 | If False, and the last element of the path is a symbolic link, |
| 3564 | stat will examine the symbolic link itself instead of the file |
| 3565 | the link points to. |
| 3566 | |
| 3567 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3568 | |
| 3569 | path may always be specified as a string. |
| 3570 | On some platforms, path may also be specified as an open file descriptor. |
| 3571 | If this functionality is unavailable, using it raises an exception. |
| 3572 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3573 | and path should be relative; path will then be relative to that directory. |
| 3574 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3575 | link, chown will modify the symbolic link itself instead of the file the |
| 3576 | link points to. |
| 3577 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3578 | an open file descriptor. |
| 3579 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3580 | If they are unavailable, using them will raise a NotImplementedError. |
| 3581 | |
| 3582 | [clinic start generated code]*/ |
| 3583 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3584 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3585 | 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] | 3586 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3587 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3588 | { |
| 3589 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3590 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3591 | #if defined(HAVE_FCHOWNAT) |
| 3592 | int fchownat_unsupported = 0; |
| 3593 | #endif |
| 3594 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3595 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3596 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3597 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3598 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3599 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3600 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3601 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3602 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3603 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, |
| 3604 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3605 | return NULL; |
| 3606 | } |
| 3607 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3608 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3609 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3610 | if (path->fd != -1) |
| 3611 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3612 | else |
| 3613 | #endif |
| 3614 | #ifdef HAVE_LCHOWN |
| 3615 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3616 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3617 | else |
| 3618 | #endif |
| 3619 | #ifdef HAVE_FCHOWNAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3620 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) { |
| 3621 | if (HAVE_FCHOWNAT_RUNTIME) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3622 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3623 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3624 | } else { |
| 3625 | fchownat_unsupported = 1; |
| 3626 | } |
| 3627 | } else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3628 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3629 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3630 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3631 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3632 | #ifdef HAVE_FCHOWNAT |
| 3633 | if (fchownat_unsupported) { |
| 3634 | /* This would be incorrect if the current platform |
| 3635 | * doesn't support lchown. |
| 3636 | */ |
| 3637 | argument_unavailable_error(NULL, "dir_fd"); |
| 3638 | return NULL; |
| 3639 | } |
| 3640 | #endif |
| 3641 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3642 | if (result) |
| 3643 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3644 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3645 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3646 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3647 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3648 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3649 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3650 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3651 | /*[clinic input] |
| 3652 | os.fchown |
| 3653 | |
| 3654 | fd: int |
| 3655 | uid: uid_t |
| 3656 | gid: gid_t |
| 3657 | |
| 3658 | Change the owner and group id of the file specified by file descriptor. |
| 3659 | |
| 3660 | Equivalent to os.chown(fd, uid, gid). |
| 3661 | |
| 3662 | [clinic start generated code]*/ |
| 3663 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3664 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3665 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3666 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3667 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3668 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3669 | int async_err = 0; |
| 3670 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3671 | if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) { |
| 3672 | return NULL; |
| 3673 | } |
| 3674 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3675 | do { |
| 3676 | Py_BEGIN_ALLOW_THREADS |
| 3677 | res = fchown(fd, uid, gid); |
| 3678 | Py_END_ALLOW_THREADS |
| 3679 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3680 | if (res != 0) |
| 3681 | return (!async_err) ? posix_error() : NULL; |
| 3682 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3683 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3684 | } |
| 3685 | #endif /* HAVE_FCHOWN */ |
| 3686 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3687 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3688 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3689 | /*[clinic input] |
| 3690 | os.lchown |
| 3691 | |
| 3692 | path : path_t |
| 3693 | uid: uid_t |
| 3694 | gid: gid_t |
| 3695 | |
| 3696 | Change the owner and group id of path to the numeric uid and gid. |
| 3697 | |
| 3698 | This function will not follow symbolic links. |
| 3699 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3700 | [clinic start generated code]*/ |
| 3701 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3702 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3703 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3704 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3705 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3706 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3707 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) { |
| 3708 | return NULL; |
| 3709 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3710 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3711 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3712 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3713 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3714 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3715 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3716 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3717 | } |
| 3718 | #endif /* HAVE_LCHOWN */ |
| 3719 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3720 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3721 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3722 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3723 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3724 | #ifdef MS_WINDOWS |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3725 | wchar_t wbuf[MAXPATHLEN]; |
| 3726 | wchar_t *wbuf2 = wbuf; |
| 3727 | DWORD len; |
| 3728 | |
| 3729 | Py_BEGIN_ALLOW_THREADS |
| 3730 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
| 3731 | /* If the buffer is large enough, len does not include the |
| 3732 | terminating \0. If the buffer is too small, len includes |
| 3733 | the space needed for the terminator. */ |
| 3734 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | ec3e20a | 2019-06-28 18:01:59 +0200 | [diff] [blame] | 3735 | if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3736 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3737 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3738 | else { |
| 3739 | wbuf2 = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3740 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3741 | if (wbuf2) { |
| 3742 | len = GetCurrentDirectoryW(len, wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3743 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3744 | } |
| 3745 | Py_END_ALLOW_THREADS |
| 3746 | |
| 3747 | if (!wbuf2) { |
| 3748 | PyErr_NoMemory(); |
| 3749 | return NULL; |
| 3750 | } |
| 3751 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3752 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3753 | PyMem_RawFree(wbuf2); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3754 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3755 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3756 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3757 | PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3758 | if (wbuf2 != wbuf) { |
| 3759 | PyMem_RawFree(wbuf2); |
| 3760 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3761 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3762 | if (use_bytes) { |
| 3763 | if (resobj == NULL) { |
| 3764 | return NULL; |
| 3765 | } |
| 3766 | Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj)); |
| 3767 | } |
| 3768 | |
| 3769 | return resobj; |
| 3770 | #else |
| 3771 | const size_t chunk = 1024; |
| 3772 | |
| 3773 | char *buf = NULL; |
| 3774 | char *cwd = NULL; |
| 3775 | size_t buflen = 0; |
| 3776 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3777 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3778 | do { |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3779 | char *newbuf; |
| 3780 | if (buflen <= PY_SSIZE_T_MAX - chunk) { |
| 3781 | buflen += chunk; |
| 3782 | newbuf = PyMem_RawRealloc(buf, buflen); |
| 3783 | } |
| 3784 | else { |
| 3785 | newbuf = NULL; |
| 3786 | } |
| 3787 | if (newbuf == NULL) { |
| 3788 | PyMem_RawFree(buf); |
| 3789 | buf = NULL; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3790 | break; |
| 3791 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3792 | buf = newbuf; |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3793 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3794 | cwd = getcwd(buf, buflen); |
| 3795 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3796 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3797 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3798 | if (buf == NULL) { |
| 3799 | return PyErr_NoMemory(); |
| 3800 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3801 | if (cwd == NULL) { |
| 3802 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3803 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3804 | } |
| 3805 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3806 | PyObject *obj; |
| 3807 | if (use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3808 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3809 | } |
| 3810 | else { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3811 | obj = PyUnicode_DecodeFSDefault(buf); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3812 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3813 | PyMem_RawFree(buf); |
| 3814 | |
| 3815 | return obj; |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3816 | #endif /* !MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3817 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3818 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3819 | |
| 3820 | /*[clinic input] |
| 3821 | os.getcwd |
| 3822 | |
| 3823 | Return a unicode string representing the current working directory. |
| 3824 | [clinic start generated code]*/ |
| 3825 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3826 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3827 | os_getcwd_impl(PyObject *module) |
| 3828 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3829 | { |
| 3830 | return posix_getcwd(0); |
| 3831 | } |
| 3832 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3833 | |
| 3834 | /*[clinic input] |
| 3835 | os.getcwdb |
| 3836 | |
| 3837 | Return a bytes string representing the current working directory. |
| 3838 | [clinic start generated code]*/ |
| 3839 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3840 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3841 | os_getcwdb_impl(PyObject *module) |
| 3842 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3843 | { |
| 3844 | return posix_getcwd(1); |
| 3845 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3847 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3848 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3849 | #define HAVE_LINK 1 |
| 3850 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3851 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3852 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3853 | /*[clinic input] |
| 3854 | |
| 3855 | os.link |
| 3856 | |
| 3857 | src : path_t |
| 3858 | dst : path_t |
| 3859 | * |
| 3860 | src_dir_fd : dir_fd = None |
| 3861 | dst_dir_fd : dir_fd = None |
| 3862 | follow_symlinks: bool = True |
| 3863 | |
| 3864 | Create a hard link to a file. |
| 3865 | |
| 3866 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3867 | descriptor open to a directory, and the respective path string (src or dst) |
| 3868 | should be relative; the path will then be relative to that directory. |
| 3869 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3870 | link, link will create a link to the symbolic link itself instead of the |
| 3871 | file the link points to. |
| 3872 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3873 | platform. If they are unavailable, using them will raise a |
| 3874 | NotImplementedError. |
| 3875 | [clinic start generated code]*/ |
| 3876 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3877 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3878 | 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] | 3879 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3880 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3881 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3882 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3883 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3884 | #else |
| 3885 | int result; |
| 3886 | #endif |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3887 | #if defined(HAVE_LINKAT) |
| 3888 | int linkat_unavailable = 0; |
| 3889 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3890 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3891 | #ifndef HAVE_LINKAT |
| 3892 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3893 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3894 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3895 | } |
| 3896 | #endif |
| 3897 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3898 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3899 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3900 | PyErr_SetString(PyExc_NotImplementedError, |
| 3901 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3902 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3903 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3904 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3905 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3906 | if (PySys_Audit("os.link", "OOii", src->object, dst->object, |
| 3907 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 3908 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 3909 | return NULL; |
| 3910 | } |
| 3911 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3912 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3913 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3914 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3915 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3916 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3917 | if (!result) |
| 3918 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3919 | #else |
| 3920 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3921 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3922 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3923 | (dst_dir_fd != DEFAULT_DIR_FD) || |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3924 | (!follow_symlinks)) { |
| 3925 | |
| 3926 | if (HAVE_LINKAT_RUNTIME) { |
| 3927 | |
| 3928 | result = linkat(src_dir_fd, src->narrow, |
| 3929 | dst_dir_fd, dst->narrow, |
| 3930 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3931 | |
| 3932 | } |
| 3933 | #ifdef __APPLE__ |
| 3934 | else { |
| 3935 | if (src_dir_fd == DEFAULT_DIR_FD && dst_dir_fd == DEFAULT_DIR_FD) { |
| 3936 | /* See issue 41355: This matches the behaviour of !HAVE_LINKAT */ |
| 3937 | result = link(src->narrow, dst->narrow); |
| 3938 | } else { |
| 3939 | linkat_unavailable = 1; |
| 3940 | } |
| 3941 | } |
| 3942 | #endif |
| 3943 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3944 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3945 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3946 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3947 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3948 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 3949 | #ifdef HAVE_LINKAT |
| 3950 | if (linkat_unavailable) { |
| 3951 | /* Either or both dir_fd arguments were specified */ |
| 3952 | if (src_dir_fd != DEFAULT_DIR_FD) { |
| 3953 | argument_unavailable_error("link", "src_dir_fd"); |
| 3954 | } else { |
| 3955 | argument_unavailable_error("link", "dst_dir_fd"); |
| 3956 | } |
| 3957 | return NULL; |
| 3958 | } |
| 3959 | #endif |
| 3960 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3961 | if (result) |
| 3962 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3963 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3964 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3965 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3966 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3967 | #endif |
| 3968 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3969 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3970 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3971 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3972 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3973 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3974 | PyObject *v; |
| 3975 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3976 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3977 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3978 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3979 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3980 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3981 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3982 | WIN32_FIND_DATAW wFileData; |
| 3983 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3984 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3985 | if (!path->wide) { /* Default arg: "." */ |
| 3986 | po_wchars = L"."; |
| 3987 | len = 1; |
| 3988 | } else { |
| 3989 | po_wchars = path->wide; |
| 3990 | len = wcslen(path->wide); |
| 3991 | } |
| 3992 | /* The +5 is so we can append "\\*.*\0" */ |
| 3993 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3994 | if (!wnamebuf) { |
| 3995 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3996 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3997 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3998 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3999 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4000 | wchar_t wch = wnamebuf[len-1]; |
| 4001 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 4002 | wnamebuf[len++] = SEP; |
| 4003 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4004 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4005 | if ((list = PyList_New(0)) == NULL) { |
| 4006 | goto exit; |
| 4007 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 4008 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4009 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 4010 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4011 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 4012 | int error = GetLastError(); |
| 4013 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4014 | goto exit; |
| 4015 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4016 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4017 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4018 | } |
| 4019 | do { |
| 4020 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4021 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 4022 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 4023 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 4024 | wcslen(wFileData.cFileName)); |
| 4025 | if (path->narrow && v) { |
| 4026 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 4027 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4028 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4029 | Py_DECREF(list); |
| 4030 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4031 | break; |
| 4032 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4033 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4034 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4035 | Py_DECREF(list); |
| 4036 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4037 | break; |
| 4038 | } |
| 4039 | Py_DECREF(v); |
| 4040 | } |
| 4041 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4042 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4043 | Py_END_ALLOW_THREADS |
| 4044 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 4045 | it got to the end of the directory. */ |
| 4046 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4047 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4048 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4049 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4050 | } |
| 4051 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4052 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4053 | exit: |
| 4054 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 4055 | if (FindClose(hFindFile) == FALSE) { |
| 4056 | if (list != NULL) { |
| 4057 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4058 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4059 | } |
| 4060 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4061 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 4062 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4063 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4064 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4065 | } /* end of _listdir_windows_no_opendir */ |
| 4066 | |
| 4067 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 4068 | |
| 4069 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4070 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4071 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4072 | PyObject *v; |
| 4073 | DIR *dirp = NULL; |
| 4074 | struct dirent *ep; |
| 4075 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 4076 | #ifdef HAVE_FDOPENDIR |
| 4077 | int fd = -1; |
| 4078 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4079 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4080 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4081 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4082 | if (path->fd != -1) { |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4083 | if (HAVE_FDOPENDIR_RUNTIME) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4084 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4085 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 4086 | if (fd == -1) |
| 4087 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4088 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 4089 | return_str = 1; |
| 4090 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4091 | Py_BEGIN_ALLOW_THREADS |
| 4092 | dirp = fdopendir(fd); |
| 4093 | Py_END_ALLOW_THREADS |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4094 | } else { |
| 4095 | PyErr_SetString(PyExc_TypeError, |
| 4096 | "listdir: path should be string, bytes, os.PathLike or None, not int"); |
| 4097 | return NULL; |
| 4098 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4099 | } |
| 4100 | else |
| 4101 | #endif |
| 4102 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4103 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4104 | if (path->narrow) { |
| 4105 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 4106 | /* only return bytes if they specified a bytes-like object */ |
| 4107 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 4108 | } |
| 4109 | else { |
| 4110 | name = "."; |
| 4111 | return_str = 1; |
| 4112 | } |
| 4113 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4114 | Py_BEGIN_ALLOW_THREADS |
| 4115 | dirp = opendir(name); |
| 4116 | Py_END_ALLOW_THREADS |
| 4117 | } |
| 4118 | |
| 4119 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4120 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 4121 | #ifdef HAVE_FDOPENDIR |
| 4122 | if (fd != -1) { |
| 4123 | Py_BEGIN_ALLOW_THREADS |
| 4124 | close(fd); |
| 4125 | Py_END_ALLOW_THREADS |
| 4126 | } |
| 4127 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4128 | goto exit; |
| 4129 | } |
| 4130 | if ((list = PyList_New(0)) == NULL) { |
| 4131 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4132 | } |
| 4133 | for (;;) { |
| 4134 | errno = 0; |
| 4135 | Py_BEGIN_ALLOW_THREADS |
| 4136 | ep = readdir(dirp); |
| 4137 | Py_END_ALLOW_THREADS |
| 4138 | if (ep == NULL) { |
| 4139 | if (errno == 0) { |
| 4140 | break; |
| 4141 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4142 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4143 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4144 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4145 | } |
| 4146 | } |
| 4147 | if (ep->d_name[0] == '.' && |
| 4148 | (NAMLEN(ep) == 1 || |
| 4149 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 4150 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 4151 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 4152 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 4153 | else |
| 4154 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4155 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4156 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4157 | break; |
| 4158 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4159 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4160 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4161 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4162 | break; |
| 4163 | } |
| 4164 | Py_DECREF(v); |
| 4165 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 4166 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4167 | exit: |
| 4168 | if (dirp != NULL) { |
| 4169 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 4170 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4171 | if (fd > -1) |
| 4172 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 4173 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4174 | closedir(dirp); |
| 4175 | Py_END_ALLOW_THREADS |
| 4176 | } |
| 4177 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4178 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4179 | } /* end of _posix_listdir */ |
| 4180 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4181 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4182 | |
| 4183 | /*[clinic input] |
| 4184 | os.listdir |
| 4185 | |
| 4186 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 4187 | |
| 4188 | Return a list containing the names of the files in the directory. |
| 4189 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 4190 | 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] | 4191 | the filenames returned will also be bytes; in all other circumstances |
| 4192 | the filenames returned will be str. |
| 4193 | If path is None, uses the path='.'. |
| 4194 | On some platforms, path may also be specified as an open file descriptor;\ |
| 4195 | the file descriptor must refer to a directory. |
| 4196 | If this functionality is unavailable, using it raises NotImplementedError. |
| 4197 | |
| 4198 | The list is in arbitrary order. It does not include the special |
| 4199 | entries '.' and '..' even if they are present in the directory. |
| 4200 | |
| 4201 | |
| 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_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 4206 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4207 | { |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 4208 | if (PySys_Audit("os.listdir", "O", |
| 4209 | path->object ? path->object : Py_None) < 0) { |
| 4210 | return NULL; |
| 4211 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4212 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 4213 | return _listdir_windows_no_opendir(path, NULL); |
| 4214 | #else |
| 4215 | return _posix_listdir(path, NULL); |
| 4216 | #endif |
| 4217 | } |
| 4218 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4219 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4220 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 4221 | /*[clinic input] |
| 4222 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4223 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 4224 | path: path_t |
| 4225 | / |
| 4226 | |
| 4227 | [clinic start generated code]*/ |
| 4228 | |
| 4229 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4230 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 4231 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 4232 | { |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4233 | wchar_t *abspath; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4234 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4235 | /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */ |
| 4236 | if (_Py_abspath(path->wide, &abspath) < 0) { |
| 4237 | return win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4238 | } |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 4239 | if (abspath == NULL) { |
| 4240 | return PyErr_NoMemory(); |
| 4241 | } |
| 4242 | |
| 4243 | PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath)); |
| 4244 | PyMem_RawFree(abspath); |
| 4245 | if (str == NULL) { |
| 4246 | return NULL; |
| 4247 | } |
| 4248 | if (path->narrow) { |
| 4249 | Py_SETREF(str, PyUnicode_EncodeFSDefault(str)); |
| 4250 | } |
| 4251 | return str; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4252 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4253 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 4254 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4255 | /*[clinic input] |
| 4256 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 4257 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4258 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4259 | / |
| 4260 | |
| 4261 | A helper function for samepath on windows. |
| 4262 | [clinic start generated code]*/ |
| 4263 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4264 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4265 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 4266 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4267 | { |
| 4268 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4269 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 4270 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4271 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4272 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4273 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4274 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4275 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4276 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4277 | 0, /* desired access */ |
| 4278 | 0, /* share mode */ |
| 4279 | NULL, /* security attributes */ |
| 4280 | OPEN_EXISTING, |
| 4281 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 4282 | FILE_FLAG_BACKUP_SEMANTICS, |
| 4283 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4284 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4285 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4286 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4287 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4288 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4289 | |
| 4290 | /* We have a good handle to the target, use it to determine the |
| 4291 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4292 | while (1) { |
| 4293 | Py_BEGIN_ALLOW_THREADS |
| 4294 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 4295 | buf_size, VOLUME_NAME_DOS); |
| 4296 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4297 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4298 | if (!result_length) { |
| 4299 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 4300 | path->object); |
| 4301 | goto cleanup; |
| 4302 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4303 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4304 | if (result_length < buf_size) { |
| 4305 | break; |
| 4306 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4307 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4308 | wchar_t *tmp; |
| 4309 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 4310 | result_length * sizeof(*tmp)); |
| 4311 | if (!tmp) { |
| 4312 | result = PyErr_NoMemory(); |
| 4313 | goto cleanup; |
| 4314 | } |
| 4315 | |
| 4316 | buf_size = result_length; |
| 4317 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4318 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4319 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4320 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4321 | if (result && path->narrow) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4322 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4323 | } |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4324 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4325 | cleanup: |
| 4326 | if (target_path != buf) { |
| 4327 | PyMem_Free(target_path); |
| 4328 | } |
| 4329 | CloseHandle(hFile); |
| 4330 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4331 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 4332 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4333 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4334 | /*[clinic input] |
| 4335 | os._getvolumepathname |
| 4336 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4337 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4338 | |
| 4339 | A helper function for ismount on Win32. |
| 4340 | [clinic start generated code]*/ |
| 4341 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4342 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4343 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 4344 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4345 | { |
| 4346 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4347 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4348 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4349 | BOOL ret; |
| 4350 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4351 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4352 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4353 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 4354 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4355 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4356 | return NULL; |
| 4357 | } |
| 4358 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4359 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4360 | if (mountpath == NULL) |
| 4361 | return PyErr_NoMemory(); |
| 4362 | |
| 4363 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4364 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4365 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4366 | Py_END_ALLOW_THREADS |
| 4367 | |
| 4368 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4369 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4370 | goto exit; |
| 4371 | } |
| 4372 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4373 | if (path->narrow) |
| 4374 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4375 | |
| 4376 | exit: |
| 4377 | PyMem_Free(mountpath); |
| 4378 | return result; |
| 4379 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4380 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4381 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4382 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4383 | |
| 4384 | /*[clinic input] |
| 4385 | os.mkdir |
| 4386 | |
| 4387 | path : path_t |
| 4388 | |
| 4389 | mode: int = 0o777 |
| 4390 | |
| 4391 | * |
| 4392 | |
| 4393 | dir_fd : dir_fd(requires='mkdirat') = None |
| 4394 | |
| 4395 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4396 | |
| 4397 | Create a directory. |
| 4398 | |
| 4399 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4400 | and path should be relative; path will then be relative to that directory. |
| 4401 | dir_fd may not be implemented on your platform. |
| 4402 | If it is unavailable, using it will raise a NotImplementedError. |
| 4403 | |
| 4404 | The mode argument is ignored on Windows. |
| 4405 | [clinic start generated code]*/ |
| 4406 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4407 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4408 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 4409 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4410 | { |
| 4411 | int result; |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4412 | #ifdef HAVE_MKDIRAT |
| 4413 | int mkdirat_unavailable = 0; |
| 4414 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4415 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4416 | if (PySys_Audit("os.mkdir", "Oii", path->object, mode, |
| 4417 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4418 | return NULL; |
| 4419 | } |
| 4420 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4421 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4422 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4423 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4424 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4425 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4426 | if (!result) |
| 4427 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4428 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4429 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4430 | #if HAVE_MKDIRAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4431 | if (dir_fd != DEFAULT_DIR_FD) { |
| 4432 | if (HAVE_MKDIRAT_RUNTIME) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4433 | result = mkdirat(dir_fd, path->narrow, mode); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4434 | |
| 4435 | } else { |
| 4436 | mkdirat_unavailable = 1; |
| 4437 | } |
| 4438 | } else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4439 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4440 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4441 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4442 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4443 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4444 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4445 | Py_END_ALLOW_THREADS |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4446 | |
| 4447 | #if HAVE_MKDIRAT |
| 4448 | if (mkdirat_unavailable) { |
| 4449 | argument_unavailable_error(NULL, "dir_fd"); |
| 4450 | return NULL; |
| 4451 | } |
| 4452 | #endif |
| 4453 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4454 | if (result < 0) |
| 4455 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4456 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4457 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4460 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4461 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4462 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4463 | #include <sys/resource.h> |
| 4464 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4465 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4466 | |
| 4467 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4468 | /*[clinic input] |
| 4469 | os.nice |
| 4470 | |
| 4471 | increment: int |
| 4472 | / |
| 4473 | |
| 4474 | Add increment to the priority of process and return the new priority. |
| 4475 | [clinic start generated code]*/ |
| 4476 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4477 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4478 | os_nice_impl(PyObject *module, int increment) |
| 4479 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4480 | { |
| 4481 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4482 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4483 | /* There are two flavours of 'nice': one that returns the new |
| 4484 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4485 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4486 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4487 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4488 | If we are of the nice family that returns the new priority, we |
| 4489 | need to clear errno before the call, and check if errno is filled |
| 4490 | before calling posix_error() on a returnvalue of -1, because the |
| 4491 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4492 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4493 | errno = 0; |
| 4494 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4495 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4496 | if (value == 0) |
| 4497 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4498 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4499 | if (value == -1 && errno != 0) |
| 4500 | /* either nice() or getpriority() returned an error */ |
| 4501 | return posix_error(); |
| 4502 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4503 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4504 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4505 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4506 | |
| 4507 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4508 | /*[clinic input] |
| 4509 | os.getpriority |
| 4510 | |
| 4511 | which: int |
| 4512 | who: int |
| 4513 | |
| 4514 | Return program scheduling priority. |
| 4515 | [clinic start generated code]*/ |
| 4516 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4517 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4518 | os_getpriority_impl(PyObject *module, int which, int who) |
| 4519 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4520 | { |
| 4521 | int retval; |
| 4522 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4523 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4524 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4525 | if (errno != 0) |
| 4526 | return posix_error(); |
| 4527 | return PyLong_FromLong((long)retval); |
| 4528 | } |
| 4529 | #endif /* HAVE_GETPRIORITY */ |
| 4530 | |
| 4531 | |
| 4532 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4533 | /*[clinic input] |
| 4534 | os.setpriority |
| 4535 | |
| 4536 | which: int |
| 4537 | who: int |
| 4538 | priority: int |
| 4539 | |
| 4540 | Set program scheduling priority. |
| 4541 | [clinic start generated code]*/ |
| 4542 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4543 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4544 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4545 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4546 | { |
| 4547 | int retval; |
| 4548 | |
| 4549 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4550 | if (retval == -1) |
| 4551 | return posix_error(); |
| 4552 | Py_RETURN_NONE; |
| 4553 | } |
| 4554 | #endif /* HAVE_SETPRIORITY */ |
| 4555 | |
| 4556 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4557 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4558 | 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] | 4559 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4560 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4561 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4562 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4563 | #ifdef HAVE_RENAMEAT |
| 4564 | int renameat_unavailable = 0; |
| 4565 | #endif |
| 4566 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4567 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4568 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4569 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4570 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4571 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4572 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4573 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4574 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4575 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4576 | #ifndef HAVE_RENAMEAT |
| 4577 | if (dir_fd_specified) { |
| 4578 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4579 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4580 | } |
| 4581 | #endif |
| 4582 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4583 | if (PySys_Audit("os.rename", "OOii", src->object, dst->object, |
| 4584 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 4585 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 4586 | return NULL; |
| 4587 | } |
| 4588 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4589 | #ifdef MS_WINDOWS |
| 4590 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4591 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4592 | Py_END_ALLOW_THREADS |
| 4593 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4594 | if (!result) |
| 4595 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4596 | |
| 4597 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4598 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4599 | PyErr_Format(PyExc_ValueError, |
| 4600 | "%s: src and dst must be the same type", function_name); |
| 4601 | return NULL; |
| 4602 | } |
| 4603 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4604 | Py_BEGIN_ALLOW_THREADS |
| 4605 | #ifdef HAVE_RENAMEAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4606 | if (dir_fd_specified) { |
| 4607 | if (HAVE_RENAMEAT_RUNTIME) { |
| 4608 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
| 4609 | } else { |
| 4610 | renameat_unavailable = 1; |
| 4611 | } |
| 4612 | } else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4613 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4614 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4615 | Py_END_ALLOW_THREADS |
| 4616 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4617 | |
| 4618 | #ifdef HAVE_RENAMEAT |
| 4619 | if (renameat_unavailable) { |
| 4620 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
| 4621 | return NULL; |
| 4622 | } |
| 4623 | #endif |
| 4624 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4625 | if (result) |
| 4626 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4627 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4628 | Py_RETURN_NONE; |
| 4629 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4630 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4631 | |
| 4632 | /*[clinic input] |
| 4633 | os.rename |
| 4634 | |
| 4635 | src : path_t |
| 4636 | dst : path_t |
| 4637 | * |
| 4638 | src_dir_fd : dir_fd = None |
| 4639 | dst_dir_fd : dir_fd = None |
| 4640 | |
| 4641 | Rename a file or directory. |
| 4642 | |
| 4643 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4644 | descriptor open to a directory, and the respective path string (src or dst) |
| 4645 | should be relative; the path will then be relative to that directory. |
| 4646 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4647 | If they are unavailable, using them will raise a NotImplementedError. |
| 4648 | [clinic start generated code]*/ |
| 4649 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4650 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4651 | 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] | 4652 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4653 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4654 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4655 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4656 | } |
| 4657 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4658 | |
| 4659 | /*[clinic input] |
| 4660 | os.replace = os.rename |
| 4661 | |
| 4662 | Rename a file or directory, overwriting the destination. |
| 4663 | |
| 4664 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4665 | descriptor open to a directory, and the respective path string (src or dst) |
| 4666 | should be relative; the path will then be relative to that directory. |
| 4667 | 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] | 4668 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4669 | [clinic start generated code]*/ |
| 4670 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4671 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4672 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4673 | int dst_dir_fd) |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4674 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4675 | { |
| 4676 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4677 | } |
| 4678 | |
| 4679 | |
| 4680 | /*[clinic input] |
| 4681 | os.rmdir |
| 4682 | |
| 4683 | path: path_t |
| 4684 | * |
| 4685 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4686 | |
| 4687 | Remove a directory. |
| 4688 | |
| 4689 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4690 | and path should be relative; path will then be relative to that directory. |
| 4691 | dir_fd may not be implemented on your platform. |
| 4692 | If it is unavailable, using it will raise a NotImplementedError. |
| 4693 | [clinic start generated code]*/ |
| 4694 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4695 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4696 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4697 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4698 | { |
| 4699 | int result; |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4700 | #ifdef HAVE_UNLINKAT |
| 4701 | int unlinkat_unavailable = 0; |
| 4702 | #endif |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4703 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4704 | if (PySys_Audit("os.rmdir", "Oi", path->object, |
| 4705 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4706 | return NULL; |
| 4707 | } |
| 4708 | |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4709 | Py_BEGIN_ALLOW_THREADS |
| 4710 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4711 | /* Windows, success=1, UNIX, success=0 */ |
| 4712 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4713 | #else |
| 4714 | #ifdef HAVE_UNLINKAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4715 | if (dir_fd != DEFAULT_DIR_FD) { |
| 4716 | if (HAVE_UNLINKAT_RUNTIME) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4717 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4718 | } else { |
| 4719 | unlinkat_unavailable = 1; |
| 4720 | result = -1; |
| 4721 | } |
| 4722 | } else |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4723 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4724 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4725 | #endif |
| 4726 | Py_END_ALLOW_THREADS |
| 4727 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4728 | #ifdef HAVE_UNLINKAT |
| 4729 | if (unlinkat_unavailable) { |
| 4730 | argument_unavailable_error("rmdir", "dir_fd"); |
| 4731 | return NULL; |
| 4732 | } |
| 4733 | #endif |
| 4734 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4735 | if (result) |
| 4736 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4737 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4738 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4741 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4742 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4743 | #ifdef MS_WINDOWS |
| 4744 | /*[clinic input] |
| 4745 | os.system -> long |
| 4746 | |
| 4747 | command: Py_UNICODE |
| 4748 | |
| 4749 | Execute the command in a subshell. |
| 4750 | [clinic start generated code]*/ |
| 4751 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4752 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4753 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4754 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4755 | { |
| 4756 | long result; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4757 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4758 | if (PySys_Audit("os.system", "(u)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4759 | return -1; |
| 4760 | } |
| 4761 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4762 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4763 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4764 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4765 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4766 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4767 | return result; |
| 4768 | } |
| 4769 | #else /* MS_WINDOWS */ |
| 4770 | /*[clinic input] |
| 4771 | os.system -> long |
| 4772 | |
| 4773 | command: FSConverter |
| 4774 | |
| 4775 | Execute the command in a subshell. |
| 4776 | [clinic start generated code]*/ |
| 4777 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4778 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4779 | os_system_impl(PyObject *module, PyObject *command) |
| 4780 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4781 | { |
| 4782 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4783 | const char *bytes = PyBytes_AsString(command); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4784 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4785 | if (PySys_Audit("os.system", "(O)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4786 | return -1; |
| 4787 | } |
| 4788 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4789 | Py_BEGIN_ALLOW_THREADS |
| 4790 | result = system(bytes); |
| 4791 | Py_END_ALLOW_THREADS |
| 4792 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4793 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4794 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4795 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4796 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4797 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4798 | /*[clinic input] |
| 4799 | os.umask |
| 4800 | |
| 4801 | mask: int |
| 4802 | / |
| 4803 | |
| 4804 | Set the current numeric umask and return the previous umask. |
| 4805 | [clinic start generated code]*/ |
| 4806 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4807 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4808 | os_umask_impl(PyObject *module, int mask) |
| 4809 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4810 | { |
| 4811 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4812 | if (i < 0) |
| 4813 | return posix_error(); |
| 4814 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4817 | #ifdef MS_WINDOWS |
| 4818 | |
| 4819 | /* override the default DeleteFileW behavior so that directory |
| 4820 | symlinks can be removed with this function, the same as with |
| 4821 | Unix symlinks */ |
| 4822 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4823 | { |
| 4824 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4825 | WIN32_FIND_DATAW find_data; |
| 4826 | HANDLE find_data_handle; |
| 4827 | int is_directory = 0; |
| 4828 | int is_link = 0; |
| 4829 | |
| 4830 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4831 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4832 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4833 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4834 | it is a symlink */ |
| 4835 | if(is_directory && |
| 4836 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4837 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4838 | |
| 4839 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4840 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4841 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4842 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4843 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4844 | FindClose(find_data_handle); |
| 4845 | } |
| 4846 | } |
| 4847 | } |
| 4848 | |
| 4849 | if (is_directory && is_link) |
| 4850 | return RemoveDirectoryW(lpFileName); |
| 4851 | |
| 4852 | return DeleteFileW(lpFileName); |
| 4853 | } |
| 4854 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4855 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4856 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4857 | /*[clinic input] |
| 4858 | os.unlink |
| 4859 | |
| 4860 | path: path_t |
| 4861 | * |
| 4862 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4863 | |
| 4864 | Remove a file (same as remove()). |
| 4865 | |
| 4866 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4867 | and path should be relative; path will then be relative to that directory. |
| 4868 | dir_fd may not be implemented on your platform. |
| 4869 | If it is unavailable, using it will raise a NotImplementedError. |
| 4870 | |
| 4871 | [clinic start generated code]*/ |
| 4872 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4873 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4874 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4875 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4876 | { |
| 4877 | int result; |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4878 | #ifdef HAVE_UNLINKAT |
| 4879 | int unlinkat_unavailable = 0; |
| 4880 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4881 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4882 | if (PySys_Audit("os.remove", "Oi", path->object, |
| 4883 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4884 | return NULL; |
| 4885 | } |
| 4886 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4887 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4888 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4889 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4890 | /* Windows, success=1, UNIX, success=0 */ |
| 4891 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4892 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4893 | #ifdef HAVE_UNLINKAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4894 | if (dir_fd != DEFAULT_DIR_FD) { |
| 4895 | if (HAVE_UNLINKAT_RUNTIME) { |
| 4896 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4897 | result = unlinkat(dir_fd, path->narrow, 0); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4898 | } else { |
| 4899 | unlinkat_unavailable = 1; |
| 4900 | } |
| 4901 | } else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4902 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4903 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4904 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4905 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4906 | Py_END_ALLOW_THREADS |
| 4907 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 4908 | #ifdef HAVE_UNLINKAT |
| 4909 | if (unlinkat_unavailable) { |
| 4910 | argument_unavailable_error(NULL, "dir_fd"); |
| 4911 | return NULL; |
| 4912 | } |
| 4913 | #endif |
| 4914 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4915 | if (result) |
| 4916 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4917 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4918 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4921 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4922 | /*[clinic input] |
| 4923 | os.remove = os.unlink |
| 4924 | |
| 4925 | Remove a file (same as unlink()). |
| 4926 | |
| 4927 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4928 | and path should be relative; path will then be relative to that directory. |
| 4929 | dir_fd may not be implemented on your platform. |
| 4930 | If it is unavailable, using it will raise a NotImplementedError. |
| 4931 | [clinic start generated code]*/ |
| 4932 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4933 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4934 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4935 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4936 | { |
| 4937 | return os_unlink_impl(module, path, dir_fd); |
| 4938 | } |
| 4939 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4940 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4941 | static PyStructSequence_Field uname_result_fields[] = { |
| 4942 | {"sysname", "operating system name"}, |
| 4943 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4944 | {"release", "operating system release"}, |
| 4945 | {"version", "operating system version"}, |
| 4946 | {"machine", "hardware identifier"}, |
| 4947 | {NULL} |
| 4948 | }; |
| 4949 | |
| 4950 | PyDoc_STRVAR(uname_result__doc__, |
| 4951 | "uname_result: Result from os.uname().\n\n\ |
| 4952 | This object may be accessed either as a tuple of\n\ |
| 4953 | (sysname, nodename, release, version, machine),\n\ |
| 4954 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4955 | \n\ |
| 4956 | See os.uname for more information."); |
| 4957 | |
| 4958 | static PyStructSequence_Desc uname_result_desc = { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4959 | MODNAME ".uname_result", /* name */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4960 | uname_result__doc__, /* doc */ |
| 4961 | uname_result_fields, |
| 4962 | 5 |
| 4963 | }; |
| 4964 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4965 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4966 | /*[clinic input] |
| 4967 | os.uname |
| 4968 | |
| 4969 | Return an object identifying the current operating system. |
| 4970 | |
| 4971 | The object behaves like a named tuple with the following fields: |
| 4972 | (sysname, nodename, release, version, machine) |
| 4973 | |
| 4974 | [clinic start generated code]*/ |
| 4975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4976 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4977 | os_uname_impl(PyObject *module) |
| 4978 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4979 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4980 | struct utsname u; |
| 4981 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4982 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4983 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4984 | Py_BEGIN_ALLOW_THREADS |
| 4985 | res = uname(&u); |
| 4986 | Py_END_ALLOW_THREADS |
| 4987 | if (res < 0) |
| 4988 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4989 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 4990 | PyObject *UnameResultType = get_posix_state(module)->UnameResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4991 | value = PyStructSequence_New((PyTypeObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4992 | if (value == NULL) |
| 4993 | return NULL; |
| 4994 | |
| 4995 | #define SET(i, field) \ |
| 4996 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4997 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4998 | if (!o) { \ |
| 4999 | Py_DECREF(value); \ |
| 5000 | return NULL; \ |
| 5001 | } \ |
| 5002 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 5003 | } \ |
| 5004 | |
| 5005 | SET(0, u.sysname); |
| 5006 | SET(1, u.nodename); |
| 5007 | SET(2, u.release); |
| 5008 | SET(3, u.version); |
| 5009 | SET(4, u.machine); |
| 5010 | |
| 5011 | #undef SET |
| 5012 | |
| 5013 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 5014 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5015 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 5016 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 5017 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5018 | |
| 5019 | typedef struct { |
| 5020 | int now; |
| 5021 | time_t atime_s; |
| 5022 | long atime_ns; |
| 5023 | time_t mtime_s; |
| 5024 | long mtime_ns; |
| 5025 | } utime_t; |
| 5026 | |
| 5027 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5028 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5029 | * they also intentionally leak the declaration of a pointer named "time" |
| 5030 | */ |
| 5031 | #define UTIME_TO_TIMESPEC \ |
| 5032 | struct timespec ts[2]; \ |
| 5033 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5034 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5035 | time = NULL; \ |
| 5036 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5037 | ts[0].tv_sec = ut->atime_s; \ |
| 5038 | ts[0].tv_nsec = ut->atime_ns; \ |
| 5039 | ts[1].tv_sec = ut->mtime_s; \ |
| 5040 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5041 | time = ts; \ |
| 5042 | } \ |
| 5043 | |
| 5044 | #define UTIME_TO_TIMEVAL \ |
| 5045 | struct timeval tv[2]; \ |
| 5046 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5047 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5048 | time = NULL; \ |
| 5049 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5050 | tv[0].tv_sec = ut->atime_s; \ |
| 5051 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 5052 | tv[1].tv_sec = ut->mtime_s; \ |
| 5053 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5054 | time = tv; \ |
| 5055 | } \ |
| 5056 | |
| 5057 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 5058 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5059 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5060 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5061 | time = NULL; \ |
| 5062 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 5063 | u.actime = ut->atime_s; \ |
| 5064 | u.modtime = ut->mtime_s; \ |
| 5065 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | #define UTIME_TO_TIME_T \ |
| 5069 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 5070 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5071 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5072 | time = NULL; \ |
| 5073 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5074 | timet[0] = ut->atime_s; \ |
| 5075 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 5076 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5077 | } \ |
| 5078 | |
| 5079 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5080 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5081 | |
| 5082 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 5083 | 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] | 5084 | { |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5085 | #if defined(__APPLE__) && defined(HAVE_UTIMENSAT) |
| 5086 | if (HAVE_UTIMENSAT_RUNTIME) { |
| 5087 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 5088 | UTIME_TO_TIMESPEC; |
| 5089 | return utimensat(dir_fd, path, time, flags); |
| 5090 | } else { |
| 5091 | errno = ENOSYS; |
| 5092 | return -1; |
| 5093 | } |
| 5094 | #elif defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5095 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 5096 | UTIME_TO_TIMESPEC; |
| 5097 | return utimensat(dir_fd, path, time, flags); |
| 5098 | #elif defined(HAVE_FUTIMESAT) |
| 5099 | UTIME_TO_TIMEVAL; |
| 5100 | /* |
| 5101 | * follow_symlinks will never be false here; |
| 5102 | * we only allow !follow_symlinks and dir_fd together |
| 5103 | * if we have utimensat() |
| 5104 | */ |
| 5105 | assert(follow_symlinks); |
| 5106 | return futimesat(dir_fd, path, time); |
| 5107 | #endif |
| 5108 | } |
| 5109 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5110 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 5111 | #else |
| 5112 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5113 | #endif |
| 5114 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5115 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5116 | |
| 5117 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5118 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5119 | { |
| 5120 | #ifdef HAVE_FUTIMENS |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5121 | |
| 5122 | if (HAVE_FUTIMENS_RUNTIME) { |
| 5123 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5124 | UTIME_TO_TIMESPEC; |
| 5125 | return futimens(fd, time); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5126 | |
| 5127 | } else |
| 5128 | #ifndef HAVE_FUTIMES |
| 5129 | { |
| 5130 | /* Not sure if this can happen */ |
| 5131 | PyErr_SetString( |
| 5132 | PyExc_RuntimeError, |
| 5133 | "neither futimens nor futimes are supported" |
| 5134 | " on this system"); |
| 5135 | return -1; |
| 5136 | } |
| 5137 | #endif |
| 5138 | |
| 5139 | #endif |
| 5140 | #ifdef HAVE_FUTIMES |
| 5141 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5142 | UTIME_TO_TIMEVAL; |
| 5143 | return futimes(fd, time); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5144 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5145 | #endif |
| 5146 | } |
| 5147 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5148 | #define PATH_UTIME_HAVE_FD 1 |
| 5149 | #else |
| 5150 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5151 | #endif |
| 5152 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 5153 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 5154 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 5155 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5156 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 5157 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5158 | |
| 5159 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 5160 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5161 | { |
| 5162 | #ifdef HAVE_UTIMENSAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5163 | if (HAVE_UTIMENSAT_RUNTIME) { |
| 5164 | UTIME_TO_TIMESPEC; |
| 5165 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 5166 | } else |
| 5167 | #ifndef HAVE_LUTIMES |
| 5168 | { |
| 5169 | /* Not sure if this can happen */ |
| 5170 | PyErr_SetString( |
| 5171 | PyExc_RuntimeError, |
| 5172 | "neither utimensat nor lutimes are supported" |
| 5173 | " on this system"); |
| 5174 | return -1; |
| 5175 | } |
| 5176 | #endif |
| 5177 | #endif |
| 5178 | |
| 5179 | #ifdef HAVE_LUTIMES |
| 5180 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5181 | UTIME_TO_TIMEVAL; |
| 5182 | return lutimes(path, time); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5183 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5184 | #endif |
| 5185 | } |
| 5186 | |
| 5187 | #endif |
| 5188 | |
| 5189 | #ifndef MS_WINDOWS |
| 5190 | |
| 5191 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 5192 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5193 | { |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5194 | #if defined(__APPLE__) && defined(HAVE_UTIMENSAT) |
| 5195 | if (HAVE_UTIMENSAT_RUNTIME) { |
| 5196 | UTIME_TO_TIMESPEC; |
| 5197 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 5198 | } else { |
| 5199 | UTIME_TO_TIMEVAL; |
| 5200 | return utimes(path, time); |
| 5201 | } |
| 5202 | #elif defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5203 | UTIME_TO_TIMESPEC; |
| 5204 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 5205 | #elif defined(HAVE_UTIMES) |
| 5206 | UTIME_TO_TIMEVAL; |
| 5207 | return utimes(path, time); |
| 5208 | #elif defined(HAVE_UTIME_H) |
| 5209 | UTIME_TO_UTIMBUF; |
| 5210 | return utime(path, time); |
| 5211 | #else |
| 5212 | UTIME_TO_TIME_T; |
| 5213 | return utime(path, time); |
| 5214 | #endif |
| 5215 | } |
| 5216 | |
| 5217 | #endif |
| 5218 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5219 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5220 | 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] | 5221 | { |
| 5222 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 5223 | PyObject *divmod; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5224 | divmod = PyNumber_Divmod(py_long, get_posix_state(module)->billion); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5225 | if (!divmod) |
| 5226 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 5227 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 5228 | PyErr_Format(PyExc_TypeError, |
| 5229 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 5230 | _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod))); |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 5231 | goto exit; |
| 5232 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5233 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 5234 | if ((*s == -1) && PyErr_Occurred()) |
| 5235 | goto exit; |
| 5236 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 5237 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5238 | goto exit; |
| 5239 | |
| 5240 | result = 1; |
| 5241 | exit: |
| 5242 | Py_XDECREF(divmod); |
| 5243 | return result; |
| 5244 | } |
| 5245 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5246 | |
| 5247 | /*[clinic input] |
| 5248 | os.utime |
| 5249 | |
| 5250 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 5251 | times: object = None |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5252 | * |
| 5253 | ns: object = NULL |
| 5254 | dir_fd: dir_fd(requires='futimensat') = None |
| 5255 | follow_symlinks: bool=True |
| 5256 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 5257 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5258 | |
| 5259 | Set the access and modified time of path. |
| 5260 | |
| 5261 | path may always be specified as a string. |
| 5262 | On some platforms, path may also be specified as an open file descriptor. |
| 5263 | If this functionality is unavailable, using it raises an exception. |
| 5264 | |
| 5265 | If times is not None, it must be a tuple (atime, mtime); |
| 5266 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 5267 | 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] | 5268 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 5269 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 5270 | 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] | 5271 | Specifying tuples for both times and ns is an error. |
| 5272 | |
| 5273 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 5274 | and path should be relative; path will then be relative to that directory. |
| 5275 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 5276 | link, utime will modify the symbolic link itself instead of the file the |
| 5277 | link points to. |
| 5278 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 5279 | as an open file descriptor. |
| 5280 | dir_fd and follow_symlinks may not be available on your platform. |
| 5281 | If they are unavailable, using them will raise a NotImplementedError. |
| 5282 | |
| 5283 | [clinic start generated code]*/ |
| 5284 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5285 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5286 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 5287 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 5288 | /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5289 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5290 | #ifdef MS_WINDOWS |
| 5291 | HANDLE hFile; |
| 5292 | FILETIME atime, mtime; |
| 5293 | #else |
| 5294 | int result; |
| 5295 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5297 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5298 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 5299 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5300 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 5301 | if (times != Py_None && ns) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5302 | PyErr_SetString(PyExc_ValueError, |
| 5303 | "utime: you may specify either 'times'" |
| 5304 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5305 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5306 | } |
| 5307 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 5308 | if (times != Py_None) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 5309 | time_t a_sec, m_sec; |
| 5310 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5311 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5312 | PyErr_SetString(PyExc_TypeError, |
| 5313 | "utime: 'times' must be either" |
| 5314 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5315 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5316 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5317 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 5318 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 5319 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 5320 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 5321 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5322 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 5323 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 5324 | utime.atime_s = a_sec; |
| 5325 | utime.atime_ns = a_nsec; |
| 5326 | utime.mtime_s = m_sec; |
| 5327 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5328 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5329 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5330 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5331 | PyErr_SetString(PyExc_TypeError, |
| 5332 | "utime: 'ns' must be a tuple of two ints"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5333 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5334 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5335 | utime.now = 0; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5336 | 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] | 5337 | &utime.atime_s, &utime.atime_ns) || |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5338 | !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] | 5339 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5340 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 5341 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5342 | } |
| 5343 | else { |
| 5344 | /* times and ns are both None/unspecified. use "now". */ |
| 5345 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5346 | } |
| 5347 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 5348 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5349 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5350 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5351 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 5352 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5353 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 5354 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 5355 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5356 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5357 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5358 | #if !defined(HAVE_UTIMENSAT) |
| 5359 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 5360 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5361 | "utime: cannot use dir_fd and follow_symlinks " |
| 5362 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5363 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5364 | } |
| 5365 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5366 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5367 | if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None, |
| 5368 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 5369 | return NULL; |
| 5370 | } |
| 5371 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5372 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5373 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5374 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 5375 | NULL, OPEN_EXISTING, |
| 5376 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5377 | Py_END_ALLOW_THREADS |
| 5378 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5379 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5380 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 5381 | } |
| 5382 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5383 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 5384 | GetSystemTimeAsFileTime(&mtime); |
| 5385 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5386 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5387 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 5388 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 5389 | _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] | 5390 | } |
| 5391 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 5392 | /* Avoid putting the file name into the error here, |
| 5393 | as that may confuse the user into believing that |
| 5394 | something is wrong with the file, when it also |
| 5395 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 5396 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5397 | CloseHandle(hFile); |
| 5398 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5399 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5400 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5401 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5402 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 5403 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 5404 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5405 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5406 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5407 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 5408 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5409 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5410 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5411 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5412 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5413 | |
| 5414 | } else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5415 | #endif |
| 5416 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5417 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5418 | if (path->fd != -1) |
| 5419 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5420 | else |
| 5421 | #endif |
| 5422 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5423 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5424 | |
| 5425 | Py_END_ALLOW_THREADS |
| 5426 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5427 | #if defined(__APPLE__) && defined(HAVE_UTIMENSAT) |
| 5428 | /* See utime_dir_fd implementation */ |
| 5429 | if (result == -1 && errno == ENOSYS) { |
| 5430 | argument_unavailable_error(NULL, "dir_fd"); |
| 5431 | return NULL; |
| 5432 | } |
| 5433 | #endif |
| 5434 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5435 | if (result < 0) { |
| 5436 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5437 | posix_error(); |
| 5438 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5439 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5440 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5441 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5442 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5443 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5444 | } |
| 5445 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5446 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5447 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5448 | |
| 5449 | /*[clinic input] |
| 5450 | os._exit |
| 5451 | |
| 5452 | status: int |
| 5453 | |
| 5454 | Exit to the system with specified status, without normal exit processing. |
| 5455 | [clinic start generated code]*/ |
| 5456 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5457 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5458 | os__exit_impl(PyObject *module, int status) |
| 5459 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5460 | { |
| 5461 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5462 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5465 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5466 | #define EXECV_CHAR wchar_t |
| 5467 | #else |
| 5468 | #define EXECV_CHAR char |
| 5469 | #endif |
| 5470 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5471 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5472 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5473 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5474 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5475 | Py_ssize_t i; |
| 5476 | for (i = 0; i < count; i++) |
| 5477 | PyMem_Free(array[i]); |
| 5478 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5479 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5480 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5481 | static int |
| 5482 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5483 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5484 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5485 | PyObject *ub; |
| 5486 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5487 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5488 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5489 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5490 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 5491 | if (*out) |
| 5492 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5493 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5494 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5495 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5496 | size = PyBytes_GET_SIZE(ub); |
| 5497 | *out = PyMem_Malloc(size + 1); |
| 5498 | if (*out) { |
| 5499 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 5500 | result = 1; |
| 5501 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5502 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5503 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5504 | Py_DECREF(ub); |
| 5505 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5506 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5507 | #endif |
| 5508 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5509 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5510 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5511 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5512 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5513 | Py_ssize_t i, pos, envc; |
| 5514 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5515 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5516 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5517 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5518 | i = PyMapping_Size(env); |
| 5519 | if (i < 0) |
| 5520 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5521 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5522 | if (envlist == NULL) { |
| 5523 | PyErr_NoMemory(); |
| 5524 | return NULL; |
| 5525 | } |
| 5526 | envc = 0; |
| 5527 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5528 | if (!keys) |
| 5529 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5530 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5531 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5532 | goto error; |
| 5533 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5534 | PyErr_Format(PyExc_TypeError, |
| 5535 | "env.keys() or env.values() is not a list"); |
| 5536 | goto error; |
| 5537 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5538 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5539 | for (pos = 0; pos < i; pos++) { |
| 5540 | key = PyList_GetItem(keys, pos); |
| 5541 | val = PyList_GetItem(vals, pos); |
| 5542 | if (!key || !val) |
| 5543 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5544 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5545 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5546 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 5547 | goto error; |
| 5548 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 5549 | Py_DECREF(key2); |
| 5550 | goto error; |
| 5551 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5552 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 5553 | defining hidden environment variables. */ |
| 5554 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 5555 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 5556 | { |
| 5557 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5558 | Py_DECREF(key2); |
| 5559 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5560 | goto error; |
| 5561 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5562 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 5563 | #else |
| 5564 | if (!PyUnicode_FSConverter(key, &key2)) |
| 5565 | goto error; |
| 5566 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 5567 | Py_DECREF(key2); |
| 5568 | goto error; |
| 5569 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5570 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 5571 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 5572 | { |
| 5573 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5574 | Py_DECREF(key2); |
| 5575 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5576 | goto error; |
| 5577 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5578 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 5579 | PyBytes_AS_STRING(val2)); |
| 5580 | #endif |
| 5581 | Py_DECREF(key2); |
| 5582 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5583 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5584 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5585 | |
| 5586 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 5587 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5588 | goto error; |
| 5589 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5590 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5591 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5592 | } |
| 5593 | Py_DECREF(vals); |
| 5594 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5595 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5596 | envlist[envc] = 0; |
| 5597 | *envc_ptr = envc; |
| 5598 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5599 | |
| 5600 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5601 | Py_XDECREF(keys); |
| 5602 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5603 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5604 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5605 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5606 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5607 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5608 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5609 | { |
| 5610 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5611 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5612 | if (argvlist == NULL) { |
| 5613 | PyErr_NoMemory(); |
| 5614 | return NULL; |
| 5615 | } |
| 5616 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5617 | PyObject* item = PySequence_ITEM(argv, i); |
| 5618 | if (item == NULL) |
| 5619 | goto fail; |
| 5620 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5621 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5622 | goto fail; |
| 5623 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5624 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5625 | } |
| 5626 | argvlist[*argc] = NULL; |
| 5627 | return argvlist; |
| 5628 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5629 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5630 | free_string_array(argvlist, *argc); |
| 5631 | return NULL; |
| 5632 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5633 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5634 | #endif |
| 5635 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5636 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5637 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5638 | /*[clinic input] |
| 5639 | os.execv |
| 5640 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5641 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5642 | Path of executable file. |
| 5643 | argv: object |
| 5644 | Tuple or list of strings. |
| 5645 | / |
| 5646 | |
| 5647 | Execute an executable path with arguments, replacing current process. |
| 5648 | [clinic start generated code]*/ |
| 5649 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5650 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5651 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5652 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5653 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5654 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5655 | Py_ssize_t argc; |
| 5656 | |
| 5657 | /* execv has two arguments: (path, argv), where |
| 5658 | argv is a list or tuple of strings. */ |
| 5659 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5660 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5661 | PyErr_SetString(PyExc_TypeError, |
| 5662 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5663 | return NULL; |
| 5664 | } |
| 5665 | argc = PySequence_Size(argv); |
| 5666 | if (argc < 1) { |
| 5667 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5668 | return NULL; |
| 5669 | } |
| 5670 | |
| 5671 | argvlist = parse_arglist(argv, &argc); |
| 5672 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5673 | return NULL; |
| 5674 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5675 | if (!argvlist[0][0]) { |
| 5676 | PyErr_SetString(PyExc_ValueError, |
| 5677 | "execv() arg 2 first element cannot be empty"); |
| 5678 | free_string_array(argvlist, argc); |
| 5679 | return NULL; |
| 5680 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5681 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5682 | if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5683 | free_string_array(argvlist, argc); |
| 5684 | return NULL; |
| 5685 | } |
| 5686 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5687 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5688 | #ifdef HAVE_WEXECV |
| 5689 | _wexecv(path->wide, argvlist); |
| 5690 | #else |
| 5691 | execv(path->narrow, argvlist); |
| 5692 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5693 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5694 | |
| 5695 | /* If we get here it's definitely an error */ |
| 5696 | |
| 5697 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5698 | return posix_error(); |
| 5699 | } |
| 5700 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5701 | |
| 5702 | /*[clinic input] |
| 5703 | os.execve |
| 5704 | |
| 5705 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5706 | Path of executable file. |
| 5707 | argv: object |
| 5708 | Tuple or list of strings. |
| 5709 | env: object |
| 5710 | Dictionary of strings mapping to strings. |
| 5711 | |
| 5712 | Execute an executable path with arguments, replacing current process. |
| 5713 | [clinic start generated code]*/ |
| 5714 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5715 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5716 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5717 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5718 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5719 | EXECV_CHAR **argvlist = NULL; |
| 5720 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5721 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5722 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5723 | /* execve has three arguments: (path, argv, env), where |
| 5724 | argv is a list or tuple of strings and env is a dictionary |
| 5725 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5726 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5727 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5728 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5729 | "execve: argv must be a tuple or list"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5730 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5731 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5732 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5733 | if (argc < 1) { |
| 5734 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5735 | return NULL; |
| 5736 | } |
| 5737 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5738 | if (!PyMapping_Check(env)) { |
| 5739 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5740 | "execve: environment must be a mapping object"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5741 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5742 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5743 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5744 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5745 | if (argvlist == NULL) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5746 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5747 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5748 | if (!argvlist[0][0]) { |
| 5749 | PyErr_SetString(PyExc_ValueError, |
| 5750 | "execve: argv first element cannot be empty"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5751 | goto fail_0; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5752 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5753 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5754 | envlist = parse_envlist(env, &envc); |
| 5755 | if (envlist == NULL) |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5756 | goto fail_0; |
| 5757 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5758 | if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5759 | goto fail_1; |
| 5760 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5761 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5762 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5763 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5764 | if (path->fd > -1) |
| 5765 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5766 | else |
| 5767 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5768 | #ifdef HAVE_WEXECV |
| 5769 | _wexecve(path->wide, argvlist, envlist); |
| 5770 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5771 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5772 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5773 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5774 | |
| 5775 | /* If we get here it's definitely an error */ |
| 5776 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5777 | posix_path_error(path); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5778 | fail_1: |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5779 | free_string_array(envlist, envc); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5780 | fail_0: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5781 | if (argvlist) |
| 5782 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5783 | return NULL; |
| 5784 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5785 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5786 | #endif /* HAVE_EXECV */ |
| 5787 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5788 | #ifdef HAVE_POSIX_SPAWN |
| 5789 | |
| 5790 | enum posix_spawn_file_actions_identifier { |
| 5791 | POSIX_SPAWN_OPEN, |
| 5792 | POSIX_SPAWN_CLOSE, |
| 5793 | POSIX_SPAWN_DUP2 |
| 5794 | }; |
| 5795 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5796 | #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] | 5797 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5798 | convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5799 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5800 | |
| 5801 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5802 | parse_posix_spawn_flags(PyObject *module, const char *func_name, PyObject *setpgroup, |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5803 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5804 | PyObject *setsigdef, PyObject *scheduler, |
| 5805 | posix_spawnattr_t *attrp) |
| 5806 | { |
| 5807 | long all_flags = 0; |
| 5808 | |
| 5809 | errno = posix_spawnattr_init(attrp); |
| 5810 | if (errno) { |
| 5811 | posix_error(); |
| 5812 | return -1; |
| 5813 | } |
| 5814 | |
| 5815 | if (setpgroup) { |
| 5816 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5817 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5818 | goto fail; |
| 5819 | } |
| 5820 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5821 | if (errno) { |
| 5822 | posix_error(); |
| 5823 | goto fail; |
| 5824 | } |
| 5825 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5826 | } |
| 5827 | |
| 5828 | if (resetids) { |
| 5829 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5830 | } |
| 5831 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5832 | if (setsid) { |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5833 | #ifdef HAVE_POSIX_SPAWN_SETSID_RUNTIME |
| 5834 | if (HAVE_POSIX_SPAWN_SETSID_RUNTIME) { |
| 5835 | #endif |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5836 | #ifdef POSIX_SPAWN_SETSID |
| 5837 | all_flags |= POSIX_SPAWN_SETSID; |
| 5838 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5839 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5840 | #else |
| 5841 | argument_unavailable_error(func_name, "setsid"); |
| 5842 | return -1; |
| 5843 | #endif |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 5844 | |
| 5845 | #ifdef HAVE_POSIX_SPAWN_SETSID_RUNTIME |
| 5846 | } else { |
| 5847 | argument_unavailable_error(func_name, "setsid"); |
| 5848 | return -1; |
| 5849 | } |
| 5850 | #endif /* HAVE_POSIX_SPAWN_SETSID_RUNTIME */ |
| 5851 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5852 | } |
| 5853 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5854 | if (setsigmask) { |
| 5855 | sigset_t set; |
| 5856 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5857 | goto fail; |
| 5858 | } |
| 5859 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5860 | if (errno) { |
| 5861 | posix_error(); |
| 5862 | goto fail; |
| 5863 | } |
| 5864 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5865 | } |
| 5866 | |
| 5867 | if (setsigdef) { |
| 5868 | sigset_t set; |
| 5869 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5870 | goto fail; |
| 5871 | } |
| 5872 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5873 | if (errno) { |
| 5874 | posix_error(); |
| 5875 | goto fail; |
| 5876 | } |
| 5877 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5878 | } |
| 5879 | |
| 5880 | if (scheduler) { |
| 5881 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5882 | PyObject *py_schedpolicy; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5883 | PyObject *schedparam_obj; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5884 | struct sched_param schedparam; |
| 5885 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5886 | if (!PyArg_ParseTuple(scheduler, "OO" |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5887 | ";A scheduler tuple must have two elements", |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 5888 | &py_schedpolicy, &schedparam_obj)) { |
| 5889 | goto fail; |
| 5890 | } |
| 5891 | if (!convert_sched_param(module, schedparam_obj, &schedparam)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5892 | goto fail; |
| 5893 | } |
| 5894 | if (py_schedpolicy != Py_None) { |
| 5895 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5896 | |
| 5897 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5898 | goto fail; |
| 5899 | } |
| 5900 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5901 | if (errno) { |
| 5902 | posix_error(); |
| 5903 | goto fail; |
| 5904 | } |
| 5905 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5906 | } |
| 5907 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5908 | if (errno) { |
| 5909 | posix_error(); |
| 5910 | goto fail; |
| 5911 | } |
| 5912 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5913 | #else |
| 5914 | PyErr_SetString(PyExc_NotImplementedError, |
| 5915 | "The scheduler option is not supported in this system."); |
| 5916 | goto fail; |
| 5917 | #endif |
| 5918 | } |
| 5919 | |
| 5920 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5921 | if (errno) { |
| 5922 | posix_error(); |
| 5923 | goto fail; |
| 5924 | } |
| 5925 | |
| 5926 | return 0; |
| 5927 | |
| 5928 | fail: |
| 5929 | (void)posix_spawnattr_destroy(attrp); |
| 5930 | return -1; |
| 5931 | } |
| 5932 | |
| 5933 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5934 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5935 | posix_spawn_file_actions_t *file_actionsp, |
| 5936 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5937 | { |
| 5938 | PyObject *seq; |
| 5939 | PyObject *file_action = NULL; |
| 5940 | PyObject *tag_obj; |
| 5941 | |
| 5942 | seq = PySequence_Fast(file_actions, |
| 5943 | "file_actions must be a sequence or None"); |
| 5944 | if (seq == NULL) { |
| 5945 | return -1; |
| 5946 | } |
| 5947 | |
| 5948 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5949 | if (errno) { |
| 5950 | posix_error(); |
| 5951 | Py_DECREF(seq); |
| 5952 | return -1; |
| 5953 | } |
| 5954 | |
Zackery Spytz | d52a83a | 2019-06-26 14:54:20 -0600 | [diff] [blame] | 5955 | 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] | 5956 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5957 | Py_INCREF(file_action); |
| 5958 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5959 | PyErr_SetString(PyExc_TypeError, |
| 5960 | "Each file_actions element must be a non-empty tuple"); |
| 5961 | goto fail; |
| 5962 | } |
| 5963 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5964 | if (tag == -1 && PyErr_Occurred()) { |
| 5965 | goto fail; |
| 5966 | } |
| 5967 | |
| 5968 | /* Populate the file_actions object */ |
| 5969 | switch (tag) { |
| 5970 | case POSIX_SPAWN_OPEN: { |
| 5971 | int fd, oflag; |
| 5972 | PyObject *path; |
| 5973 | unsigned long mode; |
| 5974 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5975 | ";A open file_action tuple must have 5 elements", |
| 5976 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5977 | &oflag, &mode)) |
| 5978 | { |
| 5979 | goto fail; |
| 5980 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5981 | if (PyList_Append(temp_buffer, path)) { |
| 5982 | Py_DECREF(path); |
| 5983 | goto fail; |
| 5984 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5985 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5986 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5987 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5988 | if (errno) { |
| 5989 | posix_error(); |
| 5990 | goto fail; |
| 5991 | } |
| 5992 | break; |
| 5993 | } |
| 5994 | case POSIX_SPAWN_CLOSE: { |
| 5995 | int fd; |
| 5996 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5997 | ";A close file_action tuple must have 2 elements", |
| 5998 | &tag_obj, &fd)) |
| 5999 | { |
| 6000 | goto fail; |
| 6001 | } |
| 6002 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 6003 | if (errno) { |
| 6004 | posix_error(); |
| 6005 | goto fail; |
| 6006 | } |
| 6007 | break; |
| 6008 | } |
| 6009 | case POSIX_SPAWN_DUP2: { |
| 6010 | int fd1, fd2; |
| 6011 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 6012 | ";A dup2 file_action tuple must have 3 elements", |
| 6013 | &tag_obj, &fd1, &fd2)) |
| 6014 | { |
| 6015 | goto fail; |
| 6016 | } |
| 6017 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 6018 | fd1, fd2); |
| 6019 | if (errno) { |
| 6020 | posix_error(); |
| 6021 | goto fail; |
| 6022 | } |
| 6023 | break; |
| 6024 | } |
| 6025 | default: { |
| 6026 | PyErr_SetString(PyExc_TypeError, |
| 6027 | "Unknown file_actions identifier"); |
| 6028 | goto fail; |
| 6029 | } |
| 6030 | } |
| 6031 | Py_DECREF(file_action); |
| 6032 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 6033 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 6034 | Py_DECREF(seq); |
| 6035 | return 0; |
| 6036 | |
| 6037 | fail: |
| 6038 | Py_DECREF(seq); |
| 6039 | Py_DECREF(file_action); |
| 6040 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 6041 | return -1; |
| 6042 | } |
| 6043 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6044 | |
| 6045 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 6046 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 6047 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 6048 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 6049 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6050 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 6051 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6052 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6053 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 6054 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6055 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 6056 | posix_spawnattr_t attr; |
| 6057 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6058 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 6059 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 6060 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 6061 | pid_t pid; |
| 6062 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6063 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 6064 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 6065 | 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] | 6066 | like posix.environ. */ |
| 6067 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 6068 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 6069 | PyErr_Format(PyExc_TypeError, |
| 6070 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6071 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6072 | } |
| 6073 | argc = PySequence_Size(argv); |
| 6074 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 6075 | PyErr_Format(PyExc_ValueError, |
| 6076 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6077 | return NULL; |
| 6078 | } |
| 6079 | |
| 6080 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 6081 | PyErr_Format(PyExc_TypeError, |
| 6082 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6083 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6084 | } |
| 6085 | |
| 6086 | argvlist = parse_arglist(argv, &argc); |
| 6087 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6088 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6089 | } |
| 6090 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 6091 | PyErr_Format(PyExc_ValueError, |
| 6092 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6093 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6094 | } |
| 6095 | |
| 6096 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6097 | if (envlist == NULL) { |
| 6098 | goto exit; |
| 6099 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6100 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 6101 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 6102 | /* There is a bug in old versions of glibc that makes some of the |
| 6103 | * helper functions for manipulating file actions not copy the provided |
| 6104 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 6105 | * copy the value of path for some old versions of glibc (<2.20). |
| 6106 | * The use of temp_buffer here is a workaround that keeps the |
| 6107 | * python objects that own the buffers alive until posix_spawn gets called. |
| 6108 | * Check https://bugs.python.org/issue33630 and |
| 6109 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 6110 | temp_buffer = PyList_New(0); |
| 6111 | if (!temp_buffer) { |
| 6112 | goto exit; |
| 6113 | } |
| 6114 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6115 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6116 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 6117 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6118 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6119 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6120 | if (parse_posix_spawn_flags(module, func_name, setpgroup, resetids, setsid, |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 6121 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 6122 | goto exit; |
| 6123 | } |
| 6124 | attrp = &attr; |
| 6125 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6126 | if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6127 | goto exit; |
| 6128 | } |
| 6129 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6130 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 6131 | #ifdef HAVE_POSIX_SPAWNP |
| 6132 | if (use_posix_spawnp) { |
| 6133 | err_code = posix_spawnp(&pid, path->narrow, |
| 6134 | file_actionsp, attrp, argvlist, envlist); |
| 6135 | } |
| 6136 | else |
| 6137 | #endif /* HAVE_POSIX_SPAWNP */ |
| 6138 | { |
| 6139 | err_code = posix_spawn(&pid, path->narrow, |
| 6140 | file_actionsp, attrp, argvlist, envlist); |
| 6141 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6142 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 6143 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 6144 | if (err_code) { |
| 6145 | errno = err_code; |
| 6146 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6147 | goto exit; |
| 6148 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6149 | #ifdef _Py_MEMORY_SANITIZER |
| 6150 | __msan_unpoison(&pid, sizeof(pid)); |
| 6151 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6152 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6153 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6154 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 6155 | if (file_actionsp) { |
| 6156 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6157 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 6158 | if (attrp) { |
| 6159 | (void)posix_spawnattr_destroy(attrp); |
| 6160 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6161 | if (envlist) { |
| 6162 | free_string_array(envlist, envc); |
| 6163 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6164 | if (argvlist) { |
| 6165 | free_string_array(argvlist, argc); |
| 6166 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 6167 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 6168 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6169 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 6170 | |
| 6171 | |
| 6172 | /*[clinic input] |
| 6173 | |
| 6174 | os.posix_spawn |
| 6175 | path: path_t |
| 6176 | Path of executable file. |
| 6177 | argv: object |
| 6178 | Tuple or list of strings. |
| 6179 | env: object |
| 6180 | Dictionary of strings mapping to strings. |
| 6181 | / |
| 6182 | * |
| 6183 | file_actions: object(c_default='NULL') = () |
| 6184 | A sequence of file action tuples. |
| 6185 | setpgroup: object = NULL |
| 6186 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 6187 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 6188 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 6189 | setsid: bool(accept={int}) = False |
| 6190 | 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] | 6191 | setsigmask: object(c_default='NULL') = () |
| 6192 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 6193 | setsigdef: object(c_default='NULL') = () |
| 6194 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 6195 | scheduler: object = NULL |
| 6196 | A tuple with the scheduler policy (optional) and parameters. |
| 6197 | |
| 6198 | Execute the program specified by path in a new process. |
| 6199 | [clinic start generated code]*/ |
| 6200 | |
| 6201 | static PyObject * |
| 6202 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 6203 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 6204 | PyObject *setpgroup, int resetids, int setsid, |
| 6205 | PyObject *setsigmask, PyObject *setsigdef, |
| 6206 | PyObject *scheduler) |
| 6207 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 6208 | { |
| 6209 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 6210 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 6211 | scheduler); |
| 6212 | } |
| 6213 | #endif /* HAVE_POSIX_SPAWN */ |
| 6214 | |
| 6215 | |
| 6216 | |
| 6217 | #ifdef HAVE_POSIX_SPAWNP |
| 6218 | /*[clinic input] |
| 6219 | |
| 6220 | os.posix_spawnp |
| 6221 | path: path_t |
| 6222 | Path of executable file. |
| 6223 | argv: object |
| 6224 | Tuple or list of strings. |
| 6225 | env: object |
| 6226 | Dictionary of strings mapping to strings. |
| 6227 | / |
| 6228 | * |
| 6229 | file_actions: object(c_default='NULL') = () |
| 6230 | A sequence of file action tuples. |
| 6231 | setpgroup: object = NULL |
| 6232 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 6233 | resetids: bool(accept={int}) = False |
| 6234 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 6235 | setsid: bool(accept={int}) = False |
| 6236 | 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] | 6237 | setsigmask: object(c_default='NULL') = () |
| 6238 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 6239 | setsigdef: object(c_default='NULL') = () |
| 6240 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 6241 | scheduler: object = NULL |
| 6242 | A tuple with the scheduler policy (optional) and parameters. |
| 6243 | |
| 6244 | Execute the program specified by path in a new process. |
| 6245 | [clinic start generated code]*/ |
| 6246 | |
| 6247 | static PyObject * |
| 6248 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 6249 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 6250 | PyObject *setpgroup, int resetids, int setsid, |
| 6251 | PyObject *setsigmask, PyObject *setsigdef, |
| 6252 | PyObject *scheduler) |
| 6253 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 6254 | { |
| 6255 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 6256 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 6257 | scheduler); |
| 6258 | } |
| 6259 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 6260 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6261 | #ifdef HAVE_RTPSPAWN |
| 6262 | static intptr_t |
| 6263 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], |
| 6264 | const char *envp[]) |
| 6265 | { |
| 6266 | RTP_ID rtpid; |
| 6267 | int status; |
| 6268 | pid_t res; |
| 6269 | int async_err = 0; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6270 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6271 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
| 6272 | uStackSize=0 cannot be used, the default stack size is too small for |
| 6273 | Python. */ |
| 6274 | if (envp) { |
| 6275 | rtpid = rtpSpawn(rtpFileName, argv, envp, |
| 6276 | 100, 0x1000000, 0, VX_FP_TASK); |
| 6277 | } |
| 6278 | else { |
| 6279 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, |
| 6280 | 100, 0x1000000, 0, VX_FP_TASK); |
| 6281 | } |
| 6282 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { |
| 6283 | do { |
| 6284 | res = waitpid((pid_t)rtpid, &status, 0); |
| 6285 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 6286 | |
| 6287 | if (res < 0) |
| 6288 | return RTP_ID_ERROR; |
| 6289 | return ((intptr_t)status); |
| 6290 | } |
| 6291 | return ((intptr_t)rtpid); |
| 6292 | } |
| 6293 | #endif |
| 6294 | |
| 6295 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6296 | /*[clinic input] |
| 6297 | os.spawnv |
| 6298 | |
| 6299 | mode: int |
| 6300 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6301 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6302 | Path of executable file. |
| 6303 | argv: object |
| 6304 | Tuple or list of strings. |
| 6305 | / |
| 6306 | |
| 6307 | Execute the program specified by path in a new process. |
| 6308 | [clinic start generated code]*/ |
| 6309 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6310 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6311 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 6312 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6313 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6314 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6315 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6316 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6317 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6318 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6319 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6320 | /* spawnv has three arguments: (mode, path, argv), where |
| 6321 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6322 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6323 | if (PyList_Check(argv)) { |
| 6324 | argc = PyList_Size(argv); |
| 6325 | getitem = PyList_GetItem; |
| 6326 | } |
| 6327 | else if (PyTuple_Check(argv)) { |
| 6328 | argc = PyTuple_Size(argv); |
| 6329 | getitem = PyTuple_GetItem; |
| 6330 | } |
| 6331 | else { |
| 6332 | PyErr_SetString(PyExc_TypeError, |
| 6333 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6334 | return NULL; |
| 6335 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 6336 | if (argc == 0) { |
| 6337 | PyErr_SetString(PyExc_ValueError, |
| 6338 | "spawnv() arg 2 cannot be empty"); |
| 6339 | return NULL; |
| 6340 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6341 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6342 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6343 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6344 | return PyErr_NoMemory(); |
| 6345 | } |
| 6346 | for (i = 0; i < argc; i++) { |
| 6347 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 6348 | &argvlist[i])) { |
| 6349 | free_string_array(argvlist, i); |
| 6350 | PyErr_SetString( |
| 6351 | PyExc_TypeError, |
| 6352 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6353 | return NULL; |
| 6354 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 6355 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 6356 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 6357 | PyErr_SetString( |
| 6358 | PyExc_ValueError, |
| 6359 | "spawnv() arg 2 first element cannot be empty"); |
| 6360 | return NULL; |
| 6361 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6362 | } |
| 6363 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6364 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6365 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6366 | if (mode == _OLD_P_OVERLAY) |
| 6367 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6368 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6369 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6370 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6371 | Py_None) < 0) { |
| 6372 | free_string_array(argvlist, argc); |
| 6373 | return NULL; |
| 6374 | } |
| 6375 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6376 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6377 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6378 | #ifdef HAVE_WSPAWNV |
| 6379 | spawnval = _wspawnv(mode, path->wide, argvlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6380 | #elif defined(HAVE_RTPSPAWN) |
| 6381 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6382 | #else |
| 6383 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 6384 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6385 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6386 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6387 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6388 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6389 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6390 | if (spawnval == -1) |
| 6391 | return posix_error(); |
| 6392 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6393 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6394 | } |
| 6395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6396 | /*[clinic input] |
| 6397 | os.spawnve |
| 6398 | |
| 6399 | mode: int |
| 6400 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6401 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6402 | Path of executable file. |
| 6403 | argv: object |
| 6404 | Tuple or list of strings. |
| 6405 | env: object |
| 6406 | Dictionary of strings mapping to strings. |
| 6407 | / |
| 6408 | |
| 6409 | Execute the program specified by path in a new process. |
| 6410 | [clinic start generated code]*/ |
| 6411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6412 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6413 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6414 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6415 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6416 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6417 | EXECV_CHAR **argvlist; |
| 6418 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6419 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 6420 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6421 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6422 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6423 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6424 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6425 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 6426 | argv is a list or tuple of strings and env is a dictionary |
| 6427 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6428 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6429 | if (PyList_Check(argv)) { |
| 6430 | argc = PyList_Size(argv); |
| 6431 | getitem = PyList_GetItem; |
| 6432 | } |
| 6433 | else if (PyTuple_Check(argv)) { |
| 6434 | argc = PyTuple_Size(argv); |
| 6435 | getitem = PyTuple_GetItem; |
| 6436 | } |
| 6437 | else { |
| 6438 | PyErr_SetString(PyExc_TypeError, |
| 6439 | "spawnve() arg 2 must be a tuple or list"); |
| 6440 | goto fail_0; |
| 6441 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 6442 | if (argc == 0) { |
| 6443 | PyErr_SetString(PyExc_ValueError, |
| 6444 | "spawnve() arg 2 cannot be empty"); |
| 6445 | goto fail_0; |
| 6446 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6447 | if (!PyMapping_Check(env)) { |
| 6448 | PyErr_SetString(PyExc_TypeError, |
| 6449 | "spawnve() arg 3 must be a mapping object"); |
| 6450 | goto fail_0; |
| 6451 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6452 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6453 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6454 | if (argvlist == NULL) { |
| 6455 | PyErr_NoMemory(); |
| 6456 | goto fail_0; |
| 6457 | } |
| 6458 | for (i = 0; i < argc; i++) { |
| 6459 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 6460 | &argvlist[i])) |
| 6461 | { |
| 6462 | lastarg = i; |
| 6463 | goto fail_1; |
| 6464 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6465 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6466 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6467 | PyErr_SetString( |
| 6468 | PyExc_ValueError, |
| 6469 | "spawnv() arg 2 first element cannot be empty"); |
| 6470 | goto fail_1; |
| 6471 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6472 | } |
| 6473 | lastarg = argc; |
| 6474 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6475 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6476 | envlist = parse_envlist(env, &envc); |
| 6477 | if (envlist == NULL) |
| 6478 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6479 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6480 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6481 | if (mode == _OLD_P_OVERLAY) |
| 6482 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6483 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6484 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6485 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6486 | goto fail_2; |
| 6487 | } |
| 6488 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6489 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6490 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6491 | #ifdef HAVE_WSPAWNV |
| 6492 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6493 | #elif defined(HAVE_RTPSPAWN) |
| 6494 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, |
| 6495 | (const char **)envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6496 | #else |
| 6497 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 6498 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6499 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6500 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6501 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6502 | if (spawnval == -1) |
| 6503 | (void) posix_error(); |
| 6504 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6505 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6506 | |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6507 | fail_2: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6508 | while (--envc >= 0) |
| 6509 | PyMem_DEL(envlist[envc]); |
| 6510 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 6511 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6512 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6513 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6514 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6515 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6516 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6517 | #endif /* HAVE_SPAWNV */ |
| 6518 | |
| 6519 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6520 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6521 | |
| 6522 | /* Helper function to validate arguments. |
| 6523 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 6524 | If obj is non-NULL it must be callable. */ |
| 6525 | static int |
| 6526 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 6527 | { |
| 6528 | if (obj && !PyCallable_Check(obj)) { |
| 6529 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6530 | obj_name, _PyType_Name(Py_TYPE(obj))); |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6531 | return -1; |
| 6532 | } |
| 6533 | return 0; |
| 6534 | } |
| 6535 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6536 | /*[clinic input] |
| 6537 | os.register_at_fork |
| 6538 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6539 | * |
| 6540 | before: object=NULL |
| 6541 | A callable to be called in the parent before the fork() syscall. |
| 6542 | after_in_child: object=NULL |
| 6543 | A callable to be called in the child after fork(). |
| 6544 | after_in_parent: object=NULL |
| 6545 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6546 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6547 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6548 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6549 | 'before' callbacks are called in reverse order. |
| 6550 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6551 | |
| 6552 | [clinic start generated code]*/ |
| 6553 | |
| 6554 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6555 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 6556 | PyObject *after_in_child, PyObject *after_in_parent) |
| 6557 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6558 | { |
| 6559 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6560 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6561 | if (!before && !after_in_child && !after_in_parent) { |
| 6562 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 6563 | return NULL; |
| 6564 | } |
| 6565 | if (check_null_or_callable(before, "before") || |
| 6566 | check_null_or_callable(after_in_child, "after_in_child") || |
| 6567 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6568 | return NULL; |
| 6569 | } |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6570 | interp = _PyInterpreterState_GET(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6571 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6572 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6573 | return NULL; |
| 6574 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6575 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6576 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6577 | } |
| 6578 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 6579 | return NULL; |
| 6580 | } |
| 6581 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6582 | } |
| 6583 | #endif /* HAVE_FORK */ |
| 6584 | |
| 6585 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6586 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6587 | /*[clinic input] |
| 6588 | os.fork1 |
| 6589 | |
| 6590 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 6591 | |
| 6592 | Return 0 to child process and PID of child to parent process. |
| 6593 | [clinic start generated code]*/ |
| 6594 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6595 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6596 | os_fork1_impl(PyObject *module) |
| 6597 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6598 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6599 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6600 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 6601 | if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6602 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6603 | return NULL; |
| 6604 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6605 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6606 | pid = fork1(); |
| 6607 | if (pid == 0) { |
| 6608 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6609 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6610 | } else { |
| 6611 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6612 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6613 | } |
| 6614 | if (pid == -1) |
| 6615 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6616 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6617 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6618 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6619 | |
| 6620 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6621 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6622 | /*[clinic input] |
| 6623 | os.fork |
| 6624 | |
| 6625 | Fork a child process. |
| 6626 | |
| 6627 | Return 0 to child process and PID of child to parent process. |
| 6628 | [clinic start generated code]*/ |
| 6629 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6630 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6631 | os_fork_impl(PyObject *module) |
| 6632 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6633 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6634 | pid_t pid; |
Victor Stinner | 252346a | 2020-05-01 11:33:44 +0200 | [diff] [blame] | 6635 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 6636 | if (interp->config._isolated_interpreter) { |
| 6637 | PyErr_SetString(PyExc_RuntimeError, |
| 6638 | "fork not supported for isolated subinterpreters"); |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6639 | return NULL; |
| 6640 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6641 | if (PySys_Audit("os.fork", NULL) < 0) { |
| 6642 | return NULL; |
| 6643 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6644 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6645 | pid = fork(); |
| 6646 | if (pid == 0) { |
| 6647 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6648 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6649 | } else { |
| 6650 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6651 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6652 | } |
| 6653 | if (pid == -1) |
| 6654 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6655 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6656 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6657 | #endif /* HAVE_FORK */ |
| 6658 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6659 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6660 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6661 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6662 | /*[clinic input] |
| 6663 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6664 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6665 | policy: int |
| 6666 | |
| 6667 | Get the maximum scheduling priority for policy. |
| 6668 | [clinic start generated code]*/ |
| 6669 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6670 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6671 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 6672 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6673 | { |
| 6674 | int max; |
| 6675 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6676 | max = sched_get_priority_max(policy); |
| 6677 | if (max < 0) |
| 6678 | return posix_error(); |
| 6679 | return PyLong_FromLong(max); |
| 6680 | } |
| 6681 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6682 | |
| 6683 | /*[clinic input] |
| 6684 | os.sched_get_priority_min |
| 6685 | |
| 6686 | policy: int |
| 6687 | |
| 6688 | Get the minimum scheduling priority for policy. |
| 6689 | [clinic start generated code]*/ |
| 6690 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6691 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6692 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 6693 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6694 | { |
| 6695 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6696 | if (min < 0) |
| 6697 | return posix_error(); |
| 6698 | return PyLong_FromLong(min); |
| 6699 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6700 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 6701 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6702 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6703 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 6704 | /*[clinic input] |
| 6705 | os.sched_getscheduler |
| 6706 | pid: pid_t |
| 6707 | / |
| 6708 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6709 | Get the scheduling policy for the process identified by pid. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6710 | |
| 6711 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6712 | [clinic start generated code]*/ |
| 6713 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6714 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6715 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6716 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6717 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6718 | int policy; |
| 6719 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6720 | policy = sched_getscheduler(pid); |
| 6721 | if (policy < 0) |
| 6722 | return posix_error(); |
| 6723 | return PyLong_FromLong(policy); |
| 6724 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6725 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6726 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6727 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6728 | #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] | 6729 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6730 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6731 | |
| 6732 | @classmethod |
| 6733 | os.sched_param.__new__ |
| 6734 | |
| 6735 | sched_priority: object |
| 6736 | A scheduling parameter. |
| 6737 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6738 | Currently has only one field: sched_priority |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6739 | [clinic start generated code]*/ |
| 6740 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6741 | static PyObject * |
| 6742 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6743 | /*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6744 | { |
| 6745 | PyObject *res; |
| 6746 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6747 | res = PyStructSequence_New(type); |
| 6748 | if (!res) |
| 6749 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6750 | Py_INCREF(sched_priority); |
| 6751 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6752 | return res; |
| 6753 | } |
| 6754 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6755 | PyDoc_VAR(os_sched_param__doc__); |
| 6756 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6757 | static PyStructSequence_Field sched_param_fields[] = { |
| 6758 | {"sched_priority", "the scheduling priority"}, |
| 6759 | {0} |
| 6760 | }; |
| 6761 | |
| 6762 | static PyStructSequence_Desc sched_param_desc = { |
| 6763 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6764 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6765 | sched_param_fields, |
| 6766 | 1 |
| 6767 | }; |
| 6768 | |
| 6769 | static int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6770 | convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6771 | { |
| 6772 | long priority; |
| 6773 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6774 | if (!Py_IS_TYPE(param, (PyTypeObject *)get_posix_state(module)->SchedParamType)) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6775 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6776 | return 0; |
| 6777 | } |
| 6778 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6779 | if (priority == -1 && PyErr_Occurred()) |
| 6780 | return 0; |
| 6781 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6782 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6783 | return 0; |
| 6784 | } |
| 6785 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6786 | return 1; |
| 6787 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6788 | #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] | 6789 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6790 | |
| 6791 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6792 | /*[clinic input] |
| 6793 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6794 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6795 | pid: pid_t |
| 6796 | policy: int |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6797 | param as param_obj: object |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6798 | / |
| 6799 | |
| 6800 | Set the scheduling policy for the process identified by pid. |
| 6801 | |
| 6802 | If pid is 0, the calling process is changed. |
| 6803 | param is an instance of sched_param. |
| 6804 | [clinic start generated code]*/ |
| 6805 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6806 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6807 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6808 | PyObject *param_obj) |
| 6809 | /*[clinic end generated code: output=cde27faa55dc993e input=73013d731bd8fbe9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6810 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6811 | struct sched_param param; |
| 6812 | if (!convert_sched_param(module, param_obj, ¶m)) { |
| 6813 | return NULL; |
| 6814 | } |
| 6815 | |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6816 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6817 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6818 | ** scheduling policy under Solaris/Illumos, and others. |
| 6819 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6820 | */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6821 | if (sched_setscheduler(pid, policy, ¶m) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6822 | return posix_error(); |
| 6823 | Py_RETURN_NONE; |
| 6824 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6825 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6826 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6827 | |
| 6828 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6829 | /*[clinic input] |
| 6830 | os.sched_getparam |
| 6831 | pid: pid_t |
| 6832 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6833 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6834 | Returns scheduling parameters for the process identified by pid. |
| 6835 | |
| 6836 | If pid is 0, returns parameters for the calling process. |
| 6837 | Return value is an instance of sched_param. |
| 6838 | [clinic start generated code]*/ |
| 6839 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6840 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6841 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6842 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6843 | { |
| 6844 | struct sched_param param; |
| 6845 | PyObject *result; |
| 6846 | PyObject *priority; |
| 6847 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6848 | if (sched_getparam(pid, ¶m)) |
| 6849 | return posix_error(); |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6850 | PyObject *SchedParamType = get_posix_state(module)->SchedParamType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6851 | result = PyStructSequence_New((PyTypeObject *)SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6852 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6853 | return NULL; |
| 6854 | priority = PyLong_FromLong(param.sched_priority); |
| 6855 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6856 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6857 | return NULL; |
| 6858 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6859 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6860 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6861 | } |
| 6862 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6863 | |
| 6864 | /*[clinic input] |
| 6865 | os.sched_setparam |
| 6866 | pid: pid_t |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6867 | param as param_obj: object |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6868 | / |
| 6869 | |
| 6870 | Set scheduling parameters for the process identified by pid. |
| 6871 | |
| 6872 | If pid is 0, sets parameters for the calling process. |
| 6873 | param should be an instance of sched_param. |
| 6874 | [clinic start generated code]*/ |
| 6875 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6876 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6877 | os_sched_setparam_impl(PyObject *module, pid_t pid, PyObject *param_obj) |
| 6878 | /*[clinic end generated code: output=f19fe020a53741c1 input=27b98337c8b2dcc7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6879 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 6880 | struct sched_param param; |
| 6881 | if (!convert_sched_param(module, param_obj, ¶m)) { |
| 6882 | return NULL; |
| 6883 | } |
| 6884 | |
| 6885 | if (sched_setparam(pid, ¶m)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6886 | return posix_error(); |
| 6887 | Py_RETURN_NONE; |
| 6888 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6889 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6890 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6891 | |
| 6892 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6893 | /*[clinic input] |
| 6894 | os.sched_rr_get_interval -> double |
| 6895 | pid: pid_t |
| 6896 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6897 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6898 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6899 | |
| 6900 | Value returned is a float. |
| 6901 | [clinic start generated code]*/ |
| 6902 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6903 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6904 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6905 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6906 | { |
| 6907 | struct timespec interval; |
| 6908 | if (sched_rr_get_interval(pid, &interval)) { |
| 6909 | posix_error(); |
| 6910 | return -1.0; |
| 6911 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6912 | #ifdef _Py_MEMORY_SANITIZER |
| 6913 | __msan_unpoison(&interval, sizeof(interval)); |
| 6914 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6915 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6916 | } |
| 6917 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6918 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6919 | |
| 6920 | /*[clinic input] |
| 6921 | os.sched_yield |
| 6922 | |
| 6923 | Voluntarily relinquish the CPU. |
| 6924 | [clinic start generated code]*/ |
| 6925 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6926 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6927 | os_sched_yield_impl(PyObject *module) |
| 6928 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6929 | { |
| 6930 | if (sched_yield()) |
| 6931 | return posix_error(); |
| 6932 | Py_RETURN_NONE; |
| 6933 | } |
| 6934 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6935 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6936 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6937 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6938 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6939 | /*[clinic input] |
| 6940 | os.sched_setaffinity |
| 6941 | pid: pid_t |
| 6942 | mask : object |
| 6943 | / |
| 6944 | |
| 6945 | Set the CPU affinity of the process identified by pid to mask. |
| 6946 | |
| 6947 | mask should be an iterable of integers identifying CPUs. |
| 6948 | [clinic start generated code]*/ |
| 6949 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6950 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6951 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6952 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6953 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6954 | int ncpus; |
| 6955 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6956 | cpu_set_t *cpu_set = NULL; |
| 6957 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6958 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6959 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6960 | if (iterator == NULL) |
| 6961 | return NULL; |
| 6962 | |
| 6963 | ncpus = NCPUS_START; |
| 6964 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6965 | cpu_set = CPU_ALLOC(ncpus); |
| 6966 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6967 | PyErr_NoMemory(); |
| 6968 | goto error; |
| 6969 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6970 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6971 | |
| 6972 | while ((item = PyIter_Next(iterator))) { |
| 6973 | long cpu; |
| 6974 | if (!PyLong_Check(item)) { |
| 6975 | PyErr_Format(PyExc_TypeError, |
| 6976 | "expected an iterator of ints, " |
| 6977 | "but iterator yielded %R", |
| 6978 | Py_TYPE(item)); |
| 6979 | Py_DECREF(item); |
| 6980 | goto error; |
| 6981 | } |
| 6982 | cpu = PyLong_AsLong(item); |
| 6983 | Py_DECREF(item); |
| 6984 | if (cpu < 0) { |
| 6985 | if (!PyErr_Occurred()) |
| 6986 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6987 | goto error; |
| 6988 | } |
| 6989 | if (cpu > INT_MAX - 1) { |
| 6990 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6991 | goto error; |
| 6992 | } |
| 6993 | if (cpu >= ncpus) { |
| 6994 | /* Grow CPU mask to fit the CPU number */ |
| 6995 | int newncpus = ncpus; |
| 6996 | cpu_set_t *newmask; |
| 6997 | size_t newsetsize; |
| 6998 | while (newncpus <= cpu) { |
| 6999 | if (newncpus > INT_MAX / 2) |
| 7000 | newncpus = cpu + 1; |
| 7001 | else |
| 7002 | newncpus = newncpus * 2; |
| 7003 | } |
| 7004 | newmask = CPU_ALLOC(newncpus); |
| 7005 | if (newmask == NULL) { |
| 7006 | PyErr_NoMemory(); |
| 7007 | goto error; |
| 7008 | } |
| 7009 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 7010 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7011 | memcpy(newmask, cpu_set, setsize); |
| 7012 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7013 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7014 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7015 | ncpus = newncpus; |
| 7016 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7017 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7018 | } |
Brandt Bucher | 45a30af | 2019-06-27 09:10:57 -0700 | [diff] [blame] | 7019 | if (PyErr_Occurred()) { |
| 7020 | goto error; |
| 7021 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7022 | Py_CLEAR(iterator); |
| 7023 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7024 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7025 | posix_error(); |
| 7026 | goto error; |
| 7027 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7028 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7029 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7030 | |
| 7031 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7032 | if (cpu_set) |
| 7033 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7034 | Py_XDECREF(iterator); |
| 7035 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7036 | } |
| 7037 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7038 | |
| 7039 | /*[clinic input] |
| 7040 | os.sched_getaffinity |
| 7041 | pid: pid_t |
| 7042 | / |
| 7043 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 7044 | 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] | 7045 | |
| 7046 | The affinity is returned as a set of CPU identifiers. |
| 7047 | [clinic start generated code]*/ |
| 7048 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7049 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7050 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 7051 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7052 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7053 | int cpu, ncpus, count; |
| 7054 | size_t setsize; |
| 7055 | cpu_set_t *mask = NULL; |
| 7056 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7057 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7058 | ncpus = NCPUS_START; |
| 7059 | while (1) { |
| 7060 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 7061 | mask = CPU_ALLOC(ncpus); |
| 7062 | if (mask == NULL) |
| 7063 | return PyErr_NoMemory(); |
| 7064 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 7065 | break; |
| 7066 | CPU_FREE(mask); |
| 7067 | if (errno != EINVAL) |
| 7068 | return posix_error(); |
| 7069 | if (ncpus > INT_MAX / 2) { |
| 7070 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 7071 | "a large enough CPU set"); |
| 7072 | return NULL; |
| 7073 | } |
| 7074 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7075 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7076 | |
| 7077 | res = PySet_New(NULL); |
| 7078 | if (res == NULL) |
| 7079 | goto error; |
| 7080 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 7081 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 7082 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 7083 | --count; |
| 7084 | if (cpu_num == NULL) |
| 7085 | goto error; |
| 7086 | if (PySet_Add(res, cpu_num)) { |
| 7087 | Py_DECREF(cpu_num); |
| 7088 | goto error; |
| 7089 | } |
| 7090 | Py_DECREF(cpu_num); |
| 7091 | } |
| 7092 | } |
| 7093 | CPU_FREE(mask); |
| 7094 | return res; |
| 7095 | |
| 7096 | error: |
| 7097 | if (mask) |
| 7098 | CPU_FREE(mask); |
| 7099 | Py_XDECREF(res); |
| 7100 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7101 | } |
| 7102 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 7103 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 7104 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7105 | #endif /* HAVE_SCHED_H */ |
| 7106 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7107 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 7108 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 7109 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Victor Stinner | 5776663 | 2020-10-29 15:16:23 +0100 | [diff] [blame] | 7110 | # define DEV_PTY_FILE "/dev/ptc" |
| 7111 | # define HAVE_DEV_PTMX |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 7112 | #else |
Victor Stinner | 5776663 | 2020-10-29 15:16:23 +0100 | [diff] [blame] | 7113 | # define DEV_PTY_FILE "/dev/ptmx" |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 7114 | #endif |
| 7115 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7116 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7117 | #ifdef HAVE_PTY_H |
| 7118 | #include <pty.h> |
| 7119 | #else |
| 7120 | #ifdef HAVE_LIBUTIL_H |
| 7121 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 7122 | #else |
| 7123 | #ifdef HAVE_UTIL_H |
| 7124 | #include <util.h> |
| 7125 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7126 | #endif /* HAVE_LIBUTIL_H */ |
| 7127 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 7128 | #ifdef HAVE_STROPTS_H |
| 7129 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7130 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 7131 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7132 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7133 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7134 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7135 | /*[clinic input] |
| 7136 | os.openpty |
| 7137 | |
| 7138 | Open a pseudo-terminal. |
| 7139 | |
| 7140 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 7141 | for both the master and slave ends. |
| 7142 | [clinic start generated code]*/ |
| 7143 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7144 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7145 | os_openpty_impl(PyObject *module) |
| 7146 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7147 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7148 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7149 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7150 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7151 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7152 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7153 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 7154 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7155 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7156 | #endif |
| 7157 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7158 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7159 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7160 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7161 | goto posix_error; |
| 7162 | |
| 7163 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 7164 | goto error; |
| 7165 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 7166 | goto error; |
| 7167 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 7168 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7169 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 7170 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7171 | goto posix_error; |
| 7172 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 7173 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7174 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7175 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7176 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 7177 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7178 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7179 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 7180 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7181 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7182 | goto posix_error; |
| 7183 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7184 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7185 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7186 | /* change permission of slave */ |
| 7187 | if (grantpt(master_fd) < 0) { |
| 7188 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7189 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7190 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7191 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7192 | /* unlock slave */ |
| 7193 | if (unlockpt(master_fd) < 0) { |
| 7194 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7195 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7196 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7197 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7198 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7199 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7200 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 7201 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7202 | goto posix_error; |
| 7203 | |
| 7204 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 7205 | if (slave_fd == -1) |
| 7206 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 7207 | |
| 7208 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 7209 | goto posix_error; |
| 7210 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 7211 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7212 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 7213 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 7214 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7215 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 7216 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7217 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 7218 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7219 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7220 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7221 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7222 | posix_error: |
| 7223 | posix_error(); |
| 7224 | error: |
| 7225 | if (master_fd != -1) |
| 7226 | close(master_fd); |
| 7227 | if (slave_fd != -1) |
| 7228 | close(slave_fd); |
| 7229 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7230 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7231 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7232 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7233 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7234 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7235 | /*[clinic input] |
| 7236 | os.forkpty |
| 7237 | |
| 7238 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 7239 | |
| 7240 | Returns a tuple of (pid, master_fd). |
| 7241 | Like fork(), return pid of 0 to the child process, |
| 7242 | and pid of child to the parent process. |
| 7243 | To both, return fd of newly opened pseudo-terminal. |
| 7244 | [clinic start generated code]*/ |
| 7245 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7246 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7247 | os_forkpty_impl(PyObject *module) |
| 7248 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7249 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 7250 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7251 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7252 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 7253 | if (_PyInterpreterState_GET() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 7254 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 7255 | return NULL; |
| 7256 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7257 | if (PySys_Audit("os.forkpty", NULL) < 0) { |
| 7258 | return NULL; |
| 7259 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 7260 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7261 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 7262 | if (pid == 0) { |
| 7263 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 7264 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7265 | } else { |
| 7266 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 7267 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7268 | } |
| 7269 | if (pid == -1) |
| 7270 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7271 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7272 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7273 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7274 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7275 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7276 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7277 | /*[clinic input] |
| 7278 | os.getegid |
| 7279 | |
| 7280 | Return the current process's effective group id. |
| 7281 | [clinic start generated code]*/ |
| 7282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7283 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7284 | os_getegid_impl(PyObject *module) |
| 7285 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7286 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7287 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7288 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7289 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7290 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7291 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7292 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7293 | /*[clinic input] |
| 7294 | os.geteuid |
| 7295 | |
| 7296 | Return the current process's effective user id. |
| 7297 | [clinic start generated code]*/ |
| 7298 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7299 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7300 | os_geteuid_impl(PyObject *module) |
| 7301 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7302 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7303 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7304 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7305 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7306 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7307 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7308 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7309 | /*[clinic input] |
| 7310 | os.getgid |
| 7311 | |
| 7312 | Return the current process's group id. |
| 7313 | [clinic start generated code]*/ |
| 7314 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7315 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7316 | os_getgid_impl(PyObject *module) |
| 7317 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7318 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7319 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7320 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7321 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7322 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7323 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 7324 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7325 | /*[clinic input] |
| 7326 | os.getpid |
| 7327 | |
| 7328 | Return the current process id. |
| 7329 | [clinic start generated code]*/ |
| 7330 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7331 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7332 | os_getpid_impl(PyObject *module) |
| 7333 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7334 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7335 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7336 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 7337 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7338 | |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 7339 | #ifdef NGROUPS_MAX |
| 7340 | #define MAX_GROUPS NGROUPS_MAX |
| 7341 | #else |
| 7342 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 7343 | #define MAX_GROUPS 64 |
| 7344 | #endif |
| 7345 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7346 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7347 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7348 | #ifdef __APPLE__ |
| 7349 | /*[clinic input] |
| 7350 | os.getgrouplist |
| 7351 | |
| 7352 | user: str |
| 7353 | username to lookup |
| 7354 | group as basegid: int |
| 7355 | base group id of the user |
| 7356 | / |
| 7357 | |
| 7358 | Returns a list of groups to which a user belongs. |
| 7359 | [clinic start generated code]*/ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7360 | |
| 7361 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7362 | os_getgrouplist_impl(PyObject *module, const char *user, int basegid) |
| 7363 | /*[clinic end generated code: output=6e734697b8c26de0 input=f8d870374b09a490]*/ |
| 7364 | #else |
| 7365 | /*[clinic input] |
| 7366 | os.getgrouplist |
| 7367 | |
| 7368 | user: str |
| 7369 | username to lookup |
| 7370 | group as basegid: gid_t |
| 7371 | base group id of the user |
| 7372 | / |
| 7373 | |
| 7374 | Returns a list of groups to which a user belongs. |
| 7375 | [clinic start generated code]*/ |
| 7376 | |
| 7377 | static PyObject * |
| 7378 | os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid) |
| 7379 | /*[clinic end generated code: output=0ebd7fb70115575b input=cc61d5c20b08958d]*/ |
| 7380 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7381 | { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7382 | int i, ngroups; |
| 7383 | PyObject *list; |
| 7384 | #ifdef __APPLE__ |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7385 | int *groups; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7386 | #else |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7387 | gid_t *groups; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7388 | #endif |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 7389 | |
| 7390 | /* |
| 7391 | * NGROUPS_MAX is defined by POSIX.1 as the maximum |
| 7392 | * number of supplimental groups a users can belong to. |
| 7393 | * We have to increment it by one because |
| 7394 | * getgrouplist() returns both the supplemental groups |
| 7395 | * and the primary group, i.e. all of the groups the |
| 7396 | * user belongs to. |
| 7397 | */ |
| 7398 | ngroups = 1 + MAX_GROUPS; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7399 | |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7400 | while (1) { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7401 | #ifdef __APPLE__ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7402 | groups = PyMem_New(int, ngroups); |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7403 | #else |
| 7404 | groups = PyMem_New(gid_t, ngroups); |
| 7405 | #endif |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7406 | if (groups == NULL) { |
| 7407 | return PyErr_NoMemory(); |
| 7408 | } |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7409 | |
| 7410 | int old_ngroups = ngroups; |
| 7411 | if (getgrouplist(user, basegid, groups, &ngroups) != -1) { |
| 7412 | /* Success */ |
| 7413 | break; |
| 7414 | } |
| 7415 | |
| 7416 | /* getgrouplist() fails if the group list is too small */ |
| 7417 | PyMem_Free(groups); |
| 7418 | |
| 7419 | if (ngroups > old_ngroups) { |
| 7420 | /* If the group list is too small, the glibc implementation of |
| 7421 | getgrouplist() sets ngroups to the total number of groups and |
| 7422 | returns -1. */ |
| 7423 | } |
| 7424 | else { |
| 7425 | /* Double the group list size */ |
| 7426 | if (ngroups > INT_MAX / 2) { |
| 7427 | return PyErr_NoMemory(); |
| 7428 | } |
| 7429 | ngroups *= 2; |
| 7430 | } |
| 7431 | |
| 7432 | /* Retry getgrouplist() with a larger group list */ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7433 | } |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7434 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 7435 | #ifdef _Py_MEMORY_SANITIZER |
| 7436 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 7437 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 7438 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 7439 | #endif |
| 7440 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7441 | list = PyList_New(ngroups); |
| 7442 | if (list == NULL) { |
| 7443 | PyMem_Del(groups); |
| 7444 | return NULL; |
| 7445 | } |
| 7446 | |
| 7447 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7448 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7449 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7450 | #else |
| 7451 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 7452 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7453 | if (o == NULL) { |
| 7454 | Py_DECREF(list); |
| 7455 | PyMem_Del(groups); |
| 7456 | return NULL; |
| 7457 | } |
| 7458 | PyList_SET_ITEM(list, i, o); |
| 7459 | } |
| 7460 | |
| 7461 | PyMem_Del(groups); |
| 7462 | |
| 7463 | return list; |
| 7464 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7465 | #endif /* HAVE_GETGROUPLIST */ |
| 7466 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7467 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7468 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7469 | /*[clinic input] |
| 7470 | os.getgroups |
| 7471 | |
| 7472 | Return list of supplemental group IDs for the process. |
| 7473 | [clinic start generated code]*/ |
| 7474 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7475 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7476 | os_getgroups_impl(PyObject *module) |
| 7477 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7478 | { |
| 7479 | PyObject *result = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7480 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7481 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7482 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7483 | * This is a helper variable to store the intermediate result when |
| 7484 | * that happens. |
| 7485 | * |
| 7486 | * To keep the code readable the OSX behaviour is unconditional, |
| 7487 | * according to the POSIX spec this should be safe on all unix-y |
| 7488 | * systems. |
| 7489 | */ |
| 7490 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7491 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7492 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7493 | #ifdef __APPLE__ |
| 7494 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 7495 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 7496 | * always first call getgroups with length 0 to get the actual number |
| 7497 | * of groups. |
| 7498 | */ |
| 7499 | n = getgroups(0, NULL); |
| 7500 | if (n < 0) { |
| 7501 | return posix_error(); |
| 7502 | } else if (n <= MAX_GROUPS) { |
| 7503 | /* groups will fit in existing array */ |
| 7504 | alt_grouplist = grouplist; |
| 7505 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7506 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7507 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7508 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7509 | } |
| 7510 | } |
| 7511 | |
| 7512 | n = getgroups(n, alt_grouplist); |
| 7513 | if (n == -1) { |
| 7514 | if (alt_grouplist != grouplist) { |
| 7515 | PyMem_Free(alt_grouplist); |
| 7516 | } |
| 7517 | return posix_error(); |
| 7518 | } |
| 7519 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7520 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7521 | if (n < 0) { |
| 7522 | if (errno == EINVAL) { |
| 7523 | n = getgroups(0, NULL); |
| 7524 | if (n == -1) { |
| 7525 | return posix_error(); |
| 7526 | } |
| 7527 | if (n == 0) { |
| 7528 | /* Avoid malloc(0) */ |
| 7529 | alt_grouplist = grouplist; |
| 7530 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7531 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7532 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7533 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7534 | } |
| 7535 | n = getgroups(n, alt_grouplist); |
| 7536 | if (n == -1) { |
| 7537 | PyMem_Free(alt_grouplist); |
| 7538 | return posix_error(); |
| 7539 | } |
| 7540 | } |
| 7541 | } else { |
| 7542 | return posix_error(); |
| 7543 | } |
| 7544 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7545 | #endif |
| 7546 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7547 | result = PyList_New(n); |
| 7548 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7549 | int i; |
| 7550 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7551 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7552 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7553 | Py_DECREF(result); |
| 7554 | result = NULL; |
| 7555 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7556 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7557 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7558 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7559 | } |
| 7560 | |
| 7561 | if (alt_grouplist != grouplist) { |
| 7562 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7563 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7564 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7565 | return result; |
| 7566 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7567 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7568 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7569 | #ifdef HAVE_INITGROUPS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7570 | #ifdef __APPLE__ |
| 7571 | /*[clinic input] |
| 7572 | os.initgroups |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7573 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7574 | username as oname: FSConverter |
| 7575 | gid: int |
| 7576 | / |
| 7577 | |
| 7578 | Initialize the group access list. |
| 7579 | |
| 7580 | Call the system initgroups() to initialize the group access list with all of |
| 7581 | the groups of which the specified username is a member, plus the specified |
| 7582 | group id. |
| 7583 | [clinic start generated code]*/ |
| 7584 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7585 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7586 | os_initgroups_impl(PyObject *module, PyObject *oname, int gid) |
| 7587 | /*[clinic end generated code: output=7f074d30a425fd3a input=df3d54331b0af204]*/ |
| 7588 | #else |
| 7589 | /*[clinic input] |
| 7590 | os.initgroups |
| 7591 | |
| 7592 | username as oname: FSConverter |
| 7593 | gid: gid_t |
| 7594 | / |
| 7595 | |
| 7596 | Initialize the group access list. |
| 7597 | |
| 7598 | Call the system initgroups() to initialize the group access list with all of |
| 7599 | the groups of which the specified username is a member, plus the specified |
| 7600 | group id. |
| 7601 | [clinic start generated code]*/ |
| 7602 | |
| 7603 | static PyObject * |
| 7604 | os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid) |
| 7605 | /*[clinic end generated code: output=59341244521a9e3f input=0cb91bdc59a4c564]*/ |
| 7606 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7607 | { |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7608 | const char *username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7609 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 7610 | if (initgroups(username, gid) == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7611 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7612 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7613 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7614 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7615 | #endif /* HAVE_INITGROUPS */ |
| 7616 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7617 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7618 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7619 | /*[clinic input] |
| 7620 | os.getpgid |
| 7621 | |
| 7622 | pid: pid_t |
| 7623 | |
| 7624 | Call the system call getpgid(), and return the result. |
| 7625 | [clinic start generated code]*/ |
| 7626 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7627 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7628 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 7629 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7630 | { |
| 7631 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7632 | if (pgid < 0) |
| 7633 | return posix_error(); |
| 7634 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7635 | } |
| 7636 | #endif /* HAVE_GETPGID */ |
| 7637 | |
| 7638 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7639 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7640 | /*[clinic input] |
| 7641 | os.getpgrp |
| 7642 | |
| 7643 | Return the current process group id. |
| 7644 | [clinic start generated code]*/ |
| 7645 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7646 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7647 | os_getpgrp_impl(PyObject *module) |
| 7648 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7649 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7650 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7651 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7652 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7653 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7654 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7655 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7656 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7657 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7658 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7659 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7660 | /*[clinic input] |
| 7661 | os.setpgrp |
| 7662 | |
| 7663 | Make the current process the leader of its process group. |
| 7664 | [clinic start generated code]*/ |
| 7665 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7666 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7667 | os_setpgrp_impl(PyObject *module) |
| 7668 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7669 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7670 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7671 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7672 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7673 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7674 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7675 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7676 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7677 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7678 | #endif /* HAVE_SETPGRP */ |
| 7679 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7680 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7681 | |
| 7682 | #ifdef MS_WINDOWS |
| 7683 | #include <tlhelp32.h> |
| 7684 | |
| 7685 | static PyObject* |
| 7686 | win32_getppid() |
| 7687 | { |
| 7688 | HANDLE snapshot; |
| 7689 | pid_t mypid; |
| 7690 | PyObject* result = NULL; |
| 7691 | BOOL have_record; |
| 7692 | PROCESSENTRY32 pe; |
| 7693 | |
| 7694 | mypid = getpid(); /* This function never fails */ |
| 7695 | |
| 7696 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 7697 | if (snapshot == INVALID_HANDLE_VALUE) |
| 7698 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 7699 | |
| 7700 | pe.dwSize = sizeof(pe); |
| 7701 | have_record = Process32First(snapshot, &pe); |
| 7702 | while (have_record) { |
| 7703 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 7704 | /* We could cache the ulong value in a static variable. */ |
| 7705 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 7706 | break; |
| 7707 | } |
| 7708 | |
| 7709 | have_record = Process32Next(snapshot, &pe); |
| 7710 | } |
| 7711 | |
| 7712 | /* If our loop exits and our pid was not found (result will be NULL) |
| 7713 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 7714 | * error anyway, so let's raise it. */ |
| 7715 | if (!result) |
| 7716 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7717 | |
| 7718 | CloseHandle(snapshot); |
| 7719 | |
| 7720 | return result; |
| 7721 | } |
| 7722 | #endif /*MS_WINDOWS*/ |
| 7723 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7724 | |
| 7725 | /*[clinic input] |
| 7726 | os.getppid |
| 7727 | |
| 7728 | Return the parent's process id. |
| 7729 | |
| 7730 | If the parent process has already exited, Windows machines will still |
| 7731 | return its id; others systems will return the id of the 'init' process (1). |
| 7732 | [clinic start generated code]*/ |
| 7733 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7734 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7735 | os_getppid_impl(PyObject *module) |
| 7736 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7737 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7738 | #ifdef MS_WINDOWS |
| 7739 | return win32_getppid(); |
| 7740 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7741 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7742 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7743 | } |
| 7744 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7745 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7746 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7747 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7748 | /*[clinic input] |
| 7749 | os.getlogin |
| 7750 | |
| 7751 | Return the actual login name. |
| 7752 | [clinic start generated code]*/ |
| 7753 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7754 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7755 | os_getlogin_impl(PyObject *module) |
| 7756 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7757 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7758 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7759 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7760 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7761 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7762 | |
| 7763 | if (GetUserNameW(user_name, &num_chars)) { |
| 7764 | /* num_chars is the number of unicode chars plus null terminator */ |
| 7765 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7766 | } |
| 7767 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7768 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7769 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7770 | char *name; |
| 7771 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7772 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7773 | errno = 0; |
| 7774 | name = getlogin(); |
| 7775 | if (name == NULL) { |
| 7776 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7777 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7778 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7779 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7780 | } |
| 7781 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7782 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7783 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7784 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7785 | return result; |
| 7786 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7787 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7788 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7789 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7790 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7791 | /*[clinic input] |
| 7792 | os.getuid |
| 7793 | |
| 7794 | Return the current process's user id. |
| 7795 | [clinic start generated code]*/ |
| 7796 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7797 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7798 | os_getuid_impl(PyObject *module) |
| 7799 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7800 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7801 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7802 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7803 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7804 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7805 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7806 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7807 | #define HAVE_KILL |
| 7808 | #endif /* MS_WINDOWS */ |
| 7809 | |
| 7810 | #ifdef HAVE_KILL |
| 7811 | /*[clinic input] |
| 7812 | os.kill |
| 7813 | |
| 7814 | pid: pid_t |
| 7815 | signal: Py_ssize_t |
| 7816 | / |
| 7817 | |
| 7818 | Kill a process with a signal. |
| 7819 | [clinic start generated code]*/ |
| 7820 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7821 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7822 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7823 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7824 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7825 | if (PySys_Audit("os.kill", "in", pid, signal) < 0) { |
| 7826 | return NULL; |
| 7827 | } |
| 7828 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7829 | if (kill(pid, (int)signal) == -1) |
| 7830 | return posix_error(); |
| 7831 | Py_RETURN_NONE; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7832 | #else /* !MS_WINDOWS */ |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7833 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7834 | DWORD sig = (DWORD)signal; |
| 7835 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7836 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7837 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7838 | /* Console processes which share a common console can be sent CTRL+C or |
| 7839 | CTRL+BREAK events, provided they handle said events. */ |
| 7840 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7841 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7842 | err = GetLastError(); |
| 7843 | PyErr_SetFromWindowsErr(err); |
| 7844 | } |
| 7845 | else |
| 7846 | Py_RETURN_NONE; |
| 7847 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7848 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7849 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7850 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7851 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7852 | if (handle == NULL) { |
| 7853 | err = GetLastError(); |
| 7854 | return PyErr_SetFromWindowsErr(err); |
| 7855 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7856 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7857 | if (TerminateProcess(handle, sig) == 0) { |
| 7858 | err = GetLastError(); |
| 7859 | result = PyErr_SetFromWindowsErr(err); |
| 7860 | } else { |
| 7861 | Py_INCREF(Py_None); |
| 7862 | result = Py_None; |
| 7863 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7864 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7865 | CloseHandle(handle); |
| 7866 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7867 | #endif /* !MS_WINDOWS */ |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7868 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7869 | #endif /* HAVE_KILL */ |
| 7870 | |
| 7871 | |
| 7872 | #ifdef HAVE_KILLPG |
| 7873 | /*[clinic input] |
| 7874 | os.killpg |
| 7875 | |
| 7876 | pgid: pid_t |
| 7877 | signal: int |
| 7878 | / |
| 7879 | |
| 7880 | Kill a process group with a signal. |
| 7881 | [clinic start generated code]*/ |
| 7882 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7883 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7884 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7885 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7886 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7887 | if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) { |
| 7888 | return NULL; |
| 7889 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7890 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7891 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7892 | take the same type. Moreover, pid_t is always at least as wide as |
| 7893 | int (else compilation of this module fails), which is safe. */ |
| 7894 | if (killpg(pgid, signal) == -1) |
| 7895 | return posix_error(); |
| 7896 | Py_RETURN_NONE; |
| 7897 | } |
| 7898 | #endif /* HAVE_KILLPG */ |
| 7899 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7900 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7901 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7902 | #ifdef HAVE_SYS_LOCK_H |
| 7903 | #include <sys/lock.h> |
| 7904 | #endif |
| 7905 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7906 | /*[clinic input] |
| 7907 | os.plock |
| 7908 | op: int |
| 7909 | / |
| 7910 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7911 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7912 | [clinic start generated code]*/ |
| 7913 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7914 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7915 | os_plock_impl(PyObject *module, int op) |
| 7916 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7917 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7918 | if (plock(op) == -1) |
| 7919 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7920 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7921 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7922 | #endif /* HAVE_PLOCK */ |
| 7923 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7924 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7925 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7926 | /*[clinic input] |
| 7927 | os.setuid |
| 7928 | |
| 7929 | uid: uid_t |
| 7930 | / |
| 7931 | |
| 7932 | Set the current process's user id. |
| 7933 | [clinic start generated code]*/ |
| 7934 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7935 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7936 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7937 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7938 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7939 | if (setuid(uid) < 0) |
| 7940 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7941 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7942 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7943 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7944 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7945 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7946 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7947 | /*[clinic input] |
| 7948 | os.seteuid |
| 7949 | |
| 7950 | euid: uid_t |
| 7951 | / |
| 7952 | |
| 7953 | Set the current process's effective user id. |
| 7954 | [clinic start generated code]*/ |
| 7955 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7956 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7957 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7958 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7959 | { |
| 7960 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7961 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7962 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7963 | } |
| 7964 | #endif /* HAVE_SETEUID */ |
| 7965 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7966 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7967 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7968 | /*[clinic input] |
| 7969 | os.setegid |
| 7970 | |
| 7971 | egid: gid_t |
| 7972 | / |
| 7973 | |
| 7974 | Set the current process's effective group id. |
| 7975 | [clinic start generated code]*/ |
| 7976 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7977 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7978 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7979 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7980 | { |
| 7981 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7982 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7983 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7984 | } |
| 7985 | #endif /* HAVE_SETEGID */ |
| 7986 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7987 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7988 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7989 | /*[clinic input] |
| 7990 | os.setreuid |
| 7991 | |
| 7992 | ruid: uid_t |
| 7993 | euid: uid_t |
| 7994 | / |
| 7995 | |
| 7996 | Set the current process's real and effective user ids. |
| 7997 | [clinic start generated code]*/ |
| 7998 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7999 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8000 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 8001 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8002 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8003 | if (setreuid(ruid, euid) < 0) { |
| 8004 | return posix_error(); |
| 8005 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 8006 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8007 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8008 | } |
| 8009 | #endif /* HAVE_SETREUID */ |
| 8010 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8011 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8012 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8013 | /*[clinic input] |
| 8014 | os.setregid |
| 8015 | |
| 8016 | rgid: gid_t |
| 8017 | egid: gid_t |
| 8018 | / |
| 8019 | |
| 8020 | Set the current process's real and effective group ids. |
| 8021 | [clinic start generated code]*/ |
| 8022 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8023 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8024 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 8025 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8026 | { |
| 8027 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8028 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8029 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8030 | } |
| 8031 | #endif /* HAVE_SETREGID */ |
| 8032 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8033 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8034 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8035 | /*[clinic input] |
| 8036 | os.setgid |
| 8037 | gid: gid_t |
| 8038 | / |
| 8039 | |
| 8040 | Set the current process's group id. |
| 8041 | [clinic start generated code]*/ |
| 8042 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8043 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8044 | os_setgid_impl(PyObject *module, gid_t gid) |
| 8045 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8046 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8047 | if (setgid(gid) < 0) |
| 8048 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8049 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 8050 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8051 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 8052 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8053 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8054 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8055 | /*[clinic input] |
| 8056 | os.setgroups |
| 8057 | |
| 8058 | groups: object |
| 8059 | / |
| 8060 | |
| 8061 | Set the groups of the current process to list. |
| 8062 | [clinic start generated code]*/ |
| 8063 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8064 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8065 | os_setgroups(PyObject *module, PyObject *groups) |
| 8066 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8067 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8068 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8069 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 8070 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8071 | if (!PySequence_Check(groups)) { |
| 8072 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 8073 | return NULL; |
| 8074 | } |
| 8075 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8076 | if (len < 0) { |
| 8077 | return NULL; |
| 8078 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8079 | if (len > MAX_GROUPS) { |
| 8080 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 8081 | return NULL; |
| 8082 | } |
| 8083 | for(i = 0; i < len; i++) { |
| 8084 | PyObject *elem; |
| 8085 | elem = PySequence_GetItem(groups, i); |
| 8086 | if (!elem) |
| 8087 | return NULL; |
| 8088 | if (!PyLong_Check(elem)) { |
| 8089 | PyErr_SetString(PyExc_TypeError, |
| 8090 | "groups must be integers"); |
| 8091 | Py_DECREF(elem); |
| 8092 | return NULL; |
| 8093 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8094 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8095 | Py_DECREF(elem); |
| 8096 | return NULL; |
| 8097 | } |
| 8098 | } |
| 8099 | Py_DECREF(elem); |
| 8100 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8101 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8102 | if (setgroups(len, grouplist) < 0) |
| 8103 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 8104 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 8105 | } |
| 8106 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8107 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8108 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 8109 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8110 | wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8111 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8112 | PyObject *result; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8113 | PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8114 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8115 | if (pid == -1) |
| 8116 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8117 | |
Zackery Spytz | 682107c | 2019-09-09 09:48:32 -0600 | [diff] [blame] | 8118 | // If wait succeeded but no child was ready to report status, ru will not |
| 8119 | // have been populated. |
| 8120 | if (pid == 0) { |
| 8121 | memset(ru, 0, sizeof(*ru)); |
| 8122 | } |
| 8123 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8124 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 8125 | if (m == NULL) |
| 8126 | return NULL; |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8127 | struct_rusage = PyObject_GetAttr(m, get_posix_state(module)->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8128 | Py_DECREF(m); |
| 8129 | if (struct_rusage == NULL) |
| 8130 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8131 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8132 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 8133 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
Eddie Elizondo | e4db1f0 | 2019-11-25 19:07:37 -0800 | [diff] [blame] | 8134 | Py_DECREF(struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8135 | if (!result) |
| 8136 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8137 | |
| 8138 | #ifndef doubletime |
| 8139 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 8140 | #endif |
| 8141 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8142 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8143 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8144 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 8145 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8146 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8147 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 8148 | SET_INT(result, 2, ru->ru_maxrss); |
| 8149 | SET_INT(result, 3, ru->ru_ixrss); |
| 8150 | SET_INT(result, 4, ru->ru_idrss); |
| 8151 | SET_INT(result, 5, ru->ru_isrss); |
| 8152 | SET_INT(result, 6, ru->ru_minflt); |
| 8153 | SET_INT(result, 7, ru->ru_majflt); |
| 8154 | SET_INT(result, 8, ru->ru_nswap); |
| 8155 | SET_INT(result, 9, ru->ru_inblock); |
| 8156 | SET_INT(result, 10, ru->ru_oublock); |
| 8157 | SET_INT(result, 11, ru->ru_msgsnd); |
| 8158 | SET_INT(result, 12, ru->ru_msgrcv); |
| 8159 | SET_INT(result, 13, ru->ru_nsignals); |
| 8160 | SET_INT(result, 14, ru->ru_nvcsw); |
| 8161 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8162 | #undef SET_INT |
| 8163 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8164 | if (PyErr_Occurred()) { |
| 8165 | Py_DECREF(result); |
| 8166 | return NULL; |
| 8167 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8168 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8169 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8170 | } |
| 8171 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 8172 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8173 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8174 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8175 | /*[clinic input] |
| 8176 | os.wait3 |
| 8177 | |
| 8178 | options: int |
| 8179 | Wait for completion of a child process. |
| 8180 | |
| 8181 | Returns a tuple of information about the child process: |
| 8182 | (pid, status, rusage) |
| 8183 | [clinic start generated code]*/ |
| 8184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8185 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8186 | os_wait3_impl(PyObject *module, int options) |
| 8187 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8188 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8189 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8190 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8191 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8192 | WAIT_TYPE status; |
| 8193 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8194 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8195 | do { |
| 8196 | Py_BEGIN_ALLOW_THREADS |
| 8197 | pid = wait3(&status, options, &ru); |
| 8198 | Py_END_ALLOW_THREADS |
| 8199 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8200 | if (pid < 0) |
| 8201 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8202 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8203 | return wait_helper(module, pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8204 | } |
| 8205 | #endif /* HAVE_WAIT3 */ |
| 8206 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8207 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8208 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8209 | /*[clinic input] |
| 8210 | |
| 8211 | os.wait4 |
| 8212 | |
| 8213 | pid: pid_t |
| 8214 | options: int |
| 8215 | |
| 8216 | Wait for completion of a specific child process. |
| 8217 | |
| 8218 | Returns a tuple of information about the child process: |
| 8219 | (pid, status, rusage) |
| 8220 | [clinic start generated code]*/ |
| 8221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8222 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8223 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 8224 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8225 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8226 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8227 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8228 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8229 | WAIT_TYPE status; |
| 8230 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8231 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8232 | do { |
| 8233 | Py_BEGIN_ALLOW_THREADS |
| 8234 | res = wait4(pid, &status, options, &ru); |
| 8235 | Py_END_ALLOW_THREADS |
| 8236 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8237 | if (res < 0) |
| 8238 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8239 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8240 | return wait_helper(module, res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8241 | } |
| 8242 | #endif /* HAVE_WAIT4 */ |
| 8243 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8244 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8245 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8246 | /*[clinic input] |
| 8247 | os.waitid |
| 8248 | |
| 8249 | idtype: idtype_t |
| 8250 | Must be one of be P_PID, P_PGID or P_ALL. |
| 8251 | id: id_t |
| 8252 | The id to wait on. |
| 8253 | options: int |
| 8254 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 8255 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 8256 | / |
| 8257 | |
| 8258 | Returns the result of waiting for a process or processes. |
| 8259 | |
| 8260 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 8261 | no children in a waitable state. |
| 8262 | [clinic start generated code]*/ |
| 8263 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8264 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8265 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 8266 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8267 | { |
| 8268 | PyObject *result; |
| 8269 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8270 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8271 | siginfo_t si; |
| 8272 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8273 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8274 | do { |
| 8275 | Py_BEGIN_ALLOW_THREADS |
| 8276 | res = waitid(idtype, id, &si, options); |
| 8277 | Py_END_ALLOW_THREADS |
| 8278 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8279 | if (res < 0) |
| 8280 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8281 | |
| 8282 | if (si.si_pid == 0) |
| 8283 | Py_RETURN_NONE; |
| 8284 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 8285 | PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8286 | result = PyStructSequence_New((PyTypeObject *)WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8287 | if (!result) |
| 8288 | return NULL; |
| 8289 | |
| 8290 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8291 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8292 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 8293 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 8294 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 8295 | if (PyErr_Occurred()) { |
| 8296 | Py_DECREF(result); |
| 8297 | return NULL; |
| 8298 | } |
| 8299 | |
| 8300 | return result; |
| 8301 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8302 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8303 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8304 | |
| 8305 | #if defined(HAVE_WAITPID) |
| 8306 | /*[clinic input] |
| 8307 | os.waitpid |
| 8308 | pid: pid_t |
| 8309 | options: int |
| 8310 | / |
| 8311 | |
| 8312 | Wait for completion of a given child process. |
| 8313 | |
| 8314 | Returns a tuple of information regarding the child process: |
| 8315 | (pid, status) |
| 8316 | |
| 8317 | The options argument is ignored on Windows. |
| 8318 | [clinic start generated code]*/ |
| 8319 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8320 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8321 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 8322 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8323 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8324 | pid_t res; |
| 8325 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8326 | WAIT_TYPE status; |
| 8327 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 8328 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8329 | do { |
| 8330 | Py_BEGIN_ALLOW_THREADS |
| 8331 | res = waitpid(pid, &status, options); |
| 8332 | Py_END_ALLOW_THREADS |
| 8333 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8334 | if (res < 0) |
| 8335 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8336 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8337 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 8338 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8339 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8340 | /* 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] | 8341 | /*[clinic input] |
| 8342 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8343 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8344 | options: int |
| 8345 | / |
| 8346 | |
| 8347 | Wait for completion of a given process. |
| 8348 | |
| 8349 | Returns a tuple of information regarding the process: |
| 8350 | (pid, status << 8) |
| 8351 | |
| 8352 | The options argument is ignored on Windows. |
| 8353 | [clinic start generated code]*/ |
| 8354 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8355 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8356 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 8357 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8358 | { |
| 8359 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 8360 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8361 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8362 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8363 | do { |
| 8364 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 8365 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8366 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 8367 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8368 | Py_END_ALLOW_THREADS |
| 8369 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8370 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8371 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8372 | |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 8373 | unsigned long long ustatus = (unsigned int)status; |
| 8374 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8375 | /* 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] | 8376 | return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 8377 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8378 | #endif |
| 8379 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8380 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8381 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8382 | /*[clinic input] |
| 8383 | os.wait |
| 8384 | |
| 8385 | Wait for completion of a child process. |
| 8386 | |
| 8387 | Returns a tuple of information about the child process: |
| 8388 | (pid, status) |
| 8389 | [clinic start generated code]*/ |
| 8390 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8391 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8392 | os_wait_impl(PyObject *module) |
| 8393 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 8394 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8395 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8396 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8397 | WAIT_TYPE status; |
| 8398 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8399 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8400 | do { |
| 8401 | Py_BEGIN_ALLOW_THREADS |
| 8402 | pid = wait(&status); |
| 8403 | Py_END_ALLOW_THREADS |
| 8404 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8405 | if (pid < 0) |
| 8406 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 8407 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8408 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8409 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8410 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8411 | |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 8412 | #if defined(__linux__) && defined(__NR_pidfd_open) |
| 8413 | /*[clinic input] |
| 8414 | os.pidfd_open |
| 8415 | pid: pid_t |
| 8416 | flags: unsigned_int = 0 |
| 8417 | |
| 8418 | Return a file descriptor referring to the process *pid*. |
| 8419 | |
| 8420 | The descriptor can be used to perform process management without races and |
| 8421 | signals. |
| 8422 | [clinic start generated code]*/ |
| 8423 | |
| 8424 | static PyObject * |
| 8425 | os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags) |
| 8426 | /*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/ |
| 8427 | { |
| 8428 | int fd = syscall(__NR_pidfd_open, pid, flags); |
| 8429 | if (fd < 0) { |
| 8430 | return posix_error(); |
| 8431 | } |
| 8432 | return PyLong_FromLong(fd); |
| 8433 | } |
| 8434 | #endif |
| 8435 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8436 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8437 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8438 | /*[clinic input] |
| 8439 | os.readlink |
| 8440 | |
| 8441 | path: path_t |
| 8442 | * |
| 8443 | dir_fd: dir_fd(requires='readlinkat') = None |
| 8444 | |
| 8445 | Return a string representing the path to which the symbolic link points. |
| 8446 | |
| 8447 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8448 | and path should be relative; path will then be relative to that directory. |
| 8449 | |
| 8450 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 8451 | using it will raise a NotImplementedError. |
| 8452 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8453 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8454 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8455 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 8456 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8457 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8458 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 8459 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8460 | ssize_t length; |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 8461 | #ifdef HAVE_READLINKAT |
| 8462 | int readlinkat_unavailable = 0; |
| 8463 | #endif |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8464 | |
| 8465 | Py_BEGIN_ALLOW_THREADS |
| 8466 | #ifdef HAVE_READLINKAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 8467 | if (dir_fd != DEFAULT_DIR_FD) { |
| 8468 | if (HAVE_READLINKAT_RUNTIME) { |
| 8469 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 8470 | } else { |
| 8471 | readlinkat_unavailable = 1; |
| 8472 | } |
| 8473 | } else |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8474 | #endif |
| 8475 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 8476 | Py_END_ALLOW_THREADS |
| 8477 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 8478 | #ifdef HAVE_READLINKAT |
| 8479 | if (readlinkat_unavailable) { |
| 8480 | argument_unavailable_error(NULL, "dir_fd"); |
| 8481 | return NULL; |
| 8482 | } |
| 8483 | #endif |
| 8484 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8485 | if (length < 0) { |
| 8486 | return path_error(path); |
| 8487 | } |
| 8488 | buffer[length] = '\0'; |
| 8489 | |
| 8490 | if (PyUnicode_Check(path->object)) |
| 8491 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 8492 | else |
| 8493 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8494 | #elif defined(MS_WINDOWS) |
| 8495 | DWORD n_bytes_returned; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8496 | DWORD io_result = 0; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8497 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8498 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 8499 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8500 | PyObject *result = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8501 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8502 | /* First get a handle to the reparse point */ |
| 8503 | Py_BEGIN_ALLOW_THREADS |
| 8504 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8505 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8506 | 0, |
| 8507 | 0, |
| 8508 | 0, |
| 8509 | OPEN_EXISTING, |
| 8510 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 8511 | 0); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8512 | if (reparse_point_handle != INVALID_HANDLE_VALUE) { |
| 8513 | /* New call DeviceIoControl to read the reparse point */ |
| 8514 | io_result = DeviceIoControl( |
| 8515 | reparse_point_handle, |
| 8516 | FSCTL_GET_REPARSE_POINT, |
| 8517 | 0, 0, /* in buffer */ |
| 8518 | target_buffer, sizeof(target_buffer), |
| 8519 | &n_bytes_returned, |
| 8520 | 0 /* we're not using OVERLAPPED_IO */ |
| 8521 | ); |
| 8522 | CloseHandle(reparse_point_handle); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8523 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8524 | Py_END_ALLOW_THREADS |
| 8525 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8526 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8527 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8528 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8529 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8530 | wchar_t *name = NULL; |
| 8531 | Py_ssize_t nameLen = 0; |
| 8532 | if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8533 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8534 | name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 8535 | rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 8536 | nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8537 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8538 | else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) |
| 8539 | { |
| 8540 | name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer + |
| 8541 | rdb->MountPointReparseBuffer.SubstituteNameOffset); |
| 8542 | nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
| 8543 | } |
| 8544 | else |
| 8545 | { |
| 8546 | PyErr_SetString(PyExc_ValueError, "not a symbolic link"); |
| 8547 | } |
| 8548 | if (name) { |
| 8549 | if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) { |
| 8550 | /* Our buffer is mutable, so this is okay */ |
| 8551 | name[1] = L'\\'; |
| 8552 | } |
| 8553 | result = PyUnicode_FromWideChar(name, nameLen); |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8554 | if (result && path->narrow) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8555 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
| 8556 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8557 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8558 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8559 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8560 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8561 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8562 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8563 | #if defined(MS_WINDOWS) |
| 8564 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8565 | /* Remove the last portion of the path - return 0 on success */ |
| 8566 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8567 | _dirnameW(WCHAR *path) |
| 8568 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8569 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8570 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 8571 | if (length == MAX_PATH) { |
| 8572 | return -1; |
| 8573 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8574 | |
| 8575 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8576 | for(ptr = path + length; ptr != path; ptr--) { |
| 8577 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8578 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8579 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8580 | } |
| 8581 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8582 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8583 | } |
| 8584 | |
Minmin Gong | 7f21c9a | 2020-05-18 09:17:19 -0700 | [diff] [blame] | 8585 | #endif |
| 8586 | |
| 8587 | #ifdef HAVE_SYMLINK |
| 8588 | |
| 8589 | #if defined(MS_WINDOWS) |
| 8590 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8591 | /* Is this path absolute? */ |
| 8592 | static int |
| 8593 | _is_absW(const WCHAR *path) |
| 8594 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8595 | return path[0] == L'\\' || path[0] == L'/' || |
| 8596 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8597 | } |
| 8598 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8599 | /* join root and rest with a backslash - return 0 on success */ |
| 8600 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8601 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 8602 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8603 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8604 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8605 | } |
| 8606 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8607 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 8608 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8609 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8610 | |
| 8611 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 8612 | return -1; |
| 8613 | } |
| 8614 | |
| 8615 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8616 | } |
| 8617 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8618 | /* Return True if the path at src relative to dest is a directory */ |
| 8619 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8620 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8621 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8622 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 8623 | WCHAR dest_parent[MAX_PATH]; |
| 8624 | WCHAR src_resolved[MAX_PATH] = L""; |
| 8625 | |
| 8626 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8627 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 8628 | _dirnameW(dest_parent)) { |
| 8629 | return 0; |
| 8630 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8631 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8632 | if (_joinW(src_resolved, dest_parent, src)) { |
| 8633 | return 0; |
| 8634 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8635 | return ( |
| 8636 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 8637 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 8638 | ); |
| 8639 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8640 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8641 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8642 | |
| 8643 | /*[clinic input] |
| 8644 | os.symlink |
| 8645 | src: path_t |
| 8646 | dst: path_t |
| 8647 | target_is_directory: bool = False |
| 8648 | * |
| 8649 | dir_fd: dir_fd(requires='symlinkat')=None |
| 8650 | |
| 8651 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 8652 | |
| 8653 | Create a symbolic link pointing to src named dst. |
| 8654 | |
| 8655 | target_is_directory is required on Windows if the target is to be |
| 8656 | interpreted as a directory. (On Windows, symlink requires |
| 8657 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 8658 | target_is_directory is ignored on non-Windows platforms. |
| 8659 | |
| 8660 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8661 | and path should be relative; path will then be relative to that directory. |
| 8662 | dir_fd may not be implemented on your platform. |
| 8663 | If it is unavailable, using it will raise a NotImplementedError. |
| 8664 | |
| 8665 | [clinic start generated code]*/ |
| 8666 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8667 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8668 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8669 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8670 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8671 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8672 | #ifdef MS_WINDOWS |
| 8673 | DWORD result; |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8674 | DWORD flags = 0; |
| 8675 | |
| 8676 | /* Assumed true, set to false if detected to not be available. */ |
| 8677 | static int windows_has_symlink_unprivileged_flag = TRUE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8678 | #else |
| 8679 | int result; |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 8680 | #ifdef HAVE_SYMLINKAT |
| 8681 | int symlinkat_unavailable = 0; |
| 8682 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8683 | #endif |
| 8684 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 8685 | if (PySys_Audit("os.symlink", "OOi", src->object, dst->object, |
| 8686 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 8687 | return NULL; |
| 8688 | } |
| 8689 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8690 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8691 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8692 | if (windows_has_symlink_unprivileged_flag) { |
| 8693 | /* Allow non-admin symlinks if system allows it. */ |
| 8694 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 8695 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8696 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8697 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8698 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8699 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
| 8700 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { |
| 8701 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 8702 | } |
| 8703 | |
| 8704 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8705 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8706 | Py_END_ALLOW_THREADS |
| 8707 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8708 | if (windows_has_symlink_unprivileged_flag && !result && |
| 8709 | ERROR_INVALID_PARAMETER == GetLastError()) { |
| 8710 | |
| 8711 | Py_BEGIN_ALLOW_THREADS |
| 8712 | _Py_BEGIN_SUPPRESS_IPH |
| 8713 | /* This error might be caused by |
| 8714 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. |
| 8715 | Try again, and update windows_has_symlink_unprivileged_flag if we |
| 8716 | are successful this time. |
| 8717 | |
| 8718 | NOTE: There is a risk of a race condition here if there are other |
| 8719 | conditions than the flag causing ERROR_INVALID_PARAMETER, and |
| 8720 | another process (or thread) changes that condition in between our |
| 8721 | calls to CreateSymbolicLink. |
| 8722 | */ |
| 8723 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 8724 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
| 8725 | _Py_END_SUPPRESS_IPH |
| 8726 | Py_END_ALLOW_THREADS |
| 8727 | |
| 8728 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { |
| 8729 | windows_has_symlink_unprivileged_flag = FALSE; |
| 8730 | } |
| 8731 | } |
| 8732 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8733 | if (!result) |
| 8734 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8735 | |
| 8736 | #else |
| 8737 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8738 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 8739 | PyErr_SetString(PyExc_ValueError, |
| 8740 | "symlink: src and dst must be the same type"); |
| 8741 | return NULL; |
| 8742 | } |
| 8743 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8744 | Py_BEGIN_ALLOW_THREADS |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 8745 | #ifdef HAVE_SYMLINKAT |
| 8746 | if (dir_fd != DEFAULT_DIR_FD) { |
| 8747 | if (HAVE_SYMLINKAT_RUNTIME) { |
| 8748 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
| 8749 | } else { |
| 8750 | symlinkat_unavailable = 1; |
| 8751 | } |
| 8752 | } else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8753 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8754 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8755 | Py_END_ALLOW_THREADS |
| 8756 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 8757 | #ifdef HAVE_SYMLINKAT |
| 8758 | if (symlinkat_unavailable) { |
| 8759 | argument_unavailable_error(NULL, "dir_fd"); |
| 8760 | return NULL; |
| 8761 | } |
| 8762 | #endif |
| 8763 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8764 | if (result) |
| 8765 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8766 | #endif |
| 8767 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8768 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8769 | } |
| 8770 | #endif /* HAVE_SYMLINK */ |
| 8771 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8772 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8773 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8774 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8775 | static PyStructSequence_Field times_result_fields[] = { |
| 8776 | {"user", "user time"}, |
| 8777 | {"system", "system time"}, |
| 8778 | {"children_user", "user time of children"}, |
| 8779 | {"children_system", "system time of children"}, |
| 8780 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 8781 | {NULL} |
| 8782 | }; |
| 8783 | |
| 8784 | PyDoc_STRVAR(times_result__doc__, |
| 8785 | "times_result: Result from os.times().\n\n\ |
| 8786 | This object may be accessed either as a tuple of\n\ |
| 8787 | (user, system, children_user, children_system, elapsed),\n\ |
| 8788 | or via the attributes user, system, children_user, children_system,\n\ |
| 8789 | and elapsed.\n\ |
| 8790 | \n\ |
| 8791 | See os.times for more information."); |
| 8792 | |
| 8793 | static PyStructSequence_Desc times_result_desc = { |
| 8794 | "times_result", /* name */ |
| 8795 | times_result__doc__, /* doc */ |
| 8796 | times_result_fields, |
| 8797 | 5 |
| 8798 | }; |
| 8799 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8800 | #ifdef MS_WINDOWS |
| 8801 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 8802 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8803 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8804 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8805 | |
| 8806 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8807 | build_times_result(PyObject *module, double user, double system, |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8808 | double children_user, double children_system, |
| 8809 | double elapsed) |
| 8810 | { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8811 | PyObject *TimesResultType = get_posix_state(module)->TimesResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8812 | PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8813 | if (value == NULL) |
| 8814 | return NULL; |
| 8815 | |
| 8816 | #define SET(i, field) \ |
| 8817 | { \ |
| 8818 | PyObject *o = PyFloat_FromDouble(field); \ |
| 8819 | if (!o) { \ |
| 8820 | Py_DECREF(value); \ |
| 8821 | return NULL; \ |
| 8822 | } \ |
| 8823 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 8824 | } \ |
| 8825 | |
| 8826 | SET(0, user); |
| 8827 | SET(1, system); |
| 8828 | SET(2, children_user); |
| 8829 | SET(3, children_system); |
| 8830 | SET(4, elapsed); |
| 8831 | |
| 8832 | #undef SET |
| 8833 | |
| 8834 | return value; |
| 8835 | } |
| 8836 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8837 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8838 | #ifndef MS_WINDOWS |
| 8839 | #define NEED_TICKS_PER_SECOND |
| 8840 | static long ticks_per_second = -1; |
| 8841 | #endif /* MS_WINDOWS */ |
| 8842 | |
| 8843 | /*[clinic input] |
| 8844 | os.times |
| 8845 | |
| 8846 | Return a collection containing process timing information. |
| 8847 | |
| 8848 | The object returned behaves like a named tuple with these fields: |
| 8849 | (utime, stime, cutime, cstime, elapsed_time) |
| 8850 | All fields are floating point numbers. |
| 8851 | [clinic start generated code]*/ |
| 8852 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8853 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8854 | os_times_impl(PyObject *module) |
| 8855 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8856 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8857 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8858 | FILETIME create, exit, kernel, user; |
| 8859 | HANDLE hProc; |
| 8860 | hProc = GetCurrentProcess(); |
| 8861 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8862 | /* The fields of a FILETIME structure are the hi and lo part |
| 8863 | of a 64-bit value expressed in 100 nanosecond units. |
| 8864 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8865 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8866 | */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8867 | return build_times_result(module, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8868 | (double)(user.dwHighDateTime*429.4967296 + |
| 8869 | user.dwLowDateTime*1e-7), |
| 8870 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8871 | kernel.dwLowDateTime*1e-7), |
| 8872 | (double)0, |
| 8873 | (double)0, |
| 8874 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8875 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8876 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8877 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8878 | |
| 8879 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8880 | struct tms t; |
| 8881 | clock_t c; |
| 8882 | errno = 0; |
| 8883 | c = times(&t); |
| 8884 | if (c == (clock_t) -1) |
| 8885 | return posix_error(); |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 8886 | return build_times_result(module, |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8887 | (double)t.tms_utime / ticks_per_second, |
| 8888 | (double)t.tms_stime / ticks_per_second, |
| 8889 | (double)t.tms_cutime / ticks_per_second, |
| 8890 | (double)t.tms_cstime / ticks_per_second, |
| 8891 | (double)c / ticks_per_second); |
| 8892 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8893 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8894 | #endif /* HAVE_TIMES */ |
| 8895 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8896 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8897 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8898 | /*[clinic input] |
| 8899 | os.getsid |
| 8900 | |
| 8901 | pid: pid_t |
| 8902 | / |
| 8903 | |
| 8904 | Call the system call getsid(pid) and return the result. |
| 8905 | [clinic start generated code]*/ |
| 8906 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8907 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8908 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8909 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8910 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8911 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8912 | sid = getsid(pid); |
| 8913 | if (sid < 0) |
| 8914 | return posix_error(); |
| 8915 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8916 | } |
| 8917 | #endif /* HAVE_GETSID */ |
| 8918 | |
| 8919 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8920 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8921 | /*[clinic input] |
| 8922 | os.setsid |
| 8923 | |
| 8924 | Call the system call setsid(). |
| 8925 | [clinic start generated code]*/ |
| 8926 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8927 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8928 | os_setsid_impl(PyObject *module) |
| 8929 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8930 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8931 | if (setsid() < 0) |
| 8932 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8933 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8934 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8935 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8936 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8937 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8938 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8939 | /*[clinic input] |
| 8940 | os.setpgid |
| 8941 | |
| 8942 | pid: pid_t |
| 8943 | pgrp: pid_t |
| 8944 | / |
| 8945 | |
| 8946 | Call the system call setpgid(pid, pgrp). |
| 8947 | [clinic start generated code]*/ |
| 8948 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8949 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8950 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8951 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8952 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8953 | if (setpgid(pid, pgrp) < 0) |
| 8954 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8955 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8956 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8957 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8958 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8959 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8960 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8961 | /*[clinic input] |
| 8962 | os.tcgetpgrp |
| 8963 | |
| 8964 | fd: int |
| 8965 | / |
| 8966 | |
| 8967 | Return the process group associated with the terminal specified by fd. |
| 8968 | [clinic start generated code]*/ |
| 8969 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8970 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8971 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8972 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8973 | { |
| 8974 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8975 | if (pgid < 0) |
| 8976 | return posix_error(); |
| 8977 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8978 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8979 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8980 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8981 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8982 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8983 | /*[clinic input] |
| 8984 | os.tcsetpgrp |
| 8985 | |
| 8986 | fd: int |
| 8987 | pgid: pid_t |
| 8988 | / |
| 8989 | |
| 8990 | Set the process group associated with the terminal specified by fd. |
| 8991 | [clinic start generated code]*/ |
| 8992 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8993 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8994 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8995 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8996 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8997 | if (tcsetpgrp(fd, pgid) < 0) |
| 8998 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8999 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 9000 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9001 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 9002 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9003 | /* Functions acting on file descriptors */ |
| 9004 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9005 | #ifdef O_CLOEXEC |
| 9006 | extern int _Py_open_cloexec_works; |
| 9007 | #endif |
| 9008 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9009 | |
| 9010 | /*[clinic input] |
| 9011 | os.open -> int |
| 9012 | path: path_t |
| 9013 | flags: int |
| 9014 | mode: int = 0o777 |
| 9015 | * |
| 9016 | dir_fd: dir_fd(requires='openat') = None |
| 9017 | |
| 9018 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 9019 | |
| 9020 | Open a file for low level IO. Returns a file descriptor (integer). |
| 9021 | |
| 9022 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9023 | and path should be relative; path will then be relative to that directory. |
| 9024 | dir_fd may not be implemented on your platform. |
| 9025 | If it is unavailable, using it will raise a NotImplementedError. |
| 9026 | [clinic start generated code]*/ |
| 9027 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9028 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9029 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 9030 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9031 | { |
| 9032 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9033 | int async_err = 0; |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 9034 | #ifdef HAVE_OPENAT |
| 9035 | int openat_unavailable = 0; |
| 9036 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9037 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9038 | #ifdef O_CLOEXEC |
| 9039 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 9040 | #elif !defined(MS_WINDOWS) |
| 9041 | int *atomic_flag_works = NULL; |
| 9042 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 9043 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9044 | #ifdef MS_WINDOWS |
| 9045 | flags |= O_NOINHERIT; |
| 9046 | #elif defined(O_CLOEXEC) |
| 9047 | flags |= O_CLOEXEC; |
| 9048 | #endif |
| 9049 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 9050 | if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) { |
| 9051 | return -1; |
| 9052 | } |
| 9053 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9054 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9055 | do { |
| 9056 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9057 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 9058 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 9059 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9060 | #ifdef HAVE_OPENAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 9061 | if (dir_fd != DEFAULT_DIR_FD) { |
| 9062 | if (HAVE_OPENAT_RUNTIME) { |
| 9063 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 9064 | |
| 9065 | } else { |
| 9066 | openat_unavailable = 1; |
| 9067 | fd = -1; |
| 9068 | } |
| 9069 | } else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 9070 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9071 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 9072 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9073 | Py_END_ALLOW_THREADS |
| 9074 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9075 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9076 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 9077 | #ifdef HAVE_OPENAT |
| 9078 | if (openat_unavailable) { |
| 9079 | argument_unavailable_error(NULL, "dir_fd"); |
| 9080 | return -1; |
| 9081 | } |
| 9082 | #endif |
| 9083 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 9084 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9085 | if (!async_err) |
| 9086 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9087 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9088 | } |
| 9089 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9090 | #ifndef MS_WINDOWS |
| 9091 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 9092 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9093 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9094 | } |
| 9095 | #endif |
| 9096 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9097 | return fd; |
| 9098 | } |
| 9099 | |
| 9100 | |
| 9101 | /*[clinic input] |
| 9102 | os.close |
| 9103 | |
| 9104 | fd: int |
| 9105 | |
| 9106 | Close a file descriptor. |
| 9107 | [clinic start generated code]*/ |
| 9108 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 9109 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9110 | os_close_impl(PyObject *module, int fd) |
| 9111 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9112 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9113 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9114 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 9115 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 9116 | * for more details. |
| 9117 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9118 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9119 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9120 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9121 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9122 | Py_END_ALLOW_THREADS |
| 9123 | if (res < 0) |
| 9124 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9125 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9126 | } |
| 9127 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9128 | /*[clinic input] |
| 9129 | os.closerange |
| 9130 | |
| 9131 | fd_low: int |
| 9132 | fd_high: int |
| 9133 | / |
| 9134 | |
| 9135 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 9136 | [clinic start generated code]*/ |
| 9137 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9138 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9139 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 9140 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9141 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9142 | Py_BEGIN_ALLOW_THREADS |
Kyle Evans | c230fde | 2020-10-11 13:54:11 -0500 | [diff] [blame] | 9143 | _Py_closerange(fd_low, fd_high - 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9144 | Py_END_ALLOW_THREADS |
| 9145 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 9146 | } |
| 9147 | |
| 9148 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9149 | /*[clinic input] |
| 9150 | os.dup -> int |
| 9151 | |
| 9152 | fd: int |
| 9153 | / |
| 9154 | |
| 9155 | Return a duplicate of a file descriptor. |
| 9156 | [clinic start generated code]*/ |
| 9157 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9158 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9159 | os_dup_impl(PyObject *module, int fd) |
| 9160 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9161 | { |
| 9162 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9163 | } |
| 9164 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9165 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9166 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 9167 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9168 | fd: int |
| 9169 | fd2: int |
| 9170 | inheritable: bool=True |
| 9171 | |
| 9172 | Duplicate file descriptor. |
| 9173 | [clinic start generated code]*/ |
| 9174 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 9175 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9176 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 9177 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9178 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 9179 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9180 | #if defined(HAVE_DUP3) && \ |
| 9181 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 9182 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 9183 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9184 | #endif |
| 9185 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 9186 | if (fd < 0 || fd2 < 0) { |
| 9187 | posix_error(); |
| 9188 | return -1; |
| 9189 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9190 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9191 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 9192 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 9193 | * upon close(), and therefore below. |
| 9194 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9195 | #ifdef MS_WINDOWS |
| 9196 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9197 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9198 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9199 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9200 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 9201 | if (res < 0) { |
| 9202 | posix_error(); |
| 9203 | return -1; |
| 9204 | } |
| 9205 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9206 | |
| 9207 | /* Character files like console cannot be make non-inheritable */ |
| 9208 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 9209 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 9210 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9211 | } |
| 9212 | |
| 9213 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 9214 | Py_BEGIN_ALLOW_THREADS |
| 9215 | if (!inheritable) |
| 9216 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 9217 | else |
| 9218 | res = dup2(fd, fd2); |
| 9219 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 9220 | if (res < 0) { |
| 9221 | posix_error(); |
| 9222 | return -1; |
| 9223 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9224 | |
| 9225 | #else |
| 9226 | |
| 9227 | #ifdef HAVE_DUP3 |
| 9228 | if (!inheritable && dup3_works != 0) { |
| 9229 | Py_BEGIN_ALLOW_THREADS |
| 9230 | res = dup3(fd, fd2, O_CLOEXEC); |
| 9231 | Py_END_ALLOW_THREADS |
| 9232 | if (res < 0) { |
| 9233 | if (dup3_works == -1) |
| 9234 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 9235 | if (dup3_works) { |
| 9236 | posix_error(); |
| 9237 | return -1; |
| 9238 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9239 | } |
| 9240 | } |
| 9241 | |
| 9242 | if (inheritable || dup3_works == 0) |
| 9243 | { |
| 9244 | #endif |
| 9245 | Py_BEGIN_ALLOW_THREADS |
| 9246 | res = dup2(fd, fd2); |
| 9247 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 9248 | if (res < 0) { |
| 9249 | posix_error(); |
| 9250 | return -1; |
| 9251 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9252 | |
| 9253 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 9254 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 9255 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9256 | } |
| 9257 | #ifdef HAVE_DUP3 |
| 9258 | } |
| 9259 | #endif |
| 9260 | |
| 9261 | #endif |
| 9262 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 9263 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9264 | } |
| 9265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9266 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9267 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9268 | /*[clinic input] |
| 9269 | os.lockf |
| 9270 | |
| 9271 | fd: int |
| 9272 | An open file descriptor. |
| 9273 | command: int |
| 9274 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 9275 | length: Py_off_t |
| 9276 | The number of bytes to lock, starting at the current position. |
| 9277 | / |
| 9278 | |
| 9279 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 9280 | |
| 9281 | [clinic start generated code]*/ |
| 9282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9283 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9284 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 9285 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9286 | { |
| 9287 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9288 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 9289 | if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) { |
| 9290 | return NULL; |
| 9291 | } |
| 9292 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9293 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9294 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9295 | Py_END_ALLOW_THREADS |
| 9296 | |
| 9297 | if (res < 0) |
| 9298 | return posix_error(); |
| 9299 | |
| 9300 | Py_RETURN_NONE; |
| 9301 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9302 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9303 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9304 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9305 | /*[clinic input] |
| 9306 | os.lseek -> Py_off_t |
| 9307 | |
| 9308 | fd: int |
| 9309 | position: Py_off_t |
| 9310 | how: int |
| 9311 | / |
| 9312 | |
| 9313 | Set the position of a file descriptor. Return the new position. |
| 9314 | |
| 9315 | Return the new cursor position in number of bytes |
| 9316 | relative to the beginning of the file. |
| 9317 | [clinic start generated code]*/ |
| 9318 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9319 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9320 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 9321 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9322 | { |
| 9323 | Py_off_t result; |
| 9324 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9325 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9326 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 9327 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9328 | case 0: how = SEEK_SET; break; |
| 9329 | case 1: how = SEEK_CUR; break; |
| 9330 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9331 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9332 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9333 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9334 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9335 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 9336 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9337 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 9338 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9339 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 9340 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9341 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9342 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9343 | if (result < 0) |
| 9344 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 9345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9346 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9347 | } |
| 9348 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9349 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9350 | /*[clinic input] |
| 9351 | os.read |
| 9352 | fd: int |
| 9353 | length: Py_ssize_t |
| 9354 | / |
| 9355 | |
| 9356 | Read from a file descriptor. Returns a bytes object. |
| 9357 | [clinic start generated code]*/ |
| 9358 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9359 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9360 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 9361 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9362 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9363 | Py_ssize_t n; |
| 9364 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9365 | |
| 9366 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9367 | errno = EINVAL; |
| 9368 | return posix_error(); |
| 9369 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9370 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 9371 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9372 | |
| 9373 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9374 | if (buffer == NULL) |
| 9375 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9376 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9377 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 9378 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9379 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9380 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9381 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9382 | |
| 9383 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9384 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9385 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9386 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9387 | } |
| 9388 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9389 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9390 | || defined(__APPLE__))) \ |
| 9391 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 9392 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9393 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9394 | 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] | 9395 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9396 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9397 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9398 | *iov = PyMem_New(struct iovec, cnt); |
| 9399 | if (*iov == NULL) { |
| 9400 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9401 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9402 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9403 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9404 | *buf = PyMem_New(Py_buffer, cnt); |
| 9405 | if (*buf == NULL) { |
| 9406 | PyMem_Del(*iov); |
| 9407 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9408 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9409 | } |
| 9410 | |
| 9411 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9412 | PyObject *item = PySequence_GetItem(seq, i); |
| 9413 | if (item == NULL) |
| 9414 | goto fail; |
| 9415 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 9416 | Py_DECREF(item); |
| 9417 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9418 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9419 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9420 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9421 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9422 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9423 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 9424 | |
| 9425 | fail: |
| 9426 | PyMem_Del(*iov); |
| 9427 | for (j = 0; j < i; j++) { |
| 9428 | PyBuffer_Release(&(*buf)[j]); |
| 9429 | } |
| 9430 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9431 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9432 | } |
| 9433 | |
| 9434 | static void |
| 9435 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 9436 | { |
| 9437 | int i; |
| 9438 | PyMem_Del(iov); |
| 9439 | for (i = 0; i < cnt; i++) { |
| 9440 | PyBuffer_Release(&buf[i]); |
| 9441 | } |
| 9442 | PyMem_Del(buf); |
| 9443 | } |
| 9444 | #endif |
| 9445 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9446 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9447 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9448 | /*[clinic input] |
| 9449 | os.readv -> Py_ssize_t |
| 9450 | |
| 9451 | fd: int |
| 9452 | buffers: object |
| 9453 | / |
| 9454 | |
| 9455 | Read from a file descriptor fd into an iterable of buffers. |
| 9456 | |
| 9457 | The buffers should be mutable buffers accepting bytes. |
| 9458 | readv will transfer data into each buffer until it is full |
| 9459 | and then move on to the next buffer in the sequence to hold |
| 9460 | the rest of the data. |
| 9461 | |
| 9462 | readv returns the total number of bytes read, |
| 9463 | which may be less than the total capacity of all the buffers. |
| 9464 | [clinic start generated code]*/ |
| 9465 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9466 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9467 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 9468 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9469 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9470 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9471 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9472 | struct iovec *iov; |
| 9473 | Py_buffer *buf; |
| 9474 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9475 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9476 | PyErr_SetString(PyExc_TypeError, |
| 9477 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9478 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9479 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9480 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9481 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9482 | if (cnt < 0) |
| 9483 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9484 | |
| 9485 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 9486 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9487 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9488 | do { |
| 9489 | Py_BEGIN_ALLOW_THREADS |
| 9490 | n = readv(fd, iov, cnt); |
| 9491 | Py_END_ALLOW_THREADS |
| 9492 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9493 | |
| 9494 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9495 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9496 | if (!async_err) |
| 9497 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9498 | return -1; |
| 9499 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9500 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9501 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9502 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9503 | #endif /* HAVE_READV */ |
| 9504 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9505 | |
| 9506 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9507 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9508 | os.pread |
| 9509 | |
| 9510 | fd: int |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9511 | length: Py_ssize_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9512 | offset: Py_off_t |
| 9513 | / |
| 9514 | |
| 9515 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 9516 | |
| 9517 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 9518 | the beginning of the file. The file offset remains unchanged. |
| 9519 | [clinic start generated code]*/ |
| 9520 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9521 | static PyObject * |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9522 | os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset) |
| 9523 | /*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9524 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9525 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9526 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9527 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9528 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9529 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9530 | errno = EINVAL; |
| 9531 | return posix_error(); |
| 9532 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9533 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9534 | if (buffer == NULL) |
| 9535 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9536 | |
| 9537 | do { |
| 9538 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9539 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9540 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9541 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9542 | Py_END_ALLOW_THREADS |
| 9543 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9544 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9545 | if (n < 0) { |
| 9546 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9547 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9548 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9549 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9550 | _PyBytes_Resize(&buffer, n); |
| 9551 | return buffer; |
| 9552 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9553 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9554 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9555 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 9556 | /*[clinic input] |
| 9557 | os.preadv -> Py_ssize_t |
| 9558 | |
| 9559 | fd: int |
| 9560 | buffers: object |
| 9561 | offset: Py_off_t |
| 9562 | flags: int = 0 |
| 9563 | / |
| 9564 | |
| 9565 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 9566 | |
| 9567 | Combines the functionality of readv() and pread(). As readv(), it will |
| 9568 | transfer data into each buffer until it is full and then move on to the next |
| 9569 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 9570 | specifies the file offset at which the input operation is to be performed. It |
| 9571 | will return the total number of bytes read (which can be less than the total |
| 9572 | capacity of all the objects). |
| 9573 | |
| 9574 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9575 | |
| 9576 | - RWF_HIPRI |
| 9577 | - RWF_NOWAIT |
| 9578 | |
| 9579 | Using non-zero flags requires Linux 4.6 or newer. |
| 9580 | [clinic start generated code]*/ |
| 9581 | |
| 9582 | static Py_ssize_t |
| 9583 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9584 | int flags) |
| 9585 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 9586 | { |
| 9587 | Py_ssize_t cnt, n; |
| 9588 | int async_err = 0; |
| 9589 | struct iovec *iov; |
| 9590 | Py_buffer *buf; |
| 9591 | |
| 9592 | if (!PySequence_Check(buffers)) { |
| 9593 | PyErr_SetString(PyExc_TypeError, |
| 9594 | "preadv2() arg 2 must be a sequence"); |
| 9595 | return -1; |
| 9596 | } |
| 9597 | |
| 9598 | cnt = PySequence_Size(buffers); |
| 9599 | if (cnt < 0) { |
| 9600 | return -1; |
| 9601 | } |
| 9602 | |
| 9603 | #ifndef HAVE_PREADV2 |
| 9604 | if(flags != 0) { |
| 9605 | argument_unavailable_error("preadv2", "flags"); |
| 9606 | return -1; |
| 9607 | } |
| 9608 | #endif |
| 9609 | |
| 9610 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 9611 | return -1; |
| 9612 | } |
| 9613 | #ifdef HAVE_PREADV2 |
| 9614 | do { |
| 9615 | Py_BEGIN_ALLOW_THREADS |
| 9616 | _Py_BEGIN_SUPPRESS_IPH |
| 9617 | n = preadv2(fd, iov, cnt, offset, flags); |
| 9618 | _Py_END_SUPPRESS_IPH |
| 9619 | Py_END_ALLOW_THREADS |
| 9620 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9621 | #else |
| 9622 | do { |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 9623 | #ifdef __APPLE__ |
| 9624 | /* This entire function will be removed from the module dict when the API |
| 9625 | * is not available. |
| 9626 | */ |
| 9627 | #pragma clang diagnostic push |
| 9628 | #pragma clang diagnostic ignored "-Wunguarded-availability" |
| 9629 | #pragma clang diagnostic ignored "-Wunguarded-availability-new" |
| 9630 | #endif |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9631 | Py_BEGIN_ALLOW_THREADS |
| 9632 | _Py_BEGIN_SUPPRESS_IPH |
| 9633 | n = preadv(fd, iov, cnt, offset); |
| 9634 | _Py_END_SUPPRESS_IPH |
| 9635 | Py_END_ALLOW_THREADS |
| 9636 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 9637 | |
| 9638 | #ifdef __APPLE__ |
| 9639 | #pragma clang diagnostic pop |
| 9640 | #endif |
| 9641 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9642 | #endif |
| 9643 | |
| 9644 | iov_cleanup(iov, buf, cnt); |
| 9645 | if (n < 0) { |
| 9646 | if (!async_err) { |
| 9647 | posix_error(); |
| 9648 | } |
| 9649 | return -1; |
| 9650 | } |
| 9651 | |
| 9652 | return n; |
| 9653 | } |
| 9654 | #endif /* HAVE_PREADV */ |
| 9655 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9656 | |
| 9657 | /*[clinic input] |
| 9658 | os.write -> Py_ssize_t |
| 9659 | |
| 9660 | fd: int |
| 9661 | data: Py_buffer |
| 9662 | / |
| 9663 | |
| 9664 | Write a bytes object to a file descriptor. |
| 9665 | [clinic start generated code]*/ |
| 9666 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9667 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9668 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 9669 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9670 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9671 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9672 | } |
| 9673 | |
| 9674 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9675 | #ifdef __APPLE__ |
| 9676 | /*[clinic input] |
| 9677 | os.sendfile |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9678 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9679 | out_fd: int |
| 9680 | in_fd: int |
| 9681 | offset: Py_off_t |
| 9682 | count as sbytes: Py_off_t |
| 9683 | headers: object(c_default="NULL") = () |
| 9684 | trailers: object(c_default="NULL") = () |
| 9685 | flags: int = 0 |
| 9686 | |
| 9687 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9688 | [clinic start generated code]*/ |
| 9689 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9690 | static PyObject * |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9691 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, |
| 9692 | Py_off_t sbytes, PyObject *headers, PyObject *trailers, |
| 9693 | int flags) |
| 9694 | /*[clinic end generated code: output=81c4bcd143f5c82b input=b0d72579d4c69afa]*/ |
| 9695 | #elif defined(__FreeBSD__) || defined(__DragonFly__) |
| 9696 | /*[clinic input] |
| 9697 | os.sendfile |
| 9698 | |
| 9699 | out_fd: int |
| 9700 | in_fd: int |
| 9701 | offset: Py_off_t |
| 9702 | count: Py_ssize_t |
| 9703 | headers: object(c_default="NULL") = () |
| 9704 | trailers: object(c_default="NULL") = () |
| 9705 | flags: int = 0 |
| 9706 | |
| 9707 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9708 | [clinic start generated code]*/ |
| 9709 | |
| 9710 | static PyObject * |
| 9711 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset, |
| 9712 | Py_ssize_t count, PyObject *headers, PyObject *trailers, |
| 9713 | int flags) |
| 9714 | /*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/ |
| 9715 | #else |
| 9716 | /*[clinic input] |
| 9717 | os.sendfile |
| 9718 | |
| 9719 | out_fd: int |
| 9720 | in_fd: int |
| 9721 | offset as offobj: object |
| 9722 | count: Py_ssize_t |
| 9723 | |
| 9724 | Copy count bytes from file descriptor in_fd to file descriptor out_fd. |
| 9725 | [clinic start generated code]*/ |
| 9726 | |
| 9727 | static PyObject * |
| 9728 | os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj, |
| 9729 | Py_ssize_t count) |
| 9730 | /*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/ |
| 9731 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9732 | { |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9733 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9734 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9735 | |
| 9736 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 9737 | #ifndef __APPLE__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9738 | off_t sbytes; |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9739 | #endif |
| 9740 | Py_buffer *hbuf, *tbuf; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9741 | struct sf_hdtr sf; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9742 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 9743 | sf.headers = NULL; |
| 9744 | sf.trailers = NULL; |
| 9745 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9746 | if (headers != NULL) { |
| 9747 | if (!PySequence_Check(headers)) { |
| 9748 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9749 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9750 | return NULL; |
| 9751 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9752 | Py_ssize_t i = PySequence_Size(headers); |
| 9753 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9754 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9755 | if (i > INT_MAX) { |
| 9756 | PyErr_SetString(PyExc_OverflowError, |
| 9757 | "sendfile() header is too large"); |
| 9758 | return NULL; |
| 9759 | } |
| 9760 | if (i > 0) { |
| 9761 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9762 | if (iov_setup(&(sf.headers), &hbuf, |
| 9763 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9764 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9765 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9766 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 9767 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 9768 | # define OFF_T_MAX 0x7fffffffffffffff |
| 9769 | if (sbytes >= OFF_T_MAX - blen) { |
| 9770 | PyErr_SetString(PyExc_OverflowError, |
| 9771 | "sendfile() header is too large"); |
| 9772 | return NULL; |
| 9773 | } |
| 9774 | sbytes += blen; |
| 9775 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9776 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9777 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9778 | } |
| 9779 | } |
| 9780 | if (trailers != NULL) { |
| 9781 | if (!PySequence_Check(trailers)) { |
| 9782 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9783 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9784 | return NULL; |
| 9785 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9786 | Py_ssize_t i = PySequence_Size(trailers); |
| 9787 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9788 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9789 | if (i > INT_MAX) { |
| 9790 | PyErr_SetString(PyExc_OverflowError, |
| 9791 | "sendfile() trailer is too large"); |
| 9792 | return NULL; |
| 9793 | } |
| 9794 | if (i > 0) { |
| 9795 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9796 | if (iov_setup(&(sf.trailers), &tbuf, |
| 9797 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9798 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9799 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9800 | } |
| 9801 | } |
| 9802 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9803 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9804 | do { |
| 9805 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9806 | #ifdef __APPLE__ |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9807 | ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9808 | #else |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9809 | ret = sendfile(in_fd, out_fd, offset, count, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9810 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9811 | Py_END_ALLOW_THREADS |
| 9812 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9813 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9814 | |
| 9815 | if (sf.headers != NULL) |
| 9816 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 9817 | if (sf.trailers != NULL) |
| 9818 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 9819 | |
| 9820 | if (ret < 0) { |
| 9821 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 9822 | if (sbytes != 0) { |
| 9823 | // some data has been sent |
| 9824 | goto done; |
| 9825 | } |
| 9826 | else { |
| 9827 | // no data has been sent; upper application is supposed |
| 9828 | // to retry on EAGAIN or EBUSY |
| 9829 | return posix_error(); |
| 9830 | } |
| 9831 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9832 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9833 | } |
| 9834 | goto done; |
| 9835 | |
| 9836 | done: |
| 9837 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 9838 | return Py_BuildValue("l", sbytes); |
| 9839 | #else |
| 9840 | return Py_BuildValue("L", sbytes); |
| 9841 | #endif |
| 9842 | |
| 9843 | #else |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9844 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9845 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9846 | do { |
| 9847 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9848 | ret = sendfile(out_fd, in_fd, NULL, count); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9849 | Py_END_ALLOW_THREADS |
| 9850 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9851 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9852 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9853 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9854 | } |
| 9855 | #endif |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9856 | off_t offset; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9857 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9858 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9859 | |
Jakub Kulík | 8c0be6f | 2020-09-05 21:10:01 +0200 | [diff] [blame] | 9860 | #if defined(__sun) && defined(__SVR4) |
| 9861 | // On Solaris, sendfile raises EINVAL rather than returning 0 |
| 9862 | // when the offset is equal or bigger than the in_fd size. |
Jakub Kulík | 8c0be6f | 2020-09-05 21:10:01 +0200 | [diff] [blame] | 9863 | struct stat st; |
| 9864 | |
| 9865 | do { |
| 9866 | Py_BEGIN_ALLOW_THREADS |
Jakub Kulík | fa8c9e7 | 2020-09-09 21:29:42 +0200 | [diff] [blame] | 9867 | ret = fstat(in_fd, &st); |
Jakub Kulík | 8c0be6f | 2020-09-05 21:10:01 +0200 | [diff] [blame] | 9868 | Py_END_ALLOW_THREADS |
Jakub Kulík | fa8c9e7 | 2020-09-09 21:29:42 +0200 | [diff] [blame] | 9869 | } while (ret != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Jakub Kulík | 8c0be6f | 2020-09-05 21:10:01 +0200 | [diff] [blame] | 9870 | if (ret < 0) |
| 9871 | return (!async_err) ? posix_error() : NULL; |
| 9872 | |
| 9873 | if (offset >= st.st_size) { |
| 9874 | return Py_BuildValue("i", 0); |
| 9875 | } |
Jakub Stasiak | fd4ed57 | 2020-11-12 10:49:30 +0100 | [diff] [blame^] | 9876 | |
| 9877 | // On illumos specifically sendfile() may perform a partial write but |
| 9878 | // return -1/an error (in one confirmed case the destination socket |
| 9879 | // had a 5 second timeout set and errno was EAGAIN) and it's on the client |
| 9880 | // code to check if the offset parameter was modified by sendfile(). |
| 9881 | // |
| 9882 | // We need this variable to track said change. |
| 9883 | off_t original_offset = offset; |
Jakub Kulík | 8c0be6f | 2020-09-05 21:10:01 +0200 | [diff] [blame] | 9884 | #endif |
| 9885 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9886 | do { |
| 9887 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 9888 | ret = sendfile(out_fd, in_fd, &offset, count); |
Jakub Stasiak | fd4ed57 | 2020-11-12 10:49:30 +0100 | [diff] [blame^] | 9889 | #if defined(__sun) && defined(__SVR4) |
| 9890 | // This handles illumos-specific sendfile() partial write behavior, |
| 9891 | // see a comment above for more details. |
| 9892 | if (ret < 0 && offset != original_offset) { |
| 9893 | ret = offset - original_offset; |
| 9894 | } |
| 9895 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9896 | Py_END_ALLOW_THREADS |
| 9897 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9898 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9899 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9900 | return Py_BuildValue("n", ret); |
| 9901 | #endif |
| 9902 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9903 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9904 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9905 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9906 | #if defined(__APPLE__) |
| 9907 | /*[clinic input] |
| 9908 | os._fcopyfile |
| 9909 | |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9910 | in_fd: int |
| 9911 | out_fd: int |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9912 | flags: int |
| 9913 | / |
| 9914 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9915 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9916 | [clinic start generated code]*/ |
| 9917 | |
| 9918 | static PyObject * |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9919 | os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags) |
| 9920 | /*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9921 | { |
| 9922 | int ret; |
| 9923 | |
| 9924 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9925 | ret = fcopyfile(in_fd, out_fd, NULL, flags); |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9926 | Py_END_ALLOW_THREADS |
| 9927 | if (ret < 0) |
| 9928 | return posix_error(); |
| 9929 | Py_RETURN_NONE; |
| 9930 | } |
| 9931 | #endif |
| 9932 | |
| 9933 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9934 | /*[clinic input] |
| 9935 | os.fstat |
| 9936 | |
| 9937 | fd : int |
| 9938 | |
| 9939 | Perform a stat system call on the given file descriptor. |
| 9940 | |
| 9941 | Like stat(), but for an open file descriptor. |
| 9942 | Equivalent to os.stat(fd). |
| 9943 | [clinic start generated code]*/ |
| 9944 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9945 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9946 | os_fstat_impl(PyObject *module, int fd) |
| 9947 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9948 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9949 | STRUCT_STAT st; |
| 9950 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9951 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9952 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9953 | do { |
| 9954 | Py_BEGIN_ALLOW_THREADS |
| 9955 | res = FSTAT(fd, &st); |
| 9956 | Py_END_ALLOW_THREADS |
| 9957 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9958 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9959 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9960 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9961 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9962 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9963 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9964 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9965 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 9966 | return _pystat_fromstructstat(module, &st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9967 | } |
| 9968 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9969 | |
| 9970 | /*[clinic input] |
| 9971 | os.isatty -> bool |
| 9972 | fd: int |
| 9973 | / |
| 9974 | |
| 9975 | Return True if the fd is connected to a terminal. |
| 9976 | |
| 9977 | Return True if the file descriptor is an open file descriptor |
| 9978 | connected to the slave end of a terminal. |
| 9979 | [clinic start generated code]*/ |
| 9980 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9981 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9982 | os_isatty_impl(PyObject *module, int fd) |
| 9983 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9984 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9985 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9986 | _Py_BEGIN_SUPPRESS_IPH |
| 9987 | return_value = isatty(fd); |
| 9988 | _Py_END_SUPPRESS_IPH |
| 9989 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9990 | } |
| 9991 | |
| 9992 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9993 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9994 | /*[clinic input] |
| 9995 | os.pipe |
| 9996 | |
| 9997 | Create a pipe. |
| 9998 | |
| 9999 | Returns a tuple of two file descriptors: |
| 10000 | (read_fd, write_fd) |
| 10001 | [clinic start generated code]*/ |
| 10002 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10003 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10004 | os_pipe_impl(PyObject *module) |
| 10005 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 10006 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10007 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10008 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10009 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10010 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10011 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10012 | #else |
| 10013 | int res; |
| 10014 | #endif |
| 10015 | |
| 10016 | #ifdef MS_WINDOWS |
| 10017 | attr.nLength = sizeof(attr); |
| 10018 | attr.lpSecurityDescriptor = NULL; |
| 10019 | attr.bInheritHandle = FALSE; |
| 10020 | |
| 10021 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 10022 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10023 | ok = CreatePipe(&read, &write, &attr, 0); |
| 10024 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 10025 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 10026 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10027 | if (fds[0] == -1 || fds[1] == -1) { |
| 10028 | CloseHandle(read); |
| 10029 | CloseHandle(write); |
| 10030 | ok = 0; |
| 10031 | } |
| 10032 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 10033 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10034 | Py_END_ALLOW_THREADS |
| 10035 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10036 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 10037 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10038 | #else |
| 10039 | |
| 10040 | #ifdef HAVE_PIPE2 |
| 10041 | Py_BEGIN_ALLOW_THREADS |
| 10042 | res = pipe2(fds, O_CLOEXEC); |
| 10043 | Py_END_ALLOW_THREADS |
| 10044 | |
| 10045 | if (res != 0 && errno == ENOSYS) |
| 10046 | { |
| 10047 | #endif |
| 10048 | Py_BEGIN_ALLOW_THREADS |
| 10049 | res = pipe(fds); |
| 10050 | Py_END_ALLOW_THREADS |
| 10051 | |
| 10052 | if (res == 0) { |
| 10053 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 10054 | close(fds[0]); |
| 10055 | close(fds[1]); |
| 10056 | return NULL; |
| 10057 | } |
| 10058 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 10059 | close(fds[0]); |
| 10060 | close(fds[1]); |
| 10061 | return NULL; |
| 10062 | } |
| 10063 | } |
| 10064 | #ifdef HAVE_PIPE2 |
| 10065 | } |
| 10066 | #endif |
| 10067 | |
| 10068 | if (res != 0) |
| 10069 | return PyErr_SetFromErrno(PyExc_OSError); |
| 10070 | #endif /* !MS_WINDOWS */ |
| 10071 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 10072 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 10073 | #endif /* HAVE_PIPE */ |
| 10074 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10075 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10076 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10077 | /*[clinic input] |
| 10078 | os.pipe2 |
| 10079 | |
| 10080 | flags: int |
| 10081 | / |
| 10082 | |
| 10083 | Create a pipe with flags set atomically. |
| 10084 | |
| 10085 | Returns a tuple of two file descriptors: |
| 10086 | (read_fd, write_fd) |
| 10087 | |
| 10088 | flags can be constructed by ORing together one or more of these values: |
| 10089 | O_NONBLOCK, O_CLOEXEC. |
| 10090 | [clinic start generated code]*/ |
| 10091 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10092 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10093 | os_pipe2_impl(PyObject *module, int flags) |
| 10094 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10095 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10096 | int fds[2]; |
| 10097 | int res; |
| 10098 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 10099 | res = pipe2(fds, flags); |
| 10100 | if (res != 0) |
| 10101 | return posix_error(); |
| 10102 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 10103 | } |
| 10104 | #endif /* HAVE_PIPE2 */ |
| 10105 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10106 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10107 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10108 | /*[clinic input] |
| 10109 | os.writev -> Py_ssize_t |
| 10110 | fd: int |
| 10111 | buffers: object |
| 10112 | / |
| 10113 | |
| 10114 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 10115 | |
| 10116 | Returns the total number of bytes written. |
| 10117 | buffers must be a sequence of bytes-like objects. |
| 10118 | [clinic start generated code]*/ |
| 10119 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10120 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10121 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 10122 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10123 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 10124 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10125 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10126 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10127 | struct iovec *iov; |
| 10128 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10129 | |
| 10130 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10131 | PyErr_SetString(PyExc_TypeError, |
| 10132 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10133 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10134 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10135 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 10136 | if (cnt < 0) |
| 10137 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10139 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 10140 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10141 | } |
| 10142 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10143 | do { |
| 10144 | Py_BEGIN_ALLOW_THREADS |
| 10145 | result = writev(fd, iov, cnt); |
| 10146 | Py_END_ALLOW_THREADS |
| 10147 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10148 | |
| 10149 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10150 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10151 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 10152 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10153 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10154 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10155 | #endif /* HAVE_WRITEV */ |
| 10156 | |
| 10157 | |
| 10158 | #ifdef HAVE_PWRITE |
| 10159 | /*[clinic input] |
| 10160 | os.pwrite -> Py_ssize_t |
| 10161 | |
| 10162 | fd: int |
| 10163 | buffer: Py_buffer |
| 10164 | offset: Py_off_t |
| 10165 | / |
| 10166 | |
| 10167 | Write bytes to a file descriptor starting at a particular offset. |
| 10168 | |
| 10169 | Write buffer to fd, starting at offset bytes from the beginning of |
| 10170 | the file. Returns the number of bytes writte. Does not change the |
| 10171 | current file offset. |
| 10172 | [clinic start generated code]*/ |
| 10173 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10174 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10175 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 10176 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10177 | { |
| 10178 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10179 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10180 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10181 | do { |
| 10182 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10183 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10184 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 10185 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10186 | Py_END_ALLOW_THREADS |
| 10187 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10188 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10189 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10190 | posix_error(); |
| 10191 | return size; |
| 10192 | } |
| 10193 | #endif /* HAVE_PWRITE */ |
| 10194 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 10195 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 10196 | /*[clinic input] |
| 10197 | os.pwritev -> Py_ssize_t |
| 10198 | |
| 10199 | fd: int |
| 10200 | buffers: object |
| 10201 | offset: Py_off_t |
| 10202 | flags: int = 0 |
| 10203 | / |
| 10204 | |
| 10205 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 10206 | |
| 10207 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 10208 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 10209 | buffer is written before proceeding to second, and so on. The operating system may |
| 10210 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 10211 | This function writes the contents of each object to the file descriptor and returns |
| 10212 | the total number of bytes written. |
| 10213 | |
| 10214 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 10215 | |
| 10216 | - RWF_DSYNC |
| 10217 | - RWF_SYNC |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 10218 | - RWF_APPEND |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 10219 | |
| 10220 | Using non-zero flags requires Linux 4.7 or newer. |
| 10221 | [clinic start generated code]*/ |
| 10222 | |
| 10223 | static Py_ssize_t |
| 10224 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 10225 | int flags) |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 10226 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=35358c327e1a2a8e]*/ |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 10227 | { |
| 10228 | Py_ssize_t cnt; |
| 10229 | Py_ssize_t result; |
| 10230 | int async_err = 0; |
| 10231 | struct iovec *iov; |
| 10232 | Py_buffer *buf; |
| 10233 | |
| 10234 | if (!PySequence_Check(buffers)) { |
| 10235 | PyErr_SetString(PyExc_TypeError, |
| 10236 | "pwritev() arg 2 must be a sequence"); |
| 10237 | return -1; |
| 10238 | } |
| 10239 | |
| 10240 | cnt = PySequence_Size(buffers); |
| 10241 | if (cnt < 0) { |
| 10242 | return -1; |
| 10243 | } |
| 10244 | |
| 10245 | #ifndef HAVE_PWRITEV2 |
| 10246 | if(flags != 0) { |
| 10247 | argument_unavailable_error("pwritev2", "flags"); |
| 10248 | return -1; |
| 10249 | } |
| 10250 | #endif |
| 10251 | |
| 10252 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 10253 | return -1; |
| 10254 | } |
| 10255 | #ifdef HAVE_PWRITEV2 |
| 10256 | do { |
| 10257 | Py_BEGIN_ALLOW_THREADS |
| 10258 | _Py_BEGIN_SUPPRESS_IPH |
| 10259 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 10260 | _Py_END_SUPPRESS_IPH |
| 10261 | Py_END_ALLOW_THREADS |
| 10262 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10263 | #else |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 10264 | |
| 10265 | #ifdef __APPLE__ |
| 10266 | /* This entire function will be removed from the module dict when the API |
| 10267 | * is not available. |
| 10268 | */ |
| 10269 | #pragma clang diagnostic push |
| 10270 | #pragma clang diagnostic ignored "-Wunguarded-availability" |
| 10271 | #pragma clang diagnostic ignored "-Wunguarded-availability-new" |
| 10272 | #endif |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 10273 | do { |
| 10274 | Py_BEGIN_ALLOW_THREADS |
| 10275 | _Py_BEGIN_SUPPRESS_IPH |
| 10276 | result = pwritev(fd, iov, cnt, offset); |
| 10277 | _Py_END_SUPPRESS_IPH |
| 10278 | Py_END_ALLOW_THREADS |
| 10279 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 10280 | |
| 10281 | #ifdef __APPLE__ |
| 10282 | #pragma clang diagnostic pop |
| 10283 | #endif |
| 10284 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 10285 | #endif |
| 10286 | |
| 10287 | iov_cleanup(iov, buf, cnt); |
| 10288 | if (result < 0) { |
| 10289 | if (!async_err) { |
| 10290 | posix_error(); |
| 10291 | } |
| 10292 | return -1; |
| 10293 | } |
| 10294 | |
| 10295 | return result; |
| 10296 | } |
| 10297 | #endif /* HAVE_PWRITEV */ |
| 10298 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 10299 | #ifdef HAVE_COPY_FILE_RANGE |
| 10300 | /*[clinic input] |
| 10301 | |
| 10302 | os.copy_file_range |
| 10303 | src: int |
| 10304 | Source file descriptor. |
| 10305 | dst: int |
| 10306 | Destination file descriptor. |
| 10307 | count: Py_ssize_t |
| 10308 | Number of bytes to copy. |
| 10309 | offset_src: object = None |
| 10310 | Starting offset in src. |
| 10311 | offset_dst: object = None |
| 10312 | Starting offset in dst. |
| 10313 | |
| 10314 | Copy count bytes from one file descriptor to another. |
| 10315 | |
| 10316 | If offset_src is None, then src is read from the current position; |
| 10317 | respectively for offset_dst. |
| 10318 | [clinic start generated code]*/ |
| 10319 | |
| 10320 | static PyObject * |
| 10321 | os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, |
| 10322 | PyObject *offset_src, PyObject *offset_dst) |
| 10323 | /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ |
| 10324 | { |
| 10325 | off_t offset_src_val, offset_dst_val; |
| 10326 | off_t *p_offset_src = NULL; |
| 10327 | off_t *p_offset_dst = NULL; |
| 10328 | Py_ssize_t ret; |
| 10329 | int async_err = 0; |
| 10330 | /* The flags argument is provided to allow |
| 10331 | * for future extensions and currently must be to 0. */ |
| 10332 | int flags = 0; |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 10333 | |
| 10334 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 10335 | if (count < 0) { |
| 10336 | PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); |
| 10337 | return NULL; |
| 10338 | } |
| 10339 | |
| 10340 | if (offset_src != Py_None) { |
| 10341 | if (!Py_off_t_converter(offset_src, &offset_src_val)) { |
| 10342 | return NULL; |
| 10343 | } |
| 10344 | p_offset_src = &offset_src_val; |
| 10345 | } |
| 10346 | |
| 10347 | if (offset_dst != Py_None) { |
| 10348 | if (!Py_off_t_converter(offset_dst, &offset_dst_val)) { |
| 10349 | return NULL; |
| 10350 | } |
| 10351 | p_offset_dst = &offset_dst_val; |
| 10352 | } |
| 10353 | |
| 10354 | do { |
| 10355 | Py_BEGIN_ALLOW_THREADS |
| 10356 | ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags); |
| 10357 | Py_END_ALLOW_THREADS |
| 10358 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10359 | |
| 10360 | if (ret < 0) { |
| 10361 | return (!async_err) ? posix_error() : NULL; |
| 10362 | } |
| 10363 | |
| 10364 | return PyLong_FromSsize_t(ret); |
| 10365 | } |
| 10366 | #endif /* HAVE_COPY_FILE_RANGE*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10367 | |
| 10368 | #ifdef HAVE_MKFIFO |
| 10369 | /*[clinic input] |
| 10370 | os.mkfifo |
| 10371 | |
| 10372 | path: path_t |
| 10373 | mode: int=0o666 |
| 10374 | * |
| 10375 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 10376 | |
| 10377 | Create a "fifo" (a POSIX named pipe). |
| 10378 | |
| 10379 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 10380 | and path should be relative; path will then be relative to that directory. |
| 10381 | dir_fd may not be implemented on your platform. |
| 10382 | If it is unavailable, using it will raise a NotImplementedError. |
| 10383 | [clinic start generated code]*/ |
| 10384 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10385 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10386 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 10387 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10388 | { |
| 10389 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10390 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10391 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10392 | do { |
| 10393 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10394 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10395 | if (dir_fd != DEFAULT_DIR_FD) |
| 10396 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 10397 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10398 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10399 | result = mkfifo(path->narrow, mode); |
| 10400 | Py_END_ALLOW_THREADS |
| 10401 | } while (result != 0 && errno == EINTR && |
| 10402 | !(async_err = PyErr_CheckSignals())); |
| 10403 | if (result != 0) |
| 10404 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10405 | |
| 10406 | Py_RETURN_NONE; |
| 10407 | } |
| 10408 | #endif /* HAVE_MKFIFO */ |
| 10409 | |
| 10410 | |
| 10411 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 10412 | /*[clinic input] |
| 10413 | os.mknod |
| 10414 | |
| 10415 | path: path_t |
| 10416 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10417 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10418 | * |
| 10419 | dir_fd: dir_fd(requires='mknodat')=None |
| 10420 | |
| 10421 | Create a node in the file system. |
| 10422 | |
| 10423 | Create a node in the file system (file, device special file or named pipe) |
| 10424 | at path. mode specifies both the permissions to use and the |
| 10425 | type of node to be created, being combined (bitwise OR) with one of |
| 10426 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 10427 | device defines the newly created device special file (probably using |
| 10428 | os.makedev()). Otherwise device is ignored. |
| 10429 | |
| 10430 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 10431 | and path should be relative; path will then be relative to that directory. |
| 10432 | dir_fd may not be implemented on your platform. |
| 10433 | If it is unavailable, using it will raise a NotImplementedError. |
| 10434 | [clinic start generated code]*/ |
| 10435 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10436 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10437 | 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] | 10438 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10439 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10440 | { |
| 10441 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10442 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10443 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10444 | do { |
| 10445 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10446 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10447 | if (dir_fd != DEFAULT_DIR_FD) |
| 10448 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 10449 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10450 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10451 | result = mknod(path->narrow, mode, device); |
| 10452 | Py_END_ALLOW_THREADS |
| 10453 | } while (result != 0 && errno == EINTR && |
| 10454 | !(async_err = PyErr_CheckSignals())); |
| 10455 | if (result != 0) |
| 10456 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10457 | |
| 10458 | Py_RETURN_NONE; |
| 10459 | } |
| 10460 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 10461 | |
| 10462 | |
| 10463 | #ifdef HAVE_DEVICE_MACROS |
| 10464 | /*[clinic input] |
| 10465 | os.major -> unsigned_int |
| 10466 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10467 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10468 | / |
| 10469 | |
| 10470 | Extracts a device major number from a raw device number. |
| 10471 | [clinic start generated code]*/ |
| 10472 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10473 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10474 | os_major_impl(PyObject *module, dev_t device) |
| 10475 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10476 | { |
| 10477 | return major(device); |
| 10478 | } |
| 10479 | |
| 10480 | |
| 10481 | /*[clinic input] |
| 10482 | os.minor -> unsigned_int |
| 10483 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10484 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10485 | / |
| 10486 | |
| 10487 | Extracts a device minor number from a raw device number. |
| 10488 | [clinic start generated code]*/ |
| 10489 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10490 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10491 | os_minor_impl(PyObject *module, dev_t device) |
| 10492 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10493 | { |
| 10494 | return minor(device); |
| 10495 | } |
| 10496 | |
| 10497 | |
| 10498 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10499 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10500 | |
| 10501 | major: int |
| 10502 | minor: int |
| 10503 | / |
| 10504 | |
| 10505 | Composes a raw device number from the major and minor device numbers. |
| 10506 | [clinic start generated code]*/ |
| 10507 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 10508 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10509 | os_makedev_impl(PyObject *module, int major, int minor) |
| 10510 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10511 | { |
| 10512 | return makedev(major, minor); |
| 10513 | } |
| 10514 | #endif /* HAVE_DEVICE_MACROS */ |
| 10515 | |
| 10516 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10517 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10518 | /*[clinic input] |
| 10519 | os.ftruncate |
| 10520 | |
| 10521 | fd: int |
| 10522 | length: Py_off_t |
| 10523 | / |
| 10524 | |
| 10525 | Truncate a file, specified by file descriptor, to a specific length. |
| 10526 | [clinic start generated code]*/ |
| 10527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10528 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10529 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 10530 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10531 | { |
| 10532 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10533 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10534 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10535 | if (PySys_Audit("os.truncate", "in", fd, length) < 0) { |
| 10536 | return NULL; |
| 10537 | } |
| 10538 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10539 | do { |
| 10540 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10541 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10542 | #ifdef MS_WINDOWS |
| 10543 | result = _chsize_s(fd, length); |
| 10544 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10545 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10546 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10547 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10548 | Py_END_ALLOW_THREADS |
| 10549 | } while (result != 0 && errno == EINTR && |
| 10550 | !(async_err = PyErr_CheckSignals())); |
| 10551 | if (result != 0) |
| 10552 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10553 | Py_RETURN_NONE; |
| 10554 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10555 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10556 | |
| 10557 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10558 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10559 | /*[clinic input] |
| 10560 | os.truncate |
| 10561 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 10562 | length: Py_off_t |
| 10563 | |
| 10564 | Truncate a file, specified by path, to a specific length. |
| 10565 | |
| 10566 | On some platforms, path may also be specified as an open file descriptor. |
| 10567 | If this functionality is unavailable, using it raises an exception. |
| 10568 | [clinic start generated code]*/ |
| 10569 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10570 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10571 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 10572 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10573 | { |
| 10574 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10575 | #ifdef MS_WINDOWS |
| 10576 | int fd; |
| 10577 | #endif |
| 10578 | |
| 10579 | if (path->fd != -1) |
| 10580 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10581 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10582 | if (PySys_Audit("os.truncate", "On", path->object, length) < 0) { |
| 10583 | return NULL; |
| 10584 | } |
| 10585 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10586 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10587 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10588 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10589 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 10590 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10591 | result = -1; |
| 10592 | else { |
| 10593 | result = _chsize_s(fd, length); |
| 10594 | close(fd); |
| 10595 | if (result < 0) |
| 10596 | errno = result; |
| 10597 | } |
| 10598 | #else |
| 10599 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10600 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10601 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10602 | Py_END_ALLOW_THREADS |
| 10603 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 10604 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10605 | |
| 10606 | Py_RETURN_NONE; |
| 10607 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10608 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10609 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10610 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10611 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 10612 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 10613 | defined, which is the case in Python on AIX. AIX bug report: |
| 10614 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 10615 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 10616 | # define POSIX_FADVISE_AIX_BUG |
| 10617 | #endif |
| 10618 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10619 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10620 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10621 | /*[clinic input] |
| 10622 | os.posix_fallocate |
| 10623 | |
| 10624 | fd: int |
| 10625 | offset: Py_off_t |
| 10626 | length: Py_off_t |
| 10627 | / |
| 10628 | |
| 10629 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 10630 | |
| 10631 | Ensure that the file specified by fd encompasses a range of bytes |
| 10632 | starting at offset bytes from the beginning and continuing for length bytes. |
| 10633 | [clinic start generated code]*/ |
| 10634 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10635 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10636 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10637 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10638 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10639 | { |
| 10640 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10641 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10642 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10643 | do { |
| 10644 | Py_BEGIN_ALLOW_THREADS |
| 10645 | result = posix_fallocate(fd, offset, length); |
| 10646 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10647 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10648 | |
| 10649 | if (result == 0) |
| 10650 | Py_RETURN_NONE; |
| 10651 | |
| 10652 | if (async_err) |
| 10653 | return NULL; |
| 10654 | |
| 10655 | errno = result; |
| 10656 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10657 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10658 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10659 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10660 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10661 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10662 | /*[clinic input] |
| 10663 | os.posix_fadvise |
| 10664 | |
| 10665 | fd: int |
| 10666 | offset: Py_off_t |
| 10667 | length: Py_off_t |
| 10668 | advice: int |
| 10669 | / |
| 10670 | |
| 10671 | Announce an intention to access data in a specific pattern. |
| 10672 | |
| 10673 | Announce an intention to access data in a specific pattern, thus allowing |
| 10674 | the kernel to make optimizations. |
| 10675 | The advice applies to the region of the file specified by fd starting at |
| 10676 | offset and continuing for length bytes. |
| 10677 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 10678 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 10679 | POSIX_FADV_DONTNEED. |
| 10680 | [clinic start generated code]*/ |
| 10681 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10682 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10683 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10684 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10685 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10686 | { |
| 10687 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10688 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10689 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10690 | do { |
| 10691 | Py_BEGIN_ALLOW_THREADS |
| 10692 | result = posix_fadvise(fd, offset, length, advice); |
| 10693 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10694 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10695 | |
| 10696 | if (result == 0) |
| 10697 | Py_RETURN_NONE; |
| 10698 | |
| 10699 | if (async_err) |
| 10700 | return NULL; |
| 10701 | |
| 10702 | errno = result; |
| 10703 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10704 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10705 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10706 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10707 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 10708 | #ifdef MS_WINDOWS |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10709 | static PyObject* |
| 10710 | win32_putenv(PyObject *name, PyObject *value) |
| 10711 | { |
| 10712 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 10713 | defining hidden environment variables. */ |
| 10714 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 10715 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 10716 | { |
| 10717 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10718 | return NULL; |
| 10719 | } |
| 10720 | PyObject *unicode; |
| 10721 | if (value != NULL) { |
| 10722 | unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 10723 | } |
| 10724 | else { |
| 10725 | unicode = PyUnicode_FromFormat("%U=", name); |
| 10726 | } |
| 10727 | if (unicode == NULL) { |
| 10728 | return NULL; |
| 10729 | } |
| 10730 | |
| 10731 | Py_ssize_t size; |
| 10732 | /* PyUnicode_AsWideCharString() rejects embedded null characters */ |
| 10733 | wchar_t *env = PyUnicode_AsWideCharString(unicode, &size); |
| 10734 | Py_DECREF(unicode); |
| 10735 | |
| 10736 | if (env == NULL) { |
| 10737 | return NULL; |
| 10738 | } |
| 10739 | if (size > _MAX_ENV) { |
| 10740 | PyErr_Format(PyExc_ValueError, |
| 10741 | "the environment variable is longer than %u characters", |
| 10742 | _MAX_ENV); |
| 10743 | PyMem_Free(env); |
| 10744 | return NULL; |
| 10745 | } |
| 10746 | |
| 10747 | /* _wputenv() and SetEnvironmentVariableW() update the environment in the |
| 10748 | Process Environment Block (PEB). _wputenv() also updates CRT 'environ' |
| 10749 | and '_wenviron' variables, whereas SetEnvironmentVariableW() does not. |
| 10750 | |
| 10751 | Prefer _wputenv() to be compatible with C libraries using CRT |
| 10752 | variables and CRT functions using these variables (ex: getenv()). */ |
| 10753 | int err = _wputenv(env); |
| 10754 | PyMem_Free(env); |
| 10755 | |
| 10756 | if (err) { |
| 10757 | posix_error(); |
| 10758 | return NULL; |
| 10759 | } |
| 10760 | |
| 10761 | Py_RETURN_NONE; |
| 10762 | } |
| 10763 | #endif |
| 10764 | |
| 10765 | |
| 10766 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10767 | /*[clinic input] |
| 10768 | os.putenv |
| 10769 | |
| 10770 | name: unicode |
| 10771 | value: unicode |
| 10772 | / |
| 10773 | |
| 10774 | Change or add an environment variable. |
| 10775 | [clinic start generated code]*/ |
| 10776 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10777 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10778 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10779 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10780 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10781 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10782 | return NULL; |
| 10783 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10784 | return win32_putenv(name, value); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10785 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10786 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10787 | /*[clinic input] |
| 10788 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10789 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10790 | name: FSConverter |
| 10791 | value: FSConverter |
| 10792 | / |
| 10793 | |
| 10794 | Change or add an environment variable. |
| 10795 | [clinic start generated code]*/ |
| 10796 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10797 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10798 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10799 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10800 | { |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10801 | const char *name_string = PyBytes_AS_STRING(name); |
| 10802 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10803 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10804 | if (strchr(name_string, '=') != NULL) { |
| 10805 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10806 | return NULL; |
| 10807 | } |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10808 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10809 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10810 | return NULL; |
| 10811 | } |
| 10812 | |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10813 | if (setenv(name_string, value_string, 1)) { |
| 10814 | return posix_error(); |
| 10815 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10816 | Py_RETURN_NONE; |
| 10817 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10818 | #endif /* !defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10819 | |
| 10820 | |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10821 | #ifdef MS_WINDOWS |
| 10822 | /*[clinic input] |
| 10823 | os.unsetenv |
| 10824 | name: unicode |
| 10825 | / |
| 10826 | |
| 10827 | Delete an environment variable. |
| 10828 | [clinic start generated code]*/ |
| 10829 | |
| 10830 | static PyObject * |
| 10831 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10832 | /*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/ |
| 10833 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10834 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10835 | return NULL; |
| 10836 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10837 | return win32_putenv(name, NULL); |
| 10838 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10839 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10840 | /*[clinic input] |
| 10841 | os.unsetenv |
| 10842 | name: FSConverter |
| 10843 | / |
| 10844 | |
| 10845 | Delete an environment variable. |
| 10846 | [clinic start generated code]*/ |
| 10847 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10848 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10849 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10850 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10851 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10852 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10853 | return NULL; |
| 10854 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10855 | #ifdef HAVE_BROKEN_UNSETENV |
| 10856 | unsetenv(PyBytes_AS_STRING(name)); |
| 10857 | #else |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10858 | int err = unsetenv(PyBytes_AS_STRING(name)); |
| 10859 | if (err) { |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10860 | return posix_error(); |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10861 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10862 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10863 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10864 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10865 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10866 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10867 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10868 | |
| 10869 | /*[clinic input] |
| 10870 | os.strerror |
| 10871 | |
| 10872 | code: int |
| 10873 | / |
| 10874 | |
| 10875 | Translate an error code to a message string. |
| 10876 | [clinic start generated code]*/ |
| 10877 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10878 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10879 | os_strerror_impl(PyObject *module, int code) |
| 10880 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10881 | { |
| 10882 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10883 | if (message == NULL) { |
| 10884 | PyErr_SetString(PyExc_ValueError, |
| 10885 | "strerror() argument out of range"); |
| 10886 | return NULL; |
| 10887 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 10888 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10889 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10890 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10891 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10892 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10893 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10894 | /*[clinic input] |
| 10895 | os.WCOREDUMP -> bool |
| 10896 | |
| 10897 | status: int |
| 10898 | / |
| 10899 | |
| 10900 | Return True if the process returning status was dumped to a core file. |
| 10901 | [clinic start generated code]*/ |
| 10902 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10903 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10904 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 10905 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10906 | { |
| 10907 | WAIT_TYPE wait_status; |
| 10908 | WAIT_STATUS_INT(wait_status) = status; |
| 10909 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10910 | } |
| 10911 | #endif /* WCOREDUMP */ |
| 10912 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10913 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10914 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10915 | /*[clinic input] |
| 10916 | os.WIFCONTINUED -> bool |
| 10917 | |
| 10918 | status: int |
| 10919 | |
| 10920 | Return True if a particular process was continued from a job control stop. |
| 10921 | |
| 10922 | Return True if the process returning status was continued from a |
| 10923 | job control stop. |
| 10924 | [clinic start generated code]*/ |
| 10925 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10926 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10927 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 10928 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10929 | { |
| 10930 | WAIT_TYPE wait_status; |
| 10931 | WAIT_STATUS_INT(wait_status) = status; |
| 10932 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10933 | } |
| 10934 | #endif /* WIFCONTINUED */ |
| 10935 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10936 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10937 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10938 | /*[clinic input] |
| 10939 | os.WIFSTOPPED -> bool |
| 10940 | |
| 10941 | status: int |
| 10942 | |
| 10943 | Return True if the process returning status was stopped. |
| 10944 | [clinic start generated code]*/ |
| 10945 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10946 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10947 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 10948 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10949 | { |
| 10950 | WAIT_TYPE wait_status; |
| 10951 | WAIT_STATUS_INT(wait_status) = status; |
| 10952 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10953 | } |
| 10954 | #endif /* WIFSTOPPED */ |
| 10955 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10956 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10957 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10958 | /*[clinic input] |
| 10959 | os.WIFSIGNALED -> bool |
| 10960 | |
| 10961 | status: int |
| 10962 | |
| 10963 | Return True if the process returning status was terminated by a signal. |
| 10964 | [clinic start generated code]*/ |
| 10965 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10966 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10967 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 10968 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10969 | { |
| 10970 | WAIT_TYPE wait_status; |
| 10971 | WAIT_STATUS_INT(wait_status) = status; |
| 10972 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10973 | } |
| 10974 | #endif /* WIFSIGNALED */ |
| 10975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10976 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10977 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10978 | /*[clinic input] |
| 10979 | os.WIFEXITED -> bool |
| 10980 | |
| 10981 | status: int |
| 10982 | |
| 10983 | Return True if the process returning status exited via the exit() system call. |
| 10984 | [clinic start generated code]*/ |
| 10985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10986 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10987 | os_WIFEXITED_impl(PyObject *module, int status) |
| 10988 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10989 | { |
| 10990 | WAIT_TYPE wait_status; |
| 10991 | WAIT_STATUS_INT(wait_status) = status; |
| 10992 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10993 | } |
| 10994 | #endif /* WIFEXITED */ |
| 10995 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10996 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10997 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10998 | /*[clinic input] |
| 10999 | os.WEXITSTATUS -> int |
| 11000 | |
| 11001 | status: int |
| 11002 | |
| 11003 | Return the process return code from status. |
| 11004 | [clinic start generated code]*/ |
| 11005 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11006 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11007 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 11008 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11009 | { |
| 11010 | WAIT_TYPE wait_status; |
| 11011 | WAIT_STATUS_INT(wait_status) = status; |
| 11012 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11013 | } |
| 11014 | #endif /* WEXITSTATUS */ |
| 11015 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11016 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11017 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11018 | /*[clinic input] |
| 11019 | os.WTERMSIG -> int |
| 11020 | |
| 11021 | status: int |
| 11022 | |
| 11023 | Return the signal that terminated the process that provided the status value. |
| 11024 | [clinic start generated code]*/ |
| 11025 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11026 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11027 | os_WTERMSIG_impl(PyObject *module, int status) |
| 11028 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11029 | { |
| 11030 | WAIT_TYPE wait_status; |
| 11031 | WAIT_STATUS_INT(wait_status) = status; |
| 11032 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11033 | } |
| 11034 | #endif /* WTERMSIG */ |
| 11035 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11036 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11037 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11038 | /*[clinic input] |
| 11039 | os.WSTOPSIG -> int |
| 11040 | |
| 11041 | status: int |
| 11042 | |
| 11043 | Return the signal that stopped the process that provided the status value. |
| 11044 | [clinic start generated code]*/ |
| 11045 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11046 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11047 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 11048 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11049 | { |
| 11050 | WAIT_TYPE wait_status; |
| 11051 | WAIT_STATUS_INT(wait_status) = status; |
| 11052 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11053 | } |
| 11054 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 11055 | #endif /* HAVE_SYS_WAIT_H */ |
| 11056 | |
| 11057 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11058 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 11059 | #ifdef _SCO_DS |
| 11060 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 11061 | needed definitions in sys/statvfs.h */ |
| 11062 | #define _SVID3 |
| 11063 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11064 | #include <sys/statvfs.h> |
| 11065 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11066 | static PyObject* |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 11067 | _pystatvfs_fromstructstatvfs(PyObject *module, struct statvfs st) { |
| 11068 | PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 11069 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11070 | if (v == NULL) |
| 11071 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11072 | |
| 11073 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11074 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 11075 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 11076 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 11077 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 11078 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 11079 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 11080 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 11081 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 11082 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 11083 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11084 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11085 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 11086 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 11087 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11088 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11089 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11090 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11091 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11092 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11093 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11094 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11095 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11096 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11097 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 11098 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11099 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 11100 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11101 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 11102 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 11103 | * (issue #32390). */ |
| 11104 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 11105 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 11106 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 11107 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 11108 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 11109 | if (PyErr_Occurred()) { |
| 11110 | Py_DECREF(v); |
| 11111 | return NULL; |
| 11112 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11113 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11114 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11115 | } |
| 11116 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11117 | |
| 11118 | /*[clinic input] |
| 11119 | os.fstatvfs |
| 11120 | fd: int |
| 11121 | / |
| 11122 | |
| 11123 | Perform an fstatvfs system call on the given fd. |
| 11124 | |
| 11125 | Equivalent to statvfs(fd). |
| 11126 | [clinic start generated code]*/ |
| 11127 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11128 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11129 | os_fstatvfs_impl(PyObject *module, int fd) |
| 11130 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11131 | { |
| 11132 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11133 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11134 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11135 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11136 | do { |
| 11137 | Py_BEGIN_ALLOW_THREADS |
| 11138 | result = fstatvfs(fd, &st); |
| 11139 | Py_END_ALLOW_THREADS |
| 11140 | } while (result != 0 && errno == EINTR && |
| 11141 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11142 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11143 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 11144 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 11145 | return _pystatvfs_fromstructstatvfs(module, st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11146 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11147 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11148 | |
| 11149 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11150 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11151 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11152 | /*[clinic input] |
| 11153 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11154 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11155 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 11156 | |
| 11157 | Perform a statvfs system call on the given path. |
| 11158 | |
| 11159 | path may always be specified as a string. |
| 11160 | On some platforms, path may also be specified as an open file descriptor. |
| 11161 | If this functionality is unavailable, using it raises an exception. |
| 11162 | [clinic start generated code]*/ |
| 11163 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11164 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11165 | os_statvfs_impl(PyObject *module, path_t *path) |
| 11166 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11167 | { |
| 11168 | int result; |
| 11169 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11170 | |
| 11171 | Py_BEGIN_ALLOW_THREADS |
| 11172 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11173 | if (path->fd != -1) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11174 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11175 | } |
| 11176 | else |
| 11177 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11178 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11179 | Py_END_ALLOW_THREADS |
| 11180 | |
| 11181 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11182 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11183 | } |
| 11184 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 11185 | return _pystatvfs_fromstructstatvfs(module, st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11186 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11187 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 11188 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11189 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11190 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11191 | /*[clinic input] |
| 11192 | os._getdiskusage |
| 11193 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 11194 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11195 | |
| 11196 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 11197 | [clinic start generated code]*/ |
| 11198 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11199 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 11200 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 11201 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11202 | { |
| 11203 | BOOL retval; |
| 11204 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 11205 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11206 | |
| 11207 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 11208 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11209 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 11210 | if (retval == 0) { |
| 11211 | if (GetLastError() == ERROR_DIRECTORY) { |
| 11212 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11213 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 11214 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 11215 | if (dir_path == NULL) { |
| 11216 | return PyErr_NoMemory(); |
| 11217 | } |
| 11218 | |
| 11219 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 11220 | |
| 11221 | if (_dirnameW(dir_path) != -1) { |
| 11222 | Py_BEGIN_ALLOW_THREADS |
| 11223 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 11224 | Py_END_ALLOW_THREADS |
| 11225 | } |
| 11226 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 11227 | err = GetLastError(); |
| 11228 | PyMem_Free(dir_path); |
| 11229 | if (retval) { |
| 11230 | goto success; |
| 11231 | } |
| 11232 | } |
| 11233 | return PyErr_SetFromWindowsErr(err); |
| 11234 | } |
| 11235 | |
| 11236 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11237 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 11238 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11239 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 11240 | |
| 11241 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11242 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 11243 | * It maps strings representing configuration variable names to |
| 11244 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 11245 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11246 | * rarely-used constants. There are three separate tables that use |
| 11247 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11248 | * |
| 11249 | * This code is always included, even if none of the interfaces that |
| 11250 | * need it are included. The #if hackery needed to avoid it would be |
| 11251 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11252 | */ |
| 11253 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11254 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 11255 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11256 | }; |
| 11257 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11258 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11259 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 11260 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11261 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 11262 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 11263 | int value = _PyLong_AsInt(arg); |
| 11264 | if (value == -1 && PyErr_Occurred()) |
| 11265 | return 0; |
| 11266 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11267 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11268 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 11269 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11270 | /* look up the value in the table using a binary search */ |
| 11271 | size_t lo = 0; |
| 11272 | size_t mid; |
| 11273 | size_t hi = tablesize; |
| 11274 | int cmp; |
| 11275 | const char *confname; |
| 11276 | if (!PyUnicode_Check(arg)) { |
| 11277 | PyErr_SetString(PyExc_TypeError, |
| 11278 | "configuration names must be strings or integers"); |
| 11279 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11280 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 11281 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11282 | if (confname == NULL) |
| 11283 | return 0; |
| 11284 | while (lo < hi) { |
| 11285 | mid = (lo + hi) / 2; |
| 11286 | cmp = strcmp(confname, table[mid].name); |
| 11287 | if (cmp < 0) |
| 11288 | hi = mid; |
| 11289 | else if (cmp > 0) |
| 11290 | lo = mid + 1; |
| 11291 | else { |
| 11292 | *valuep = table[mid].value; |
| 11293 | return 1; |
| 11294 | } |
| 11295 | } |
| 11296 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 11297 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11298 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 11299 | } |
| 11300 | |
| 11301 | |
| 11302 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 11303 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11304 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11305 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11306 | #endif |
| 11307 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11308 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11309 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11310 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11311 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11312 | #endif |
| 11313 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11314 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11315 | #endif |
| 11316 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11317 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11318 | #endif |
| 11319 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11320 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11321 | #endif |
| 11322 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11323 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11324 | #endif |
| 11325 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11326 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11327 | #endif |
| 11328 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11329 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11330 | #endif |
| 11331 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11332 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11333 | #endif |
| 11334 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11335 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11336 | #endif |
| 11337 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11338 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11339 | #endif |
| 11340 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11341 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11342 | #endif |
| 11343 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11344 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11345 | #endif |
| 11346 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11347 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11348 | #endif |
| 11349 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11350 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11351 | #endif |
| 11352 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11353 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11354 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 11355 | #ifdef _PC_ACL_ENABLED |
| 11356 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 11357 | #endif |
| 11358 | #ifdef _PC_MIN_HOLE_SIZE |
| 11359 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 11360 | #endif |
| 11361 | #ifdef _PC_ALLOC_SIZE_MIN |
| 11362 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 11363 | #endif |
| 11364 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 11365 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 11366 | #endif |
| 11367 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 11368 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 11369 | #endif |
| 11370 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 11371 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 11372 | #endif |
| 11373 | #ifdef _PC_REC_XFER_ALIGN |
| 11374 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 11375 | #endif |
| 11376 | #ifdef _PC_SYMLINK_MAX |
| 11377 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 11378 | #endif |
| 11379 | #ifdef _PC_XATTR_ENABLED |
| 11380 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 11381 | #endif |
| 11382 | #ifdef _PC_XATTR_EXISTS |
| 11383 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 11384 | #endif |
| 11385 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 11386 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 11387 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11388 | }; |
| 11389 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11390 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11391 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11392 | { |
| 11393 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 11394 | sizeof(posix_constants_pathconf) |
| 11395 | / sizeof(struct constdef)); |
| 11396 | } |
| 11397 | #endif |
| 11398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11399 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11400 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11401 | /*[clinic input] |
| 11402 | os.fpathconf -> long |
| 11403 | |
Gregory P. Smith | 3ccb96c | 2020-06-20 15:06:48 -0700 | [diff] [blame] | 11404 | fd: fildes |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11405 | name: path_confname |
| 11406 | / |
| 11407 | |
| 11408 | Return the configuration limit name for the file descriptor fd. |
| 11409 | |
| 11410 | If there is no limit, return -1. |
| 11411 | [clinic start generated code]*/ |
| 11412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11413 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11414 | os_fpathconf_impl(PyObject *module, int fd, int name) |
Gregory P. Smith | 3ccb96c | 2020-06-20 15:06:48 -0700 | [diff] [blame] | 11415 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5b8d2471cfaae186]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11416 | { |
| 11417 | long limit; |
| 11418 | |
| 11419 | errno = 0; |
| 11420 | limit = fpathconf(fd, name); |
| 11421 | if (limit == -1 && errno != 0) |
| 11422 | posix_error(); |
| 11423 | |
| 11424 | return limit; |
| 11425 | } |
| 11426 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11427 | |
| 11428 | |
| 11429 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11430 | /*[clinic input] |
| 11431 | os.pathconf -> long |
| 11432 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 11433 | name: path_confname |
| 11434 | |
| 11435 | Return the configuration limit name for the file or directory path. |
| 11436 | |
| 11437 | If there is no limit, return -1. |
| 11438 | On some platforms, path may also be specified as an open file descriptor. |
| 11439 | If this functionality is unavailable, using it raises an exception. |
| 11440 | [clinic start generated code]*/ |
| 11441 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11442 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11443 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 11444 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11445 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11446 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11447 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11448 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11449 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11450 | if (path->fd != -1) |
| 11451 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 11452 | else |
| 11453 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11454 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11455 | if (limit == -1 && errno != 0) { |
| 11456 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 11457 | /* could be a path or name problem */ |
| 11458 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11459 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11460 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11461 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11462 | |
| 11463 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11464 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11465 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11466 | |
| 11467 | #ifdef HAVE_CONFSTR |
| 11468 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11469 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11470 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11471 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11472 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11473 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11474 | #endif |
| 11475 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11476 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 11477 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11478 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11479 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11480 | #endif |
| 11481 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11482 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11483 | #endif |
| 11484 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11485 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11486 | #endif |
| 11487 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11488 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11489 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11490 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11491 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11492 | #endif |
| 11493 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11494 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11495 | #endif |
| 11496 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11497 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11498 | #endif |
| 11499 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11500 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11501 | #endif |
| 11502 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11503 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11504 | #endif |
| 11505 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11506 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11507 | #endif |
| 11508 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11509 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11510 | #endif |
| 11511 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11512 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11513 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11514 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11515 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11516 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11517 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11518 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11519 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11520 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11521 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11522 | #endif |
| 11523 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11524 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11525 | #endif |
| 11526 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11527 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11528 | #endif |
| 11529 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11530 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11531 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11532 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11533 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11534 | #endif |
| 11535 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11536 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11537 | #endif |
| 11538 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11539 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11540 | #endif |
| 11541 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11542 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11543 | #endif |
| 11544 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11545 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11546 | #endif |
| 11547 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11548 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11549 | #endif |
| 11550 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11551 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11552 | #endif |
| 11553 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11554 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11555 | #endif |
| 11556 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11557 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11558 | #endif |
| 11559 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11560 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11561 | #endif |
| 11562 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11563 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11564 | #endif |
| 11565 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11566 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11567 | #endif |
| 11568 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11569 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11570 | #endif |
| 11571 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11572 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11573 | #endif |
| 11574 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11575 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11576 | #endif |
| 11577 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11578 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11579 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11580 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11581 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11582 | #endif |
| 11583 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11584 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11585 | #endif |
| 11586 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11587 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11588 | #endif |
| 11589 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11590 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11591 | #endif |
| 11592 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11593 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11594 | #endif |
| 11595 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11596 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11597 | #endif |
| 11598 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11599 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11600 | #endif |
| 11601 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11602 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11603 | #endif |
| 11604 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11605 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11606 | #endif |
| 11607 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11608 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11609 | #endif |
| 11610 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11611 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11612 | #endif |
| 11613 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11614 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11615 | #endif |
| 11616 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11617 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11618 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11619 | }; |
| 11620 | |
| 11621 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11622 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11623 | { |
| 11624 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 11625 | sizeof(posix_constants_confstr) |
| 11626 | / sizeof(struct constdef)); |
| 11627 | } |
| 11628 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11629 | |
| 11630 | /*[clinic input] |
| 11631 | os.confstr |
| 11632 | |
| 11633 | name: confstr_confname |
| 11634 | / |
| 11635 | |
| 11636 | Return a string-valued system configuration variable. |
| 11637 | [clinic start generated code]*/ |
| 11638 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11639 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11640 | os_confstr_impl(PyObject *module, int name) |
| 11641 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11642 | { |
| 11643 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11644 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11645 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11646 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11647 | errno = 0; |
| 11648 | len = confstr(name, buffer, sizeof(buffer)); |
| 11649 | if (len == 0) { |
| 11650 | if (errno) { |
| 11651 | posix_error(); |
| 11652 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11653 | } |
| 11654 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11655 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11656 | } |
| 11657 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11658 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11659 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11660 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11661 | char *buf = PyMem_Malloc(len); |
| 11662 | if (buf == NULL) |
| 11663 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11664 | len2 = confstr(name, buf, len); |
| 11665 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 11666 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11667 | PyMem_Free(buf); |
| 11668 | } |
| 11669 | else |
| 11670 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11671 | return result; |
| 11672 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11673 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11674 | |
| 11675 | |
| 11676 | #ifdef HAVE_SYSCONF |
| 11677 | static struct constdef posix_constants_sysconf[] = { |
| 11678 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11679 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11680 | #endif |
| 11681 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11682 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11683 | #endif |
| 11684 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11685 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11686 | #endif |
| 11687 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11688 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11689 | #endif |
| 11690 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11691 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11692 | #endif |
| 11693 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11694 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11695 | #endif |
| 11696 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11697 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11698 | #endif |
| 11699 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11700 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11701 | #endif |
| 11702 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11703 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11704 | #endif |
| 11705 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11706 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11707 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11708 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11709 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11710 | #endif |
| 11711 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11712 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11713 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11714 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11715 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11716 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11717 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11718 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11719 | #endif |
| 11720 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11721 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11722 | #endif |
| 11723 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11724 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11725 | #endif |
| 11726 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11727 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11728 | #endif |
| 11729 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11730 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11731 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11732 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11733 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11734 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11735 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11736 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11737 | #endif |
| 11738 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11739 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11740 | #endif |
| 11741 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11742 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11743 | #endif |
| 11744 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11745 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11746 | #endif |
| 11747 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11748 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11749 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11750 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11751 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11752 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11753 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11754 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11755 | #endif |
| 11756 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11757 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11758 | #endif |
| 11759 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11760 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11761 | #endif |
| 11762 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11763 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11764 | #endif |
| 11765 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11766 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11767 | #endif |
| 11768 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11769 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11770 | #endif |
| 11771 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11772 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11773 | #endif |
| 11774 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11775 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11776 | #endif |
| 11777 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11778 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11779 | #endif |
| 11780 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11781 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11782 | #endif |
| 11783 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11784 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11785 | #endif |
| 11786 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11787 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11788 | #endif |
| 11789 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11790 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11791 | #endif |
| 11792 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11793 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11794 | #endif |
| 11795 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11796 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11797 | #endif |
| 11798 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11799 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11800 | #endif |
| 11801 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11802 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11803 | #endif |
| 11804 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11805 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11806 | #endif |
| 11807 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11808 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11809 | #endif |
| 11810 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11811 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11812 | #endif |
| 11813 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11814 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11815 | #endif |
| 11816 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11817 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11818 | #endif |
| 11819 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11820 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11821 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11822 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11823 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11824 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11825 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11826 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11827 | #endif |
| 11828 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11829 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11830 | #endif |
| 11831 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11832 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11833 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11834 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11835 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11836 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11837 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11838 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11839 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11840 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11841 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11842 | #endif |
| 11843 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11844 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11845 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11846 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11847 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11848 | #endif |
| 11849 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11850 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11851 | #endif |
| 11852 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11853 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11854 | #endif |
| 11855 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11856 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11857 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11858 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11859 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11860 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11861 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11862 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11863 | #endif |
| 11864 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11865 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11866 | #endif |
| 11867 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11868 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11869 | #endif |
| 11870 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11871 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11872 | #endif |
| 11873 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11874 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11875 | #endif |
| 11876 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11877 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11878 | #endif |
| 11879 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11880 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11881 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11882 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11883 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11884 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11885 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11886 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11887 | #endif |
| 11888 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11889 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11890 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11891 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11892 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11893 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11894 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11895 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11896 | #endif |
| 11897 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11898 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11899 | #endif |
| 11900 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11901 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11902 | #endif |
| 11903 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11904 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11905 | #endif |
| 11906 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11907 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11908 | #endif |
| 11909 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11910 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11911 | #endif |
| 11912 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11913 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11914 | #endif |
| 11915 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11916 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11917 | #endif |
| 11918 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11919 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11920 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11921 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11922 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11923 | #endif |
| 11924 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11925 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11926 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11927 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11928 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11929 | #endif |
| 11930 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11931 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11932 | #endif |
| 11933 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11934 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11935 | #endif |
| 11936 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11937 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11938 | #endif |
Batuhan Taşkaya | 909f4a3 | 2020-04-05 03:40:49 +0300 | [diff] [blame] | 11939 | #ifdef _SC_AIX_REALMEM |
| 11940 | {"SC_AIX_REALMEM", _SC_AIX_REALMEM}, |
| 11941 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11942 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11943 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11944 | #endif |
| 11945 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11946 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11947 | #endif |
| 11948 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11949 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11950 | #endif |
| 11951 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11952 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11953 | #endif |
| 11954 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11955 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11956 | #endif |
| 11957 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11958 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11959 | #endif |
| 11960 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11961 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11962 | #endif |
| 11963 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11964 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11965 | #endif |
| 11966 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11967 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11968 | #endif |
| 11969 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11970 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11971 | #endif |
| 11972 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11973 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11974 | #endif |
| 11975 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11976 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11977 | #endif |
| 11978 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11979 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11980 | #endif |
| 11981 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11982 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11983 | #endif |
| 11984 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11985 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11986 | #endif |
| 11987 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11988 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11989 | #endif |
| 11990 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11991 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11992 | #endif |
| 11993 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11994 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11995 | #endif |
| 11996 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11997 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11998 | #endif |
| 11999 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12000 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12001 | #endif |
| 12002 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12003 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12004 | #endif |
| 12005 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12006 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12007 | #endif |
| 12008 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12009 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12010 | #endif |
| 12011 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12012 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12013 | #endif |
| 12014 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12015 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12016 | #endif |
| 12017 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12018 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12019 | #endif |
| 12020 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12021 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12022 | #endif |
| 12023 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12024 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12025 | #endif |
| 12026 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12027 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12028 | #endif |
| 12029 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12030 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12031 | #endif |
| 12032 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12033 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12034 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12035 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12036 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12037 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12038 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12039 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12040 | #endif |
| 12041 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12042 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12043 | #endif |
| 12044 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12045 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12046 | #endif |
| 12047 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12048 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12049 | #endif |
| 12050 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12051 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12052 | #endif |
| 12053 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12054 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12055 | #endif |
| 12056 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12057 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12058 | #endif |
| 12059 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12060 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12061 | #endif |
| 12062 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12063 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12064 | #endif |
| 12065 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12066 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12067 | #endif |
| 12068 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12069 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12070 | #endif |
| 12071 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12072 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12073 | #endif |
| 12074 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12075 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12076 | #endif |
| 12077 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12078 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12079 | #endif |
| 12080 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12081 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12082 | #endif |
| 12083 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12084 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12085 | #endif |
| 12086 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12087 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12088 | #endif |
| 12089 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12090 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12091 | #endif |
| 12092 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12093 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12094 | #endif |
| 12095 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12096 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12097 | #endif |
| 12098 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12099 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12100 | #endif |
| 12101 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12102 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12103 | #endif |
| 12104 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12105 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12106 | #endif |
| 12107 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12108 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12109 | #endif |
| 12110 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12111 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12112 | #endif |
| 12113 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12114 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12115 | #endif |
| 12116 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12117 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12118 | #endif |
| 12119 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12120 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12121 | #endif |
| 12122 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12123 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12124 | #endif |
| 12125 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12126 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12127 | #endif |
| 12128 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12129 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12130 | #endif |
| 12131 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12132 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12133 | #endif |
| 12134 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12135 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12136 | #endif |
| 12137 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12138 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12139 | #endif |
| 12140 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12141 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12142 | #endif |
| 12143 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12144 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12145 | #endif |
| 12146 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12147 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12148 | #endif |
| 12149 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12150 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12151 | #endif |
| 12152 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12153 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12154 | #endif |
| 12155 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12156 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12157 | #endif |
| 12158 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12159 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12160 | #endif |
| 12161 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12162 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12163 | #endif |
| 12164 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12165 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12166 | #endif |
| 12167 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12168 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12169 | #endif |
| 12170 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12171 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12172 | #endif |
| 12173 | }; |
| 12174 | |
| 12175 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 12176 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12177 | { |
| 12178 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 12179 | sizeof(posix_constants_sysconf) |
| 12180 | / sizeof(struct constdef)); |
| 12181 | } |
| 12182 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12183 | |
| 12184 | /*[clinic input] |
| 12185 | os.sysconf -> long |
| 12186 | name: sysconf_confname |
| 12187 | / |
| 12188 | |
| 12189 | Return an integer-valued system configuration variable. |
| 12190 | [clinic start generated code]*/ |
| 12191 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12192 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12193 | os_sysconf_impl(PyObject *module, int name) |
| 12194 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12195 | { |
| 12196 | long value; |
| 12197 | |
| 12198 | errno = 0; |
| 12199 | value = sysconf(name); |
| 12200 | if (value == -1 && errno != 0) |
| 12201 | posix_error(); |
| 12202 | return value; |
| 12203 | } |
| 12204 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 12205 | |
| 12206 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12207 | /* 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] | 12208 | * 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] | 12209 | * the exported dictionaries that are used to publish information about the |
| 12210 | * names available on the host platform. |
| 12211 | * |
| 12212 | * Sorting the table at runtime ensures that the table is properly ordered |
| 12213 | * when used, even for platforms we're not able to test on. It also makes |
| 12214 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12215 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12216 | |
| 12217 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 12218 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12219 | { |
| 12220 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12221 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12222 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12223 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12224 | |
| 12225 | return strcmp(c1->name, c2->name); |
| 12226 | } |
| 12227 | |
| 12228 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 12229 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12230 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12231 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12232 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 12233 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12234 | |
| 12235 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 12236 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 12237 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12238 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12239 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 12240 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12241 | PyObject *o = PyLong_FromLong(table[i].value); |
| 12242 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 12243 | Py_XDECREF(o); |
| 12244 | Py_DECREF(d); |
| 12245 | return -1; |
| 12246 | } |
| 12247 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12248 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 12249 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12250 | } |
| 12251 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12252 | /* Return -1 on failure, 0 on success. */ |
| 12253 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 12254 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12255 | { |
| 12256 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12257 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12258 | sizeof(posix_constants_pathconf) |
| 12259 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 12260 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 12261 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12262 | #endif |
| 12263 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12264 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12265 | sizeof(posix_constants_confstr) |
| 12266 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 12267 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 12268 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12269 | #endif |
| 12270 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12271 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12272 | sizeof(posix_constants_sysconf) |
| 12273 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 12274 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 12275 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12276 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12277 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12278 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 12279 | |
| 12280 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12281 | /*[clinic input] |
| 12282 | os.abort |
| 12283 | |
| 12284 | Abort the interpreter immediately. |
| 12285 | |
| 12286 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 12287 | on the hosting operating system. This function never returns. |
| 12288 | [clinic start generated code]*/ |
| 12289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12290 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12291 | os_abort_impl(PyObject *module) |
| 12292 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12293 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12294 | abort(); |
| 12295 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 12296 | #ifndef __clang__ |
| 12297 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 12298 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 12299 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12300 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 12301 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 12302 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12303 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 12304 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 12305 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 12306 | /* Grab ShellExecute dynamically from shell32 */ |
| 12307 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 12308 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 12309 | LPCWSTR, INT); |
| 12310 | static int |
| 12311 | check_ShellExecute() |
| 12312 | { |
| 12313 | HINSTANCE hShell32; |
| 12314 | |
| 12315 | /* only recheck */ |
| 12316 | if (-1 == has_ShellExecute) { |
| 12317 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 12318 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 12319 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 12320 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 12321 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 12322 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 12323 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 12324 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 12325 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12326 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 12327 | } else { |
| 12328 | has_ShellExecute = 0; |
| 12329 | } |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 12330 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 12331 | } |
| 12332 | return has_ShellExecute; |
| 12333 | } |
| 12334 | |
| 12335 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12336 | /*[clinic input] |
| 12337 | os.startfile |
| 12338 | filepath: path_t |
| 12339 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 12340 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12341 | Start a file with its associated application. |
| 12342 | |
| 12343 | When "operation" is not specified or "open", this acts like |
| 12344 | double-clicking the file in Explorer, or giving the file name as an |
| 12345 | argument to the DOS "start" command: the file is opened with whatever |
| 12346 | application (if any) its extension is associated. |
| 12347 | When another "operation" is given, it specifies what should be done with |
| 12348 | the file. A typical operation is "print". |
| 12349 | |
| 12350 | startfile returns as soon as the associated application is launched. |
| 12351 | There is no option to wait for the application to close, and no way |
| 12352 | to retrieve the application's exit status. |
| 12353 | |
| 12354 | The filepath is relative to the current directory. If you want to use |
| 12355 | an absolute path, make sure the first character is not a slash ("/"); |
| 12356 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 12357 | [clinic start generated code]*/ |
| 12358 | |
| 12359 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 12360 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 12361 | const Py_UNICODE *operation) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 12362 | /*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12363 | { |
| 12364 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 12365 | |
| 12366 | if(!check_ShellExecute()) { |
| 12367 | /* If the OS doesn't have ShellExecute, return a |
| 12368 | NotImplementedError. */ |
| 12369 | return PyErr_Format(PyExc_NotImplementedError, |
| 12370 | "startfile not available on this platform"); |
| 12371 | } |
| 12372 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12373 | if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 12374 | return NULL; |
| 12375 | } |
| 12376 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12377 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12378 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 12379 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12380 | Py_END_ALLOW_THREADS |
| 12381 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12382 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12383 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 12384 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12385 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12386 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 12387 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12388 | #endif /* MS_WINDOWS */ |
| 12389 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 12390 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12391 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12392 | /*[clinic input] |
| 12393 | os.getloadavg |
| 12394 | |
| 12395 | Return average recent system load information. |
| 12396 | |
| 12397 | Return the number of processes in the system run queue averaged over |
| 12398 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 12399 | Raises OSError if the load average was unobtainable. |
| 12400 | [clinic start generated code]*/ |
| 12401 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12402 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12403 | os_getloadavg_impl(PyObject *module) |
| 12404 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12405 | { |
| 12406 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12407 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 12408 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 12409 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12410 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 12411 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12412 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12413 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 12414 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12415 | |
| 12416 | /*[clinic input] |
| 12417 | os.device_encoding |
| 12418 | fd: int |
| 12419 | |
| 12420 | Return a string describing the encoding of a terminal's file descriptor. |
| 12421 | |
| 12422 | The file descriptor must be attached to a terminal. |
| 12423 | If the device is not a terminal, return None. |
| 12424 | [clinic start generated code]*/ |
| 12425 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12426 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12427 | os_device_encoding_impl(PyObject *module, int fd) |
| 12428 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12429 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 12430 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 12431 | } |
| 12432 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12433 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12434 | #ifdef HAVE_SETRESUID |
| 12435 | /*[clinic input] |
| 12436 | os.setresuid |
| 12437 | |
| 12438 | ruid: uid_t |
| 12439 | euid: uid_t |
| 12440 | suid: uid_t |
| 12441 | / |
| 12442 | |
| 12443 | Set the current process's real, effective, and saved user ids. |
| 12444 | [clinic start generated code]*/ |
| 12445 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12446 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12447 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 12448 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12449 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12450 | if (setresuid(ruid, euid, suid) < 0) |
| 12451 | return posix_error(); |
| 12452 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12453 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12454 | #endif /* HAVE_SETRESUID */ |
| 12455 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12456 | |
| 12457 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12458 | /*[clinic input] |
| 12459 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12461 | rgid: gid_t |
| 12462 | egid: gid_t |
| 12463 | sgid: gid_t |
| 12464 | / |
| 12465 | |
| 12466 | Set the current process's real, effective, and saved group ids. |
| 12467 | [clinic start generated code]*/ |
| 12468 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12469 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12470 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 12471 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12472 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12473 | if (setresgid(rgid, egid, sgid) < 0) |
| 12474 | return posix_error(); |
| 12475 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12476 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12477 | #endif /* HAVE_SETRESGID */ |
| 12478 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12479 | |
| 12480 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12481 | /*[clinic input] |
| 12482 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12483 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12484 | Return a tuple of the current process's real, effective, and saved user ids. |
| 12485 | [clinic start generated code]*/ |
| 12486 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12487 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12488 | os_getresuid_impl(PyObject *module) |
| 12489 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12490 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12491 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12492 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 12493 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 12494 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 12495 | _PyLong_FromUid(euid), |
| 12496 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12497 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12498 | #endif /* HAVE_GETRESUID */ |
| 12499 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12500 | |
| 12501 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12502 | /*[clinic input] |
| 12503 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12504 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12505 | Return a tuple of the current process's real, effective, and saved group ids. |
| 12506 | [clinic start generated code]*/ |
| 12507 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12508 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12509 | os_getresgid_impl(PyObject *module) |
| 12510 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12511 | { |
| 12512 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12513 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 12514 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 12515 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 12516 | _PyLong_FromGid(egid), |
| 12517 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12518 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12519 | #endif /* HAVE_GETRESGID */ |
| 12520 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12521 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12522 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12523 | /*[clinic input] |
| 12524 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12525 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12526 | path: path_t(allow_fd=True) |
| 12527 | attribute: path_t |
| 12528 | * |
| 12529 | follow_symlinks: bool = True |
| 12530 | |
| 12531 | Return the value of extended attribute attribute on path. |
| 12532 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12533 | 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] | 12534 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12535 | link, getxattr will examine the symbolic link itself instead of the file |
| 12536 | the link points to. |
| 12537 | |
| 12538 | [clinic start generated code]*/ |
| 12539 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12540 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12541 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12542 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12543 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12544 | { |
| 12545 | Py_ssize_t i; |
| 12546 | PyObject *buffer = NULL; |
| 12547 | |
| 12548 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 12549 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12550 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12551 | if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) { |
| 12552 | return NULL; |
| 12553 | } |
| 12554 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12555 | for (i = 0; ; i++) { |
| 12556 | void *ptr; |
| 12557 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12558 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12559 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12560 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12561 | path_error(path); |
| 12562 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12563 | } |
| 12564 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 12565 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12566 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12567 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12568 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12569 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12570 | if (path->fd >= 0) |
| 12571 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12572 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12573 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12574 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12575 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12576 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12577 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12578 | if (result < 0) { |
| 12579 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12580 | if (errno == ERANGE) |
| 12581 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12582 | path_error(path); |
| 12583 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12584 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12585 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12586 | if (result != buffer_size) { |
| 12587 | /* Can only shrink. */ |
| 12588 | _PyBytes_Resize(&buffer, result); |
| 12589 | } |
| 12590 | break; |
| 12591 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12592 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12593 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12594 | } |
| 12595 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12596 | |
| 12597 | /*[clinic input] |
| 12598 | os.setxattr |
| 12599 | |
| 12600 | path: path_t(allow_fd=True) |
| 12601 | attribute: path_t |
| 12602 | value: Py_buffer |
| 12603 | flags: int = 0 |
| 12604 | * |
| 12605 | follow_symlinks: bool = True |
| 12606 | |
| 12607 | Set extended attribute attribute on path to value. |
| 12608 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12609 | 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] | 12610 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12611 | link, setxattr will modify the symbolic link itself instead of the file |
| 12612 | the link points to. |
| 12613 | |
| 12614 | [clinic start generated code]*/ |
| 12615 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12616 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12617 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12618 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12619 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12620 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12621 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12622 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12623 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12624 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12625 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12626 | if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object, |
| 12627 | value->buf, value->len, flags) < 0) { |
| 12628 | return NULL; |
| 12629 | } |
| 12630 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12631 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12632 | if (path->fd > -1) |
| 12633 | result = fsetxattr(path->fd, attribute->narrow, |
| 12634 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12635 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12636 | result = setxattr(path->narrow, attribute->narrow, |
| 12637 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12638 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12639 | result = lsetxattr(path->narrow, attribute->narrow, |
| 12640 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12641 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12642 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12643 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12644 | path_error(path); |
| 12645 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12646 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12647 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12648 | Py_RETURN_NONE; |
| 12649 | } |
| 12650 | |
| 12651 | |
| 12652 | /*[clinic input] |
| 12653 | os.removexattr |
| 12654 | |
| 12655 | path: path_t(allow_fd=True) |
| 12656 | attribute: path_t |
| 12657 | * |
| 12658 | follow_symlinks: bool = True |
| 12659 | |
| 12660 | Remove extended attribute attribute on path. |
| 12661 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12662 | 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] | 12663 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12664 | link, removexattr will modify the symbolic link itself instead of the file |
| 12665 | the link points to. |
| 12666 | |
| 12667 | [clinic start generated code]*/ |
| 12668 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12669 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12670 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12671 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12672 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12673 | { |
| 12674 | ssize_t result; |
| 12675 | |
| 12676 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 12677 | return NULL; |
| 12678 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12679 | if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) { |
| 12680 | return NULL; |
| 12681 | } |
| 12682 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12683 | Py_BEGIN_ALLOW_THREADS; |
| 12684 | if (path->fd > -1) |
| 12685 | result = fremovexattr(path->fd, attribute->narrow); |
| 12686 | else if (follow_symlinks) |
| 12687 | result = removexattr(path->narrow, attribute->narrow); |
| 12688 | else |
| 12689 | result = lremovexattr(path->narrow, attribute->narrow); |
| 12690 | Py_END_ALLOW_THREADS; |
| 12691 | |
| 12692 | if (result) { |
| 12693 | return path_error(path); |
| 12694 | } |
| 12695 | |
| 12696 | Py_RETURN_NONE; |
| 12697 | } |
| 12698 | |
| 12699 | |
| 12700 | /*[clinic input] |
| 12701 | os.listxattr |
| 12702 | |
| 12703 | path: path_t(allow_fd=True, nullable=True) = None |
| 12704 | * |
| 12705 | follow_symlinks: bool = True |
| 12706 | |
| 12707 | Return a list of extended attributes on path. |
| 12708 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12709 | 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] | 12710 | if path is None, listxattr will examine the current directory. |
| 12711 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12712 | link, listxattr will examine the symbolic link itself instead of the file |
| 12713 | the link points to. |
| 12714 | [clinic start generated code]*/ |
| 12715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12716 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12717 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12718 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12719 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12720 | Py_ssize_t i; |
| 12721 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12722 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12723 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12724 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12725 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12726 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12727 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12728 | if (PySys_Audit("os.listxattr", "(O)", |
| 12729 | path->object ? path->object : Py_None) < 0) { |
| 12730 | return NULL; |
| 12731 | } |
| 12732 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12733 | name = path->narrow ? path->narrow : "."; |
| 12734 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12735 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12736 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12737 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12738 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12739 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12740 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 12741 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12742 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12743 | break; |
| 12744 | } |
| 12745 | buffer = PyMem_MALLOC(buffer_size); |
| 12746 | if (!buffer) { |
| 12747 | PyErr_NoMemory(); |
| 12748 | break; |
| 12749 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12750 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12751 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12752 | if (path->fd > -1) |
| 12753 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12754 | else if (follow_symlinks) |
| 12755 | length = listxattr(name, buffer, buffer_size); |
| 12756 | else |
| 12757 | length = llistxattr(name, buffer, buffer_size); |
| 12758 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12759 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12760 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12761 | if (errno == ERANGE) { |
| 12762 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 12763 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12764 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12765 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12766 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12767 | break; |
| 12768 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12769 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12770 | result = PyList_New(0); |
| 12771 | if (!result) { |
| 12772 | goto exit; |
| 12773 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12774 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12775 | end = buffer + length; |
| 12776 | for (trace = start = buffer; trace != end; trace++) { |
| 12777 | if (!*trace) { |
| 12778 | int error; |
| 12779 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 12780 | trace - start); |
| 12781 | if (!attribute) { |
| 12782 | Py_DECREF(result); |
| 12783 | result = NULL; |
| 12784 | goto exit; |
| 12785 | } |
| 12786 | error = PyList_Append(result, attribute); |
| 12787 | Py_DECREF(attribute); |
| 12788 | if (error) { |
| 12789 | Py_DECREF(result); |
| 12790 | result = NULL; |
| 12791 | goto exit; |
| 12792 | } |
| 12793 | start = trace + 1; |
| 12794 | } |
| 12795 | } |
| 12796 | break; |
| 12797 | } |
| 12798 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12799 | if (buffer) |
| 12800 | PyMem_FREE(buffer); |
| 12801 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12802 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12803 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12804 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12805 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12806 | /*[clinic input] |
| 12807 | os.urandom |
| 12808 | |
| 12809 | size: Py_ssize_t |
| 12810 | / |
| 12811 | |
| 12812 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 12813 | [clinic start generated code]*/ |
| 12814 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12815 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12816 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 12817 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12818 | { |
| 12819 | PyObject *bytes; |
| 12820 | int result; |
| 12821 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12822 | if (size < 0) |
| 12823 | return PyErr_Format(PyExc_ValueError, |
| 12824 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12825 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 12826 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12827 | return NULL; |
| 12828 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 12829 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12830 | if (result == -1) { |
| 12831 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12832 | return NULL; |
| 12833 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12834 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12835 | } |
| 12836 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 12837 | #ifdef HAVE_MEMFD_CREATE |
| 12838 | /*[clinic input] |
| 12839 | os.memfd_create |
| 12840 | |
| 12841 | name: FSConverter |
| 12842 | flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC |
| 12843 | |
| 12844 | [clinic start generated code]*/ |
| 12845 | |
| 12846 | static PyObject * |
| 12847 | os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags) |
| 12848 | /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/ |
| 12849 | { |
| 12850 | int fd; |
| 12851 | const char *bytes = PyBytes_AS_STRING(name); |
| 12852 | Py_BEGIN_ALLOW_THREADS |
| 12853 | fd = memfd_create(bytes, flags); |
| 12854 | Py_END_ALLOW_THREADS |
| 12855 | if (fd == -1) { |
| 12856 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12857 | } |
| 12858 | return PyLong_FromLong(fd); |
| 12859 | } |
| 12860 | #endif |
| 12861 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12862 | /* Terminal size querying */ |
| 12863 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12864 | PyDoc_STRVAR(TerminalSize_docstring, |
| 12865 | "A tuple of (columns, lines) for holding terminal window size"); |
| 12866 | |
| 12867 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 12868 | {"columns", "width of the terminal window in characters"}, |
| 12869 | {"lines", "height of the terminal window in characters"}, |
| 12870 | {NULL, NULL} |
| 12871 | }; |
| 12872 | |
| 12873 | static PyStructSequence_Desc TerminalSize_desc = { |
| 12874 | "os.terminal_size", |
| 12875 | TerminalSize_docstring, |
| 12876 | TerminalSize_fields, |
| 12877 | 2, |
| 12878 | }; |
| 12879 | |
| 12880 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12881 | /*[clinic input] |
| 12882 | os.get_terminal_size |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12883 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12884 | fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1 |
| 12885 | / |
| 12886 | |
| 12887 | Return the size of the terminal window as (columns, lines). |
| 12888 | |
| 12889 | The optional argument fd (default standard output) specifies |
| 12890 | which file descriptor should be queried. |
| 12891 | |
| 12892 | If the file descriptor is not connected to a terminal, an OSError |
| 12893 | is thrown. |
| 12894 | |
| 12895 | This function will only be defined if an implementation is |
| 12896 | available for this system. |
| 12897 | |
| 12898 | shutil.get_terminal_size is the high-level function which should |
| 12899 | normally be used, os.get_terminal_size is the low-level implementation. |
| 12900 | [clinic start generated code]*/ |
| 12901 | |
| 12902 | static PyObject * |
| 12903 | os_get_terminal_size_impl(PyObject *module, int fd) |
| 12904 | /*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12905 | { |
| 12906 | int columns, lines; |
| 12907 | PyObject *termsize; |
| 12908 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12909 | /* Under some conditions stdout may not be connected and |
| 12910 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 12911 | * GUI apps don't have valid standard streams by default. |
| 12912 | * |
| 12913 | * If this happens, and the optional fd argument is not present, |
| 12914 | * the ioctl below will fail returning EBADF. This is what we want. |
| 12915 | */ |
| 12916 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12917 | #ifdef TERMSIZE_USE_IOCTL |
| 12918 | { |
| 12919 | struct winsize w; |
| 12920 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 12921 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12922 | columns = w.ws_col; |
| 12923 | lines = w.ws_row; |
| 12924 | } |
| 12925 | #endif /* TERMSIZE_USE_IOCTL */ |
| 12926 | |
| 12927 | #ifdef TERMSIZE_USE_CONIO |
| 12928 | { |
| 12929 | DWORD nhandle; |
| 12930 | HANDLE handle; |
| 12931 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 12932 | switch (fd) { |
| 12933 | case 0: nhandle = STD_INPUT_HANDLE; |
| 12934 | break; |
| 12935 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 12936 | break; |
| 12937 | case 2: nhandle = STD_ERROR_HANDLE; |
| 12938 | break; |
| 12939 | default: |
| 12940 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 12941 | } |
| 12942 | handle = GetStdHandle(nhandle); |
| 12943 | if (handle == NULL) |
| 12944 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 12945 | if (handle == INVALID_HANDLE_VALUE) |
| 12946 | return PyErr_SetFromWindowsErr(0); |
| 12947 | |
| 12948 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 12949 | return PyErr_SetFromWindowsErr(0); |
| 12950 | |
| 12951 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 12952 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 12953 | } |
| 12954 | #endif /* TERMSIZE_USE_CONIO */ |
| 12955 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 12956 | PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12957 | termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12958 | if (termsize == NULL) |
| 12959 | return NULL; |
| 12960 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 12961 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 12962 | if (PyErr_Occurred()) { |
| 12963 | Py_DECREF(termsize); |
| 12964 | return NULL; |
| 12965 | } |
| 12966 | return termsize; |
| 12967 | } |
| 12968 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 12969 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12970 | |
| 12971 | /*[clinic input] |
| 12972 | os.cpu_count |
| 12973 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 12974 | Return the number of CPUs in the system; return None if indeterminable. |
| 12975 | |
| 12976 | This number is not equivalent to the number of CPUs the current process can |
| 12977 | use. The number of usable CPUs can be obtained with |
| 12978 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12979 | [clinic start generated code]*/ |
| 12980 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12981 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12982 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12983 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12984 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12985 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12986 | #ifdef MS_WINDOWS |
Steve Dower | aa92927 | 2019-09-11 16:15:39 +0100 | [diff] [blame] | 12987 | ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12988 | #elif defined(__hpux) |
| 12989 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 12990 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 12991 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
pxinwr | 3405e05 | 2020-08-07 13:21:52 +0800 | [diff] [blame] | 12992 | #elif defined(__VXWORKS__) |
| 12993 | ncpu = _Py_popcount32(vxCpuEnabledGet()); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12994 | #elif defined(__DragonFly__) || \ |
| 12995 | defined(__OpenBSD__) || \ |
| 12996 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12997 | defined(__NetBSD__) || \ |
| 12998 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12999 | int mib[2]; |
| 13000 | size_t len = sizeof(ncpu); |
| 13001 | mib[0] = CTL_HW; |
| 13002 | mib[1] = HW_NCPU; |
| 13003 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 13004 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 13005 | #endif |
| 13006 | if (ncpu >= 1) |
| 13007 | return PyLong_FromLong(ncpu); |
| 13008 | else |
| 13009 | Py_RETURN_NONE; |
| 13010 | } |
| 13011 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13012 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13013 | /*[clinic input] |
| 13014 | os.get_inheritable -> bool |
| 13015 | |
| 13016 | fd: int |
| 13017 | / |
| 13018 | |
| 13019 | Get the close-on-exe flag of the specified file descriptor. |
| 13020 | [clinic start generated code]*/ |
| 13021 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13022 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 13023 | os_get_inheritable_impl(PyObject *module, int fd) |
| 13024 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13025 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 13026 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 13027 | _Py_BEGIN_SUPPRESS_IPH |
| 13028 | return_value = _Py_get_inheritable(fd); |
| 13029 | _Py_END_SUPPRESS_IPH |
| 13030 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13031 | } |
| 13032 | |
| 13033 | |
| 13034 | /*[clinic input] |
| 13035 | os.set_inheritable |
| 13036 | fd: int |
| 13037 | inheritable: int |
| 13038 | / |
| 13039 | |
| 13040 | Set the inheritable flag of the specified file descriptor. |
| 13041 | [clinic start generated code]*/ |
| 13042 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13043 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 13044 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 13045 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13046 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 13047 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13048 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 13049 | _Py_BEGIN_SUPPRESS_IPH |
| 13050 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 13051 | _Py_END_SUPPRESS_IPH |
| 13052 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13053 | return NULL; |
| 13054 | Py_RETURN_NONE; |
| 13055 | } |
| 13056 | |
| 13057 | |
| 13058 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13059 | /*[clinic input] |
| 13060 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 13061 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13062 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13063 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13064 | Get the close-on-exe flag of the specified file descriptor. |
| 13065 | [clinic start generated code]*/ |
| 13066 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13067 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 13068 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 13069 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13070 | { |
| 13071 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13072 | |
| 13073 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 13074 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13075 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13076 | } |
| 13077 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13078 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13079 | } |
| 13080 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13081 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13082 | /*[clinic input] |
| 13083 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 13084 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13085 | inheritable: bool |
| 13086 | / |
| 13087 | |
| 13088 | Set the inheritable flag of the specified handle. |
| 13089 | [clinic start generated code]*/ |
| 13090 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13091 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 13092 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 13093 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 13094 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13095 | { |
| 13096 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 13097 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 13098 | PyErr_SetFromWindowsErr(0); |
| 13099 | return NULL; |
| 13100 | } |
| 13101 | Py_RETURN_NONE; |
| 13102 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13103 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13104 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13105 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13106 | /*[clinic input] |
| 13107 | os.get_blocking -> bool |
| 13108 | fd: int |
| 13109 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13110 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13111 | Get the blocking mode of the file descriptor. |
| 13112 | |
| 13113 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 13114 | [clinic start generated code]*/ |
| 13115 | |
| 13116 | static int |
| 13117 | os_get_blocking_impl(PyObject *module, int fd) |
| 13118 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13119 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13120 | int blocking; |
| 13121 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 13122 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13123 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 13124 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13125 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13126 | } |
| 13127 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13128 | /*[clinic input] |
| 13129 | os.set_blocking |
| 13130 | fd: int |
| 13131 | blocking: bool(accept={int}) |
| 13132 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13133 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13134 | Set the blocking mode of the specified file descriptor. |
| 13135 | |
| 13136 | Set the O_NONBLOCK flag if blocking is False, |
| 13137 | clear the O_NONBLOCK flag otherwise. |
| 13138 | [clinic start generated code]*/ |
| 13139 | |
| 13140 | static PyObject * |
| 13141 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 13142 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13143 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13144 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13145 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 13146 | _Py_BEGIN_SUPPRESS_IPH |
| 13147 | result = _Py_set_blocking(fd, blocking); |
| 13148 | _Py_END_SUPPRESS_IPH |
| 13149 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13150 | return NULL; |
| 13151 | Py_RETURN_NONE; |
| 13152 | } |
| 13153 | #endif /* !MS_WINDOWS */ |
| 13154 | |
| 13155 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13156 | /*[clinic input] |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13157 | class os.DirEntry "DirEntry *" "DirEntryType" |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13158 | [clinic start generated code]*/ |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13159 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13160 | |
| 13161 | typedef struct { |
| 13162 | PyObject_HEAD |
| 13163 | PyObject *name; |
| 13164 | PyObject *path; |
| 13165 | PyObject *stat; |
| 13166 | PyObject *lstat; |
| 13167 | #ifdef MS_WINDOWS |
| 13168 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 13169 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13170 | int got_file_index; |
| 13171 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13172 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13173 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13174 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13175 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13176 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13177 | #endif |
| 13178 | } DirEntry; |
| 13179 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13180 | static PyObject * |
| 13181 | _disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 13182 | { |
| 13183 | PyErr_Format(PyExc_TypeError, |
| 13184 | "cannot create '%.100s' instances", _PyType_Name(type)); |
| 13185 | return NULL; |
| 13186 | } |
| 13187 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13188 | static void |
| 13189 | DirEntry_dealloc(DirEntry *entry) |
| 13190 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13191 | PyTypeObject *tp = Py_TYPE(entry); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13192 | Py_XDECREF(entry->name); |
| 13193 | Py_XDECREF(entry->path); |
| 13194 | Py_XDECREF(entry->stat); |
| 13195 | Py_XDECREF(entry->lstat); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13196 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 13197 | free_func(entry); |
| 13198 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13199 | } |
| 13200 | |
| 13201 | /* Forward reference */ |
| 13202 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13203 | DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self, |
| 13204 | int follow_symlinks, unsigned short mode_bits); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13205 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13206 | /*[clinic input] |
| 13207 | os.DirEntry.is_symlink -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13208 | defining_class: defining_class |
| 13209 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13210 | |
| 13211 | Return True if the entry is a symbolic link; cached per entry. |
| 13212 | [clinic start generated code]*/ |
| 13213 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13214 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13215 | os_DirEntry_is_symlink_impl(DirEntry *self, PyTypeObject *defining_class) |
| 13216 | /*[clinic end generated code: output=293096d589b6d47c input=e9acc5ee4d511113]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13217 | { |
| 13218 | #ifdef MS_WINDOWS |
| 13219 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13220 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 13221 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13222 | if (self->d_type != DT_UNKNOWN) |
| 13223 | return self->d_type == DT_LNK; |
| 13224 | else |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13225 | return DirEntry_test_mode(defining_class, self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13226 | #else |
| 13227 | /* POSIX without d_type */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13228 | return DirEntry_test_mode(defining_class, self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13229 | #endif |
| 13230 | } |
| 13231 | |
| 13232 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13233 | DirEntry_fetch_stat(PyObject *module, DirEntry *self, int follow_symlinks) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13234 | { |
| 13235 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13236 | STRUCT_STAT st; |
| 13237 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13238 | |
| 13239 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13240 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 13241 | return NULL; |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13242 | #if USE_UNICODE_WCHAR_CACHE |
| 13243 | _Py_COMP_DIAG_PUSH |
| 13244 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13245 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13246 | _Py_COMP_DIAG_POP |
| 13247 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 13248 | wchar_t *path = PyUnicode_AsWideCharString(ub, NULL); |
| 13249 | Py_DECREF(ub); |
| 13250 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13251 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13252 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 13253 | return NULL; |
| 13254 | const char *path = PyBytes_AS_STRING(ub); |
| 13255 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 13256 | #ifdef HAVE_FSTATAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 13257 | if (HAVE_FSTATAT_RUNTIME) { |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13258 | result = fstatat(self->dir_fd, path, &st, |
| 13259 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 13260 | } else |
| 13261 | |
| 13262 | #endif /* HAVE_FSTATAT */ |
| 13263 | { |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13264 | Py_DECREF(ub); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13265 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 13266 | return NULL; |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 13267 | } |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13268 | } |
| 13269 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13270 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13271 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13272 | if (follow_symlinks) |
| 13273 | result = STAT(path, &st); |
| 13274 | else |
| 13275 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13276 | } |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13277 | #if defined(MS_WINDOWS) && !USE_UNICODE_WCHAR_CACHE |
| 13278 | PyMem_Free(path); |
| 13279 | #else /* USE_UNICODE_WCHAR_CACHE */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13280 | Py_DECREF(ub); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13281 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13282 | |
| 13283 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13284 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13285 | |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13286 | return _pystat_fromstructstat(module, &st); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13287 | } |
| 13288 | |
| 13289 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13290 | DirEntry_get_lstat(PyTypeObject *defining_class, DirEntry *self) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13291 | { |
| 13292 | if (!self->lstat) { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13293 | PyObject *module = PyType_GetModule(defining_class); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13294 | #ifdef MS_WINDOWS |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13295 | self->lstat = _pystat_fromstructstat(module, &self->win32_lstat); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13296 | #else /* POSIX */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13297 | self->lstat = DirEntry_fetch_stat(module, self, 0); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13298 | #endif |
| 13299 | } |
| 13300 | Py_XINCREF(self->lstat); |
| 13301 | return self->lstat; |
| 13302 | } |
| 13303 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13304 | /*[clinic input] |
| 13305 | os.DirEntry.stat |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13306 | defining_class: defining_class |
| 13307 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13308 | * |
| 13309 | follow_symlinks: bool = True |
| 13310 | |
| 13311 | Return stat_result object for the entry; cached per entry. |
| 13312 | [clinic start generated code]*/ |
| 13313 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13314 | static PyObject * |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13315 | os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class, |
| 13316 | int follow_symlinks) |
| 13317 | /*[clinic end generated code: output=23f803e19c3e780e input=e816273c4e67ee98]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13318 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13319 | if (!follow_symlinks) { |
| 13320 | return DirEntry_get_lstat(defining_class, self); |
| 13321 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13322 | |
| 13323 | if (!self->stat) { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13324 | int result = os_DirEntry_is_symlink_impl(self, defining_class); |
| 13325 | if (result == -1) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13326 | return NULL; |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13327 | } |
| 13328 | if (result) { |
| 13329 | PyObject *module = PyType_GetModule(defining_class); |
| 13330 | self->stat = DirEntry_fetch_stat(module, self, 1); |
| 13331 | } |
| 13332 | else { |
| 13333 | self->stat = DirEntry_get_lstat(defining_class, self); |
| 13334 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13335 | } |
| 13336 | |
| 13337 | Py_XINCREF(self->stat); |
| 13338 | return self->stat; |
| 13339 | } |
| 13340 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13341 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 13342 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13343 | DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self, |
| 13344 | int follow_symlinks, unsigned short mode_bits) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13345 | { |
| 13346 | PyObject *stat = NULL; |
| 13347 | PyObject *st_mode = NULL; |
| 13348 | long mode; |
| 13349 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13350 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13351 | int is_symlink; |
| 13352 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13353 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13354 | #ifdef MS_WINDOWS |
| 13355 | unsigned long dir_bits; |
| 13356 | #endif |
| 13357 | |
| 13358 | #ifdef MS_WINDOWS |
| 13359 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 13360 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13361 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13362 | is_symlink = self->d_type == DT_LNK; |
| 13363 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 13364 | #endif |
| 13365 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13366 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13367 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13368 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13369 | stat = os_DirEntry_stat_impl(self, defining_class, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13370 | if (!stat) { |
| 13371 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 13372 | /* If file doesn't exist (anymore), then return False |
| 13373 | (i.e., say it's not a file/directory) */ |
| 13374 | PyErr_Clear(); |
| 13375 | return 0; |
| 13376 | } |
| 13377 | goto error; |
| 13378 | } |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13379 | _posixstate* state = get_posix_state(PyType_GetModule(defining_class)); |
| 13380 | st_mode = PyObject_GetAttr(stat, state->st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13381 | if (!st_mode) |
| 13382 | goto error; |
| 13383 | |
| 13384 | mode = PyLong_AsLong(st_mode); |
| 13385 | if (mode == -1 && PyErr_Occurred()) |
| 13386 | goto error; |
| 13387 | Py_CLEAR(st_mode); |
| 13388 | Py_CLEAR(stat); |
| 13389 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13390 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13391 | } |
| 13392 | else if (is_symlink) { |
| 13393 | assert(mode_bits != S_IFLNK); |
| 13394 | result = 0; |
| 13395 | } |
| 13396 | else { |
| 13397 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 13398 | #ifdef MS_WINDOWS |
| 13399 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 13400 | if (mode_bits == S_IFDIR) |
| 13401 | result = dir_bits != 0; |
| 13402 | else |
| 13403 | result = dir_bits == 0; |
| 13404 | #else /* POSIX */ |
| 13405 | if (mode_bits == S_IFDIR) |
| 13406 | result = self->d_type == DT_DIR; |
| 13407 | else |
| 13408 | result = self->d_type == DT_REG; |
| 13409 | #endif |
| 13410 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13411 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13412 | |
| 13413 | return result; |
| 13414 | |
| 13415 | error: |
| 13416 | Py_XDECREF(st_mode); |
| 13417 | Py_XDECREF(stat); |
| 13418 | return -1; |
| 13419 | } |
| 13420 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13421 | /*[clinic input] |
| 13422 | os.DirEntry.is_dir -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13423 | defining_class: defining_class |
| 13424 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13425 | * |
| 13426 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13427 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13428 | Return True if the entry is a directory; cached per entry. |
| 13429 | [clinic start generated code]*/ |
| 13430 | |
| 13431 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13432 | os_DirEntry_is_dir_impl(DirEntry *self, PyTypeObject *defining_class, |
| 13433 | int follow_symlinks) |
| 13434 | /*[clinic end generated code: output=0cd453b9c0987fdf input=1a4ffd6dec9920cb]*/ |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13435 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13436 | return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13437 | } |
| 13438 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13439 | /*[clinic input] |
| 13440 | os.DirEntry.is_file -> bool |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13441 | defining_class: defining_class |
| 13442 | / |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13443 | * |
| 13444 | follow_symlinks: bool = True |
| 13445 | |
| 13446 | Return True if the entry is a file; cached per entry. |
| 13447 | [clinic start generated code]*/ |
| 13448 | |
| 13449 | static int |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13450 | os_DirEntry_is_file_impl(DirEntry *self, PyTypeObject *defining_class, |
| 13451 | int follow_symlinks) |
| 13452 | /*[clinic end generated code: output=f7c277ab5ba80908 input=0a64c5a12e802e3b]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13453 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13454 | return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13455 | } |
| 13456 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13457 | /*[clinic input] |
| 13458 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13459 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13460 | Return inode of the entry; cached per entry. |
| 13461 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13462 | |
| 13463 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13464 | os_DirEntry_inode_impl(DirEntry *self) |
| 13465 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13466 | { |
| 13467 | #ifdef MS_WINDOWS |
| 13468 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13469 | PyObject *unicode; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13470 | STRUCT_STAT stat; |
| 13471 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13472 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13473 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13474 | return NULL; |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13475 | #if USE_UNICODE_WCHAR_CACHE |
| 13476 | _Py_COMP_DIAG_PUSH |
| 13477 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
| 13478 | const wchar_t *path = PyUnicode_AsUnicode(unicode); |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13479 | result = LSTAT(path, &stat); |
| 13480 | Py_DECREF(unicode); |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 13481 | _Py_COMP_DIAG_POP |
| 13482 | #else /* USE_UNICODE_WCHAR_CACHE */ |
| 13483 | wchar_t *path = PyUnicode_AsWideCharString(unicode, NULL); |
| 13484 | Py_DECREF(unicode); |
| 13485 | result = LSTAT(path, &stat); |
| 13486 | PyMem_Free(path); |
| 13487 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13488 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 13489 | if (result != 0) |
| 13490 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13491 | |
| 13492 | self->win32_file_index = stat.st_ino; |
| 13493 | self->got_file_index = 1; |
| 13494 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 13495 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 13496 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13497 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 13498 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 13499 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13500 | #endif |
| 13501 | } |
| 13502 | |
| 13503 | static PyObject * |
| 13504 | DirEntry_repr(DirEntry *self) |
| 13505 | { |
| 13506 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 13507 | } |
| 13508 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13509 | /*[clinic input] |
| 13510 | os.DirEntry.__fspath__ |
| 13511 | |
| 13512 | Returns the path for the entry. |
| 13513 | [clinic start generated code]*/ |
| 13514 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 13515 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13516 | os_DirEntry___fspath___impl(DirEntry *self) |
| 13517 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 13518 | { |
| 13519 | Py_INCREF(self->path); |
| 13520 | return self->path; |
| 13521 | } |
| 13522 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13523 | static PyMemberDef DirEntry_members[] = { |
| 13524 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 13525 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 13526 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 13527 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 13528 | {NULL} |
| 13529 | }; |
| 13530 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13531 | #include "clinic/posixmodule.c.h" |
| 13532 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13533 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13534 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 13535 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 13536 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 13537 | OS_DIRENTRY_STAT_METHODDEF |
| 13538 | OS_DIRENTRY_INODE_METHODDEF |
| 13539 | OS_DIRENTRY___FSPATH___METHODDEF |
Batuhan Taşkaya | f9dd51e | 2020-04-08 00:37:19 +0300 | [diff] [blame] | 13540 | {"__class_getitem__", (PyCFunction)Py_GenericAlias, |
| 13541 | METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13542 | {NULL} |
| 13543 | }; |
| 13544 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13545 | static PyType_Slot DirEntryType_slots[] = { |
| 13546 | {Py_tp_new, _disabled_new}, |
| 13547 | {Py_tp_dealloc, DirEntry_dealloc}, |
| 13548 | {Py_tp_repr, DirEntry_repr}, |
| 13549 | {Py_tp_methods, DirEntry_methods}, |
| 13550 | {Py_tp_members, DirEntry_members}, |
| 13551 | {0, 0}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13552 | }; |
| 13553 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13554 | static PyType_Spec DirEntryType_spec = { |
| 13555 | MODNAME ".DirEntry", |
| 13556 | sizeof(DirEntry), |
| 13557 | 0, |
| 13558 | Py_TPFLAGS_DEFAULT, |
| 13559 | DirEntryType_slots |
| 13560 | }; |
| 13561 | |
| 13562 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13563 | #ifdef MS_WINDOWS |
| 13564 | |
| 13565 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 13566 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13567 | { |
| 13568 | Py_ssize_t path_len; |
| 13569 | Py_ssize_t size; |
| 13570 | wchar_t *result; |
| 13571 | wchar_t ch; |
| 13572 | |
| 13573 | if (!path_wide) { /* Default arg: "." */ |
| 13574 | path_wide = L"."; |
| 13575 | path_len = 1; |
| 13576 | } |
| 13577 | else { |
| 13578 | path_len = wcslen(path_wide); |
| 13579 | } |
| 13580 | |
| 13581 | /* The +1's are for the path separator and the NUL */ |
| 13582 | size = path_len + 1 + wcslen(filename) + 1; |
| 13583 | result = PyMem_New(wchar_t, size); |
| 13584 | if (!result) { |
| 13585 | PyErr_NoMemory(); |
| 13586 | return NULL; |
| 13587 | } |
| 13588 | wcscpy(result, path_wide); |
| 13589 | if (path_len > 0) { |
| 13590 | ch = result[path_len - 1]; |
| 13591 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 13592 | result[path_len++] = SEP; |
| 13593 | wcscpy(result + path_len, filename); |
| 13594 | } |
| 13595 | return result; |
| 13596 | } |
| 13597 | |
| 13598 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13599 | 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] | 13600 | { |
| 13601 | DirEntry *entry; |
| 13602 | BY_HANDLE_FILE_INFORMATION file_info; |
| 13603 | ULONG reparse_tag; |
| 13604 | wchar_t *joined_path; |
| 13605 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13606 | PyObject *DirEntryType = get_posix_state(module)->DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13607 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13608 | if (!entry) |
| 13609 | return NULL; |
| 13610 | entry->name = NULL; |
| 13611 | entry->path = NULL; |
| 13612 | entry->stat = NULL; |
| 13613 | entry->lstat = NULL; |
| 13614 | entry->got_file_index = 0; |
| 13615 | |
| 13616 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 13617 | if (!entry->name) |
| 13618 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13619 | if (path->narrow) { |
| 13620 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 13621 | if (!entry->name) |
| 13622 | goto error; |
| 13623 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13624 | |
| 13625 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 13626 | if (!joined_path) |
| 13627 | goto error; |
| 13628 | |
| 13629 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 13630 | PyMem_Free(joined_path); |
| 13631 | if (!entry->path) |
| 13632 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13633 | if (path->narrow) { |
| 13634 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 13635 | if (!entry->path) |
| 13636 | goto error; |
| 13637 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13638 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13639 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13640 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 13641 | |
| 13642 | return (PyObject *)entry; |
| 13643 | |
| 13644 | error: |
| 13645 | Py_DECREF(entry); |
| 13646 | return NULL; |
| 13647 | } |
| 13648 | |
| 13649 | #else /* POSIX */ |
| 13650 | |
| 13651 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 13652 | 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] | 13653 | { |
| 13654 | Py_ssize_t path_len; |
| 13655 | Py_ssize_t size; |
| 13656 | char *result; |
| 13657 | |
| 13658 | if (!path_narrow) { /* Default arg: "." */ |
| 13659 | path_narrow = "."; |
| 13660 | path_len = 1; |
| 13661 | } |
| 13662 | else { |
| 13663 | path_len = strlen(path_narrow); |
| 13664 | } |
| 13665 | |
| 13666 | if (filename_len == -1) |
| 13667 | filename_len = strlen(filename); |
| 13668 | |
| 13669 | /* The +1's are for the path separator and the NUL */ |
| 13670 | size = path_len + 1 + filename_len + 1; |
| 13671 | result = PyMem_New(char, size); |
| 13672 | if (!result) { |
| 13673 | PyErr_NoMemory(); |
| 13674 | return NULL; |
| 13675 | } |
| 13676 | strcpy(result, path_narrow); |
| 13677 | if (path_len > 0 && result[path_len - 1] != '/') |
| 13678 | result[path_len++] = '/'; |
| 13679 | strcpy(result + path_len, filename); |
| 13680 | return result; |
| 13681 | } |
| 13682 | |
| 13683 | static PyObject * |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13684 | DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name, |
| 13685 | Py_ssize_t name_len, ino_t d_ino |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13686 | #ifdef HAVE_DIRENT_D_TYPE |
| 13687 | , unsigned char d_type |
| 13688 | #endif |
| 13689 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13690 | { |
| 13691 | DirEntry *entry; |
| 13692 | char *joined_path; |
| 13693 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13694 | PyObject *DirEntryType = get_posix_state(module)->DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13695 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13696 | if (!entry) |
| 13697 | return NULL; |
| 13698 | entry->name = NULL; |
| 13699 | entry->path = NULL; |
| 13700 | entry->stat = NULL; |
| 13701 | entry->lstat = NULL; |
| 13702 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13703 | if (path->fd != -1) { |
| 13704 | entry->dir_fd = path->fd; |
| 13705 | joined_path = NULL; |
| 13706 | } |
| 13707 | else { |
| 13708 | entry->dir_fd = DEFAULT_DIR_FD; |
| 13709 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 13710 | if (!joined_path) |
| 13711 | goto error; |
| 13712 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13713 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 13714 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13715 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13716 | if (joined_path) |
| 13717 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13718 | } |
| 13719 | else { |
| 13720 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13721 | if (joined_path) |
| 13722 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13723 | } |
| 13724 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13725 | if (!entry->name) |
| 13726 | goto error; |
| 13727 | |
| 13728 | if (path->fd != -1) { |
| 13729 | entry->path = entry->name; |
| 13730 | Py_INCREF(entry->path); |
| 13731 | } |
| 13732 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13733 | goto error; |
| 13734 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13735 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13736 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13737 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13738 | entry->d_ino = d_ino; |
| 13739 | |
| 13740 | return (PyObject *)entry; |
| 13741 | |
| 13742 | error: |
| 13743 | Py_XDECREF(entry); |
| 13744 | return NULL; |
| 13745 | } |
| 13746 | |
| 13747 | #endif |
| 13748 | |
| 13749 | |
| 13750 | typedef struct { |
| 13751 | PyObject_HEAD |
| 13752 | path_t path; |
| 13753 | #ifdef MS_WINDOWS |
| 13754 | HANDLE handle; |
| 13755 | WIN32_FIND_DATAW file_data; |
| 13756 | int first_time; |
| 13757 | #else /* POSIX */ |
| 13758 | DIR *dirp; |
| 13759 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13760 | #ifdef HAVE_FDOPENDIR |
| 13761 | int fd; |
| 13762 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13763 | } ScandirIterator; |
| 13764 | |
| 13765 | #ifdef MS_WINDOWS |
| 13766 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13767 | static int |
| 13768 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13769 | { |
| 13770 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 13771 | } |
| 13772 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13773 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13774 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13775 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13776 | HANDLE handle = iterator->handle; |
| 13777 | |
| 13778 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13779 | return; |
| 13780 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13781 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13782 | Py_BEGIN_ALLOW_THREADS |
| 13783 | FindClose(handle); |
| 13784 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13785 | } |
| 13786 | |
| 13787 | static PyObject * |
| 13788 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13789 | { |
| 13790 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 13791 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13792 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13793 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13794 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13795 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13796 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13797 | |
| 13798 | while (1) { |
| 13799 | if (!iterator->first_time) { |
| 13800 | Py_BEGIN_ALLOW_THREADS |
| 13801 | success = FindNextFileW(iterator->handle, file_data); |
| 13802 | Py_END_ALLOW_THREADS |
| 13803 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13804 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13805 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13806 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13807 | break; |
| 13808 | } |
| 13809 | } |
| 13810 | iterator->first_time = 0; |
| 13811 | |
| 13812 | /* Skip over . and .. */ |
| 13813 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13814 | wcscmp(file_data->cFileName, L"..") != 0) |
| 13815 | { |
| 13816 | PyObject *module = PyType_GetModule(Py_TYPE(iterator)); |
| 13817 | entry = DirEntry_from_find_data(module, &iterator->path, file_data); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13818 | if (!entry) |
| 13819 | break; |
| 13820 | return entry; |
| 13821 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13822 | |
| 13823 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13824 | } |
| 13825 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13826 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13827 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13828 | return NULL; |
| 13829 | } |
| 13830 | |
| 13831 | #else /* POSIX */ |
| 13832 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13833 | static int |
| 13834 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13835 | { |
| 13836 | return !iterator->dirp; |
| 13837 | } |
| 13838 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13839 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13840 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13841 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13842 | DIR *dirp = iterator->dirp; |
| 13843 | |
| 13844 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13845 | return; |
| 13846 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13847 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13848 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13849 | #ifdef HAVE_FDOPENDIR |
| 13850 | if (iterator->path.fd != -1) |
| 13851 | rewinddir(dirp); |
| 13852 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13853 | closedir(dirp); |
| 13854 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13855 | return; |
| 13856 | } |
| 13857 | |
| 13858 | static PyObject * |
| 13859 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13860 | { |
| 13861 | struct dirent *direntp; |
| 13862 | Py_ssize_t name_len; |
| 13863 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13864 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13865 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13866 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13867 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13868 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13869 | |
| 13870 | while (1) { |
| 13871 | errno = 0; |
| 13872 | Py_BEGIN_ALLOW_THREADS |
| 13873 | direntp = readdir(iterator->dirp); |
| 13874 | Py_END_ALLOW_THREADS |
| 13875 | |
| 13876 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13877 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13878 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13879 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13880 | break; |
| 13881 | } |
| 13882 | |
| 13883 | /* Skip over . and .. */ |
| 13884 | name_len = NAMLEN(direntp); |
| 13885 | is_dot = direntp->d_name[0] == '.' && |
| 13886 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 13887 | if (!is_dot) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13888 | PyObject *module = PyType_GetModule(Py_TYPE(iterator)); |
| 13889 | entry = DirEntry_from_posix_info(module, |
| 13890 | &iterator->path, direntp->d_name, |
| 13891 | name_len, direntp->d_ino |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13892 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 13893 | , direntp->d_type |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13894 | #endif |
| 13895 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13896 | if (!entry) |
| 13897 | break; |
| 13898 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13899 | } |
| 13900 | |
| 13901 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13902 | } |
| 13903 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13904 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13905 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13906 | return NULL; |
| 13907 | } |
| 13908 | |
| 13909 | #endif |
| 13910 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13911 | static PyObject * |
| 13912 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 13913 | { |
| 13914 | ScandirIterator_closedir(self); |
| 13915 | Py_RETURN_NONE; |
| 13916 | } |
| 13917 | |
| 13918 | static PyObject * |
| 13919 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 13920 | { |
| 13921 | Py_INCREF(self); |
| 13922 | return self; |
| 13923 | } |
| 13924 | |
| 13925 | static PyObject * |
| 13926 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 13927 | { |
| 13928 | ScandirIterator_closedir(self); |
| 13929 | Py_RETURN_NONE; |
| 13930 | } |
| 13931 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13932 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13933 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 13934 | { |
| 13935 | PyObject *error_type, *error_value, *error_traceback; |
| 13936 | |
| 13937 | /* Save the current exception, if any. */ |
| 13938 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 13939 | |
| 13940 | if (!ScandirIterator_is_closed(iterator)) { |
| 13941 | ScandirIterator_closedir(iterator); |
| 13942 | |
| 13943 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 13944 | "unclosed scandir iterator %R", iterator)) { |
| 13945 | /* Spurious errors can appear at shutdown */ |
| 13946 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 13947 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 13948 | } |
| 13949 | } |
| 13950 | } |
| 13951 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13952 | path_cleanup(&iterator->path); |
| 13953 | |
| 13954 | /* Restore the saved exception. */ |
| 13955 | PyErr_Restore(error_type, error_value, error_traceback); |
| 13956 | } |
| 13957 | |
| 13958 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13959 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 13960 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13961 | PyTypeObject *tp = Py_TYPE(iterator); |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13962 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 13963 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13964 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13965 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 13966 | free_func(iterator); |
| 13967 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13968 | } |
| 13969 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13970 | static PyMethodDef ScandirIterator_methods[] = { |
| 13971 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 13972 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 13973 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 13974 | {NULL} |
| 13975 | }; |
| 13976 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13977 | static PyType_Slot ScandirIteratorType_slots[] = { |
| 13978 | {Py_tp_new, _disabled_new}, |
| 13979 | {Py_tp_dealloc, ScandirIterator_dealloc}, |
| 13980 | {Py_tp_finalize, ScandirIterator_finalize}, |
| 13981 | {Py_tp_iter, PyObject_SelfIter}, |
| 13982 | {Py_tp_iternext, ScandirIterator_iternext}, |
| 13983 | {Py_tp_methods, ScandirIterator_methods}, |
| 13984 | {0, 0}, |
| 13985 | }; |
| 13986 | |
| 13987 | static PyType_Spec ScandirIteratorType_spec = { |
| 13988 | MODNAME ".ScandirIterator", |
| 13989 | sizeof(ScandirIterator), |
| 13990 | 0, |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 13991 | // bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since |
| 13992 | // PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance. |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13993 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 13994 | ScandirIteratorType_slots |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13995 | }; |
| 13996 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13997 | /*[clinic input] |
| 13998 | os.scandir |
| 13999 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 14000 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 14001 | |
| 14002 | Return an iterator of DirEntry objects for given path. |
| 14003 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 14004 | 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] | 14005 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 14006 | all other circumstances they will be str. |
| 14007 | |
| 14008 | If path is None, uses the path='.'. |
| 14009 | [clinic start generated code]*/ |
| 14010 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14011 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 14012 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 14013 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14014 | { |
| 14015 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14016 | #ifdef MS_WINDOWS |
| 14017 | wchar_t *path_strW; |
| 14018 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 14019 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 14020 | #ifdef HAVE_FDOPENDIR |
| 14021 | int fd = -1; |
| 14022 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14023 | #endif |
| 14024 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 14025 | if (PySys_Audit("os.scandir", "O", |
| 14026 | path->object ? path->object : Py_None) < 0) { |
| 14027 | return NULL; |
| 14028 | } |
| 14029 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14030 | PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14031 | iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14032 | if (!iterator) |
| 14033 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14034 | |
| 14035 | #ifdef MS_WINDOWS |
| 14036 | iterator->handle = INVALID_HANDLE_VALUE; |
| 14037 | #else |
| 14038 | iterator->dirp = NULL; |
| 14039 | #endif |
| 14040 | |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 14041 | /* Move the ownership to iterator->path */ |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 14042 | memcpy(&iterator->path, path, sizeof(path_t)); |
| 14043 | memset(path, 0, sizeof(path_t)); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14044 | |
| 14045 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14046 | iterator->first_time = 1; |
| 14047 | |
| 14048 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 14049 | if (!path_strW) |
| 14050 | goto error; |
| 14051 | |
| 14052 | Py_BEGIN_ALLOW_THREADS |
| 14053 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 14054 | Py_END_ALLOW_THREADS |
| 14055 | |
| 14056 | PyMem_Free(path_strW); |
| 14057 | |
| 14058 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 14059 | path_error(&iterator->path); |
| 14060 | goto error; |
| 14061 | } |
| 14062 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14063 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 14064 | #ifdef HAVE_FDOPENDIR |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 14065 | if (iterator->path.fd != -1) { |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 14066 | if (HAVE_FDOPENDIR_RUNTIME) { |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 14067 | /* closedir() closes the FD, so we duplicate it */ |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 14068 | fd = _Py_dup(iterator->path.fd); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 14069 | if (fd == -1) |
| 14070 | goto error; |
| 14071 | |
| 14072 | Py_BEGIN_ALLOW_THREADS |
| 14073 | iterator->dirp = fdopendir(fd); |
| 14074 | Py_END_ALLOW_THREADS |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 14075 | } else { |
| 14076 | PyErr_SetString(PyExc_TypeError, |
| 14077 | "scandir: path should be string, bytes, os.PathLike or None, not int"); |
| 14078 | return NULL; |
| 14079 | } |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 14080 | } |
| 14081 | else |
| 14082 | #endif |
| 14083 | { |
| 14084 | if (iterator->path.narrow) |
| 14085 | path_str = iterator->path.narrow; |
| 14086 | else |
| 14087 | path_str = "."; |
| 14088 | |
| 14089 | Py_BEGIN_ALLOW_THREADS |
| 14090 | iterator->dirp = opendir(path_str); |
| 14091 | Py_END_ALLOW_THREADS |
| 14092 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14093 | |
| 14094 | if (!iterator->dirp) { |
| 14095 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 14096 | #ifdef HAVE_FDOPENDIR |
| 14097 | if (fd != -1) { |
| 14098 | Py_BEGIN_ALLOW_THREADS |
| 14099 | close(fd); |
| 14100 | Py_END_ALLOW_THREADS |
| 14101 | } |
| 14102 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14103 | goto error; |
| 14104 | } |
| 14105 | #endif |
| 14106 | |
| 14107 | return (PyObject *)iterator; |
| 14108 | |
| 14109 | error: |
| 14110 | Py_DECREF(iterator); |
| 14111 | return NULL; |
| 14112 | } |
| 14113 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14114 | /* |
| 14115 | Return the file system path representation of the object. |
| 14116 | |
| 14117 | If the object is str or bytes, then allow it to pass through with |
| 14118 | an incremented refcount. If the object defines __fspath__(), then |
| 14119 | return the result of that method. All other types raise a TypeError. |
| 14120 | */ |
| 14121 | PyObject * |
| 14122 | PyOS_FSPath(PyObject *path) |
| 14123 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 14124 | /* For error message reasons, this function is manually inlined in |
| 14125 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14126 | PyObject *func = NULL; |
| 14127 | PyObject *path_repr = NULL; |
| 14128 | |
| 14129 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 14130 | Py_INCREF(path); |
| 14131 | return path; |
| 14132 | } |
| 14133 | |
| 14134 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 14135 | if (NULL == func) { |
| 14136 | return PyErr_Format(PyExc_TypeError, |
| 14137 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 14138 | "not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14139 | _PyType_Name(Py_TYPE(path))); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14140 | } |
| 14141 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 14142 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14143 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 14144 | if (NULL == path_repr) { |
| 14145 | return NULL; |
| 14146 | } |
| 14147 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 14148 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 14149 | PyErr_Format(PyExc_TypeError, |
| 14150 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14151 | "not %.200s", _PyType_Name(Py_TYPE(path)), |
| 14152 | _PyType_Name(Py_TYPE(path_repr))); |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 14153 | Py_DECREF(path_repr); |
| 14154 | return NULL; |
| 14155 | } |
| 14156 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14157 | return path_repr; |
| 14158 | } |
| 14159 | |
| 14160 | /*[clinic input] |
| 14161 | os.fspath |
| 14162 | |
| 14163 | path: object |
| 14164 | |
| 14165 | Return the file system path representation of the object. |
| 14166 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 14167 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 14168 | object defines __fspath__(), then return the result of that method. All other |
| 14169 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14170 | [clinic start generated code]*/ |
| 14171 | |
| 14172 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 14173 | os_fspath_impl(PyObject *module, PyObject *path) |
| 14174 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14175 | { |
| 14176 | return PyOS_FSPath(path); |
| 14177 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14178 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14179 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 14180 | /*[clinic input] |
| 14181 | os.getrandom |
| 14182 | |
| 14183 | size: Py_ssize_t |
| 14184 | flags: int=0 |
| 14185 | |
| 14186 | Obtain a series of random bytes. |
| 14187 | [clinic start generated code]*/ |
| 14188 | |
| 14189 | static PyObject * |
| 14190 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 14191 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 14192 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14193 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 14194 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14195 | |
| 14196 | if (size < 0) { |
| 14197 | errno = EINVAL; |
| 14198 | return posix_error(); |
| 14199 | } |
| 14200 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 14201 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 14202 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14203 | PyErr_NoMemory(); |
| 14204 | return NULL; |
| 14205 | } |
| 14206 | |
| 14207 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 14208 | n = syscall(SYS_getrandom, |
| 14209 | PyBytes_AS_STRING(bytes), |
| 14210 | PyBytes_GET_SIZE(bytes), |
| 14211 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14212 | if (n < 0 && errno == EINTR) { |
| 14213 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 14214 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14215 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 14216 | |
| 14217 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14218 | continue; |
| 14219 | } |
| 14220 | break; |
| 14221 | } |
| 14222 | |
| 14223 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14224 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 14225 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14226 | } |
| 14227 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 14228 | if (n != size) { |
| 14229 | _PyBytes_Resize(&bytes, n); |
| 14230 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14231 | |
| 14232 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 14233 | |
| 14234 | error: |
| 14235 | Py_DECREF(bytes); |
| 14236 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14237 | } |
| 14238 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 14239 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14240 | #ifdef MS_WINDOWS |
| 14241 | /* bpo-36085: Helper functions for managing DLL search directories |
| 14242 | * on win32 |
| 14243 | */ |
| 14244 | |
| 14245 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); |
| 14246 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); |
| 14247 | |
| 14248 | /*[clinic input] |
| 14249 | os._add_dll_directory |
| 14250 | |
| 14251 | path: path_t |
| 14252 | |
| 14253 | Add a path to the DLL search path. |
| 14254 | |
| 14255 | This search path is used when resolving dependencies for imported |
| 14256 | extension modules (the module itself is resolved through sys.path), |
| 14257 | and also by ctypes. |
| 14258 | |
| 14259 | Returns an opaque value that may be passed to os.remove_dll_directory |
| 14260 | to remove this directory from the search path. |
| 14261 | [clinic start generated code]*/ |
| 14262 | |
| 14263 | static PyObject * |
| 14264 | os__add_dll_directory_impl(PyObject *module, path_t *path) |
| 14265 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ |
| 14266 | { |
| 14267 | HMODULE hKernel32; |
| 14268 | PAddDllDirectory AddDllDirectory; |
| 14269 | DLL_DIRECTORY_COOKIE cookie = 0; |
| 14270 | DWORD err = 0; |
| 14271 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 14272 | if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) { |
| 14273 | return NULL; |
| 14274 | } |
| 14275 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14276 | /* For Windows 7, we have to load this. As this will be a fairly |
| 14277 | infrequent operation, just do it each time. Kernel32 is always |
| 14278 | loaded. */ |
| 14279 | Py_BEGIN_ALLOW_THREADS |
| 14280 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 14281 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( |
| 14282 | hKernel32, "AddDllDirectory")) || |
| 14283 | !(cookie = (*AddDllDirectory)(path->wide))) { |
| 14284 | err = GetLastError(); |
| 14285 | } |
| 14286 | Py_END_ALLOW_THREADS |
| 14287 | |
| 14288 | if (err) { |
| 14289 | return win32_error_object_err("add_dll_directory", |
| 14290 | path->object, err); |
| 14291 | } |
| 14292 | |
| 14293 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); |
| 14294 | } |
| 14295 | |
| 14296 | /*[clinic input] |
| 14297 | os._remove_dll_directory |
| 14298 | |
| 14299 | cookie: object |
| 14300 | |
| 14301 | Removes a path from the DLL search path. |
| 14302 | |
| 14303 | The parameter is an opaque value that was returned from |
| 14304 | os.add_dll_directory. You can only remove directories that you added |
| 14305 | yourself. |
| 14306 | [clinic start generated code]*/ |
| 14307 | |
| 14308 | static PyObject * |
| 14309 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) |
| 14310 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ |
| 14311 | { |
| 14312 | HMODULE hKernel32; |
| 14313 | PRemoveDllDirectory RemoveDllDirectory; |
| 14314 | DLL_DIRECTORY_COOKIE cookieValue; |
| 14315 | DWORD err = 0; |
| 14316 | |
| 14317 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { |
| 14318 | PyErr_SetString(PyExc_TypeError, |
| 14319 | "Provided cookie was not returned from os.add_dll_directory"); |
| 14320 | return NULL; |
| 14321 | } |
| 14322 | |
| 14323 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( |
| 14324 | cookie, "DLL directory cookie"); |
| 14325 | |
| 14326 | /* For Windows 7, we have to load this. As this will be a fairly |
| 14327 | infrequent operation, just do it each time. Kernel32 is always |
| 14328 | loaded. */ |
| 14329 | Py_BEGIN_ALLOW_THREADS |
| 14330 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 14331 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( |
| 14332 | hKernel32, "RemoveDllDirectory")) || |
| 14333 | !(*RemoveDllDirectory)(cookieValue)) { |
| 14334 | err = GetLastError(); |
| 14335 | } |
| 14336 | Py_END_ALLOW_THREADS |
| 14337 | |
| 14338 | if (err) { |
| 14339 | return win32_error_object_err("remove_dll_directory", |
| 14340 | NULL, err); |
| 14341 | } |
| 14342 | |
| 14343 | if (PyCapsule_SetName(cookie, NULL)) { |
| 14344 | return NULL; |
| 14345 | } |
| 14346 | |
| 14347 | Py_RETURN_NONE; |
| 14348 | } |
| 14349 | |
| 14350 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 14351 | |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14352 | |
| 14353 | /* Only check if WIFEXITED is available: expect that it comes |
| 14354 | with WEXITSTATUS, WIFSIGNALED, etc. |
| 14355 | |
| 14356 | os.waitstatus_to_exitcode() is implemented in C and not in Python, so |
| 14357 | subprocess can safely call it during late Python finalization without |
| 14358 | risking that used os attributes were set to None by _PyImport_Cleanup(). */ |
| 14359 | #if defined(WIFEXITED) || defined(MS_WINDOWS) |
| 14360 | /*[clinic input] |
| 14361 | os.waitstatus_to_exitcode |
| 14362 | |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 14363 | status as status_obj: object |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14364 | |
| 14365 | Convert a wait status to an exit code. |
| 14366 | |
| 14367 | On Unix: |
| 14368 | |
| 14369 | * If WIFEXITED(status) is true, return WEXITSTATUS(status). |
| 14370 | * If WIFSIGNALED(status) is true, return -WTERMSIG(status). |
| 14371 | * Otherwise, raise a ValueError. |
| 14372 | |
| 14373 | On Windows, return status shifted right by 8 bits. |
| 14374 | |
| 14375 | On Unix, if the process is being traced or if waitpid() was called with |
| 14376 | WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true. |
| 14377 | This function must not be called if WIFSTOPPED(status) is true. |
| 14378 | [clinic start generated code]*/ |
| 14379 | |
| 14380 | static PyObject * |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 14381 | os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj) |
| 14382 | /*[clinic end generated code: output=db50b1b0ba3c7153 input=7fe2d7fdaea3db42]*/ |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14383 | { |
| 14384 | #ifndef MS_WINDOWS |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 14385 | int status = _PyLong_AsInt(status_obj); |
| 14386 | if (status == -1 && PyErr_Occurred()) { |
| 14387 | return NULL; |
| 14388 | } |
| 14389 | |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14390 | WAIT_TYPE wait_status; |
| 14391 | WAIT_STATUS_INT(wait_status) = status; |
| 14392 | int exitcode; |
| 14393 | if (WIFEXITED(wait_status)) { |
| 14394 | exitcode = WEXITSTATUS(wait_status); |
| 14395 | /* Sanity check to provide warranty on the function behavior. |
| 14396 | It should not occur in practice */ |
| 14397 | if (exitcode < 0) { |
| 14398 | PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode); |
| 14399 | return NULL; |
| 14400 | } |
| 14401 | } |
| 14402 | else if (WIFSIGNALED(wait_status)) { |
| 14403 | int signum = WTERMSIG(wait_status); |
| 14404 | /* Sanity check to provide warranty on the function behavior. |
| 14405 | It should not occurs in practice */ |
| 14406 | if (signum <= 0) { |
| 14407 | PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum); |
| 14408 | return NULL; |
| 14409 | } |
| 14410 | exitcode = -signum; |
| 14411 | } else if (WIFSTOPPED(wait_status)) { |
| 14412 | /* Status only received if the process is being traced |
| 14413 | or if waitpid() was called with WUNTRACED option. */ |
| 14414 | int signum = WSTOPSIG(wait_status); |
| 14415 | PyErr_Format(PyExc_ValueError, |
| 14416 | "process stopped by delivery of signal %i", |
| 14417 | signum); |
| 14418 | return NULL; |
| 14419 | } |
| 14420 | else { |
| 14421 | PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status); |
| 14422 | return NULL; |
| 14423 | } |
| 14424 | return PyLong_FromLong(exitcode); |
| 14425 | #else |
| 14426 | /* Windows implementation: see os.waitpid() implementation |
| 14427 | which uses _cwait(). */ |
Victor Stinner | 9bee32b | 2020-04-22 16:30:35 +0200 | [diff] [blame] | 14428 | unsigned long long status = PyLong_AsUnsignedLongLong(status_obj); |
| 14429 | if (status == (unsigned long long)-1 && PyErr_Occurred()) { |
| 14430 | return NULL; |
| 14431 | } |
| 14432 | |
| 14433 | unsigned long long exitcode = (status >> 8); |
| 14434 | /* ExitProcess() accepts an UINT type: |
| 14435 | reject exit code which doesn't fit in an UINT */ |
| 14436 | if (exitcode > UINT_MAX) { |
| 14437 | PyErr_Format(PyExc_ValueError, "invalid exit code: %llu", exitcode); |
| 14438 | return NULL; |
| 14439 | } |
| 14440 | return PyLong_FromUnsignedLong((unsigned long)exitcode); |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14441 | #endif |
| 14442 | } |
| 14443 | #endif |
| 14444 | |
| 14445 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14446 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 14447 | |
| 14448 | OS_STAT_METHODDEF |
| 14449 | OS_ACCESS_METHODDEF |
| 14450 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14451 | OS_CHDIR_METHODDEF |
| 14452 | OS_CHFLAGS_METHODDEF |
| 14453 | OS_CHMOD_METHODDEF |
| 14454 | OS_FCHMOD_METHODDEF |
| 14455 | OS_LCHMOD_METHODDEF |
| 14456 | OS_CHOWN_METHODDEF |
| 14457 | OS_FCHOWN_METHODDEF |
| 14458 | OS_LCHOWN_METHODDEF |
| 14459 | OS_LCHFLAGS_METHODDEF |
| 14460 | OS_CHROOT_METHODDEF |
| 14461 | OS_CTERMID_METHODDEF |
| 14462 | OS_GETCWD_METHODDEF |
| 14463 | OS_GETCWDB_METHODDEF |
| 14464 | OS_LINK_METHODDEF |
| 14465 | OS_LISTDIR_METHODDEF |
| 14466 | OS_LSTAT_METHODDEF |
| 14467 | OS_MKDIR_METHODDEF |
| 14468 | OS_NICE_METHODDEF |
| 14469 | OS_GETPRIORITY_METHODDEF |
| 14470 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14471 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 14472 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 14473 | OS_READLINK_METHODDEF |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 14474 | OS_COPY_FILE_RANGE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14475 | OS_RENAME_METHODDEF |
| 14476 | OS_REPLACE_METHODDEF |
| 14477 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14478 | OS_SYMLINK_METHODDEF |
| 14479 | OS_SYSTEM_METHODDEF |
| 14480 | OS_UMASK_METHODDEF |
| 14481 | OS_UNAME_METHODDEF |
| 14482 | OS_UNLINK_METHODDEF |
| 14483 | OS_REMOVE_METHODDEF |
| 14484 | OS_UTIME_METHODDEF |
| 14485 | OS_TIMES_METHODDEF |
| 14486 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14487 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14488 | OS_EXECV_METHODDEF |
| 14489 | OS_EXECVE_METHODDEF |
| 14490 | OS_SPAWNV_METHODDEF |
| 14491 | OS_SPAWNVE_METHODDEF |
| 14492 | OS_FORK1_METHODDEF |
| 14493 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 14494 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14495 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 14496 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 14497 | OS_SCHED_GETPARAM_METHODDEF |
| 14498 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 14499 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 14500 | OS_SCHED_SETPARAM_METHODDEF |
| 14501 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 14502 | OS_SCHED_YIELD_METHODDEF |
| 14503 | OS_SCHED_SETAFFINITY_METHODDEF |
| 14504 | OS_SCHED_GETAFFINITY_METHODDEF |
| 14505 | OS_OPENPTY_METHODDEF |
| 14506 | OS_FORKPTY_METHODDEF |
| 14507 | OS_GETEGID_METHODDEF |
| 14508 | OS_GETEUID_METHODDEF |
| 14509 | OS_GETGID_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14510 | OS_GETGROUPLIST_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14511 | OS_GETGROUPS_METHODDEF |
| 14512 | OS_GETPID_METHODDEF |
| 14513 | OS_GETPGRP_METHODDEF |
| 14514 | OS_GETPPID_METHODDEF |
| 14515 | OS_GETUID_METHODDEF |
| 14516 | OS_GETLOGIN_METHODDEF |
| 14517 | OS_KILL_METHODDEF |
| 14518 | OS_KILLPG_METHODDEF |
| 14519 | OS_PLOCK_METHODDEF |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 14520 | OS_STARTFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14521 | OS_SETUID_METHODDEF |
| 14522 | OS_SETEUID_METHODDEF |
| 14523 | OS_SETREUID_METHODDEF |
| 14524 | OS_SETGID_METHODDEF |
| 14525 | OS_SETEGID_METHODDEF |
| 14526 | OS_SETREGID_METHODDEF |
| 14527 | OS_SETGROUPS_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14528 | OS_INITGROUPS_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14529 | OS_GETPGID_METHODDEF |
| 14530 | OS_SETPGRP_METHODDEF |
| 14531 | OS_WAIT_METHODDEF |
| 14532 | OS_WAIT3_METHODDEF |
| 14533 | OS_WAIT4_METHODDEF |
| 14534 | OS_WAITID_METHODDEF |
| 14535 | OS_WAITPID_METHODDEF |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 14536 | OS_PIDFD_OPEN_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14537 | OS_GETSID_METHODDEF |
| 14538 | OS_SETSID_METHODDEF |
| 14539 | OS_SETPGID_METHODDEF |
| 14540 | OS_TCGETPGRP_METHODDEF |
| 14541 | OS_TCSETPGRP_METHODDEF |
| 14542 | OS_OPEN_METHODDEF |
| 14543 | OS_CLOSE_METHODDEF |
| 14544 | OS_CLOSERANGE_METHODDEF |
| 14545 | OS_DEVICE_ENCODING_METHODDEF |
| 14546 | OS_DUP_METHODDEF |
| 14547 | OS_DUP2_METHODDEF |
| 14548 | OS_LOCKF_METHODDEF |
| 14549 | OS_LSEEK_METHODDEF |
| 14550 | OS_READ_METHODDEF |
| 14551 | OS_READV_METHODDEF |
| 14552 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14553 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14554 | OS_WRITE_METHODDEF |
| 14555 | OS_WRITEV_METHODDEF |
| 14556 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14557 | OS_PWRITEV_METHODDEF |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14558 | OS_SENDFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14559 | OS_FSTAT_METHODDEF |
| 14560 | OS_ISATTY_METHODDEF |
| 14561 | OS_PIPE_METHODDEF |
| 14562 | OS_PIPE2_METHODDEF |
| 14563 | OS_MKFIFO_METHODDEF |
| 14564 | OS_MKNOD_METHODDEF |
| 14565 | OS_MAJOR_METHODDEF |
| 14566 | OS_MINOR_METHODDEF |
| 14567 | OS_MAKEDEV_METHODDEF |
| 14568 | OS_FTRUNCATE_METHODDEF |
| 14569 | OS_TRUNCATE_METHODDEF |
| 14570 | OS_POSIX_FALLOCATE_METHODDEF |
| 14571 | OS_POSIX_FADVISE_METHODDEF |
| 14572 | OS_PUTENV_METHODDEF |
| 14573 | OS_UNSETENV_METHODDEF |
| 14574 | OS_STRERROR_METHODDEF |
| 14575 | OS_FCHDIR_METHODDEF |
| 14576 | OS_FSYNC_METHODDEF |
| 14577 | OS_SYNC_METHODDEF |
| 14578 | OS_FDATASYNC_METHODDEF |
| 14579 | OS_WCOREDUMP_METHODDEF |
| 14580 | OS_WIFCONTINUED_METHODDEF |
| 14581 | OS_WIFSTOPPED_METHODDEF |
| 14582 | OS_WIFSIGNALED_METHODDEF |
| 14583 | OS_WIFEXITED_METHODDEF |
| 14584 | OS_WEXITSTATUS_METHODDEF |
| 14585 | OS_WTERMSIG_METHODDEF |
| 14586 | OS_WSTOPSIG_METHODDEF |
| 14587 | OS_FSTATVFS_METHODDEF |
| 14588 | OS_STATVFS_METHODDEF |
| 14589 | OS_CONFSTR_METHODDEF |
| 14590 | OS_SYSCONF_METHODDEF |
| 14591 | OS_FPATHCONF_METHODDEF |
| 14592 | OS_PATHCONF_METHODDEF |
| 14593 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 14594 | OS__GETFULLPATHNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14595 | OS__GETDISKUSAGE_METHODDEF |
| 14596 | OS__GETFINALPATHNAME_METHODDEF |
| 14597 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 14598 | OS_GETLOADAVG_METHODDEF |
| 14599 | OS_URANDOM_METHODDEF |
| 14600 | OS_SETRESUID_METHODDEF |
| 14601 | OS_SETRESGID_METHODDEF |
| 14602 | OS_GETRESUID_METHODDEF |
| 14603 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 14604 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14605 | OS_GETXATTR_METHODDEF |
| 14606 | OS_SETXATTR_METHODDEF |
| 14607 | OS_REMOVEXATTR_METHODDEF |
| 14608 | OS_LISTXATTR_METHODDEF |
| 14609 | |
Serhiy Storchaka | 2b56031 | 2020-04-18 19:14:10 +0300 | [diff] [blame] | 14610 | OS_GET_TERMINAL_SIZE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14611 | OS_CPU_COUNT_METHODDEF |
| 14612 | OS_GET_INHERITABLE_METHODDEF |
| 14613 | OS_SET_INHERITABLE_METHODDEF |
| 14614 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 14615 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 14616 | OS_GET_BLOCKING_METHODDEF |
| 14617 | OS_SET_BLOCKING_METHODDEF |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 14618 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14619 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14620 | OS_GETRANDOM_METHODDEF |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14621 | OS_MEMFD_CREATE_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14622 | OS__ADD_DLL_DIRECTORY_METHODDEF |
| 14623 | OS__REMOVE_DLL_DIRECTORY_METHODDEF |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14624 | OS_WAITSTATUS_TO_EXITCODE_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14625 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14626 | }; |
| 14627 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14628 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14629 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14630 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14631 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14632 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14633 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14634 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14635 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14636 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14637 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14638 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14639 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14640 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14641 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14642 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14643 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14644 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14645 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14646 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14647 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14648 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14649 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14650 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14651 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14652 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14653 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14654 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14655 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14656 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14657 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14658 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14659 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14660 | #endif |
| 14661 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14662 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14663 | #endif |
| 14664 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14665 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14666 | #endif |
| 14667 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14668 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14669 | #endif |
| 14670 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14671 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14672 | #endif |
| 14673 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14674 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14675 | #endif |
| 14676 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14677 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14678 | #endif |
| 14679 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14680 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14681 | #endif |
| 14682 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14683 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14684 | #endif |
| 14685 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14686 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14687 | #endif |
| 14688 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14689 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14690 | #endif |
| 14691 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14692 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14693 | #endif |
| 14694 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14695 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14696 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14697 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14698 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14699 | #endif |
| 14700 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14701 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14702 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14703 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14704 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14705 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14706 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14707 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14708 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14709 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14710 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14711 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14712 | #endif |
| 14713 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14714 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14715 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14716 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14717 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14718 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14719 | #endif |
| 14720 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14721 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14722 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14723 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14724 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14725 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14726 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14727 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14728 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 14729 | #ifdef O_TMPFILE |
| 14730 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 14731 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14732 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14733 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14734 | #endif |
| 14735 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14736 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14737 | #endif |
| 14738 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14739 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14740 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14741 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14742 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14743 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14744 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14745 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14746 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14747 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14748 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14749 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14750 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14751 | #endif |
| 14752 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14753 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14754 | #endif |
| 14755 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14756 | /* MS Windows */ |
| 14757 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14758 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14759 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14760 | #endif |
| 14761 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14762 | /* Optimize for short life (keep in memory). */ |
| 14763 | /* 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] | 14764 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14765 | #endif |
| 14766 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14767 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14768 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14769 | #endif |
| 14770 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14771 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14772 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14773 | #endif |
| 14774 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14775 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14776 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14777 | #endif |
| 14778 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14779 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14780 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14781 | /* Send a SIGIO signal whenever input or output |
| 14782 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14783 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14784 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14785 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14786 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14787 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14788 | #endif |
| 14789 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14790 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14791 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14792 | #endif |
| 14793 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14794 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14795 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14796 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14797 | #ifdef O_NOLINKS |
| 14798 | /* 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] | 14799 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14800 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14801 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14802 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14803 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14804 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 14805 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14806 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14807 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14808 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14809 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14810 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14811 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14812 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14813 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14814 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14815 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14816 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14817 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14818 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14819 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14820 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14821 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14822 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14823 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14824 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14825 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14826 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14827 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14828 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14829 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14830 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14831 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14832 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14833 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14834 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14835 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14836 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14837 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14838 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14839 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14840 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14841 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14842 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14843 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14844 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14845 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14846 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14847 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14848 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14849 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14850 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14851 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14852 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14853 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14854 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14855 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14856 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14857 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14858 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 14859 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14860 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14861 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14862 | #endif /* ST_RDONLY */ |
| 14863 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14864 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14865 | #endif /* ST_NOSUID */ |
| 14866 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 14867 | /* GNU extensions */ |
| 14868 | #ifdef ST_NODEV |
| 14869 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 14870 | #endif /* ST_NODEV */ |
| 14871 | #ifdef ST_NOEXEC |
| 14872 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 14873 | #endif /* ST_NOEXEC */ |
| 14874 | #ifdef ST_SYNCHRONOUS |
| 14875 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 14876 | #endif /* ST_SYNCHRONOUS */ |
| 14877 | #ifdef ST_MANDLOCK |
| 14878 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 14879 | #endif /* ST_MANDLOCK */ |
| 14880 | #ifdef ST_WRITE |
| 14881 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 14882 | #endif /* ST_WRITE */ |
| 14883 | #ifdef ST_APPEND |
| 14884 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 14885 | #endif /* ST_APPEND */ |
| 14886 | #ifdef ST_NOATIME |
| 14887 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 14888 | #endif /* ST_NOATIME */ |
| 14889 | #ifdef ST_NODIRATIME |
| 14890 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 14891 | #endif /* ST_NODIRATIME */ |
| 14892 | #ifdef ST_RELATIME |
| 14893 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 14894 | #endif /* ST_RELATIME */ |
| 14895 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14896 | /* FreeBSD sendfile() constants */ |
| 14897 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14898 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14899 | #endif |
| 14900 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14901 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14902 | #endif |
| 14903 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14904 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14905 | #endif |
| 14906 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14907 | /* constants for posix_fadvise */ |
| 14908 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14909 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14910 | #endif |
| 14911 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14912 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14913 | #endif |
| 14914 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14915 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14916 | #endif |
| 14917 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14918 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14919 | #endif |
| 14920 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14921 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14922 | #endif |
| 14923 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14924 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14925 | #endif |
| 14926 | |
| 14927 | /* constants for waitid */ |
| 14928 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14929 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 14930 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 14931 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 14932 | #ifdef P_PIDFD |
| 14933 | if (PyModule_AddIntMacro(m, P_PIDFD)) return -1; |
| 14934 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14935 | #endif |
| 14936 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14937 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14938 | #endif |
| 14939 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14940 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14941 | #endif |
| 14942 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14943 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14944 | #endif |
| 14945 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14946 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14947 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14948 | #ifdef CLD_KILLED |
| 14949 | if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1; |
| 14950 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14951 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14952 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14953 | #endif |
| 14954 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14955 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14956 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14957 | #ifdef CLD_STOPPED |
| 14958 | if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1; |
| 14959 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14960 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14961 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14962 | #endif |
| 14963 | |
| 14964 | /* constants for lockf */ |
| 14965 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14966 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14967 | #endif |
| 14968 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14969 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14970 | #endif |
| 14971 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14972 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14973 | #endif |
| 14974 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14975 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14976 | #endif |
| 14977 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14978 | #ifdef RWF_DSYNC |
| 14979 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 14980 | #endif |
| 14981 | #ifdef RWF_HIPRI |
| 14982 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 14983 | #endif |
| 14984 | #ifdef RWF_SYNC |
| 14985 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 14986 | #endif |
| 14987 | #ifdef RWF_NOWAIT |
| 14988 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 14989 | #endif |
YoSTEALTH | 76ef255 | 2020-05-27 15:32:22 -0600 | [diff] [blame] | 14990 | #ifdef RWF_APPEND |
| 14991 | if (PyModule_AddIntConstant(m, "RWF_APPEND", RWF_APPEND)) return -1; |
| 14992 | #endif |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14993 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14994 | /* constants for posix_spawn */ |
| 14995 | #ifdef HAVE_POSIX_SPAWN |
| 14996 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 14997 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 14998 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 14999 | #endif |
| 15000 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 15001 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15002 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 15003 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15004 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 15005 | #endif |
| 15006 | #ifdef HAVE_SPAWNV |
| 15007 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15008 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 15009 | #endif |
| 15010 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 15011 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 15012 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15013 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 15014 | #endif |
| 15015 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15016 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 15017 | #endif |
| 15018 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15019 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 15020 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 15021 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 15022 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 15023 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 15024 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15025 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 15026 | #endif |
| 15027 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15028 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 15029 | #endif |
| 15030 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15031 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 15032 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 15033 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15034 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 15035 | #endif |
| 15036 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15037 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 15038 | #endif |
| 15039 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15040 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 15041 | #endif |
| 15042 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15043 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 15044 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 15045 | #endif |
| 15046 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 15047 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15048 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 15049 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 15050 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15051 | #endif |
| 15052 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 15053 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15054 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 15055 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 15056 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15057 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 15058 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 15059 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15060 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 15061 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 15062 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15063 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 15064 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 15065 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15066 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 15067 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 15068 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15069 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 15070 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 15071 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 15072 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 15073 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 15074 | #if HAVE_DECL_RTLD_MEMBER |
| 15075 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 15076 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 15077 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 15078 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 15079 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 15080 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 15081 | #endif |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15082 | #ifdef HAVE_MEMFD_CREATE |
| 15083 | if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1; |
| 15084 | if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1; |
| 15085 | #ifdef MFD_HUGETLB |
| 15086 | if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15087 | #endif |
| 15088 | #ifdef MFD_HUGE_SHIFT |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15089 | if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15090 | #endif |
| 15091 | #ifdef MFD_HUGE_MASK |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15092 | if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15093 | #endif |
| 15094 | #ifdef MFD_HUGE_64KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15095 | if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15096 | #endif |
| 15097 | #ifdef MFD_HUGE_512KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15098 | if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15099 | #endif |
| 15100 | #ifdef MFD_HUGE_1MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15101 | if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15102 | #endif |
| 15103 | #ifdef MFD_HUGE_2MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15104 | if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15105 | #endif |
| 15106 | #ifdef MFD_HUGE_8MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15107 | if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15108 | #endif |
| 15109 | #ifdef MFD_HUGE_16MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15110 | if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15111 | #endif |
| 15112 | #ifdef MFD_HUGE_32MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15113 | if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15114 | #endif |
| 15115 | #ifdef MFD_HUGE_256MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15116 | if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15117 | #endif |
| 15118 | #ifdef MFD_HUGE_512MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15119 | if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15120 | #endif |
| 15121 | #ifdef MFD_HUGE_1GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15122 | if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15123 | #endif |
| 15124 | #ifdef MFD_HUGE_2GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15125 | if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 15126 | #endif |
| 15127 | #ifdef MFD_HUGE_16GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15128 | if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1; |
| 15129 | #endif |
| 15130 | #endif |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 15131 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 15132 | #if defined(__APPLE__) |
| 15133 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 15134 | #endif |
| 15135 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 15136 | #ifdef MS_WINDOWS |
| 15137 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; |
| 15138 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; |
| 15139 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; |
| 15140 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; |
| 15141 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; |
| 15142 | #endif |
| 15143 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15144 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 15145 | } |
| 15146 | |
| 15147 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15148 | |
| 15149 | #define PROBE(name, test) \ |
| 15150 | static int name(void) \ |
| 15151 | { \ |
| 15152 | if (test) { \ |
| 15153 | return 1; \ |
| 15154 | } else { \ |
| 15155 | return 0; \ |
| 15156 | } \ |
| 15157 | } |
| 15158 | |
| 15159 | #ifdef HAVE_FSTATAT |
| 15160 | PROBE(probe_fstatat, HAVE_FSTATAT_RUNTIME) |
| 15161 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15162 | |
| 15163 | #ifdef HAVE_FACCESSAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15164 | PROBE(probe_faccessat, HAVE_FACCESSAT_RUNTIME) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15165 | #endif |
| 15166 | |
| 15167 | #ifdef HAVE_FCHMODAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15168 | PROBE(probe_fchmodat, HAVE_FCHMODAT_RUNTIME) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15169 | #endif |
| 15170 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 15171 | #ifdef HAVE_FCHOWNAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15172 | PROBE(probe_fchownat, HAVE_FCHOWNAT_RUNTIME) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15173 | #endif |
| 15174 | |
| 15175 | #ifdef HAVE_LINKAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15176 | PROBE(probe_linkat, HAVE_LINKAT_RUNTIME) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15177 | #endif |
| 15178 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15179 | #ifdef HAVE_FDOPENDIR |
| 15180 | PROBE(probe_fdopendir, HAVE_FDOPENDIR_RUNTIME) |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 15181 | #endif |
| 15182 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15183 | #ifdef HAVE_MKDIRAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15184 | PROBE(probe_mkdirat, HAVE_MKDIRAT_RUNTIME) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15185 | #endif |
| 15186 | |
| 15187 | #ifdef HAVE_RENAMEAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15188 | PROBE(probe_renameat, HAVE_RENAMEAT_RUNTIME) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15189 | #endif |
| 15190 | |
| 15191 | #ifdef HAVE_UNLINKAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15192 | PROBE(probe_unlinkat, HAVE_UNLINKAT_RUNTIME) |
| 15193 | #endif |
| 15194 | |
| 15195 | #ifdef HAVE_OPENAT |
| 15196 | PROBE(probe_openat, HAVE_OPENAT_RUNTIME) |
| 15197 | #endif |
| 15198 | |
| 15199 | #ifdef HAVE_READLINKAT |
| 15200 | PROBE(probe_readlinkat, HAVE_READLINKAT_RUNTIME) |
| 15201 | #endif |
| 15202 | |
| 15203 | #ifdef HAVE_SYMLINKAT |
| 15204 | PROBE(probe_symlinkat, HAVE_SYMLINKAT_RUNTIME) |
| 15205 | #endif |
| 15206 | |
| 15207 | #ifdef HAVE_FUTIMENS |
| 15208 | PROBE(probe_futimens, HAVE_FUTIMENS_RUNTIME) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15209 | #endif |
| 15210 | |
| 15211 | #ifdef HAVE_UTIMENSAT |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15212 | PROBE(probe_utimensat, HAVE_UTIMENSAT_RUNTIME) |
| 15213 | #endif |
| 15214 | |
| 15215 | |
| 15216 | |
| 15217 | |
| 15218 | static const struct have_function { |
| 15219 | const char * const label; |
| 15220 | int (*probe)(void); |
| 15221 | } have_functions[] = { |
| 15222 | |
| 15223 | #ifdef HAVE_FACCESSAT |
| 15224 | { "HAVE_FACCESSAT", probe_faccessat }, |
| 15225 | #endif |
| 15226 | |
| 15227 | #ifdef HAVE_FCHDIR |
| 15228 | { "HAVE_FCHDIR", NULL }, |
| 15229 | #endif |
| 15230 | |
| 15231 | #ifdef HAVE_FCHMOD |
| 15232 | { "HAVE_FCHMOD", NULL }, |
| 15233 | #endif |
| 15234 | |
| 15235 | #ifdef HAVE_FCHMODAT |
| 15236 | { "HAVE_FCHMODAT", probe_fchmodat }, |
| 15237 | #endif |
| 15238 | |
| 15239 | #ifdef HAVE_FCHOWN |
| 15240 | { "HAVE_FCHOWN", NULL }, |
| 15241 | #endif |
| 15242 | |
| 15243 | #ifdef HAVE_FCHOWNAT |
| 15244 | { "HAVE_FCHOWNAT", probe_fchownat }, |
| 15245 | #endif |
| 15246 | |
| 15247 | #ifdef HAVE_FEXECVE |
| 15248 | { "HAVE_FEXECVE", NULL }, |
| 15249 | #endif |
| 15250 | |
| 15251 | #ifdef HAVE_FDOPENDIR |
| 15252 | { "HAVE_FDOPENDIR", probe_fdopendir }, |
| 15253 | #endif |
| 15254 | |
| 15255 | #ifdef HAVE_FPATHCONF |
| 15256 | { "HAVE_FPATHCONF", NULL }, |
| 15257 | #endif |
| 15258 | |
| 15259 | #ifdef HAVE_FSTATAT |
| 15260 | { "HAVE_FSTATAT", probe_fstatat }, |
| 15261 | #endif |
| 15262 | |
| 15263 | #ifdef HAVE_FSTATVFS |
| 15264 | { "HAVE_FSTATVFS", NULL }, |
| 15265 | #endif |
| 15266 | |
| 15267 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
| 15268 | { "HAVE_FTRUNCATE", NULL }, |
| 15269 | #endif |
| 15270 | |
| 15271 | #ifdef HAVE_FUTIMENS |
| 15272 | { "HAVE_FUTIMENS", probe_futimens }, |
| 15273 | #endif |
| 15274 | |
| 15275 | #ifdef HAVE_FUTIMES |
| 15276 | { "HAVE_FUTIMES", NULL }, |
| 15277 | #endif |
| 15278 | |
| 15279 | #ifdef HAVE_FUTIMESAT |
| 15280 | { "HAVE_FUTIMESAT", NULL }, |
| 15281 | #endif |
| 15282 | |
| 15283 | #ifdef HAVE_LINKAT |
| 15284 | { "HAVE_LINKAT", probe_linkat }, |
| 15285 | #endif |
| 15286 | |
| 15287 | #ifdef HAVE_LCHFLAGS |
| 15288 | { "HAVE_LCHFLAGS", NULL }, |
| 15289 | #endif |
| 15290 | |
| 15291 | #ifdef HAVE_LCHMOD |
| 15292 | { "HAVE_LCHMOD", NULL }, |
| 15293 | #endif |
| 15294 | |
| 15295 | #ifdef HAVE_LCHOWN |
| 15296 | { "HAVE_LCHOWN", NULL }, |
| 15297 | #endif |
| 15298 | |
| 15299 | #ifdef HAVE_LSTAT |
| 15300 | { "HAVE_LSTAT", NULL }, |
| 15301 | #endif |
| 15302 | |
| 15303 | #ifdef HAVE_LUTIMES |
| 15304 | { "HAVE_LUTIMES", NULL }, |
| 15305 | #endif |
| 15306 | |
| 15307 | #ifdef HAVE_MEMFD_CREATE |
| 15308 | { "HAVE_MEMFD_CREATE", NULL }, |
| 15309 | #endif |
| 15310 | |
| 15311 | #ifdef HAVE_MKDIRAT |
| 15312 | { "HAVE_MKDIRAT", probe_mkdirat }, |
| 15313 | #endif |
| 15314 | |
| 15315 | #ifdef HAVE_MKFIFOAT |
| 15316 | { "HAVE_MKFIFOAT", NULL }, |
| 15317 | #endif |
| 15318 | |
| 15319 | #ifdef HAVE_MKNODAT |
| 15320 | { "HAVE_MKNODAT", NULL }, |
| 15321 | #endif |
| 15322 | |
| 15323 | #ifdef HAVE_OPENAT |
| 15324 | { "HAVE_OPENAT", probe_openat }, |
| 15325 | #endif |
| 15326 | |
| 15327 | #ifdef HAVE_READLINKAT |
| 15328 | { "HAVE_READLINKAT", probe_readlinkat }, |
| 15329 | #endif |
| 15330 | |
| 15331 | #ifdef HAVE_RENAMEAT |
| 15332 | { "HAVE_RENAMEAT", probe_renameat }, |
| 15333 | #endif |
| 15334 | |
| 15335 | #ifdef HAVE_SYMLINKAT |
| 15336 | { "HAVE_SYMLINKAT", probe_symlinkat }, |
| 15337 | #endif |
| 15338 | |
| 15339 | #ifdef HAVE_UNLINKAT |
| 15340 | { "HAVE_UNLINKAT", probe_unlinkat }, |
| 15341 | #endif |
| 15342 | |
| 15343 | #ifdef HAVE_UTIMENSAT |
| 15344 | { "HAVE_UTIMENSAT", probe_utimensat }, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15345 | #endif |
| 15346 | |
| 15347 | #ifdef MS_WINDOWS |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15348 | { "MS_WINDOWS", NULL }, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15349 | #endif |
| 15350 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15351 | { NULL, NULL } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15352 | }; |
| 15353 | |
| 15354 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15355 | static int |
| 15356 | posixmodule_exec(PyObject *m) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 15357 | { |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15358 | _posixstate *state = get_posix_state(m); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 15359 | |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15360 | #if defined(HAVE_PWRITEV) |
| 15361 | if (HAVE_PWRITEV_RUNTIME) {} else { |
| 15362 | PyObject* dct = PyModule_GetDict(m); |
| 15363 | |
| 15364 | if (dct == NULL) { |
| 15365 | return -1; |
| 15366 | } |
| 15367 | |
| 15368 | if (PyDict_DelItemString(dct, "pwritev") == -1) { |
| 15369 | PyErr_Clear(); |
| 15370 | } |
| 15371 | if (PyDict_DelItemString(dct, "preadv") == -1) { |
| 15372 | PyErr_Clear(); |
| 15373 | } |
| 15374 | } |
| 15375 | #endif |
| 15376 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15377 | /* Initialize environ dictionary */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15378 | PyObject *v = convertenviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15379 | Py_XINCREF(v); |
| 15380 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15381 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15382 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 15383 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15384 | if (all_ins(m)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15385 | return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 15386 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15387 | if (setup_confname_tables(m)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15388 | return -1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 15389 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15390 | Py_INCREF(PyExc_OSError); |
| 15391 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 15392 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 15393 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15394 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 15395 | PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc); |
| 15396 | if (WaitidResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15397 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15398 | } |
| 15399 | Py_INCREF(WaitidResultType); |
| 15400 | PyModule_AddObject(m, "waitid_result", WaitidResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15401 | state->WaitidResultType = WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 15402 | #endif |
| 15403 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15404 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
| 15405 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 15406 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 15407 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 15408 | PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc); |
| 15409 | if (StatResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15410 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15411 | } |
| 15412 | Py_INCREF(StatResultType); |
| 15413 | PyModule_AddObject(m, "stat_result", StatResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15414 | state->StatResultType = StatResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15415 | structseq_new = ((PyTypeObject *)StatResultType)->tp_new; |
| 15416 | ((PyTypeObject *)StatResultType)->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15417 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15418 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
| 15419 | PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc); |
| 15420 | if (StatVFSResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15421 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15422 | } |
| 15423 | Py_INCREF(StatVFSResultType); |
| 15424 | PyModule_AddObject(m, "statvfs_result", StatVFSResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15425 | state->StatVFSResultType = StatVFSResultType; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 15426 | #ifdef NEED_TICKS_PER_SECOND |
| 15427 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15428 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 15429 | # elif defined(HZ) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15430 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 15431 | # else |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15432 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 15433 | # endif |
| 15434 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 15435 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 15436 | #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] | 15437 | sched_param_desc.name = MODNAME ".sched_param"; |
| 15438 | PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc); |
| 15439 | if (SchedParamType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15440 | return -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15441 | } |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 15442 | Py_INCREF(SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15443 | PyModule_AddObject(m, "sched_param", SchedParamType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15444 | state->SchedParamType = SchedParamType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15445 | ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param; |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 15446 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15447 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15448 | /* initialize TerminalSize_info */ |
| 15449 | PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc); |
| 15450 | if (TerminalSizeType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15451 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15452 | } |
| 15453 | Py_INCREF(TerminalSizeType); |
| 15454 | PyModule_AddObject(m, "terminal_size", TerminalSizeType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15455 | state->TerminalSizeType = TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15456 | |
| 15457 | /* initialize scandir types */ |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15458 | PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15459 | if (ScandirIteratorType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15460 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15461 | } |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15462 | state->ScandirIteratorType = ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15463 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15464 | PyObject *DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15465 | if (DirEntryType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15466 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15467 | } |
| 15468 | Py_INCREF(DirEntryType); |
| 15469 | PyModule_AddObject(m, "DirEntry", DirEntryType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15470 | state->DirEntryType = DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15471 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 15472 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15473 | PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(×_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 15474 | if (TimesResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15475 | return -1; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 15476 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15477 | Py_INCREF(TimesResultType); |
| 15478 | PyModule_AddObject(m, "times_result", TimesResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15479 | state->TimesResultType = TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 15480 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15481 | PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 15482 | if (UnameResultType == NULL) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15483 | return -1; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 15484 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15485 | Py_INCREF(UnameResultType); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 15486 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15487 | state->UnameResultType = (PyObject *)UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 15488 | |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15489 | if ((state->billion = PyLong_FromLong(1000000000)) == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15490 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15491 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15492 | state->struct_rusage = PyUnicode_InternFromString("struct_rusage"); |
| 15493 | if (state->struct_rusage == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15494 | return -1; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 15495 | #endif |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15496 | state->st_mode = PyUnicode_InternFromString("st_mode"); |
| 15497 | if (state->st_mode == NULL) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15498 | return -1; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 15499 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15500 | /* suppress "function not used" warnings */ |
| 15501 | { |
| 15502 | int ignored; |
| 15503 | fd_specified("", -1); |
| 15504 | follow_symlinks_specified("", 1); |
| 15505 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 15506 | dir_fd_converter(Py_None, &ignored); |
| 15507 | dir_fd_unavailable(Py_None, &ignored); |
| 15508 | } |
| 15509 | |
| 15510 | /* |
| 15511 | * provide list of locally available functions |
| 15512 | * so os.py can populate support_* lists |
| 15513 | */ |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15514 | PyObject *list = PyList_New(0); |
| 15515 | if (!list) { |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15516 | return -1; |
Victor Stinner | 97f33c3 | 2020-05-14 18:05:58 +0200 | [diff] [blame] | 15517 | } |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15518 | for (const struct have_function *trace = have_functions; trace->label; trace++) { |
| 15519 | PyObject *unicode; |
| 15520 | if (trace->probe && !trace->probe()) continue; |
| 15521 | unicode = PyUnicode_DecodeASCII(trace->label, strlen(trace->label), NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15522 | if (!unicode) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15523 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15524 | if (PyList_Append(list, unicode)) |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15525 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15526 | Py_DECREF(unicode); |
| 15527 | } |
Ronald Oussoren | 4176193 | 2020-11-08 10:05:27 +0100 | [diff] [blame] | 15528 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15529 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 15530 | |
Victor Stinner | 1c2fa78 | 2020-05-10 11:05:29 +0200 | [diff] [blame] | 15531 | return 0; |
| 15532 | } |
| 15533 | |
| 15534 | |
| 15535 | static PyModuleDef_Slot posixmodile_slots[] = { |
| 15536 | {Py_mod_exec, posixmodule_exec}, |
| 15537 | {0, NULL} |
| 15538 | }; |
| 15539 | |
| 15540 | static struct PyModuleDef posixmodule = { |
| 15541 | PyModuleDef_HEAD_INIT, |
| 15542 | .m_name = MODNAME, |
| 15543 | .m_doc = posix__doc__, |
| 15544 | .m_size = sizeof(_posixstate), |
| 15545 | .m_methods = posix_methods, |
| 15546 | .m_slots = posixmodile_slots, |
| 15547 | .m_traverse = _posix_traverse, |
| 15548 | .m_clear = _posix_clear, |
| 15549 | .m_free = _posix_free, |
| 15550 | }; |
| 15551 | |
| 15552 | PyMODINIT_FUNC |
| 15553 | INITFUNC(void) |
| 15554 | { |
| 15555 | return PyModuleDef_Init(&posixmodule); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 15556 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15557 | |
| 15558 | #ifdef __cplusplus |
| 15559 | } |
| 15560 | #endif |