Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* POSIX module implementation */ |
| 3 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 4 | /* This file is also used for Windows NT/MS-Win. In that case the |
| 5 | module actually calls itself 'nt', not 'posix', and a few |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 6 | functions are either unimplemented or implemented differently. The source |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | 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] | 8 | of the compiler used. Different compilers define their own feature |
Victor Stinner | f427a14 | 2014-10-22 12:33:23 +0200 | [diff] [blame] | 9 | test macro, e.g. '_MSC_VER'. */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 10 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 11 | |
| 12 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13 | #ifdef __APPLE__ |
| 14 | /* |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15 | * Step 1 of support for weak-linking a number of symbols existing on |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 16 | * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block |
| 17 | * at the end of this file for more information. |
| 18 | */ |
| 19 | # pragma weak lchown |
| 20 | # pragma weak statvfs |
| 21 | # pragma weak fstatvfs |
| 22 | |
| 23 | #endif /* __APPLE__ */ |
| 24 | |
Thomas Wouters | 68bc4f9 | 2006-03-01 01:05:10 +0000 | [diff] [blame] | 25 | #define PY_SSIZE_T_CLEAN |
| 26 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 27 | #include "Python.h" |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 28 | #ifdef MS_WINDOWS |
| 29 | /* include <windows.h> early to avoid conflict with pycore_condvar.h: |
| 30 | |
| 31 | #define WIN32_LEAN_AND_MEAN |
| 32 | #include <windows.h> |
| 33 | |
| 34 | FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */ |
| 35 | # include <windows.h> |
| 36 | #endif |
| 37 | |
| 38 | #include "pycore_ceval.h" /* _PyEval_ReInitThreads() */ |
Victor Stinner | 01b63ec | 2019-06-19 00:48:09 +0200 | [diff] [blame] | 39 | #include "pycore_import.h" /* _PyImport_ReInitLock() */ |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 40 | #include "pycore_pystate.h" /* _PyRuntime */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 41 | #include "pythread.h" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 42 | #include "structmember.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 43 | #ifndef MS_WINDOWS |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 44 | # include "posixmodule.h" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 45 | #else |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 46 | # include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 47 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 48 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 49 | /* On android API level 21, 'AT_EACCESS' is not declared although |
| 50 | * HAVE_FACCESSAT is defined. */ |
| 51 | #ifdef __ANDROID__ |
| 52 | #undef HAVE_FACCESSAT |
| 53 | #endif |
| 54 | |
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | fa76eee | 2016-05-28 21:03:48 +0000 | [diff] [blame] | 55 | #include <stdio.h> /* needed for ctermid() */ |
| 56 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 57 | #ifdef __cplusplus |
| 58 | extern "C" { |
| 59 | #endif |
| 60 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 61 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 62 | "This module provides access to operating system functionality that is\n\ |
| 63 | standardized by the C Standard and the POSIX standard (a thinly\n\ |
| 64 | disguised Unix interface). Refer to the library manual and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 65 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 67 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 68 | #ifdef HAVE_SYS_UIO_H |
| 69 | #include <sys/uio.h> |
| 70 | #endif |
| 71 | |
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 72 | #ifdef HAVE_SYS_SYSMACROS_H |
| 73 | /* GNU C Library: major(), minor(), makedev() */ |
| 74 | #include <sys/sysmacros.h> |
| 75 | #endif |
| 76 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 77 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 78 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 79 | #endif /* HAVE_SYS_TYPES_H */ |
| 80 | |
| 81 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 82 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 83 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 85 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 86 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 87 | #endif |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 88 | #ifdef HAVE_LINUX_WAIT_H |
| 89 | #include <linux/wait.h> // For P_PIDFD |
| 90 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 91 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 92 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 93 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 94 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 96 | #ifdef HAVE_FCNTL_H |
| 97 | #include <fcntl.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 98 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 100 | #ifdef HAVE_GRP_H |
| 101 | #include <grp.h> |
| 102 | #endif |
| 103 | |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 104 | #ifdef HAVE_SYSEXITS_H |
| 105 | #include <sysexits.h> |
| 106 | #endif /* HAVE_SYSEXITS_H */ |
| 107 | |
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 108 | #ifdef HAVE_SYS_LOADAVG_H |
| 109 | #include <sys/loadavg.h> |
| 110 | #endif |
| 111 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 112 | #ifdef HAVE_SYS_SENDFILE_H |
| 113 | #include <sys/sendfile.h> |
| 114 | #endif |
| 115 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 116 | #if defined(__APPLE__) |
| 117 | #include <copyfile.h> |
| 118 | #endif |
| 119 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 120 | #ifdef HAVE_SCHED_H |
| 121 | #include <sched.h> |
| 122 | #endif |
| 123 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 124 | #ifdef HAVE_COPY_FILE_RANGE |
| 125 | #include <unistd.h> |
| 126 | #endif |
| 127 | |
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 128 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 129 | #undef HAVE_SCHED_SETAFFINITY |
| 130 | #endif |
| 131 | |
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 132 | #if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__) |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 133 | #define USE_XATTRS |
| 134 | #endif |
| 135 | |
| 136 | #ifdef USE_XATTRS |
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 137 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 138 | #endif |
| 139 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 140 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 141 | #ifdef HAVE_SYS_SOCKET_H |
| 142 | #include <sys/socket.h> |
| 143 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 144 | #endif |
| 145 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 146 | #ifdef HAVE_DLFCN_H |
| 147 | #include <dlfcn.h> |
| 148 | #endif |
| 149 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 150 | #ifdef __hpux |
| 151 | #include <sys/mpctl.h> |
| 152 | #endif |
| 153 | |
| 154 | #if defined(__DragonFly__) || \ |
| 155 | defined(__OpenBSD__) || \ |
| 156 | defined(__FreeBSD__) || \ |
| 157 | defined(__NetBSD__) || \ |
| 158 | defined(__APPLE__) |
| 159 | #include <sys/sysctl.h> |
| 160 | #endif |
| 161 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 162 | #ifdef HAVE_LINUX_RANDOM_H |
| 163 | # include <linux/random.h> |
| 164 | #endif |
| 165 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 166 | # include <sys/syscall.h> |
| 167 | #endif |
| 168 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 169 | #if defined(MS_WINDOWS) |
| 170 | # define TERMSIZE_USE_CONIO |
| 171 | #elif defined(HAVE_SYS_IOCTL_H) |
| 172 | # include <sys/ioctl.h> |
| 173 | # if defined(HAVE_TERMIOS_H) |
| 174 | # include <termios.h> |
| 175 | # endif |
| 176 | # if defined(TIOCGWINSZ) |
| 177 | # define TERMSIZE_USE_IOCTL |
| 178 | # endif |
| 179 | #endif /* MS_WINDOWS */ |
| 180 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 181 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 182 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 183 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 184 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 185 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 186 | #include <process.h> |
| 187 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 188 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 189 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 190 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 191 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 192 | #define HAVE_EXECV 1 |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 193 | #define HAVE_WSPAWNV 1 |
| 194 | #define HAVE_WEXECV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 195 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 196 | #define HAVE_SYSTEM 1 |
| 197 | #define HAVE_CWAIT 1 |
| 198 | #define HAVE_FSYNC 1 |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 199 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 200 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 201 | /* Unix functions that the configure script doesn't check for */ |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 202 | #ifndef __VXWORKS__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 203 | #define HAVE_EXECV 1 |
| 204 | #define HAVE_FORK 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 205 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 206 | #define HAVE_FORK1 1 |
| 207 | #endif |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 208 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 209 | #define HAVE_GETEGID 1 |
| 210 | #define HAVE_GETEUID 1 |
| 211 | #define HAVE_GETGID 1 |
| 212 | #define HAVE_GETPPID 1 |
| 213 | #define HAVE_GETUID 1 |
| 214 | #define HAVE_KILL 1 |
| 215 | #define HAVE_OPENDIR 1 |
| 216 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 217 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 218 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 219 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 220 | #endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 221 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 222 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 223 | _Py_IDENTIFIER(__fspath__); |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 224 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 225 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 226 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 227 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 228 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 229 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 230 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 231 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 232 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 233 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
| 234 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode |
| 235 | (default) */ |
| 236 | extern char *ctermid_r(char *); |
| 237 | #endif |
| 238 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 239 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 240 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 241 | #if defined(__VXWORKS__) |
| 242 | #include <vxCpuLib.h> |
| 243 | #include <rtpLib.h> |
| 244 | #include <wait.h> |
| 245 | #include <taskLib.h> |
| 246 | #ifndef _P_WAIT |
| 247 | #define _P_WAIT 0 |
| 248 | #define _P_NOWAIT 1 |
| 249 | #define _P_NOWAITO 1 |
| 250 | #endif |
| 251 | #endif /* __VXWORKS__ */ |
| 252 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 253 | #ifdef HAVE_POSIX_SPAWN |
| 254 | #include <spawn.h> |
| 255 | #endif |
| 256 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 257 | #ifdef HAVE_UTIME_H |
| 258 | #include <utime.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 259 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 261 | #ifdef HAVE_SYS_UTIME_H |
| 262 | #include <sys/utime.h> |
| 263 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ |
| 264 | #endif /* HAVE_SYS_UTIME_H */ |
| 265 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 266 | #ifdef HAVE_SYS_TIMES_H |
| 267 | #include <sys/times.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 268 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 269 | |
| 270 | #ifdef HAVE_SYS_PARAM_H |
| 271 | #include <sys/param.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 272 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | |
| 274 | #ifdef HAVE_SYS_UTSNAME_H |
| 275 | #include <sys/utsname.h> |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 276 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 277 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 278 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 280 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 281 | #else |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 282 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 283 | #include <direct.h> |
| 284 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
| 285 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 286 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 287 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 288 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 289 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 290 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 291 | #endif |
| 292 | #ifdef HAVE_SYS_DIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 293 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 294 | #endif |
| 295 | #ifdef HAVE_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 296 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 297 | #endif |
| 298 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 300 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 301 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 302 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 303 | #endif |
| 304 | #ifdef HAVE_IO_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 305 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 306 | #endif |
| 307 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 308 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 309 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 310 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 311 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 312 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 313 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
| 314 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
| 315 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 316 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 317 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 318 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 319 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 320 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 321 | #define HAVE_SYMLINK |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 322 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 323 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 324 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 325 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 326 | #define MAXPATHLEN PATH_MAX |
| 327 | #else |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 328 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 329 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 330 | #endif /* MAXPATHLEN */ |
| 331 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 332 | #ifdef UNION_WAIT |
| 333 | /* Emulate some macros on systems that have a union instead of macros */ |
| 334 | |
| 335 | #ifndef WIFEXITED |
| 336 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) |
| 337 | #endif |
| 338 | |
| 339 | #ifndef WEXITSTATUS |
| 340 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) |
| 341 | #endif |
| 342 | |
| 343 | #ifndef WTERMSIG |
| 344 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) |
| 345 | #endif |
| 346 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 347 | #define WAIT_TYPE union wait |
| 348 | #define WAIT_STATUS_INT(s) (s.w_status) |
| 349 | |
| 350 | #else /* !UNION_WAIT */ |
| 351 | #define WAIT_TYPE int |
| 352 | #define WAIT_STATUS_INT(s) (s) |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 353 | #endif /* UNION_WAIT */ |
| 354 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 355 | /* Don't use the "_r" form if we don't need it (also, won't have a |
| 356 | prototype for it, at least on Solaris -- maybe others as well?). */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 357 | #if defined(HAVE_CTERMID_R) |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 358 | #define USE_CTERMID_R |
| 359 | #endif |
| 360 | |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 361 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 362 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 363 | #undef FSTAT |
| 364 | #undef STRUCT_STAT |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 365 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 366 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 367 | # define LSTAT win32_lstat |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 368 | # define FSTAT _Py_fstat_noraise |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 369 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 370 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 371 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 372 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 373 | # define FSTAT fstat |
| 374 | # define STRUCT_STAT struct stat |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 375 | #endif |
| 376 | |
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 377 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 378 | #include <sys/mkdev.h> |
| 379 | #else |
| 380 | #if defined(MAJOR_IN_SYSMACROS) |
| 381 | #include <sys/sysmacros.h> |
| 382 | #endif |
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 383 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
| 384 | #include <sys/mkdev.h> |
| 385 | #endif |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 386 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 387 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 388 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 389 | #define INITFUNC PyInit_nt |
| 390 | #define MODNAME "nt" |
| 391 | #else |
| 392 | #define INITFUNC PyInit_posix |
| 393 | #define MODNAME "posix" |
| 394 | #endif |
| 395 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 396 | #if defined(__sun) |
| 397 | /* Something to implement in autoconf, not present in autoconf 2.69 */ |
| 398 | #define HAVE_STRUCT_STAT_ST_FSTYPE 1 |
| 399 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 400 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 401 | /* memfd_create is either defined in sys/mman.h or sys/memfd.h |
| 402 | * linux/memfd.h defines additional flags |
| 403 | */ |
| 404 | #ifdef HAVE_SYS_MMAN_H |
| 405 | #include <sys/mman.h> |
| 406 | #endif |
| 407 | #ifdef HAVE_SYS_MEMFD_H |
| 408 | #include <sys/memfd.h> |
| 409 | #endif |
| 410 | #ifdef HAVE_LINUX_MEMFD_H |
| 411 | #include <linux/memfd.h> |
| 412 | #endif |
| 413 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 414 | #ifdef _Py_MEMORY_SANITIZER |
| 415 | # include <sanitizer/msan_interface.h> |
| 416 | #endif |
| 417 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 418 | #ifdef HAVE_FORK |
| 419 | static void |
| 420 | run_at_forkers(PyObject *lst, int reverse) |
| 421 | { |
| 422 | Py_ssize_t i; |
| 423 | PyObject *cpy; |
| 424 | |
| 425 | if (lst != NULL) { |
| 426 | assert(PyList_CheckExact(lst)); |
| 427 | |
| 428 | /* Use a list copy in case register_at_fork() is called from |
| 429 | * one of the callbacks. |
| 430 | */ |
| 431 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); |
| 432 | if (cpy == NULL) |
| 433 | PyErr_WriteUnraisable(lst); |
| 434 | else { |
| 435 | if (reverse) |
| 436 | PyList_Reverse(cpy); |
| 437 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { |
| 438 | PyObject *func, *res; |
| 439 | func = PyList_GET_ITEM(cpy, i); |
Jeroen Demeyer | 7f41c8e | 2019-07-04 12:35:31 +0200 | [diff] [blame] | 440 | res = _PyObject_CallNoArg(func); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 441 | if (res == NULL) |
| 442 | PyErr_WriteUnraisable(func); |
| 443 | else |
| 444 | Py_DECREF(res); |
| 445 | } |
| 446 | Py_DECREF(cpy); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void |
| 452 | PyOS_BeforeFork(void) |
| 453 | { |
Victor Stinner | ff4584c | 2020-03-13 18:03:56 +0100 | [diff] [blame] | 454 | run_at_forkers(_PyInterpreterState_GET_UNSAFE()->before_forkers, 1); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 455 | |
| 456 | _PyImport_AcquireLock(); |
| 457 | } |
| 458 | |
| 459 | void |
| 460 | PyOS_AfterFork_Parent(void) |
| 461 | { |
| 462 | if (_PyImport_ReleaseLock() <= 0) |
| 463 | Py_FatalError("failed releasing import lock after fork"); |
| 464 | |
Victor Stinner | ff4584c | 2020-03-13 18:03:56 +0100 | [diff] [blame] | 465 | run_at_forkers(_PyInterpreterState_GET_UNSAFE()->after_forkers_parent, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void |
| 469 | PyOS_AfterFork_Child(void) |
| 470 | { |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 471 | _PyRuntimeState *runtime = &_PyRuntime; |
| 472 | _PyGILState_Reinit(runtime); |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 473 | _PyEval_ReInitThreads(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 474 | _PyImport_ReInitLock(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 475 | _PySignal_AfterFork(); |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 476 | _PyRuntimeState_ReInitThreads(runtime); |
Victor Stinner | b49858b | 2019-05-24 15:20:23 +0200 | [diff] [blame] | 477 | _PyInterpreterState_DeleteExceptMain(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 478 | |
Victor Stinner | ff4584c | 2020-03-13 18:03:56 +0100 | [diff] [blame] | 479 | run_at_forkers(_PyInterpreterState_GET_UNSAFE()->after_forkers_child, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static int |
| 483 | register_at_forker(PyObject **lst, PyObject *func) |
| 484 | { |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 485 | if (func == NULL) /* nothing to register? do nothing. */ |
| 486 | return 0; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 487 | if (*lst == NULL) { |
| 488 | *lst = PyList_New(0); |
| 489 | if (*lst == NULL) |
| 490 | return -1; |
| 491 | } |
| 492 | return PyList_Append(*lst, func); |
| 493 | } |
Victor Stinner | 87255be | 2020-04-07 23:11:49 +0200 | [diff] [blame] | 494 | #endif /* HAVE_FORK */ |
| 495 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 496 | |
| 497 | /* Legacy wrapper */ |
| 498 | void |
| 499 | PyOS_AfterFork(void) |
| 500 | { |
| 501 | #ifdef HAVE_FORK |
| 502 | PyOS_AfterFork_Child(); |
| 503 | #endif |
| 504 | } |
| 505 | |
| 506 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 507 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 508 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 509 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 510 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 511 | ULONG, struct _Py_stat_struct *); |
| 512 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 513 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 514 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 515 | #ifndef MS_WINDOWS |
| 516 | PyObject * |
| 517 | _PyLong_FromUid(uid_t uid) |
| 518 | { |
| 519 | if (uid == (uid_t)-1) |
| 520 | return PyLong_FromLong(-1); |
| 521 | return PyLong_FromUnsignedLong(uid); |
| 522 | } |
| 523 | |
| 524 | PyObject * |
| 525 | _PyLong_FromGid(gid_t gid) |
| 526 | { |
| 527 | if (gid == (gid_t)-1) |
| 528 | return PyLong_FromLong(-1); |
| 529 | return PyLong_FromUnsignedLong(gid); |
| 530 | } |
| 531 | |
| 532 | int |
| 533 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 534 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 535 | uid_t uid; |
| 536 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 537 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 538 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 539 | unsigned long uresult; |
| 540 | |
| 541 | index = PyNumber_Index(obj); |
| 542 | if (index == NULL) { |
| 543 | PyErr_Format(PyExc_TypeError, |
| 544 | "uid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 545 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 546 | return 0; |
| 547 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 548 | |
| 549 | /* |
| 550 | * Handling uid_t is complicated for two reasons: |
| 551 | * * Although uid_t is (always?) unsigned, it still |
| 552 | * accepts -1. |
| 553 | * * We don't know its size in advance--it may be |
| 554 | * bigger than an int, or it may be smaller than |
| 555 | * a long. |
| 556 | * |
| 557 | * So a bit of defensive programming is in order. |
| 558 | * Start with interpreting the value passed |
| 559 | * in as a signed long and see if it works. |
| 560 | */ |
| 561 | |
| 562 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 563 | |
| 564 | if (!overflow) { |
| 565 | uid = (uid_t)result; |
| 566 | |
| 567 | if (result == -1) { |
| 568 | if (PyErr_Occurred()) |
| 569 | goto fail; |
| 570 | /* It's a legitimate -1, we're done. */ |
| 571 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 572 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 573 | |
| 574 | /* Any other negative number is disallowed. */ |
| 575 | if (result < 0) |
| 576 | goto underflow; |
| 577 | |
| 578 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 579 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 580 | (long)uid != result) |
| 581 | goto underflow; |
| 582 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 583 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 584 | |
| 585 | if (overflow < 0) |
| 586 | goto underflow; |
| 587 | |
| 588 | /* |
| 589 | * Okay, the value overflowed a signed long. If it |
| 590 | * fits in an *unsigned* long, it may still be okay, |
| 591 | * as uid_t may be unsigned long on this platform. |
| 592 | */ |
| 593 | uresult = PyLong_AsUnsignedLong(index); |
| 594 | if (PyErr_Occurred()) { |
| 595 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 596 | goto overflow; |
| 597 | goto fail; |
| 598 | } |
| 599 | |
| 600 | uid = (uid_t)uresult; |
| 601 | |
| 602 | /* |
| 603 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 604 | * but this value would get interpreted as (uid_t)-1 by chown |
| 605 | * and its siblings. That's not what the user meant! So we |
| 606 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 607 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 608 | */ |
| 609 | if (uid == (uid_t)-1) |
| 610 | goto overflow; |
| 611 | |
| 612 | /* Ensure the value wasn't truncated. */ |
| 613 | if (sizeof(uid_t) < sizeof(long) && |
| 614 | (unsigned long)uid != uresult) |
| 615 | goto overflow; |
| 616 | /* fallthrough */ |
| 617 | |
| 618 | success: |
| 619 | Py_DECREF(index); |
| 620 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 621 | return 1; |
| 622 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 623 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 624 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 625 | "uid is less than minimum"); |
| 626 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 627 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 628 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 629 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 630 | "uid is greater than maximum"); |
| 631 | /* fallthrough */ |
| 632 | |
| 633 | fail: |
| 634 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | int |
| 639 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 640 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 641 | gid_t gid; |
| 642 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 643 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 644 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 645 | unsigned long uresult; |
| 646 | |
| 647 | index = PyNumber_Index(obj); |
| 648 | if (index == NULL) { |
| 649 | PyErr_Format(PyExc_TypeError, |
| 650 | "gid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 651 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 652 | return 0; |
| 653 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 654 | |
| 655 | /* |
| 656 | * Handling gid_t is complicated for two reasons: |
| 657 | * * Although gid_t is (always?) unsigned, it still |
| 658 | * accepts -1. |
| 659 | * * We don't know its size in advance--it may be |
| 660 | * bigger than an int, or it may be smaller than |
| 661 | * a long. |
| 662 | * |
| 663 | * So a bit of defensive programming is in order. |
| 664 | * Start with interpreting the value passed |
| 665 | * in as a signed long and see if it works. |
| 666 | */ |
| 667 | |
| 668 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 669 | |
| 670 | if (!overflow) { |
| 671 | gid = (gid_t)result; |
| 672 | |
| 673 | if (result == -1) { |
| 674 | if (PyErr_Occurred()) |
| 675 | goto fail; |
| 676 | /* It's a legitimate -1, we're done. */ |
| 677 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 678 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 679 | |
| 680 | /* Any other negative number is disallowed. */ |
| 681 | if (result < 0) { |
| 682 | goto underflow; |
| 683 | } |
| 684 | |
| 685 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 686 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 687 | (long)gid != result) |
| 688 | goto underflow; |
| 689 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 690 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 691 | |
| 692 | if (overflow < 0) |
| 693 | goto underflow; |
| 694 | |
| 695 | /* |
| 696 | * Okay, the value overflowed a signed long. If it |
| 697 | * fits in an *unsigned* long, it may still be okay, |
| 698 | * as gid_t may be unsigned long on this platform. |
| 699 | */ |
| 700 | uresult = PyLong_AsUnsignedLong(index); |
| 701 | if (PyErr_Occurred()) { |
| 702 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 703 | goto overflow; |
| 704 | goto fail; |
| 705 | } |
| 706 | |
| 707 | gid = (gid_t)uresult; |
| 708 | |
| 709 | /* |
| 710 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 711 | * but this value would get interpreted as (gid_t)-1 by chown |
| 712 | * and its siblings. That's not what the user meant! So we |
| 713 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 714 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 715 | */ |
| 716 | if (gid == (gid_t)-1) |
| 717 | goto overflow; |
| 718 | |
| 719 | /* Ensure the value wasn't truncated. */ |
| 720 | if (sizeof(gid_t) < sizeof(long) && |
| 721 | (unsigned long)gid != uresult) |
| 722 | goto overflow; |
| 723 | /* fallthrough */ |
| 724 | |
| 725 | success: |
| 726 | Py_DECREF(index); |
| 727 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 728 | return 1; |
| 729 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 730 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 731 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 732 | "gid is less than minimum"); |
| 733 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 734 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 735 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 736 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 737 | "gid is greater than maximum"); |
| 738 | /* fallthrough */ |
| 739 | |
| 740 | fail: |
| 741 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 742 | return 0; |
| 743 | } |
| 744 | #endif /* MS_WINDOWS */ |
| 745 | |
| 746 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 747 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 748 | |
| 749 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 750 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 751 | static int |
| 752 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 753 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 754 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 755 | if (PyErr_Occurred()) |
| 756 | return 0; |
| 757 | return 1; |
| 758 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 759 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 760 | |
| 761 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 762 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 763 | /* |
| 764 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 765 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 766 | * which doesn't play nicely with all the initializer lines in this file that |
| 767 | * look like this: |
| 768 | * int dir_fd = DEFAULT_DIR_FD; |
| 769 | */ |
| 770 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 771 | #else |
| 772 | #define DEFAULT_DIR_FD (-100) |
| 773 | #endif |
| 774 | |
| 775 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 776 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 777 | { |
| 778 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 779 | long long_value; |
| 780 | |
| 781 | PyObject *index = PyNumber_Index(o); |
| 782 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 783 | return 0; |
| 784 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 785 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 786 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 787 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 788 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 789 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 790 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 791 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 792 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 793 | return 0; |
| 794 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 795 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 796 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 797 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 798 | return 0; |
| 799 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 800 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 801 | *p = (int)long_value; |
| 802 | return 1; |
| 803 | } |
| 804 | |
| 805 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 806 | dir_fd_converter(PyObject *o, void *p) |
| 807 | { |
| 808 | if (o == Py_None) { |
| 809 | *(int *)p = DEFAULT_DIR_FD; |
| 810 | return 1; |
| 811 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 812 | else if (PyIndex_Check(o)) { |
| 813 | return _fd_converter(o, (int *)p); |
| 814 | } |
| 815 | else { |
| 816 | PyErr_Format(PyExc_TypeError, |
| 817 | "argument should be integer or None, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 818 | _PyType_Name(Py_TYPE(o))); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 819 | return 0; |
| 820 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 823 | typedef struct { |
| 824 | PyObject *billion; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 825 | PyObject *DirEntryType; |
| 826 | PyObject *ScandirIteratorType; |
| 827 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 828 | PyObject *SchedParamType; |
| 829 | #endif |
| 830 | PyObject *StatResultType; |
| 831 | PyObject *StatVFSResultType; |
| 832 | PyObject *TerminalSizeType; |
| 833 | PyObject *TimesResultType; |
| 834 | PyObject *UnameResultType; |
| 835 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 836 | PyObject *WaitidResultType; |
| 837 | #endif |
| 838 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 839 | PyObject *struct_rusage; |
| 840 | #endif |
| 841 | PyObject *st_mode; |
| 842 | } _posixstate; |
| 843 | |
| 844 | static struct PyModuleDef posixmodule; |
| 845 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 846 | static inline _posixstate* |
| 847 | get_posix_state(PyObject *module) |
| 848 | { |
| 849 | void *state = PyModule_GetState(module); |
| 850 | assert(state != NULL); |
| 851 | return (_posixstate *)state; |
| 852 | } |
| 853 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 854 | #define _posixstate_global ((_posixstate *)PyModule_GetState(PyState_FindModule(&posixmodule))) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 855 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 856 | /* |
| 857 | * A PyArg_ParseTuple "converter" function |
| 858 | * that handles filesystem paths in the manner |
| 859 | * preferred by the os module. |
| 860 | * |
| 861 | * path_converter accepts (Unicode) strings and their |
| 862 | * subclasses, and bytes and their subclasses. What |
| 863 | * it does with the argument depends on the platform: |
| 864 | * |
| 865 | * * On Windows, if we get a (Unicode) string we |
| 866 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 867 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 868 | * |
| 869 | * * On all other platforms, strings are encoded |
| 870 | * to bytes using PyUnicode_FSConverter, then we |
| 871 | * extract the char * from the bytes object and |
| 872 | * return that. |
| 873 | * |
| 874 | * path_converter also optionally accepts signed |
| 875 | * integers (representing open file descriptors) instead |
| 876 | * of path strings. |
| 877 | * |
| 878 | * Input fields: |
| 879 | * path.nullable |
| 880 | * If nonzero, the path is permitted to be None. |
| 881 | * path.allow_fd |
| 882 | * If nonzero, the path is permitted to be a file handle |
| 883 | * (a signed int) instead of a string. |
| 884 | * path.function_name |
| 885 | * If non-NULL, path_converter will use that as the name |
| 886 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 887 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 888 | * path.argument_name |
| 889 | * If non-NULL, path_converter will use that as the name |
| 890 | * of the parameter in error messages. |
| 891 | * (If path.argument_name is NULL it uses "path".) |
| 892 | * |
| 893 | * Output fields: |
| 894 | * path.wide |
| 895 | * Points to the path if it was expressed as Unicode |
| 896 | * and was not encoded. (Only used on Windows.) |
| 897 | * path.narrow |
| 898 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 899 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 900 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 901 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 902 | * path.fd |
| 903 | * Contains a file descriptor if path.accept_fd was true |
| 904 | * and the caller provided a signed integer instead of any |
| 905 | * sort of string. |
| 906 | * |
| 907 | * WARNING: if your "path" parameter is optional, and is |
| 908 | * unspecified, path_converter will never get called. |
| 909 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 910 | * yourself! |
| 911 | * path.length |
| 912 | * The length of the path in characters, if specified as |
| 913 | * a string. |
| 914 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 915 | * The original object passed in (if get a PathLike object, |
| 916 | * the result of PyOS_FSPath() is treated as the original object). |
| 917 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 918 | * path.cleanup |
| 919 | * For internal use only. May point to a temporary object. |
| 920 | * (Pay no attention to the man behind the curtain.) |
| 921 | * |
| 922 | * At most one of path.wide or path.narrow will be non-NULL. |
| 923 | * If path was None and path.nullable was set, |
| 924 | * or if path was an integer and path.allow_fd was set, |
| 925 | * both path.wide and path.narrow will be NULL |
| 926 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 927 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 928 | * path_converter takes care to not write to the path_t |
| 929 | * unless it's successful. However it must reset the |
| 930 | * "cleanup" field each time it's called. |
| 931 | * |
| 932 | * Use as follows: |
| 933 | * path_t path; |
| 934 | * memset(&path, 0, sizeof(path)); |
| 935 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 936 | * // ... use values from path ... |
| 937 | * path_cleanup(&path); |
| 938 | * |
| 939 | * (Note that if PyArg_Parse fails you don't need to call |
| 940 | * path_cleanup(). However it is safe to do so.) |
| 941 | */ |
| 942 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 943 | const char *function_name; |
| 944 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 945 | int nullable; |
| 946 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 947 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 948 | #ifdef MS_WINDOWS |
| 949 | BOOL narrow; |
| 950 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 951 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 952 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 953 | int fd; |
| 954 | Py_ssize_t length; |
| 955 | PyObject *object; |
| 956 | PyObject *cleanup; |
| 957 | } path_t; |
| 958 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 959 | #ifdef MS_WINDOWS |
| 960 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 961 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 962 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 963 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 964 | {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] | 965 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 966 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 967 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 968 | path_cleanup(path_t *path) |
| 969 | { |
| 970 | Py_CLEAR(path->object); |
| 971 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 975 | path_converter(PyObject *o, void *p) |
| 976 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 977 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 978 | PyObject *bytes = NULL; |
| 979 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 980 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 981 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 982 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 983 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 984 | const wchar_t *wide; |
| 985 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 986 | |
| 987 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 988 | PyErr_Format(exc, "%s%s" fmt, \ |
| 989 | path->function_name ? path->function_name : "", \ |
| 990 | path->function_name ? ": " : "", \ |
| 991 | path->argument_name ? path->argument_name : "path") |
| 992 | |
| 993 | /* Py_CLEANUP_SUPPORTED support */ |
| 994 | if (o == NULL) { |
| 995 | path_cleanup(path); |
| 996 | return 1; |
| 997 | } |
| 998 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 999 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1000 | path->object = path->cleanup = NULL; |
| 1001 | /* path->object owns a reference to the original object */ |
| 1002 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1003 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1004 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1005 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1006 | #ifdef MS_WINDOWS |
| 1007 | path->narrow = FALSE; |
| 1008 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1009 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1010 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1011 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1012 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1015 | /* Only call this here so that we don't treat the return value of |
| 1016 | os.fspath() as an fd or buffer. */ |
| 1017 | is_index = path->allow_fd && PyIndex_Check(o); |
| 1018 | is_buffer = PyObject_CheckBuffer(o); |
| 1019 | is_bytes = PyBytes_Check(o); |
| 1020 | is_unicode = PyUnicode_Check(o); |
| 1021 | |
| 1022 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 1023 | /* Inline PyOS_FSPath() for better error messages. */ |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1024 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1025 | |
| 1026 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 1027 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1028 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1029 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1030 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1031 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1032 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1033 | goto error_exit; |
| 1034 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1035 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1036 | is_unicode = 1; |
| 1037 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1038 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1039 | is_bytes = 1; |
| 1040 | } |
| 1041 | else { |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1042 | PyErr_Format(PyExc_TypeError, |
| 1043 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1044 | "not %.200s", _PyType_Name(Py_TYPE(o)), |
| 1045 | _PyType_Name(Py_TYPE(res))); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1046 | Py_DECREF(res); |
| 1047 | goto error_exit; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1048 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1049 | |
| 1050 | /* still owns a reference to the original object */ |
| 1051 | Py_DECREF(o); |
| 1052 | o = res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1056 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1057 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1058 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1059 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1060 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1061 | if (length > 32767) { |
| 1062 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1063 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1064 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1065 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1066 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1067 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1068 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1069 | |
| 1070 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1071 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1072 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1073 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1074 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1075 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1076 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1077 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1078 | #endif |
| 1079 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1080 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1081 | bytes = o; |
| 1082 | Py_INCREF(bytes); |
| 1083 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1084 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1085 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1086 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1087 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1088 | "%s%s%s should be %s, not %.200s", |
| 1089 | path->function_name ? path->function_name : "", |
| 1090 | path->function_name ? ": " : "", |
| 1091 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1092 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1093 | "integer or None" : |
| 1094 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1095 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1096 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1097 | _PyType_Name(Py_TYPE(o)))) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1098 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1099 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1100 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1101 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1102 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1103 | } |
| 1104 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1105 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1106 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1107 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1108 | } |
| 1109 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1110 | #ifdef MS_WINDOWS |
| 1111 | path->narrow = FALSE; |
| 1112 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1113 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1114 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1115 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1116 | } |
| 1117 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1118 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1119 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1120 | path->function_name ? path->function_name : "", |
| 1121 | path->function_name ? ": " : "", |
| 1122 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1123 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1124 | "integer or None" : |
| 1125 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1126 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1127 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1128 | _PyType_Name(Py_TYPE(o))); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1129 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1132 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1133 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1134 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1135 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1136 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1139 | #ifdef MS_WINDOWS |
| 1140 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1141 | narrow, |
| 1142 | length |
| 1143 | ); |
| 1144 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1145 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1148 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1149 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1150 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1151 | } |
| 1152 | if (length > 32767) { |
| 1153 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1154 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1155 | } |
| 1156 | if (wcslen(wide) != length) { |
| 1157 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1158 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1159 | } |
| 1160 | path->wide = wide; |
| 1161 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1162 | path->cleanup = wo; |
| 1163 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1164 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1165 | path->wide = NULL; |
| 1166 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1167 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1168 | /* Still a reference owned by path->object, don't have to |
| 1169 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1170 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1171 | } |
| 1172 | else { |
| 1173 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1174 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1175 | #endif |
| 1176 | path->fd = -1; |
| 1177 | |
| 1178 | success_exit: |
| 1179 | path->length = length; |
| 1180 | path->object = o; |
| 1181 | return Py_CLEANUP_SUPPORTED; |
| 1182 | |
| 1183 | error_exit: |
| 1184 | Py_XDECREF(o); |
| 1185 | Py_XDECREF(bytes); |
| 1186 | #ifdef MS_WINDOWS |
| 1187 | Py_XDECREF(wo); |
| 1188 | #endif |
| 1189 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1193 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1194 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1195 | PyErr_Format(PyExc_NotImplementedError, |
| 1196 | "%s%s%s unavailable on this platform", |
| 1197 | (function_name != NULL) ? function_name : "", |
| 1198 | (function_name != NULL) ? ": ": "", |
| 1199 | argument_name); |
| 1200 | } |
| 1201 | |
| 1202 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1203 | dir_fd_unavailable(PyObject *o, void *p) |
| 1204 | { |
| 1205 | int dir_fd; |
| 1206 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1207 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1208 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1209 | argument_unavailable_error(NULL, "dir_fd"); |
| 1210 | return 0; |
| 1211 | } |
| 1212 | *(int *)p = dir_fd; |
| 1213 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1217 | fd_specified(const char *function_name, int fd) |
| 1218 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1219 | if (fd == -1) |
| 1220 | return 0; |
| 1221 | |
| 1222 | argument_unavailable_error(function_name, "fd"); |
| 1223 | return 1; |
| 1224 | } |
| 1225 | |
| 1226 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1227 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1228 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1229 | if (follow_symlinks) |
| 1230 | return 0; |
| 1231 | |
| 1232 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1233 | return 1; |
| 1234 | } |
| 1235 | |
| 1236 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1237 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1238 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1239 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1240 | #ifndef MS_WINDOWS |
| 1241 | && !path->narrow |
| 1242 | #endif |
| 1243 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1244 | PyErr_Format(PyExc_ValueError, |
| 1245 | "%s: can't specify dir_fd without matching path", |
| 1246 | function_name); |
| 1247 | return 1; |
| 1248 | } |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1253 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1254 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1255 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1256 | PyErr_Format(PyExc_ValueError, |
| 1257 | "%s: can't specify both dir_fd and fd", |
| 1258 | function_name); |
| 1259 | return 1; |
| 1260 | } |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1265 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1266 | int follow_symlinks) |
| 1267 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1268 | if ((fd > 0) && (!follow_symlinks)) { |
| 1269 | PyErr_Format(PyExc_ValueError, |
| 1270 | "%s: cannot use fd and follow_symlinks together", |
| 1271 | function_name); |
| 1272 | return 1; |
| 1273 | } |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1278 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1279 | int follow_symlinks) |
| 1280 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1281 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1282 | PyErr_Format(PyExc_ValueError, |
| 1283 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1284 | function_name); |
| 1285 | return 1; |
| 1286 | } |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1290 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1291 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1292 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1293 | typedef off_t Py_off_t; |
| 1294 | #endif |
| 1295 | |
| 1296 | static int |
| 1297 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1298 | { |
| 1299 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1300 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1301 | #else |
| 1302 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1303 | #endif |
| 1304 | if (PyErr_Occurred()) |
| 1305 | return 0; |
| 1306 | return 1; |
| 1307 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1308 | |
| 1309 | static PyObject * |
| 1310 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1311 | { |
| 1312 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1313 | return PyLong_FromLongLong(offset); |
| 1314 | #else |
| 1315 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1316 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1317 | } |
| 1318 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1319 | #ifdef HAVE_SIGSET_T |
| 1320 | /* Convert an iterable of integers to a sigset. |
| 1321 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1322 | int |
| 1323 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1324 | { |
| 1325 | sigset_t *mask = (sigset_t *)addr; |
| 1326 | PyObject *iterator, *item; |
| 1327 | long signum; |
| 1328 | int overflow; |
| 1329 | |
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1330 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
| 1331 | if (sigemptyset(mask) < (0)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1332 | /* Probably only if mask == NULL. */ |
| 1333 | PyErr_SetFromErrno(PyExc_OSError); |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | iterator = PyObject_GetIter(obj); |
| 1338 | if (iterator == NULL) { |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1343 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1344 | Py_DECREF(item); |
| 1345 | if (signum <= 0 || signum >= NSIG) { |
| 1346 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1347 | PyErr_Format(PyExc_ValueError, |
| 1348 | "signal number %ld out of range", signum); |
| 1349 | } |
| 1350 | goto error; |
| 1351 | } |
| 1352 | if (sigaddset(mask, (int)signum)) { |
| 1353 | if (errno != EINVAL) { |
| 1354 | /* Probably impossible */ |
| 1355 | PyErr_SetFromErrno(PyExc_OSError); |
| 1356 | goto error; |
| 1357 | } |
| 1358 | /* For backwards compatibility, allow idioms such as |
| 1359 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1360 | */ |
| 1361 | const char msg[] = |
| 1362 | "invalid signal number %ld, please use valid_signals()"; |
| 1363 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1364 | goto error; |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | if (!PyErr_Occurred()) { |
| 1369 | Py_DECREF(iterator); |
| 1370 | return 1; |
| 1371 | } |
| 1372 | |
| 1373 | error: |
| 1374 | Py_DECREF(iterator); |
| 1375 | return 0; |
| 1376 | } |
| 1377 | #endif /* HAVE_SIGSET_T */ |
| 1378 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1379 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1380 | |
| 1381 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1382 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1383 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1384 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1385 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1386 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1387 | |
| 1388 | if (0 == DeviceIoControl( |
| 1389 | reparse_point_handle, |
| 1390 | FSCTL_GET_REPARSE_POINT, |
| 1391 | NULL, 0, /* in buffer */ |
| 1392 | target_buffer, sizeof(target_buffer), |
| 1393 | &n_bytes_returned, |
| 1394 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1395 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1396 | |
| 1397 | if (reparse_tag) |
| 1398 | *reparse_tag = rdb->ReparseTag; |
| 1399 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1400 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1401 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1402 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1403 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1404 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1405 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1406 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1407 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1408 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1409 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1410 | */ |
| 1411 | #include <crt_externs.h> |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1412 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1413 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1414 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1415 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1416 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1417 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1418 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1419 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1420 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1421 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1422 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1423 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1424 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1425 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1426 | d = PyDict_New(); |
| 1427 | if (d == NULL) |
| 1428 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1429 | #ifdef MS_WINDOWS |
| 1430 | /* _wenviron must be initialized in this way if the program is started |
| 1431 | through main() instead of wmain(). */ |
| 1432 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1433 | e = _wenviron; |
Benoit Hudson | 723f71a | 2019-12-06 14:15:03 -0500 | [diff] [blame] | 1434 | #elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
| 1435 | /* environ is not accessible as an extern in a shared object on OSX; use |
| 1436 | _NSGetEnviron to resolve it. The value changes if you add environment |
| 1437 | variables between calls to Py_Initialize, so don't cache the value. */ |
| 1438 | e = *_NSGetEnviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1439 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1440 | e = environ; |
| 1441 | #endif |
| 1442 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1444 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1445 | PyObject *k; |
| 1446 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1447 | #ifdef MS_WINDOWS |
| 1448 | const wchar_t *p = wcschr(*e, L'='); |
| 1449 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1450 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1451 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1452 | if (p == NULL) |
| 1453 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1454 | #ifdef MS_WINDOWS |
| 1455 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1456 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1457 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1458 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1459 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1460 | Py_DECREF(d); |
| 1461 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1462 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1463 | #ifdef MS_WINDOWS |
| 1464 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1465 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1466 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1467 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1468 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1469 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1470 | Py_DECREF(d); |
| 1471 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1472 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1473 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1474 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1475 | Py_DECREF(v); |
| 1476 | Py_DECREF(k); |
| 1477 | Py_DECREF(d); |
| 1478 | return NULL; |
| 1479 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1480 | } |
| 1481 | Py_DECREF(k); |
| 1482 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1483 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1484 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1487 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1488 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1489 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1490 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1491 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1492 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1493 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1494 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1495 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1496 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1497 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1498 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1499 | /* XXX We should pass the function name along in the future. |
| 1500 | (winreg.c also wants to pass the function name.) |
| 1501 | This would however require an additional param to the |
| 1502 | Windows error object, which is non-trivial. |
| 1503 | */ |
| 1504 | errno = GetLastError(); |
| 1505 | if (filename) |
| 1506 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1507 | else |
| 1508 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1509 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1510 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1511 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1512 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1513 | { |
| 1514 | /* XXX - see win32_error for comments on 'function' */ |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1515 | if (filename) |
| 1516 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1517 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1518 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1519 | filename); |
| 1520 | else |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1521 | return PyErr_SetFromWindowsErr(err); |
| 1522 | } |
| 1523 | |
| 1524 | static PyObject * |
| 1525 | win32_error_object(const char* function, PyObject* filename) |
| 1526 | { |
| 1527 | errno = GetLastError(); |
| 1528 | return win32_error_object_err(function, filename, errno); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1529 | } |
| 1530 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1531 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1532 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1533 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1534 | posix_path_object_error(PyObject *path) |
| 1535 | { |
| 1536 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1537 | } |
| 1538 | |
| 1539 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1540 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1541 | { |
| 1542 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1543 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1544 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1545 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1546 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1547 | #endif |
| 1548 | } |
| 1549 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1550 | static PyObject * |
| 1551 | path_object_error2(PyObject *path, PyObject *path2) |
| 1552 | { |
| 1553 | #ifdef MS_WINDOWS |
| 1554 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1555 | PyExc_OSError, 0, path, path2); |
| 1556 | #else |
| 1557 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1558 | #endif |
| 1559 | } |
| 1560 | |
| 1561 | static PyObject * |
| 1562 | path_error(path_t *path) |
| 1563 | { |
| 1564 | return path_object_error(path->object); |
| 1565 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1566 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1567 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1568 | posix_path_error(path_t *path) |
| 1569 | { |
| 1570 | return posix_path_object_error(path->object); |
| 1571 | } |
| 1572 | |
| 1573 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1574 | path_error2(path_t *path, path_t *path2) |
| 1575 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1576 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1580 | /* POSIX generic methods */ |
| 1581 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1582 | static int |
| 1583 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1584 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1585 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1586 | int *pointer = (int *)p; |
| 1587 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1588 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1589 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1590 | *pointer = fd; |
| 1591 | return 1; |
| 1592 | } |
| 1593 | |
| 1594 | static PyObject * |
| 1595 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1596 | { |
| 1597 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1598 | int async_err = 0; |
| 1599 | |
| 1600 | do { |
| 1601 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1602 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1603 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1604 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1605 | Py_END_ALLOW_THREADS |
| 1606 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1607 | if (res != 0) |
| 1608 | return (!async_err) ? posix_error() : NULL; |
| 1609 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1610 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1611 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1612 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1613 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1614 | /* This is a reimplementation of the C library's chdir function, |
| 1615 | but one that produces Win32 errors instead of DOS error codes. |
| 1616 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1617 | it also needs to set "magic" environment variables indicating |
| 1618 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1619 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1620 | win32_wchdir(LPCWSTR path) |
| 1621 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1622 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1623 | int result; |
| 1624 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1625 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1626 | if(!SetCurrentDirectoryW(path)) |
| 1627 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1628 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1629 | if (!result) |
| 1630 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1631 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1632 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1633 | if (!new_path) { |
| 1634 | SetLastError(ERROR_OUTOFMEMORY); |
| 1635 | return FALSE; |
| 1636 | } |
| 1637 | result = GetCurrentDirectoryW(result, new_path); |
| 1638 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1639 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1640 | return FALSE; |
| 1641 | } |
| 1642 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1643 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1644 | wcsncmp(new_path, L"//", 2) == 0); |
| 1645 | if (!is_unc_like_path) { |
| 1646 | env[1] = new_path[0]; |
| 1647 | result = SetEnvironmentVariableW(env, new_path); |
| 1648 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1649 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1650 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1651 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1652 | } |
| 1653 | #endif |
| 1654 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1655 | #ifdef MS_WINDOWS |
| 1656 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1657 | - time stamps are restricted to second resolution |
| 1658 | - file modification times suffer from forth-and-back conversions between |
| 1659 | UTC and local time |
| 1660 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1661 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1662 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1663 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1664 | #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1665 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1666 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1667 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1668 | BY_HANDLE_FILE_INFORMATION *info, |
| 1669 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1670 | { |
| 1671 | memset(info, 0, sizeof(*info)); |
| 1672 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1673 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1674 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1675 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1676 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1677 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1678 | /* info->nNumberOfLinks = 1; */ |
| 1679 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1680 | *reparse_tag = pFileData->dwReserved0; |
| 1681 | else |
| 1682 | *reparse_tag = 0; |
| 1683 | } |
| 1684 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1685 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1686 | 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] | 1687 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1688 | HANDLE hFindFile; |
| 1689 | WIN32_FIND_DATAW FileData; |
| 1690 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1691 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1692 | return FALSE; |
| 1693 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1694 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1695 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1698 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1699 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1700 | BOOL traverse) |
| 1701 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1702 | HANDLE hFile; |
| 1703 | BY_HANDLE_FILE_INFORMATION fileInfo; |
| 1704 | FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 }; |
| 1705 | DWORD fileType, error; |
| 1706 | BOOL isUnhandledTag = FALSE; |
| 1707 | int retval = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1708 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1709 | DWORD access = FILE_READ_ATTRIBUTES; |
| 1710 | DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */ |
| 1711 | if (!traverse) { |
| 1712 | flags |= FILE_FLAG_OPEN_REPARSE_POINT; |
| 1713 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1714 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1715 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1716 | if (hFile == INVALID_HANDLE_VALUE) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1717 | /* Either the path doesn't exist, or the caller lacks access. */ |
| 1718 | error = GetLastError(); |
| 1719 | switch (error) { |
| 1720 | case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */ |
| 1721 | case ERROR_SHARING_VIOLATION: /* It's a paging file. */ |
| 1722 | /* Try reading the parent directory. */ |
| 1723 | if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) { |
| 1724 | /* Cannot read the parent directory. */ |
| 1725 | SetLastError(error); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1726 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1727 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1728 | if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1729 | if (traverse || |
| 1730 | !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1731 | /* The stat call has to traverse but cannot, so fail. */ |
| 1732 | SetLastError(error); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1733 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1734 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1735 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1736 | break; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1737 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1738 | case ERROR_INVALID_PARAMETER: |
| 1739 | /* \\.\con requires read or write access. */ |
| 1740 | hFile = CreateFileW(path, access | GENERIC_READ, |
| 1741 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1742 | OPEN_EXISTING, flags, NULL); |
| 1743 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1744 | SetLastError(error); |
| 1745 | return -1; |
| 1746 | } |
| 1747 | break; |
| 1748 | |
| 1749 | case ERROR_CANT_ACCESS_FILE: |
| 1750 | /* bpo37834: open unhandled reparse points if traverse fails. */ |
| 1751 | if (traverse) { |
| 1752 | traverse = FALSE; |
| 1753 | isUnhandledTag = TRUE; |
| 1754 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, |
| 1755 | flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 1756 | } |
| 1757 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1758 | SetLastError(error); |
| 1759 | return -1; |
| 1760 | } |
| 1761 | break; |
| 1762 | |
| 1763 | default: |
| 1764 | return -1; |
| 1765 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1766 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1767 | |
| 1768 | if (hFile != INVALID_HANDLE_VALUE) { |
| 1769 | /* Handle types other than files on disk. */ |
| 1770 | fileType = GetFileType(hFile); |
| 1771 | if (fileType != FILE_TYPE_DISK) { |
| 1772 | if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) { |
| 1773 | retval = -1; |
| 1774 | goto cleanup; |
| 1775 | } |
| 1776 | DWORD fileAttributes = GetFileAttributesW(path); |
| 1777 | memset(result, 0, sizeof(*result)); |
| 1778 | if (fileAttributes != INVALID_FILE_ATTRIBUTES && |
| 1779 | fileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 1780 | /* \\.\pipe\ or \\.\mailslot\ */ |
| 1781 | result->st_mode = _S_IFDIR; |
| 1782 | } else if (fileType == FILE_TYPE_CHAR) { |
| 1783 | /* \\.\nul */ |
| 1784 | result->st_mode = _S_IFCHR; |
| 1785 | } else if (fileType == FILE_TYPE_PIPE) { |
| 1786 | /* \\.\pipe\spam */ |
| 1787 | result->st_mode = _S_IFIFO; |
| 1788 | } |
| 1789 | /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */ |
| 1790 | goto cleanup; |
| 1791 | } |
| 1792 | |
| 1793 | /* Query the reparse tag, and traverse a non-link. */ |
| 1794 | if (!traverse) { |
| 1795 | if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, |
| 1796 | &tagInfo, sizeof(tagInfo))) { |
| 1797 | /* Allow devices that do not support FileAttributeTagInfo. */ |
| 1798 | switch (GetLastError()) { |
| 1799 | case ERROR_INVALID_PARAMETER: |
| 1800 | case ERROR_INVALID_FUNCTION: |
| 1801 | case ERROR_NOT_SUPPORTED: |
| 1802 | tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL; |
| 1803 | tagInfo.ReparseTag = 0; |
| 1804 | break; |
| 1805 | default: |
| 1806 | retval = -1; |
| 1807 | goto cleanup; |
| 1808 | } |
| 1809 | } else if (tagInfo.FileAttributes & |
| 1810 | FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1811 | if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1812 | if (isUnhandledTag) { |
| 1813 | /* Traversing previously failed for either this link |
| 1814 | or its target. */ |
| 1815 | SetLastError(ERROR_CANT_ACCESS_FILE); |
| 1816 | retval = -1; |
| 1817 | goto cleanup; |
| 1818 | } |
| 1819 | /* Traverse a non-link, but not if traversing already failed |
| 1820 | for an unhandled tag. */ |
| 1821 | } else if (!isUnhandledTag) { |
| 1822 | CloseHandle(hFile); |
| 1823 | return win32_xstat_impl(path, result, TRUE); |
| 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | if (!GetFileInformationByHandle(hFile, &fileInfo)) { |
| 1829 | switch (GetLastError()) { |
| 1830 | case ERROR_INVALID_PARAMETER: |
| 1831 | case ERROR_INVALID_FUNCTION: |
| 1832 | case ERROR_NOT_SUPPORTED: |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1833 | /* Volumes and physical disks are block devices, e.g. |
| 1834 | \\.\C: and \\.\PhysicalDrive0. */ |
| 1835 | memset(result, 0, sizeof(*result)); |
| 1836 | result->st_mode = 0x6000; /* S_IFBLK */ |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1837 | goto cleanup; |
| 1838 | } |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1839 | retval = -1; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1840 | goto cleanup; |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result); |
| 1845 | |
| 1846 | if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1847 | /* Fix the file execute permissions. This hack sets S_IEXEC if |
| 1848 | the filename has an extension that is commonly used by files |
| 1849 | that CreateProcessW can execute. A real implementation calls |
| 1850 | GetSecurityInfo, OpenThreadToken/OpenProcessToken, and |
| 1851 | AccessCheck to check for generic read, write, and execute |
| 1852 | access. */ |
| 1853 | const wchar_t *fileExtension = wcsrchr(path, '.'); |
| 1854 | if (fileExtension) { |
| 1855 | if (_wcsicmp(fileExtension, L".exe") == 0 || |
| 1856 | _wcsicmp(fileExtension, L".bat") == 0 || |
| 1857 | _wcsicmp(fileExtension, L".cmd") == 0 || |
| 1858 | _wcsicmp(fileExtension, L".com") == 0) { |
| 1859 | result->st_mode |= 0111; |
| 1860 | } |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | cleanup: |
| 1865 | if (hFile != INVALID_HANDLE_VALUE) { |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1866 | /* Preserve last error if we are failing */ |
| 1867 | error = retval ? GetLastError() : 0; |
| 1868 | if (!CloseHandle(hFile)) { |
| 1869 | retval = -1; |
| 1870 | } else if (retval) { |
| 1871 | /* Restore last error */ |
| 1872 | SetLastError(error); |
| 1873 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | return retval; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1880 | 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] | 1881 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1882 | /* Protocol violation: we explicitly clear errno, instead of |
| 1883 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1884 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1885 | errno = 0; |
| 1886 | return code; |
| 1887 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1888 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1889 | |
| 1890 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1891 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1892 | default does not traverse symlinks and instead returns attributes for |
| 1893 | the symlink. |
| 1894 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1895 | Instead, we will open the file (which *does* traverse symlinks by default) |
| 1896 | and GetFileInformationByHandle(). */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1897 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1898 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1899 | 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] | 1900 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1901 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1904 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1905 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1906 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1907 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1910 | #endif /* MS_WINDOWS */ |
| 1911 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1912 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1913 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1914 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1915 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1916 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1917 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1918 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1919 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1920 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1921 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1922 | |
| 1923 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1924 | {"st_mode", "protection bits"}, |
| 1925 | {"st_ino", "inode"}, |
| 1926 | {"st_dev", "device"}, |
| 1927 | {"st_nlink", "number of hard links"}, |
| 1928 | {"st_uid", "user ID of owner"}, |
| 1929 | {"st_gid", "group ID of owner"}, |
| 1930 | {"st_size", "total size, in bytes"}, |
| 1931 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1932 | {NULL, "integer time of last access"}, |
| 1933 | {NULL, "integer time of last modification"}, |
| 1934 | {NULL, "integer time of last change"}, |
| 1935 | {"st_atime", "time of last access"}, |
| 1936 | {"st_mtime", "time of last modification"}, |
| 1937 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1938 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1939 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1940 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1941 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1942 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1943 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1944 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1945 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1946 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1947 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1948 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1949 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1950 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1951 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1952 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1953 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1954 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1955 | #endif |
| 1956 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1957 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1958 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1959 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1960 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1961 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1962 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1963 | {"st_fstype", "Type of filesystem"}, |
| 1964 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1965 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 1966 | {"st_reparse_tag", "Windows reparse tag"}, |
| 1967 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1968 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1969 | }; |
| 1970 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1971 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1972 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1973 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1974 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1975 | #endif |
| 1976 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1977 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1978 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1979 | #else |
| 1980 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1981 | #endif |
| 1982 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1983 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1984 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1985 | #else |
| 1986 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1987 | #endif |
| 1988 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1989 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1990 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1991 | #else |
| 1992 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1993 | #endif |
| 1994 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1995 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1996 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1997 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1998 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1999 | #endif |
| 2000 | |
| 2001 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 2002 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 2003 | #else |
| 2004 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 2005 | #endif |
| 2006 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2007 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2008 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 2009 | #else |
| 2010 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 2011 | #endif |
| 2012 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2013 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2014 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 2015 | #else |
| 2016 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 2017 | #endif |
| 2018 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2019 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2020 | #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1) |
| 2021 | #else |
| 2022 | #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX |
| 2023 | #endif |
| 2024 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2025 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2026 | "stat_result", /* name */ |
| 2027 | stat_result__doc__, /* doc */ |
| 2028 | stat_result_fields, |
| 2029 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2030 | }; |
| 2031 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2032 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2033 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2034 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2035 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2036 | 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] | 2037 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2038 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2039 | |
| 2040 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2041 | {"f_bsize", }, |
| 2042 | {"f_frsize", }, |
| 2043 | {"f_blocks", }, |
| 2044 | {"f_bfree", }, |
| 2045 | {"f_bavail", }, |
| 2046 | {"f_files", }, |
| 2047 | {"f_ffree", }, |
| 2048 | {"f_favail", }, |
| 2049 | {"f_flag", }, |
| 2050 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 2051 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2052 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2053 | }; |
| 2054 | |
| 2055 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2056 | "statvfs_result", /* name */ |
| 2057 | statvfs_result__doc__, /* doc */ |
| 2058 | statvfs_result_fields, |
| 2059 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2060 | }; |
| 2061 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2062 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2063 | PyDoc_STRVAR(waitid_result__doc__, |
| 2064 | "waitid_result: Result from waitid.\n\n\ |
| 2065 | This object may be accessed either as a tuple of\n\ |
| 2066 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2067 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2068 | \n\ |
| 2069 | See os.waitid for more information."); |
| 2070 | |
| 2071 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2072 | {"si_pid", }, |
| 2073 | {"si_uid", }, |
| 2074 | {"si_signo", }, |
| 2075 | {"si_status", }, |
| 2076 | {"si_code", }, |
| 2077 | {0} |
| 2078 | }; |
| 2079 | |
| 2080 | static PyStructSequence_Desc waitid_result_desc = { |
| 2081 | "waitid_result", /* name */ |
| 2082 | waitid_result__doc__, /* doc */ |
| 2083 | waitid_result_fields, |
| 2084 | 5 |
| 2085 | }; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2086 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2087 | static newfunc structseq_new; |
| 2088 | |
| 2089 | static PyObject * |
| 2090 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2091 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2092 | PyStructSequence *result; |
| 2093 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2094 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2095 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2096 | if (!result) |
| 2097 | return NULL; |
| 2098 | /* If we have been initialized from a tuple, |
| 2099 | st_?time might be set to None. Initialize it |
| 2100 | from the int slots. */ |
| 2101 | for (i = 7; i <= 9; i++) { |
| 2102 | if (result->ob_item[i+3] == Py_None) { |
| 2103 | Py_DECREF(Py_None); |
| 2104 | Py_INCREF(result->ob_item[i]); |
| 2105 | result->ob_item[i+3] = result->ob_item[i]; |
| 2106 | } |
| 2107 | } |
| 2108 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2111 | static int |
| 2112 | _posix_clear(PyObject *module) |
| 2113 | { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2114 | Py_CLEAR(get_posix_state(module)->billion); |
| 2115 | Py_CLEAR(get_posix_state(module)->DirEntryType); |
| 2116 | Py_CLEAR(get_posix_state(module)->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2117 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2118 | Py_CLEAR(get_posix_state(module)->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2119 | #endif |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2120 | Py_CLEAR(get_posix_state(module)->StatResultType); |
| 2121 | Py_CLEAR(get_posix_state(module)->StatVFSResultType); |
| 2122 | Py_CLEAR(get_posix_state(module)->TerminalSizeType); |
| 2123 | Py_CLEAR(get_posix_state(module)->TimesResultType); |
| 2124 | Py_CLEAR(get_posix_state(module)->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2125 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2126 | Py_CLEAR(get_posix_state(module)->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2127 | #endif |
| 2128 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2129 | Py_CLEAR(get_posix_state(module)->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2130 | #endif |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2131 | Py_CLEAR(get_posix_state(module)->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2132 | return 0; |
| 2133 | } |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2134 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2135 | static int |
| 2136 | _posix_traverse(PyObject *module, visitproc visit, void *arg) |
| 2137 | { |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2138 | Py_VISIT(get_posix_state(module)->billion); |
| 2139 | Py_VISIT(get_posix_state(module)->DirEntryType); |
| 2140 | Py_VISIT(get_posix_state(module)->ScandirIteratorType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2141 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2142 | Py_VISIT(get_posix_state(module)->SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2143 | #endif |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2144 | Py_VISIT(get_posix_state(module)->StatResultType); |
| 2145 | Py_VISIT(get_posix_state(module)->StatVFSResultType); |
| 2146 | Py_VISIT(get_posix_state(module)->TerminalSizeType); |
| 2147 | Py_VISIT(get_posix_state(module)->TimesResultType); |
| 2148 | Py_VISIT(get_posix_state(module)->UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2149 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2150 | Py_VISIT(get_posix_state(module)->WaitidResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2151 | #endif |
| 2152 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2153 | Py_VISIT(get_posix_state(module)->struct_rusage); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2154 | #endif |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 2155 | Py_VISIT(get_posix_state(module)->st_mode); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2156 | return 0; |
| 2157 | } |
| 2158 | |
| 2159 | static void |
| 2160 | _posix_free(void *module) |
| 2161 | { |
| 2162 | _posix_clear((PyObject *)module); |
| 2163 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2164 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2165 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2166 | fill_time(PyObject *v, int index, time_t sec, unsigned long nsec) |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2167 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2168 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2169 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2170 | PyObject *s_in_ns = NULL; |
| 2171 | PyObject *ns_total = NULL; |
| 2172 | PyObject *float_s = NULL; |
| 2173 | |
| 2174 | if (!(s && ns_fractional)) |
| 2175 | goto exit; |
| 2176 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2177 | s_in_ns = PyNumber_Multiply(s, _posixstate_global->billion); |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2178 | if (!s_in_ns) |
| 2179 | goto exit; |
| 2180 | |
| 2181 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2182 | if (!ns_total) |
| 2183 | goto exit; |
| 2184 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2185 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2186 | if (!float_s) { |
| 2187 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | PyStructSequence_SET_ITEM(v, index, s); |
| 2191 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2192 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2193 | s = NULL; |
| 2194 | float_s = NULL; |
| 2195 | ns_total = NULL; |
| 2196 | exit: |
| 2197 | Py_XDECREF(s); |
| 2198 | Py_XDECREF(ns_fractional); |
| 2199 | Py_XDECREF(s_in_ns); |
| 2200 | Py_XDECREF(ns_total); |
| 2201 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2204 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2205 | (used by posix_stat() and posix_fstat()) */ |
| 2206 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2207 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2208 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2209 | unsigned long ansec, mnsec, cnsec; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2210 | PyObject *StatResultType = _posixstate_global->StatResultType; |
| 2211 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2212 | if (v == NULL) |
| 2213 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2214 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2215 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2216 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2217 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2218 | #ifdef MS_WINDOWS |
| 2219 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2220 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2221 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2222 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2223 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2224 | #if defined(MS_WINDOWS) |
| 2225 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2226 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2227 | #else |
| 2228 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2229 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2230 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2231 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2232 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2233 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2234 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2235 | ansec = st->st_atim.tv_nsec; |
| 2236 | mnsec = st->st_mtim.tv_nsec; |
| 2237 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2238 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2239 | ansec = st->st_atimespec.tv_nsec; |
| 2240 | mnsec = st->st_mtimespec.tv_nsec; |
| 2241 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2242 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2243 | ansec = st->st_atime_nsec; |
| 2244 | mnsec = st->st_mtime_nsec; |
| 2245 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2246 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2247 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2248 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2249 | fill_time(v, 7, st->st_atime, ansec); |
| 2250 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2251 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2252 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2253 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2254 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2255 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2256 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2257 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2258 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2259 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2260 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2261 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2262 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2263 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2264 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2265 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2266 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2267 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2268 | #endif |
| 2269 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2270 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2271 | PyObject *val; |
| 2272 | unsigned long bsec,bnsec; |
| 2273 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2274 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2275 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2276 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2277 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2278 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2279 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2280 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2281 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2282 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2283 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2284 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2285 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2286 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2287 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2288 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2289 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2290 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2291 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2292 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2293 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2294 | PyUnicode_FromString(st->st_fstype)); |
| 2295 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2296 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2297 | PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX, |
| 2298 | PyLong_FromUnsignedLong(st->st_reparse_tag)); |
| 2299 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2300 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2301 | if (PyErr_Occurred()) { |
| 2302 | Py_DECREF(v); |
| 2303 | return NULL; |
| 2304 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2305 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2306 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2309 | /* POSIX methods */ |
| 2310 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2311 | |
| 2312 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2313 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2314 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2315 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2316 | STRUCT_STAT st; |
| 2317 | int result; |
| 2318 | |
| 2319 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2320 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2321 | return NULL; |
| 2322 | #endif |
| 2323 | |
| 2324 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2325 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2326 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2327 | return NULL; |
| 2328 | |
| 2329 | Py_BEGIN_ALLOW_THREADS |
| 2330 | if (path->fd != -1) |
| 2331 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2332 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2333 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2334 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2335 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2336 | result = win32_lstat(path->wide, &st); |
| 2337 | #else |
| 2338 | else |
| 2339 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2340 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2341 | result = LSTAT(path->narrow, &st); |
| 2342 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2343 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2344 | #ifdef HAVE_FSTATAT |
| 2345 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2346 | result = fstatat(dir_fd, path->narrow, &st, |
| 2347 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2348 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2349 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2350 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2351 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2352 | Py_END_ALLOW_THREADS |
| 2353 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2354 | if (result != 0) { |
| 2355 | return path_error(path); |
| 2356 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2357 | |
| 2358 | return _pystat_fromstructstat(&st); |
| 2359 | } |
| 2360 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2361 | /*[python input] |
| 2362 | |
| 2363 | for s in """ |
| 2364 | |
| 2365 | FACCESSAT |
| 2366 | FCHMODAT |
| 2367 | FCHOWNAT |
| 2368 | FSTATAT |
| 2369 | LINKAT |
| 2370 | MKDIRAT |
| 2371 | MKFIFOAT |
| 2372 | MKNODAT |
| 2373 | OPENAT |
| 2374 | READLINKAT |
| 2375 | SYMLINKAT |
| 2376 | UNLINKAT |
| 2377 | |
| 2378 | """.strip().split(): |
| 2379 | s = s.strip() |
| 2380 | print(""" |
| 2381 | #ifdef HAVE_{s} |
| 2382 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2383 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2384 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2385 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2386 | """.rstrip().format(s=s)) |
| 2387 | |
| 2388 | for s in """ |
| 2389 | |
| 2390 | FCHDIR |
| 2391 | FCHMOD |
| 2392 | FCHOWN |
| 2393 | FDOPENDIR |
| 2394 | FEXECVE |
| 2395 | FPATHCONF |
| 2396 | FSTATVFS |
| 2397 | FTRUNCATE |
| 2398 | |
| 2399 | """.strip().split(): |
| 2400 | s = s.strip() |
| 2401 | print(""" |
| 2402 | #ifdef HAVE_{s} |
| 2403 | #define PATH_HAVE_{s} 1 |
| 2404 | #else |
| 2405 | #define PATH_HAVE_{s} 0 |
| 2406 | #endif |
| 2407 | |
| 2408 | """.rstrip().format(s=s)) |
| 2409 | [python start generated code]*/ |
| 2410 | |
| 2411 | #ifdef HAVE_FACCESSAT |
| 2412 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2413 | #else |
| 2414 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2415 | #endif |
| 2416 | |
| 2417 | #ifdef HAVE_FCHMODAT |
| 2418 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2419 | #else |
| 2420 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2421 | #endif |
| 2422 | |
| 2423 | #ifdef HAVE_FCHOWNAT |
| 2424 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2425 | #else |
| 2426 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2427 | #endif |
| 2428 | |
| 2429 | #ifdef HAVE_FSTATAT |
| 2430 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2431 | #else |
| 2432 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2433 | #endif |
| 2434 | |
| 2435 | #ifdef HAVE_LINKAT |
| 2436 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2437 | #else |
| 2438 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2439 | #endif |
| 2440 | |
| 2441 | #ifdef HAVE_MKDIRAT |
| 2442 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2443 | #else |
| 2444 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2445 | #endif |
| 2446 | |
| 2447 | #ifdef HAVE_MKFIFOAT |
| 2448 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2449 | #else |
| 2450 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2451 | #endif |
| 2452 | |
| 2453 | #ifdef HAVE_MKNODAT |
| 2454 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2455 | #else |
| 2456 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2457 | #endif |
| 2458 | |
| 2459 | #ifdef HAVE_OPENAT |
| 2460 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2461 | #else |
| 2462 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2463 | #endif |
| 2464 | |
| 2465 | #ifdef HAVE_READLINKAT |
| 2466 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2467 | #else |
| 2468 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2469 | #endif |
| 2470 | |
| 2471 | #ifdef HAVE_SYMLINKAT |
| 2472 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2473 | #else |
| 2474 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2475 | #endif |
| 2476 | |
| 2477 | #ifdef HAVE_UNLINKAT |
| 2478 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2479 | #else |
| 2480 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2481 | #endif |
| 2482 | |
| 2483 | #ifdef HAVE_FCHDIR |
| 2484 | #define PATH_HAVE_FCHDIR 1 |
| 2485 | #else |
| 2486 | #define PATH_HAVE_FCHDIR 0 |
| 2487 | #endif |
| 2488 | |
| 2489 | #ifdef HAVE_FCHMOD |
| 2490 | #define PATH_HAVE_FCHMOD 1 |
| 2491 | #else |
| 2492 | #define PATH_HAVE_FCHMOD 0 |
| 2493 | #endif |
| 2494 | |
| 2495 | #ifdef HAVE_FCHOWN |
| 2496 | #define PATH_HAVE_FCHOWN 1 |
| 2497 | #else |
| 2498 | #define PATH_HAVE_FCHOWN 0 |
| 2499 | #endif |
| 2500 | |
| 2501 | #ifdef HAVE_FDOPENDIR |
| 2502 | #define PATH_HAVE_FDOPENDIR 1 |
| 2503 | #else |
| 2504 | #define PATH_HAVE_FDOPENDIR 0 |
| 2505 | #endif |
| 2506 | |
| 2507 | #ifdef HAVE_FEXECVE |
| 2508 | #define PATH_HAVE_FEXECVE 1 |
| 2509 | #else |
| 2510 | #define PATH_HAVE_FEXECVE 0 |
| 2511 | #endif |
| 2512 | |
| 2513 | #ifdef HAVE_FPATHCONF |
| 2514 | #define PATH_HAVE_FPATHCONF 1 |
| 2515 | #else |
| 2516 | #define PATH_HAVE_FPATHCONF 0 |
| 2517 | #endif |
| 2518 | |
| 2519 | #ifdef HAVE_FSTATVFS |
| 2520 | #define PATH_HAVE_FSTATVFS 1 |
| 2521 | #else |
| 2522 | #define PATH_HAVE_FSTATVFS 0 |
| 2523 | #endif |
| 2524 | |
| 2525 | #ifdef HAVE_FTRUNCATE |
| 2526 | #define PATH_HAVE_FTRUNCATE 1 |
| 2527 | #else |
| 2528 | #define PATH_HAVE_FTRUNCATE 0 |
| 2529 | #endif |
| 2530 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2531 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2532 | #ifdef MS_WINDOWS |
| 2533 | #undef PATH_HAVE_FTRUNCATE |
| 2534 | #define PATH_HAVE_FTRUNCATE 1 |
| 2535 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2536 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2537 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2538 | |
| 2539 | class path_t_converter(CConverter): |
| 2540 | |
| 2541 | type = "path_t" |
| 2542 | impl_by_reference = True |
| 2543 | parse_by_reference = True |
| 2544 | |
| 2545 | converter = 'path_converter' |
| 2546 | |
| 2547 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2548 | # right now path_t doesn't support default values. |
| 2549 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2550 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2551 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2552 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2553 | if self.c_default not in (None, 'Py_None'): |
| 2554 | 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] | 2555 | |
| 2556 | self.nullable = nullable |
| 2557 | self.allow_fd = allow_fd |
| 2558 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2559 | def pre_render(self): |
| 2560 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2561 | if isinstance(value, str): |
| 2562 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2563 | return str(int(bool(value))) |
| 2564 | |
| 2565 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2566 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2567 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2568 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2569 | strify(self.nullable), |
| 2570 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2571 | ) |
| 2572 | |
| 2573 | def cleanup(self): |
| 2574 | return "path_cleanup(&" + self.name + ");\n" |
| 2575 | |
| 2576 | |
| 2577 | class dir_fd_converter(CConverter): |
| 2578 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2579 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2580 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2581 | if self.default in (unspecified, None): |
| 2582 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2583 | if isinstance(requires, str): |
| 2584 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2585 | else: |
| 2586 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2587 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2588 | class fildes_converter(CConverter): |
| 2589 | type = 'int' |
| 2590 | converter = 'fildes_converter' |
| 2591 | |
| 2592 | class uid_t_converter(CConverter): |
| 2593 | type = "uid_t" |
| 2594 | converter = '_Py_Uid_Converter' |
| 2595 | |
| 2596 | class gid_t_converter(CConverter): |
| 2597 | type = "gid_t" |
| 2598 | converter = '_Py_Gid_Converter' |
| 2599 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2600 | class dev_t_converter(CConverter): |
| 2601 | type = 'dev_t' |
| 2602 | converter = '_Py_Dev_Converter' |
| 2603 | |
| 2604 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2605 | type = 'dev_t' |
| 2606 | conversion_fn = '_PyLong_FromDev' |
| 2607 | unsigned_cast = '(dev_t)' |
| 2608 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2609 | class FSConverter_converter(CConverter): |
| 2610 | type = 'PyObject *' |
| 2611 | converter = 'PyUnicode_FSConverter' |
| 2612 | def converter_init(self): |
| 2613 | if self.default is not unspecified: |
| 2614 | fail("FSConverter_converter does not support default values") |
| 2615 | self.c_default = 'NULL' |
| 2616 | |
| 2617 | def cleanup(self): |
| 2618 | return "Py_XDECREF(" + self.name + ");\n" |
| 2619 | |
| 2620 | class pid_t_converter(CConverter): |
| 2621 | type = 'pid_t' |
| 2622 | format_unit = '" _Py_PARSE_PID "' |
| 2623 | |
| 2624 | class idtype_t_converter(int_converter): |
| 2625 | type = 'idtype_t' |
| 2626 | |
| 2627 | class id_t_converter(CConverter): |
| 2628 | type = 'id_t' |
| 2629 | format_unit = '" _Py_PARSE_PID "' |
| 2630 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2631 | class intptr_t_converter(CConverter): |
| 2632 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2633 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2634 | |
| 2635 | class Py_off_t_converter(CConverter): |
| 2636 | type = 'Py_off_t' |
| 2637 | converter = 'Py_off_t_converter' |
| 2638 | |
| 2639 | class Py_off_t_return_converter(long_return_converter): |
| 2640 | type = 'Py_off_t' |
| 2641 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2642 | |
| 2643 | class path_confname_converter(CConverter): |
| 2644 | type="int" |
| 2645 | converter="conv_path_confname" |
| 2646 | |
| 2647 | class confstr_confname_converter(path_confname_converter): |
| 2648 | converter='conv_confstr_confname' |
| 2649 | |
| 2650 | class sysconf_confname_converter(path_confname_converter): |
| 2651 | converter="conv_sysconf_confname" |
| 2652 | |
| 2653 | class sched_param_converter(CConverter): |
| 2654 | type = 'struct sched_param' |
| 2655 | converter = 'convert_sched_param' |
| 2656 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2657 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2658 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2659 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2660 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2661 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2662 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2663 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2664 | |
| 2665 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2666 | 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] | 2667 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2668 | |
| 2669 | * |
| 2670 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2671 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2672 | If not None, it should be a file descriptor open to a directory, |
| 2673 | and path should be a relative string; path will then be relative to |
| 2674 | that directory. |
| 2675 | |
| 2676 | follow_symlinks: bool = True |
| 2677 | If False, and the last element of the path is a symbolic link, |
| 2678 | stat will examine the symbolic link itself instead of the file |
| 2679 | the link points to. |
| 2680 | |
| 2681 | Perform a stat system call on the given path. |
| 2682 | |
| 2683 | dir_fd and follow_symlinks may not be implemented |
| 2684 | on your platform. If they are unavailable, using them will raise a |
| 2685 | NotImplementedError. |
| 2686 | |
| 2687 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2688 | an open file descriptor. |
| 2689 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2690 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2691 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2692 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2693 | 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] | 2694 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2695 | { |
| 2696 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2697 | } |
| 2698 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2699 | |
| 2700 | /*[clinic input] |
| 2701 | os.lstat |
| 2702 | |
| 2703 | path : path_t |
| 2704 | |
| 2705 | * |
| 2706 | |
| 2707 | dir_fd : dir_fd(requires='fstatat') = None |
| 2708 | |
| 2709 | Perform a stat system call on the given path, without following symbolic links. |
| 2710 | |
| 2711 | Like stat(), but do not follow symbolic links. |
| 2712 | Equivalent to stat(path, follow_symlinks=False). |
| 2713 | [clinic start generated code]*/ |
| 2714 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2715 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2716 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2717 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2718 | { |
| 2719 | int follow_symlinks = 0; |
| 2720 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2721 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2722 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2723 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2724 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2725 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2726 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2727 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2728 | 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] | 2729 | |
| 2730 | mode: int |
| 2731 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2732 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2733 | |
| 2734 | * |
| 2735 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2736 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2737 | If not None, it should be a file descriptor open to a directory, |
| 2738 | and path should be relative; path will then be relative to that |
| 2739 | directory. |
| 2740 | |
| 2741 | effective_ids: bool = False |
| 2742 | If True, access will use the effective uid/gid instead of |
| 2743 | the real uid/gid. |
| 2744 | |
| 2745 | follow_symlinks: bool = True |
| 2746 | If False, and the last element of the path is a symbolic link, |
| 2747 | access will examine the symbolic link itself instead of the file |
| 2748 | the link points to. |
| 2749 | |
| 2750 | Use the real uid/gid to test for access to a path. |
| 2751 | |
| 2752 | {parameters} |
| 2753 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2754 | on your platform. If they are unavailable, using them will raise a |
| 2755 | NotImplementedError. |
| 2756 | |
| 2757 | Note that most operations will use the effective uid/gid, therefore this |
| 2758 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2759 | has the specified access to the path. |
| 2760 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2761 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2762 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2763 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2764 | 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] | 2765 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2766 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2767 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2768 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2769 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2770 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2771 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2772 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2773 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2774 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2775 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2776 | #ifndef HAVE_FACCESSAT |
| 2777 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2778 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2779 | |
| 2780 | if (effective_ids) { |
| 2781 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2782 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2783 | } |
| 2784 | #endif |
| 2785 | |
| 2786 | #ifdef MS_WINDOWS |
| 2787 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2788 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2789 | Py_END_ALLOW_THREADS |
| 2790 | |
| 2791 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2792 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2793 | * * we didn't get a -1, and |
| 2794 | * * write access wasn't requested, |
| 2795 | * * or the file isn't read-only, |
| 2796 | * * or it's a directory. |
| 2797 | * (Directories cannot be read-only on Windows.) |
| 2798 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2799 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2800 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2801 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2802 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2803 | #else |
| 2804 | |
| 2805 | Py_BEGIN_ALLOW_THREADS |
| 2806 | #ifdef HAVE_FACCESSAT |
| 2807 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2808 | effective_ids || |
| 2809 | !follow_symlinks) { |
| 2810 | int flags = 0; |
| 2811 | if (!follow_symlinks) |
| 2812 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2813 | if (effective_ids) |
| 2814 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2815 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2816 | } |
| 2817 | else |
| 2818 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2819 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2820 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2821 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2822 | #endif |
| 2823 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2824 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2827 | #ifndef F_OK |
| 2828 | #define F_OK 0 |
| 2829 | #endif |
| 2830 | #ifndef R_OK |
| 2831 | #define R_OK 4 |
| 2832 | #endif |
| 2833 | #ifndef W_OK |
| 2834 | #define W_OK 2 |
| 2835 | #endif |
| 2836 | #ifndef X_OK |
| 2837 | #define X_OK 1 |
| 2838 | #endif |
| 2839 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2840 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2841 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2842 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2843 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2844 | |
| 2845 | fd: int |
| 2846 | Integer file descriptor handle. |
| 2847 | |
| 2848 | / |
| 2849 | |
| 2850 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2851 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2852 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2853 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2854 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2855 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2856 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2857 | |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2858 | long size = sysconf(_SC_TTY_NAME_MAX); |
| 2859 | if (size == -1) { |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2860 | return posix_error(); |
| 2861 | } |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2862 | char *buffer = (char *)PyMem_RawMalloc(size); |
| 2863 | if (buffer == NULL) { |
| 2864 | return PyErr_NoMemory(); |
| 2865 | } |
| 2866 | int ret = ttyname_r(fd, buffer, size); |
| 2867 | if (ret != 0) { |
| 2868 | PyMem_RawFree(buffer); |
| 2869 | errno = ret; |
| 2870 | return posix_error(); |
| 2871 | } |
| 2872 | PyObject *res = PyUnicode_DecodeFSDefault(buffer); |
| 2873 | PyMem_RawFree(buffer); |
| 2874 | return res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2875 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2876 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2877 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2878 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2879 | /*[clinic input] |
| 2880 | os.ctermid |
| 2881 | |
| 2882 | Return the name of the controlling terminal for this process. |
| 2883 | [clinic start generated code]*/ |
| 2884 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2885 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2886 | os_ctermid_impl(PyObject *module) |
| 2887 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2888 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2889 | char *ret; |
| 2890 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2891 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2892 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2893 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2894 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2895 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2896 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2897 | if (ret == NULL) |
| 2898 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2899 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2900 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2901 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2902 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2903 | |
| 2904 | /*[clinic input] |
| 2905 | os.chdir |
| 2906 | |
| 2907 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2908 | |
| 2909 | Change the current working directory to the specified path. |
| 2910 | |
| 2911 | path may always be specified as a string. |
| 2912 | On some platforms, path may also be specified as an open file descriptor. |
| 2913 | If this functionality is unavailable, using it raises an exception. |
| 2914 | [clinic start generated code]*/ |
| 2915 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2916 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2917 | os_chdir_impl(PyObject *module, path_t *path) |
| 2918 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2919 | { |
| 2920 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2921 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 2922 | if (PySys_Audit("os.chdir", "(O)", path->object) < 0) { |
| 2923 | return NULL; |
| 2924 | } |
| 2925 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2926 | Py_BEGIN_ALLOW_THREADS |
| 2927 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2928 | /* on unix, success = 0, on windows, success = !0 */ |
| 2929 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2930 | #else |
| 2931 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2932 | if (path->fd != -1) |
| 2933 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2934 | else |
| 2935 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2936 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2937 | #endif |
| 2938 | Py_END_ALLOW_THREADS |
| 2939 | |
| 2940 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2941 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2942 | } |
| 2943 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2944 | Py_RETURN_NONE; |
| 2945 | } |
| 2946 | |
| 2947 | |
| 2948 | #ifdef HAVE_FCHDIR |
| 2949 | /*[clinic input] |
| 2950 | os.fchdir |
| 2951 | |
| 2952 | fd: fildes |
| 2953 | |
| 2954 | Change to the directory of the given file descriptor. |
| 2955 | |
| 2956 | fd must be opened on a directory, not a file. |
| 2957 | Equivalent to os.chdir(fd). |
| 2958 | |
| 2959 | [clinic start generated code]*/ |
| 2960 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2961 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2962 | os_fchdir_impl(PyObject *module, int fd) |
| 2963 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2964 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 2965 | if (PySys_Audit("os.chdir", "(i)", fd) < 0) { |
| 2966 | return NULL; |
| 2967 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2968 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2969 | } |
| 2970 | #endif /* HAVE_FCHDIR */ |
| 2971 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2972 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2973 | /*[clinic input] |
| 2974 | os.chmod |
| 2975 | |
| 2976 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2977 | 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] | 2978 | On some platforms, path may also be specified as an open file descriptor. |
| 2979 | If this functionality is unavailable, using it raises an exception. |
| 2980 | |
| 2981 | mode: int |
| 2982 | Operating-system mode bitfield. |
| 2983 | |
| 2984 | * |
| 2985 | |
| 2986 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2987 | If not None, it should be a file descriptor open to a directory, |
| 2988 | and path should be relative; path will then be relative to that |
| 2989 | directory. |
| 2990 | |
| 2991 | follow_symlinks: bool = True |
| 2992 | If False, and the last element of the path is a symbolic link, |
| 2993 | chmod will modify the symbolic link itself instead of the file |
| 2994 | the link points to. |
| 2995 | |
| 2996 | Change the access permissions of a file. |
| 2997 | |
| 2998 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2999 | an open file descriptor. |
| 3000 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3001 | If they are unavailable, using them will raise a NotImplementedError. |
| 3002 | |
| 3003 | [clinic start generated code]*/ |
| 3004 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3005 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3006 | 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] | 3007 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3008 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3009 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3010 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3011 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3012 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3013 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3014 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3015 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3016 | #ifdef HAVE_FCHMODAT |
| 3017 | int fchmodat_nofollow_unsupported = 0; |
| 3018 | #endif |
| 3019 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3020 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 3021 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3022 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3023 | #endif |
| 3024 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3025 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, |
| 3026 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3027 | return NULL; |
| 3028 | } |
| 3029 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3030 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3031 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3032 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 3033 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3034 | result = 0; |
| 3035 | else { |
| 3036 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3037 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 3038 | else |
| 3039 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3040 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3041 | } |
| 3042 | Py_END_ALLOW_THREADS |
| 3043 | |
| 3044 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3045 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3046 | } |
| 3047 | #else /* MS_WINDOWS */ |
| 3048 | Py_BEGIN_ALLOW_THREADS |
| 3049 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3050 | if (path->fd != -1) |
| 3051 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3052 | else |
| 3053 | #endif |
| 3054 | #ifdef HAVE_LCHMOD |
| 3055 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3056 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3057 | else |
| 3058 | #endif |
| 3059 | #ifdef HAVE_FCHMODAT |
| 3060 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 3061 | /* |
| 3062 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 3063 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3064 | * and then says it isn't implemented yet. |
| 3065 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3066 | * |
| 3067 | * Once it is supported, os.chmod will automatically |
| 3068 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 3069 | * Until then, we need to be careful what exception we raise. |
| 3070 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3071 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3072 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3073 | /* |
| 3074 | * But wait! We can't throw the exception without allowing threads, |
| 3075 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 3076 | */ |
| 3077 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3078 | result && |
| 3079 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 3080 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3081 | } |
| 3082 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3083 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3084 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3085 | Py_END_ALLOW_THREADS |
| 3086 | |
| 3087 | if (result) { |
| 3088 | #ifdef HAVE_FCHMODAT |
| 3089 | if (fchmodat_nofollow_unsupported) { |
| 3090 | if (dir_fd != DEFAULT_DIR_FD) |
| 3091 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3092 | dir_fd, follow_symlinks); |
| 3093 | else |
| 3094 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 3095 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3096 | } |
| 3097 | else |
| 3098 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3099 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3100 | } |
| 3101 | #endif |
| 3102 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3103 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3106 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3107 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3108 | /*[clinic input] |
| 3109 | os.fchmod |
| 3110 | |
| 3111 | fd: int |
| 3112 | mode: int |
| 3113 | |
| 3114 | Change the access permissions of the file given by file descriptor fd. |
| 3115 | |
| 3116 | Equivalent to os.chmod(fd, mode). |
| 3117 | [clinic start generated code]*/ |
| 3118 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3119 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3120 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 3121 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3122 | { |
| 3123 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3124 | int async_err = 0; |
| 3125 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3126 | if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) { |
| 3127 | return NULL; |
| 3128 | } |
| 3129 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3130 | do { |
| 3131 | Py_BEGIN_ALLOW_THREADS |
| 3132 | res = fchmod(fd, mode); |
| 3133 | Py_END_ALLOW_THREADS |
| 3134 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3135 | if (res != 0) |
| 3136 | return (!async_err) ? posix_error() : NULL; |
| 3137 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3138 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3139 | } |
| 3140 | #endif /* HAVE_FCHMOD */ |
| 3141 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3142 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3143 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3144 | /*[clinic input] |
| 3145 | os.lchmod |
| 3146 | |
| 3147 | path: path_t |
| 3148 | mode: int |
| 3149 | |
| 3150 | Change the access permissions of a file, without following symbolic links. |
| 3151 | |
| 3152 | If path is a symlink, this affects the link itself rather than the target. |
| 3153 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 3154 | [clinic start generated code]*/ |
| 3155 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3156 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3157 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 3158 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3159 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3160 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3161 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) { |
| 3162 | return NULL; |
| 3163 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3164 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3165 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3166 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3167 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3168 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3169 | return NULL; |
| 3170 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3171 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3172 | } |
| 3173 | #endif /* HAVE_LCHMOD */ |
| 3174 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3175 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3176 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3177 | /*[clinic input] |
| 3178 | os.chflags |
| 3179 | |
| 3180 | path: path_t |
| 3181 | flags: unsigned_long(bitwise=True) |
| 3182 | follow_symlinks: bool=True |
| 3183 | |
| 3184 | Set file flags. |
| 3185 | |
| 3186 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3187 | link, chflags will change flags on the symbolic link itself instead of the |
| 3188 | file the link points to. |
| 3189 | follow_symlinks may not be implemented on your platform. If it is |
| 3190 | unavailable, using it will raise a NotImplementedError. |
| 3191 | |
| 3192 | [clinic start generated code]*/ |
| 3193 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3194 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3195 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3196 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3197 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3198 | { |
| 3199 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3200 | |
| 3201 | #ifndef HAVE_LCHFLAGS |
| 3202 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3203 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3204 | #endif |
| 3205 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3206 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3207 | return NULL; |
| 3208 | } |
| 3209 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3210 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3211 | #ifdef HAVE_LCHFLAGS |
| 3212 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3213 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3214 | else |
| 3215 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3216 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3217 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3218 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3219 | if (result) |
| 3220 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3222 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3223 | } |
| 3224 | #endif /* HAVE_CHFLAGS */ |
| 3225 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3226 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3227 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3228 | /*[clinic input] |
| 3229 | os.lchflags |
| 3230 | |
| 3231 | path: path_t |
| 3232 | flags: unsigned_long(bitwise=True) |
| 3233 | |
| 3234 | Set file flags. |
| 3235 | |
| 3236 | This function will not follow symbolic links. |
| 3237 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3238 | [clinic start generated code]*/ |
| 3239 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3240 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3241 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3242 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3243 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3244 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3245 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
| 3246 | return NULL; |
| 3247 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3248 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3249 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3250 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3251 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3252 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3253 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3254 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3255 | } |
| 3256 | #endif /* HAVE_LCHFLAGS */ |
| 3257 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3258 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3259 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3260 | /*[clinic input] |
| 3261 | os.chroot |
| 3262 | path: path_t |
| 3263 | |
| 3264 | Change root directory to path. |
| 3265 | |
| 3266 | [clinic start generated code]*/ |
| 3267 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3268 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3269 | os_chroot_impl(PyObject *module, path_t *path) |
| 3270 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3271 | { |
| 3272 | int res; |
| 3273 | Py_BEGIN_ALLOW_THREADS |
| 3274 | res = chroot(path->narrow); |
| 3275 | Py_END_ALLOW_THREADS |
| 3276 | if (res < 0) |
| 3277 | return path_error(path); |
| 3278 | Py_RETURN_NONE; |
| 3279 | } |
| 3280 | #endif /* HAVE_CHROOT */ |
| 3281 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3282 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3283 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3284 | /*[clinic input] |
| 3285 | os.fsync |
| 3286 | |
| 3287 | fd: fildes |
| 3288 | |
| 3289 | Force write of fd to disk. |
| 3290 | [clinic start generated code]*/ |
| 3291 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3292 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3293 | os_fsync_impl(PyObject *module, int fd) |
| 3294 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3295 | { |
| 3296 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3297 | } |
| 3298 | #endif /* HAVE_FSYNC */ |
| 3299 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3300 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3301 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3302 | /*[clinic input] |
| 3303 | os.sync |
| 3304 | |
| 3305 | Force write of everything to disk. |
| 3306 | [clinic start generated code]*/ |
| 3307 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3308 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3309 | os_sync_impl(PyObject *module) |
| 3310 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3311 | { |
| 3312 | Py_BEGIN_ALLOW_THREADS |
| 3313 | sync(); |
| 3314 | Py_END_ALLOW_THREADS |
| 3315 | Py_RETURN_NONE; |
| 3316 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3317 | #endif /* HAVE_SYNC */ |
| 3318 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3319 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3320 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3321 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3322 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3323 | #endif |
| 3324 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3325 | /*[clinic input] |
| 3326 | os.fdatasync |
| 3327 | |
| 3328 | fd: fildes |
| 3329 | |
| 3330 | Force write of fd to disk without forcing update of metadata. |
| 3331 | [clinic start generated code]*/ |
| 3332 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3333 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3334 | os_fdatasync_impl(PyObject *module, int fd) |
| 3335 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3336 | { |
| 3337 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3338 | } |
| 3339 | #endif /* HAVE_FDATASYNC */ |
| 3340 | |
| 3341 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3342 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3343 | /*[clinic input] |
| 3344 | os.chown |
| 3345 | |
| 3346 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3347 | 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] | 3348 | |
| 3349 | uid: uid_t |
| 3350 | |
| 3351 | gid: gid_t |
| 3352 | |
| 3353 | * |
| 3354 | |
| 3355 | dir_fd : dir_fd(requires='fchownat') = None |
| 3356 | If not None, it should be a file descriptor open to a directory, |
| 3357 | and path should be relative; path will then be relative to that |
| 3358 | directory. |
| 3359 | |
| 3360 | follow_symlinks: bool = True |
| 3361 | If False, and the last element of the path is a symbolic link, |
| 3362 | stat will examine the symbolic link itself instead of the file |
| 3363 | the link points to. |
| 3364 | |
| 3365 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3366 | |
| 3367 | path may always be specified as a string. |
| 3368 | On some platforms, path may also be specified as an open file descriptor. |
| 3369 | If this functionality is unavailable, using it raises an exception. |
| 3370 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3371 | and path should be relative; path will then be relative to that directory. |
| 3372 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3373 | link, chown will modify the symbolic link itself instead of the file the |
| 3374 | link points to. |
| 3375 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3376 | an open file descriptor. |
| 3377 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3378 | If they are unavailable, using them will raise a NotImplementedError. |
| 3379 | |
| 3380 | [clinic start generated code]*/ |
| 3381 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3382 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3383 | 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] | 3384 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3385 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3386 | { |
| 3387 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3388 | |
| 3389 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3390 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3391 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3392 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3393 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3394 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3395 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3396 | |
| 3397 | #ifdef __APPLE__ |
| 3398 | /* |
| 3399 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3400 | * (But we still have an lchown symbol because of weak-linking.) |
| 3401 | * It doesn't have fchownat either. So there's no possibility |
| 3402 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3403 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3404 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3405 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3406 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3407 | } |
| 3408 | #endif |
| 3409 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3410 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, |
| 3411 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 3412 | return NULL; |
| 3413 | } |
| 3414 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3415 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3416 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3417 | if (path->fd != -1) |
| 3418 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3419 | else |
| 3420 | #endif |
| 3421 | #ifdef HAVE_LCHOWN |
| 3422 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3423 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3424 | else |
| 3425 | #endif |
| 3426 | #ifdef HAVE_FCHOWNAT |
| 3427 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3428 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3429 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3430 | else |
| 3431 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3432 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3433 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3434 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3435 | if (result) |
| 3436 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3438 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3439 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3440 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3441 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3442 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3443 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3444 | /*[clinic input] |
| 3445 | os.fchown |
| 3446 | |
| 3447 | fd: int |
| 3448 | uid: uid_t |
| 3449 | gid: gid_t |
| 3450 | |
| 3451 | Change the owner and group id of the file specified by file descriptor. |
| 3452 | |
| 3453 | Equivalent to os.chown(fd, uid, gid). |
| 3454 | |
| 3455 | [clinic start generated code]*/ |
| 3456 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3457 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3458 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3459 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3460 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3461 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3462 | int async_err = 0; |
| 3463 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3464 | if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) { |
| 3465 | return NULL; |
| 3466 | } |
| 3467 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3468 | do { |
| 3469 | Py_BEGIN_ALLOW_THREADS |
| 3470 | res = fchown(fd, uid, gid); |
| 3471 | Py_END_ALLOW_THREADS |
| 3472 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3473 | if (res != 0) |
| 3474 | return (!async_err) ? posix_error() : NULL; |
| 3475 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3476 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3477 | } |
| 3478 | #endif /* HAVE_FCHOWN */ |
| 3479 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3480 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3481 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3482 | /*[clinic input] |
| 3483 | os.lchown |
| 3484 | |
| 3485 | path : path_t |
| 3486 | uid: uid_t |
| 3487 | gid: gid_t |
| 3488 | |
| 3489 | Change the owner and group id of path to the numeric uid and gid. |
| 3490 | |
| 3491 | This function will not follow symbolic links. |
| 3492 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3493 | [clinic start generated code]*/ |
| 3494 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3495 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3496 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3497 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3498 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3499 | int res; |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3500 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) { |
| 3501 | return NULL; |
| 3502 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3503 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3504 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3505 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3506 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3507 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3508 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3509 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3510 | } |
| 3511 | #endif /* HAVE_LCHOWN */ |
| 3512 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3513 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3514 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3515 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3516 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3517 | #ifdef MS_WINDOWS |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3518 | wchar_t wbuf[MAXPATHLEN]; |
| 3519 | wchar_t *wbuf2 = wbuf; |
| 3520 | DWORD len; |
| 3521 | |
| 3522 | Py_BEGIN_ALLOW_THREADS |
| 3523 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
| 3524 | /* If the buffer is large enough, len does not include the |
| 3525 | terminating \0. If the buffer is too small, len includes |
| 3526 | the space needed for the terminator. */ |
| 3527 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | ec3e20a | 2019-06-28 18:01:59 +0200 | [diff] [blame] | 3528 | if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3529 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3530 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3531 | else { |
| 3532 | wbuf2 = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3533 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3534 | if (wbuf2) { |
| 3535 | len = GetCurrentDirectoryW(len, wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3536 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3537 | } |
| 3538 | Py_END_ALLOW_THREADS |
| 3539 | |
| 3540 | if (!wbuf2) { |
| 3541 | PyErr_NoMemory(); |
| 3542 | return NULL; |
| 3543 | } |
| 3544 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3545 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3546 | PyMem_RawFree(wbuf2); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3547 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3548 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3549 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3550 | PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3551 | if (wbuf2 != wbuf) { |
| 3552 | PyMem_RawFree(wbuf2); |
| 3553 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3554 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3555 | if (use_bytes) { |
| 3556 | if (resobj == NULL) { |
| 3557 | return NULL; |
| 3558 | } |
| 3559 | Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj)); |
| 3560 | } |
| 3561 | |
| 3562 | return resobj; |
| 3563 | #else |
| 3564 | const size_t chunk = 1024; |
| 3565 | |
| 3566 | char *buf = NULL; |
| 3567 | char *cwd = NULL; |
| 3568 | size_t buflen = 0; |
| 3569 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3570 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3571 | do { |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3572 | char *newbuf; |
| 3573 | if (buflen <= PY_SSIZE_T_MAX - chunk) { |
| 3574 | buflen += chunk; |
| 3575 | newbuf = PyMem_RawRealloc(buf, buflen); |
| 3576 | } |
| 3577 | else { |
| 3578 | newbuf = NULL; |
| 3579 | } |
| 3580 | if (newbuf == NULL) { |
| 3581 | PyMem_RawFree(buf); |
| 3582 | buf = NULL; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3583 | break; |
| 3584 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3585 | buf = newbuf; |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3586 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3587 | cwd = getcwd(buf, buflen); |
| 3588 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3589 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3590 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3591 | if (buf == NULL) { |
| 3592 | return PyErr_NoMemory(); |
| 3593 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3594 | if (cwd == NULL) { |
| 3595 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3596 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3597 | } |
| 3598 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3599 | PyObject *obj; |
| 3600 | if (use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3601 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3602 | } |
| 3603 | else { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3604 | obj = PyUnicode_DecodeFSDefault(buf); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3605 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3606 | PyMem_RawFree(buf); |
| 3607 | |
| 3608 | return obj; |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3609 | #endif /* !MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3610 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3611 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3612 | |
| 3613 | /*[clinic input] |
| 3614 | os.getcwd |
| 3615 | |
| 3616 | Return a unicode string representing the current working directory. |
| 3617 | [clinic start generated code]*/ |
| 3618 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3619 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3620 | os_getcwd_impl(PyObject *module) |
| 3621 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3622 | { |
| 3623 | return posix_getcwd(0); |
| 3624 | } |
| 3625 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3626 | |
| 3627 | /*[clinic input] |
| 3628 | os.getcwdb |
| 3629 | |
| 3630 | Return a bytes string representing the current working directory. |
| 3631 | [clinic start generated code]*/ |
| 3632 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3633 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3634 | os_getcwdb_impl(PyObject *module) |
| 3635 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3636 | { |
| 3637 | return posix_getcwd(1); |
| 3638 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3639 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3640 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3641 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3642 | #define HAVE_LINK 1 |
| 3643 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3644 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3645 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3646 | /*[clinic input] |
| 3647 | |
| 3648 | os.link |
| 3649 | |
| 3650 | src : path_t |
| 3651 | dst : path_t |
| 3652 | * |
| 3653 | src_dir_fd : dir_fd = None |
| 3654 | dst_dir_fd : dir_fd = None |
| 3655 | follow_symlinks: bool = True |
| 3656 | |
| 3657 | Create a hard link to a file. |
| 3658 | |
| 3659 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3660 | descriptor open to a directory, and the respective path string (src or dst) |
| 3661 | should be relative; the path will then be relative to that directory. |
| 3662 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3663 | link, link will create a link to the symbolic link itself instead of the |
| 3664 | file the link points to. |
| 3665 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3666 | platform. If they are unavailable, using them will raise a |
| 3667 | NotImplementedError. |
| 3668 | [clinic start generated code]*/ |
| 3669 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3670 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3671 | 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] | 3672 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3673 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3674 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3675 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3676 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3677 | #else |
| 3678 | int result; |
| 3679 | #endif |
| 3680 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3681 | #ifndef HAVE_LINKAT |
| 3682 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3683 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3684 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3685 | } |
| 3686 | #endif |
| 3687 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3688 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3689 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3690 | PyErr_SetString(PyExc_NotImplementedError, |
| 3691 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3692 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3693 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3694 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3695 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 3696 | if (PySys_Audit("os.link", "OOii", src->object, dst->object, |
| 3697 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 3698 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 3699 | return NULL; |
| 3700 | } |
| 3701 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3702 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3703 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3704 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3705 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3706 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3707 | if (!result) |
| 3708 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3709 | #else |
| 3710 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3711 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3712 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3713 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3714 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3715 | result = linkat(src_dir_fd, src->narrow, |
| 3716 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3717 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3718 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3719 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3720 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3721 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3722 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3723 | if (result) |
| 3724 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3725 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3726 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3727 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3728 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3729 | #endif |
| 3730 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3731 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3732 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3733 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3734 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3735 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3736 | PyObject *v; |
| 3737 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3738 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3739 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3740 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3741 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3742 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3743 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3744 | WIN32_FIND_DATAW wFileData; |
| 3745 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3746 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3747 | if (!path->wide) { /* Default arg: "." */ |
| 3748 | po_wchars = L"."; |
| 3749 | len = 1; |
| 3750 | } else { |
| 3751 | po_wchars = path->wide; |
| 3752 | len = wcslen(path->wide); |
| 3753 | } |
| 3754 | /* The +5 is so we can append "\\*.*\0" */ |
| 3755 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3756 | if (!wnamebuf) { |
| 3757 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3758 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3759 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3760 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3761 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3762 | wchar_t wch = wnamebuf[len-1]; |
| 3763 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3764 | wnamebuf[len++] = SEP; |
| 3765 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3766 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3767 | if ((list = PyList_New(0)) == NULL) { |
| 3768 | goto exit; |
| 3769 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3770 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3771 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3772 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3773 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3774 | int error = GetLastError(); |
| 3775 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3776 | goto exit; |
| 3777 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3778 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3779 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3780 | } |
| 3781 | do { |
| 3782 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3783 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3784 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3785 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3786 | wcslen(wFileData.cFileName)); |
| 3787 | if (path->narrow && v) { |
| 3788 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3789 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3790 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3791 | Py_DECREF(list); |
| 3792 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3793 | break; |
| 3794 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3795 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3796 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3797 | Py_DECREF(list); |
| 3798 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3799 | break; |
| 3800 | } |
| 3801 | Py_DECREF(v); |
| 3802 | } |
| 3803 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3804 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3805 | Py_END_ALLOW_THREADS |
| 3806 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3807 | it got to the end of the directory. */ |
| 3808 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3809 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3810 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3811 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3812 | } |
| 3813 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3814 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3815 | exit: |
| 3816 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3817 | if (FindClose(hFindFile) == FALSE) { |
| 3818 | if (list != NULL) { |
| 3819 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3820 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3821 | } |
| 3822 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3823 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3824 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3825 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3826 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3827 | } /* end of _listdir_windows_no_opendir */ |
| 3828 | |
| 3829 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3830 | |
| 3831 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3832 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3833 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3834 | PyObject *v; |
| 3835 | DIR *dirp = NULL; |
| 3836 | struct dirent *ep; |
| 3837 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3838 | #ifdef HAVE_FDOPENDIR |
| 3839 | int fd = -1; |
| 3840 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3841 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3842 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3843 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3844 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3845 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3846 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3847 | if (fd == -1) |
| 3848 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3849 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3850 | return_str = 1; |
| 3851 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3852 | Py_BEGIN_ALLOW_THREADS |
| 3853 | dirp = fdopendir(fd); |
| 3854 | Py_END_ALLOW_THREADS |
| 3855 | } |
| 3856 | else |
| 3857 | #endif |
| 3858 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3859 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3860 | if (path->narrow) { |
| 3861 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3862 | /* only return bytes if they specified a bytes-like object */ |
| 3863 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3864 | } |
| 3865 | else { |
| 3866 | name = "."; |
| 3867 | return_str = 1; |
| 3868 | } |
| 3869 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3870 | Py_BEGIN_ALLOW_THREADS |
| 3871 | dirp = opendir(name); |
| 3872 | Py_END_ALLOW_THREADS |
| 3873 | } |
| 3874 | |
| 3875 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3876 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3877 | #ifdef HAVE_FDOPENDIR |
| 3878 | if (fd != -1) { |
| 3879 | Py_BEGIN_ALLOW_THREADS |
| 3880 | close(fd); |
| 3881 | Py_END_ALLOW_THREADS |
| 3882 | } |
| 3883 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3884 | goto exit; |
| 3885 | } |
| 3886 | if ((list = PyList_New(0)) == NULL) { |
| 3887 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3888 | } |
| 3889 | for (;;) { |
| 3890 | errno = 0; |
| 3891 | Py_BEGIN_ALLOW_THREADS |
| 3892 | ep = readdir(dirp); |
| 3893 | Py_END_ALLOW_THREADS |
| 3894 | if (ep == NULL) { |
| 3895 | if (errno == 0) { |
| 3896 | break; |
| 3897 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3898 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3899 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3900 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3901 | } |
| 3902 | } |
| 3903 | if (ep->d_name[0] == '.' && |
| 3904 | (NAMLEN(ep) == 1 || |
| 3905 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3906 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3907 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3908 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3909 | else |
| 3910 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3911 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3912 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3913 | break; |
| 3914 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3915 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3916 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3917 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3918 | break; |
| 3919 | } |
| 3920 | Py_DECREF(v); |
| 3921 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3922 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3923 | exit: |
| 3924 | if (dirp != NULL) { |
| 3925 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3926 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3927 | if (fd > -1) |
| 3928 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3929 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3930 | closedir(dirp); |
| 3931 | Py_END_ALLOW_THREADS |
| 3932 | } |
| 3933 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3934 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3935 | } /* end of _posix_listdir */ |
| 3936 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3937 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3938 | |
| 3939 | /*[clinic input] |
| 3940 | os.listdir |
| 3941 | |
| 3942 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3943 | |
| 3944 | Return a list containing the names of the files in the directory. |
| 3945 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3946 | 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] | 3947 | the filenames returned will also be bytes; in all other circumstances |
| 3948 | the filenames returned will be str. |
| 3949 | If path is None, uses the path='.'. |
| 3950 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3951 | the file descriptor must refer to a directory. |
| 3952 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3953 | |
| 3954 | The list is in arbitrary order. It does not include the special |
| 3955 | entries '.' and '..' even if they are present in the directory. |
| 3956 | |
| 3957 | |
| 3958 | [clinic start generated code]*/ |
| 3959 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3960 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3961 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3962 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3963 | { |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 3964 | if (PySys_Audit("os.listdir", "O", |
| 3965 | path->object ? path->object : Py_None) < 0) { |
| 3966 | return NULL; |
| 3967 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3968 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3969 | return _listdir_windows_no_opendir(path, NULL); |
| 3970 | #else |
| 3971 | return _posix_listdir(path, NULL); |
| 3972 | #endif |
| 3973 | } |
| 3974 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3975 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3976 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3977 | /*[clinic input] |
| 3978 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3979 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3980 | path: path_t |
| 3981 | / |
| 3982 | |
| 3983 | [clinic start generated code]*/ |
| 3984 | |
| 3985 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3986 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3987 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3988 | { |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3989 | wchar_t *abspath; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3990 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3991 | /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */ |
| 3992 | if (_Py_abspath(path->wide, &abspath) < 0) { |
| 3993 | return win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3994 | } |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3995 | if (abspath == NULL) { |
| 3996 | return PyErr_NoMemory(); |
| 3997 | } |
| 3998 | |
| 3999 | PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath)); |
| 4000 | PyMem_RawFree(abspath); |
| 4001 | if (str == NULL) { |
| 4002 | return NULL; |
| 4003 | } |
| 4004 | if (path->narrow) { |
| 4005 | Py_SETREF(str, PyUnicode_EncodeFSDefault(str)); |
| 4006 | } |
| 4007 | return str; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4008 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4009 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 4010 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4011 | /*[clinic input] |
| 4012 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 4013 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4014 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4015 | / |
| 4016 | |
| 4017 | A helper function for samepath on windows. |
| 4018 | [clinic start generated code]*/ |
| 4019 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4020 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4021 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 4022 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4023 | { |
| 4024 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4025 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 4026 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4027 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4028 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4029 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4030 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4031 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4032 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4033 | 0, /* desired access */ |
| 4034 | 0, /* share mode */ |
| 4035 | NULL, /* security attributes */ |
| 4036 | OPEN_EXISTING, |
| 4037 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 4038 | FILE_FLAG_BACKUP_SEMANTICS, |
| 4039 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4040 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4041 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4042 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4043 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4044 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4045 | |
| 4046 | /* We have a good handle to the target, use it to determine the |
| 4047 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4048 | while (1) { |
| 4049 | Py_BEGIN_ALLOW_THREADS |
| 4050 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 4051 | buf_size, VOLUME_NAME_DOS); |
| 4052 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4053 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4054 | if (!result_length) { |
| 4055 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 4056 | path->object); |
| 4057 | goto cleanup; |
| 4058 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4059 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4060 | if (result_length < buf_size) { |
| 4061 | break; |
| 4062 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4063 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4064 | wchar_t *tmp; |
| 4065 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 4066 | result_length * sizeof(*tmp)); |
| 4067 | if (!tmp) { |
| 4068 | result = PyErr_NoMemory(); |
| 4069 | goto cleanup; |
| 4070 | } |
| 4071 | |
| 4072 | buf_size = result_length; |
| 4073 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4074 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4075 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4076 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4077 | if (result && path->narrow) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4078 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4079 | } |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4080 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4081 | cleanup: |
| 4082 | if (target_path != buf) { |
| 4083 | PyMem_Free(target_path); |
| 4084 | } |
| 4085 | CloseHandle(hFile); |
| 4086 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4087 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 4088 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4089 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4090 | /*[clinic input] |
| 4091 | os._getvolumepathname |
| 4092 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4093 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4094 | |
| 4095 | A helper function for ismount on Win32. |
| 4096 | [clinic start generated code]*/ |
| 4097 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4098 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4099 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 4100 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4101 | { |
| 4102 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4103 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4104 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4105 | BOOL ret; |
| 4106 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4107 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4108 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4109 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 4110 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4111 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4112 | return NULL; |
| 4113 | } |
| 4114 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4115 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4116 | if (mountpath == NULL) |
| 4117 | return PyErr_NoMemory(); |
| 4118 | |
| 4119 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4120 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4121 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4122 | Py_END_ALLOW_THREADS |
| 4123 | |
| 4124 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4125 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4126 | goto exit; |
| 4127 | } |
| 4128 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4129 | if (path->narrow) |
| 4130 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4131 | |
| 4132 | exit: |
| 4133 | PyMem_Free(mountpath); |
| 4134 | return result; |
| 4135 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4136 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4137 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4139 | |
| 4140 | /*[clinic input] |
| 4141 | os.mkdir |
| 4142 | |
| 4143 | path : path_t |
| 4144 | |
| 4145 | mode: int = 0o777 |
| 4146 | |
| 4147 | * |
| 4148 | |
| 4149 | dir_fd : dir_fd(requires='mkdirat') = None |
| 4150 | |
| 4151 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4152 | |
| 4153 | Create a directory. |
| 4154 | |
| 4155 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4156 | and path should be relative; path will then be relative to that directory. |
| 4157 | dir_fd may not be implemented on your platform. |
| 4158 | If it is unavailable, using it will raise a NotImplementedError. |
| 4159 | |
| 4160 | The mode argument is ignored on Windows. |
| 4161 | [clinic start generated code]*/ |
| 4162 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4163 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4164 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 4165 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4166 | { |
| 4167 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4168 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4169 | if (PySys_Audit("os.mkdir", "Oii", path->object, mode, |
| 4170 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4171 | return NULL; |
| 4172 | } |
| 4173 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4174 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4175 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4176 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4177 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4178 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4179 | if (!result) |
| 4180 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4181 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4182 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4183 | #if HAVE_MKDIRAT |
| 4184 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4185 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4186 | else |
| 4187 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4188 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4189 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4190 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4191 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4192 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4193 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4194 | if (result < 0) |
| 4195 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4196 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4197 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4200 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4201 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4202 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4203 | #include <sys/resource.h> |
| 4204 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4205 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4206 | |
| 4207 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4208 | /*[clinic input] |
| 4209 | os.nice |
| 4210 | |
| 4211 | increment: int |
| 4212 | / |
| 4213 | |
| 4214 | Add increment to the priority of process and return the new priority. |
| 4215 | [clinic start generated code]*/ |
| 4216 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4217 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4218 | os_nice_impl(PyObject *module, int increment) |
| 4219 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4220 | { |
| 4221 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4222 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4223 | /* There are two flavours of 'nice': one that returns the new |
| 4224 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4225 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4226 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4227 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4228 | If we are of the nice family that returns the new priority, we |
| 4229 | need to clear errno before the call, and check if errno is filled |
| 4230 | before calling posix_error() on a returnvalue of -1, because the |
| 4231 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4232 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4233 | errno = 0; |
| 4234 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4235 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4236 | if (value == 0) |
| 4237 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4238 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4239 | if (value == -1 && errno != 0) |
| 4240 | /* either nice() or getpriority() returned an error */ |
| 4241 | return posix_error(); |
| 4242 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4243 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4244 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4245 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4246 | |
| 4247 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4248 | /*[clinic input] |
| 4249 | os.getpriority |
| 4250 | |
| 4251 | which: int |
| 4252 | who: int |
| 4253 | |
| 4254 | Return program scheduling priority. |
| 4255 | [clinic start generated code]*/ |
| 4256 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4257 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4258 | os_getpriority_impl(PyObject *module, int which, int who) |
| 4259 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4260 | { |
| 4261 | int retval; |
| 4262 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4263 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4264 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4265 | if (errno != 0) |
| 4266 | return posix_error(); |
| 4267 | return PyLong_FromLong((long)retval); |
| 4268 | } |
| 4269 | #endif /* HAVE_GETPRIORITY */ |
| 4270 | |
| 4271 | |
| 4272 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4273 | /*[clinic input] |
| 4274 | os.setpriority |
| 4275 | |
| 4276 | which: int |
| 4277 | who: int |
| 4278 | priority: int |
| 4279 | |
| 4280 | Set program scheduling priority. |
| 4281 | [clinic start generated code]*/ |
| 4282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4283 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4284 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4285 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4286 | { |
| 4287 | int retval; |
| 4288 | |
| 4289 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4290 | if (retval == -1) |
| 4291 | return posix_error(); |
| 4292 | Py_RETURN_NONE; |
| 4293 | } |
| 4294 | #endif /* HAVE_SETPRIORITY */ |
| 4295 | |
| 4296 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4297 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4298 | 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] | 4299 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4300 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4301 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4302 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4303 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4304 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4305 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4306 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4307 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4308 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4309 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4310 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4311 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4312 | #ifndef HAVE_RENAMEAT |
| 4313 | if (dir_fd_specified) { |
| 4314 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4315 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4316 | } |
| 4317 | #endif |
| 4318 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4319 | if (PySys_Audit("os.rename", "OOii", src->object, dst->object, |
| 4320 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, |
| 4321 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { |
| 4322 | return NULL; |
| 4323 | } |
| 4324 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4325 | #ifdef MS_WINDOWS |
| 4326 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4327 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4328 | Py_END_ALLOW_THREADS |
| 4329 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4330 | if (!result) |
| 4331 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4332 | |
| 4333 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4334 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4335 | PyErr_Format(PyExc_ValueError, |
| 4336 | "%s: src and dst must be the same type", function_name); |
| 4337 | return NULL; |
| 4338 | } |
| 4339 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4340 | Py_BEGIN_ALLOW_THREADS |
| 4341 | #ifdef HAVE_RENAMEAT |
| 4342 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4343 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4344 | else |
| 4345 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4346 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4347 | Py_END_ALLOW_THREADS |
| 4348 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4349 | if (result) |
| 4350 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4351 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4352 | Py_RETURN_NONE; |
| 4353 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4354 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4355 | |
| 4356 | /*[clinic input] |
| 4357 | os.rename |
| 4358 | |
| 4359 | src : path_t |
| 4360 | dst : path_t |
| 4361 | * |
| 4362 | src_dir_fd : dir_fd = None |
| 4363 | dst_dir_fd : dir_fd = None |
| 4364 | |
| 4365 | Rename a file or directory. |
| 4366 | |
| 4367 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4368 | descriptor open to a directory, and the respective path string (src or dst) |
| 4369 | should be relative; the path will then be relative to that directory. |
| 4370 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4371 | If they are unavailable, using them will raise a NotImplementedError. |
| 4372 | [clinic start generated code]*/ |
| 4373 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4374 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4375 | 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] | 4376 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4377 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4378 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4379 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4380 | } |
| 4381 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4382 | |
| 4383 | /*[clinic input] |
| 4384 | os.replace = os.rename |
| 4385 | |
| 4386 | Rename a file or directory, overwriting the destination. |
| 4387 | |
| 4388 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4389 | descriptor open to a directory, and the respective path string (src or dst) |
| 4390 | should be relative; the path will then be relative to that directory. |
| 4391 | 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] | 4392 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4393 | [clinic start generated code]*/ |
| 4394 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4395 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4396 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4397 | int dst_dir_fd) |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4398 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4399 | { |
| 4400 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4401 | } |
| 4402 | |
| 4403 | |
| 4404 | /*[clinic input] |
| 4405 | os.rmdir |
| 4406 | |
| 4407 | path: path_t |
| 4408 | * |
| 4409 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4410 | |
| 4411 | Remove a directory. |
| 4412 | |
| 4413 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4414 | and path should be relative; path will then be relative to that directory. |
| 4415 | dir_fd may not be implemented on your platform. |
| 4416 | If it is unavailable, using it will raise a NotImplementedError. |
| 4417 | [clinic start generated code]*/ |
| 4418 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4419 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4420 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4421 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4422 | { |
| 4423 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4424 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4425 | if (PySys_Audit("os.rmdir", "Oi", path->object, |
| 4426 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4427 | return NULL; |
| 4428 | } |
| 4429 | |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4430 | Py_BEGIN_ALLOW_THREADS |
| 4431 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4432 | /* Windows, success=1, UNIX, success=0 */ |
| 4433 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4434 | #else |
| 4435 | #ifdef HAVE_UNLINKAT |
| 4436 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4437 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4438 | else |
| 4439 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4440 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4441 | #endif |
| 4442 | Py_END_ALLOW_THREADS |
| 4443 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4444 | if (result) |
| 4445 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4446 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4447 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4450 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4451 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4452 | #ifdef MS_WINDOWS |
| 4453 | /*[clinic input] |
| 4454 | os.system -> long |
| 4455 | |
| 4456 | command: Py_UNICODE |
| 4457 | |
| 4458 | Execute the command in a subshell. |
| 4459 | [clinic start generated code]*/ |
| 4460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4461 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4462 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4463 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4464 | { |
| 4465 | long result; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4466 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4467 | if (PySys_Audit("os.system", "(u)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4468 | return -1; |
| 4469 | } |
| 4470 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4471 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4472 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4473 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4474 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4475 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4476 | return result; |
| 4477 | } |
| 4478 | #else /* MS_WINDOWS */ |
| 4479 | /*[clinic input] |
| 4480 | os.system -> long |
| 4481 | |
| 4482 | command: FSConverter |
| 4483 | |
| 4484 | Execute the command in a subshell. |
| 4485 | [clinic start generated code]*/ |
| 4486 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4487 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4488 | os_system_impl(PyObject *module, PyObject *command) |
| 4489 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4490 | { |
| 4491 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4492 | const char *bytes = PyBytes_AsString(command); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4493 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4494 | if (PySys_Audit("os.system", "(O)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4495 | return -1; |
| 4496 | } |
| 4497 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4498 | Py_BEGIN_ALLOW_THREADS |
| 4499 | result = system(bytes); |
| 4500 | Py_END_ALLOW_THREADS |
| 4501 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4502 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4503 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4504 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4505 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4506 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4507 | /*[clinic input] |
| 4508 | os.umask |
| 4509 | |
| 4510 | mask: int |
| 4511 | / |
| 4512 | |
| 4513 | Set the current numeric umask and return the previous umask. |
| 4514 | [clinic start generated code]*/ |
| 4515 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4516 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4517 | os_umask_impl(PyObject *module, int mask) |
| 4518 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4519 | { |
| 4520 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4521 | if (i < 0) |
| 4522 | return posix_error(); |
| 4523 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4526 | #ifdef MS_WINDOWS |
| 4527 | |
| 4528 | /* override the default DeleteFileW behavior so that directory |
| 4529 | symlinks can be removed with this function, the same as with |
| 4530 | Unix symlinks */ |
| 4531 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4532 | { |
| 4533 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4534 | WIN32_FIND_DATAW find_data; |
| 4535 | HANDLE find_data_handle; |
| 4536 | int is_directory = 0; |
| 4537 | int is_link = 0; |
| 4538 | |
| 4539 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4540 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4541 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4542 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4543 | it is a symlink */ |
| 4544 | if(is_directory && |
| 4545 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4546 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4547 | |
| 4548 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4549 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4550 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4551 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4552 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4553 | FindClose(find_data_handle); |
| 4554 | } |
| 4555 | } |
| 4556 | } |
| 4557 | |
| 4558 | if (is_directory && is_link) |
| 4559 | return RemoveDirectoryW(lpFileName); |
| 4560 | |
| 4561 | return DeleteFileW(lpFileName); |
| 4562 | } |
| 4563 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4564 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4565 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4566 | /*[clinic input] |
| 4567 | os.unlink |
| 4568 | |
| 4569 | path: path_t |
| 4570 | * |
| 4571 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4572 | |
| 4573 | Remove a file (same as remove()). |
| 4574 | |
| 4575 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4576 | and path should be relative; path will then be relative to that directory. |
| 4577 | dir_fd may not be implemented on your platform. |
| 4578 | If it is unavailable, using it will raise a NotImplementedError. |
| 4579 | |
| 4580 | [clinic start generated code]*/ |
| 4581 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4582 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4583 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4584 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4585 | { |
| 4586 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4587 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 4588 | if (PySys_Audit("os.remove", "Oi", path->object, |
| 4589 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 4590 | return NULL; |
| 4591 | } |
| 4592 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4593 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4594 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4595 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4596 | /* Windows, success=1, UNIX, success=0 */ |
| 4597 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4598 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4599 | #ifdef HAVE_UNLINKAT |
| 4600 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4601 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4602 | else |
| 4603 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4604 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4605 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4606 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4607 | Py_END_ALLOW_THREADS |
| 4608 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4609 | if (result) |
| 4610 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4611 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4612 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4615 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4616 | /*[clinic input] |
| 4617 | os.remove = os.unlink |
| 4618 | |
| 4619 | Remove a file (same as unlink()). |
| 4620 | |
| 4621 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4622 | and path should be relative; path will then be relative to that directory. |
| 4623 | dir_fd may not be implemented on your platform. |
| 4624 | If it is unavailable, using it will raise a NotImplementedError. |
| 4625 | [clinic start generated code]*/ |
| 4626 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4627 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4628 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4629 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4630 | { |
| 4631 | return os_unlink_impl(module, path, dir_fd); |
| 4632 | } |
| 4633 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4634 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4635 | static PyStructSequence_Field uname_result_fields[] = { |
| 4636 | {"sysname", "operating system name"}, |
| 4637 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4638 | {"release", "operating system release"}, |
| 4639 | {"version", "operating system version"}, |
| 4640 | {"machine", "hardware identifier"}, |
| 4641 | {NULL} |
| 4642 | }; |
| 4643 | |
| 4644 | PyDoc_STRVAR(uname_result__doc__, |
| 4645 | "uname_result: Result from os.uname().\n\n\ |
| 4646 | This object may be accessed either as a tuple of\n\ |
| 4647 | (sysname, nodename, release, version, machine),\n\ |
| 4648 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4649 | \n\ |
| 4650 | See os.uname for more information."); |
| 4651 | |
| 4652 | static PyStructSequence_Desc uname_result_desc = { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4653 | MODNAME ".uname_result", /* name */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4654 | uname_result__doc__, /* doc */ |
| 4655 | uname_result_fields, |
| 4656 | 5 |
| 4657 | }; |
| 4658 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4659 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4660 | /*[clinic input] |
| 4661 | os.uname |
| 4662 | |
| 4663 | Return an object identifying the current operating system. |
| 4664 | |
| 4665 | The object behaves like a named tuple with the following fields: |
| 4666 | (sysname, nodename, release, version, machine) |
| 4667 | |
| 4668 | [clinic start generated code]*/ |
| 4669 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4670 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4671 | os_uname_impl(PyObject *module) |
| 4672 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4673 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4674 | struct utsname u; |
| 4675 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4676 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4677 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4678 | Py_BEGIN_ALLOW_THREADS |
| 4679 | res = uname(&u); |
| 4680 | Py_END_ALLOW_THREADS |
| 4681 | if (res < 0) |
| 4682 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4683 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 4684 | PyObject *UnameResultType = get_posix_state(module)->UnameResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4685 | value = PyStructSequence_New((PyTypeObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4686 | if (value == NULL) |
| 4687 | return NULL; |
| 4688 | |
| 4689 | #define SET(i, field) \ |
| 4690 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4691 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4692 | if (!o) { \ |
| 4693 | Py_DECREF(value); \ |
| 4694 | return NULL; \ |
| 4695 | } \ |
| 4696 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4697 | } \ |
| 4698 | |
| 4699 | SET(0, u.sysname); |
| 4700 | SET(1, u.nodename); |
| 4701 | SET(2, u.release); |
| 4702 | SET(3, u.version); |
| 4703 | SET(4, u.machine); |
| 4704 | |
| 4705 | #undef SET |
| 4706 | |
| 4707 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4708 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4709 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4710 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4711 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4712 | |
| 4713 | typedef struct { |
| 4714 | int now; |
| 4715 | time_t atime_s; |
| 4716 | long atime_ns; |
| 4717 | time_t mtime_s; |
| 4718 | long mtime_ns; |
| 4719 | } utime_t; |
| 4720 | |
| 4721 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4722 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4723 | * they also intentionally leak the declaration of a pointer named "time" |
| 4724 | */ |
| 4725 | #define UTIME_TO_TIMESPEC \ |
| 4726 | struct timespec ts[2]; \ |
| 4727 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4728 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4729 | time = NULL; \ |
| 4730 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4731 | ts[0].tv_sec = ut->atime_s; \ |
| 4732 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4733 | ts[1].tv_sec = ut->mtime_s; \ |
| 4734 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4735 | time = ts; \ |
| 4736 | } \ |
| 4737 | |
| 4738 | #define UTIME_TO_TIMEVAL \ |
| 4739 | struct timeval tv[2]; \ |
| 4740 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4741 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4742 | time = NULL; \ |
| 4743 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4744 | tv[0].tv_sec = ut->atime_s; \ |
| 4745 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4746 | tv[1].tv_sec = ut->mtime_s; \ |
| 4747 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4748 | time = tv; \ |
| 4749 | } \ |
| 4750 | |
| 4751 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4752 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4753 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4754 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4755 | time = NULL; \ |
| 4756 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4757 | u.actime = ut->atime_s; \ |
| 4758 | u.modtime = ut->mtime_s; \ |
| 4759 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | #define UTIME_TO_TIME_T \ |
| 4763 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4764 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4765 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4766 | time = NULL; \ |
| 4767 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4768 | timet[0] = ut->atime_s; \ |
| 4769 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4770 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4771 | } \ |
| 4772 | |
| 4773 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4774 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4775 | |
| 4776 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4777 | 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] | 4778 | { |
| 4779 | #ifdef HAVE_UTIMENSAT |
| 4780 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4781 | UTIME_TO_TIMESPEC; |
| 4782 | return utimensat(dir_fd, path, time, flags); |
| 4783 | #elif defined(HAVE_FUTIMESAT) |
| 4784 | UTIME_TO_TIMEVAL; |
| 4785 | /* |
| 4786 | * follow_symlinks will never be false here; |
| 4787 | * we only allow !follow_symlinks and dir_fd together |
| 4788 | * if we have utimensat() |
| 4789 | */ |
| 4790 | assert(follow_symlinks); |
| 4791 | return futimesat(dir_fd, path, time); |
| 4792 | #endif |
| 4793 | } |
| 4794 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4795 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4796 | #else |
| 4797 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4798 | #endif |
| 4799 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4800 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4801 | |
| 4802 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4803 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4804 | { |
| 4805 | #ifdef HAVE_FUTIMENS |
| 4806 | UTIME_TO_TIMESPEC; |
| 4807 | return futimens(fd, time); |
| 4808 | #else |
| 4809 | UTIME_TO_TIMEVAL; |
| 4810 | return futimes(fd, time); |
| 4811 | #endif |
| 4812 | } |
| 4813 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4814 | #define PATH_UTIME_HAVE_FD 1 |
| 4815 | #else |
| 4816 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4817 | #endif |
| 4818 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4819 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4820 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4821 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4822 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4823 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4824 | |
| 4825 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4826 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4827 | { |
| 4828 | #ifdef HAVE_UTIMENSAT |
| 4829 | UTIME_TO_TIMESPEC; |
| 4830 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4831 | #else |
| 4832 | UTIME_TO_TIMEVAL; |
| 4833 | return lutimes(path, time); |
| 4834 | #endif |
| 4835 | } |
| 4836 | |
| 4837 | #endif |
| 4838 | |
| 4839 | #ifndef MS_WINDOWS |
| 4840 | |
| 4841 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4842 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4843 | { |
| 4844 | #ifdef HAVE_UTIMENSAT |
| 4845 | UTIME_TO_TIMESPEC; |
| 4846 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4847 | #elif defined(HAVE_UTIMES) |
| 4848 | UTIME_TO_TIMEVAL; |
| 4849 | return utimes(path, time); |
| 4850 | #elif defined(HAVE_UTIME_H) |
| 4851 | UTIME_TO_UTIMBUF; |
| 4852 | return utime(path, time); |
| 4853 | #else |
| 4854 | UTIME_TO_TIME_T; |
| 4855 | return utime(path, time); |
| 4856 | #endif |
| 4857 | } |
| 4858 | |
| 4859 | #endif |
| 4860 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4861 | static int |
| 4862 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4863 | { |
| 4864 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4865 | PyObject *divmod; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4866 | divmod = PyNumber_Divmod(py_long, _posixstate_global->billion); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4867 | if (!divmod) |
| 4868 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4869 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4870 | PyErr_Format(PyExc_TypeError, |
| 4871 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4872 | _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod))); |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4873 | goto exit; |
| 4874 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4875 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4876 | if ((*s == -1) && PyErr_Occurred()) |
| 4877 | goto exit; |
| 4878 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4879 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4880 | goto exit; |
| 4881 | |
| 4882 | result = 1; |
| 4883 | exit: |
| 4884 | Py_XDECREF(divmod); |
| 4885 | return result; |
| 4886 | } |
| 4887 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4888 | |
| 4889 | /*[clinic input] |
| 4890 | os.utime |
| 4891 | |
| 4892 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4893 | times: object = None |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4894 | * |
| 4895 | ns: object = NULL |
| 4896 | dir_fd: dir_fd(requires='futimensat') = None |
| 4897 | follow_symlinks: bool=True |
| 4898 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4899 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4900 | |
| 4901 | Set the access and modified time of path. |
| 4902 | |
| 4903 | path may always be specified as a string. |
| 4904 | On some platforms, path may also be specified as an open file descriptor. |
| 4905 | If this functionality is unavailable, using it raises an exception. |
| 4906 | |
| 4907 | If times is not None, it must be a tuple (atime, mtime); |
| 4908 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4909 | 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] | 4910 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4911 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4912 | 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] | 4913 | Specifying tuples for both times and ns is an error. |
| 4914 | |
| 4915 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4916 | and path should be relative; path will then be relative to that directory. |
| 4917 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4918 | link, utime will modify the symbolic link itself instead of the file the |
| 4919 | link points to. |
| 4920 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4921 | as an open file descriptor. |
| 4922 | dir_fd and follow_symlinks may not be available on your platform. |
| 4923 | If they are unavailable, using them will raise a NotImplementedError. |
| 4924 | |
| 4925 | [clinic start generated code]*/ |
| 4926 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4927 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4928 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4929 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4930 | /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4931 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4932 | #ifdef MS_WINDOWS |
| 4933 | HANDLE hFile; |
| 4934 | FILETIME atime, mtime; |
| 4935 | #else |
| 4936 | int result; |
| 4937 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4938 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4939 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4940 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4941 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4942 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4943 | if (times != Py_None && ns) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4944 | PyErr_SetString(PyExc_ValueError, |
| 4945 | "utime: you may specify either 'times'" |
| 4946 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4947 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4948 | } |
| 4949 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4950 | if (times != Py_None) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4951 | time_t a_sec, m_sec; |
| 4952 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4953 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4954 | PyErr_SetString(PyExc_TypeError, |
| 4955 | "utime: 'times' must be either" |
| 4956 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4957 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4958 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4959 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4960 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4961 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4962 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4963 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4964 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4965 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4966 | utime.atime_s = a_sec; |
| 4967 | utime.atime_ns = a_nsec; |
| 4968 | utime.mtime_s = m_sec; |
| 4969 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4970 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4971 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4972 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4973 | PyErr_SetString(PyExc_TypeError, |
| 4974 | "utime: 'ns' must be a tuple of two ints"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4975 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4976 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4977 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4978 | if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4979 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4980 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4981 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4982 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4983 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4984 | } |
| 4985 | else { |
| 4986 | /* times and ns are both None/unspecified. use "now". */ |
| 4987 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4988 | } |
| 4989 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4990 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4991 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4992 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4993 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4994 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4995 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4996 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4997 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4998 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4999 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5000 | #if !defined(HAVE_UTIMENSAT) |
| 5001 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 5002 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5003 | "utime: cannot use dir_fd and follow_symlinks " |
| 5004 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5005 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5006 | } |
| 5007 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5008 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5009 | if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None, |
| 5010 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 5011 | return NULL; |
| 5012 | } |
| 5013 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5014 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5015 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5016 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 5017 | NULL, OPEN_EXISTING, |
| 5018 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5019 | Py_END_ALLOW_THREADS |
| 5020 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5021 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5022 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 5023 | } |
| 5024 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5025 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 5026 | GetSystemTimeAsFileTime(&mtime); |
| 5027 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5028 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5029 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 5030 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 5031 | _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] | 5032 | } |
| 5033 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 5034 | /* Avoid putting the file name into the error here, |
| 5035 | as that may confuse the user into believing that |
| 5036 | something is wrong with the file, when it also |
| 5037 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 5038 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5039 | CloseHandle(hFile); |
| 5040 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5041 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5042 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5043 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5044 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 5045 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 5046 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5047 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5048 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5049 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 5050 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5051 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5052 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5053 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5054 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5055 | else |
| 5056 | #endif |
| 5057 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 5058 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5059 | if (path->fd != -1) |
| 5060 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5061 | else |
| 5062 | #endif |
| 5063 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5064 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5065 | |
| 5066 | Py_END_ALLOW_THREADS |
| 5067 | |
| 5068 | if (result < 0) { |
| 5069 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5070 | posix_error(); |
| 5071 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5072 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5073 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5074 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5075 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5076 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5079 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5080 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5081 | |
| 5082 | /*[clinic input] |
| 5083 | os._exit |
| 5084 | |
| 5085 | status: int |
| 5086 | |
| 5087 | Exit to the system with specified status, without normal exit processing. |
| 5088 | [clinic start generated code]*/ |
| 5089 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5090 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5091 | os__exit_impl(PyObject *module, int status) |
| 5092 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5093 | { |
| 5094 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5095 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5098 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5099 | #define EXECV_CHAR wchar_t |
| 5100 | #else |
| 5101 | #define EXECV_CHAR char |
| 5102 | #endif |
| 5103 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5104 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5105 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5106 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5107 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5108 | Py_ssize_t i; |
| 5109 | for (i = 0; i < count; i++) |
| 5110 | PyMem_Free(array[i]); |
| 5111 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5112 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5113 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5114 | static int |
| 5115 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5116 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5117 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5118 | PyObject *ub; |
| 5119 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5120 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5121 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5122 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5123 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 5124 | if (*out) |
| 5125 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5126 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5127 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5128 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5129 | size = PyBytes_GET_SIZE(ub); |
| 5130 | *out = PyMem_Malloc(size + 1); |
| 5131 | if (*out) { |
| 5132 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 5133 | result = 1; |
| 5134 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5135 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5136 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5137 | Py_DECREF(ub); |
| 5138 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5139 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5140 | #endif |
| 5141 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5142 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5143 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5144 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5145 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5146 | Py_ssize_t i, pos, envc; |
| 5147 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5148 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5149 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5150 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5151 | i = PyMapping_Size(env); |
| 5152 | if (i < 0) |
| 5153 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5154 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5155 | if (envlist == NULL) { |
| 5156 | PyErr_NoMemory(); |
| 5157 | return NULL; |
| 5158 | } |
| 5159 | envc = 0; |
| 5160 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5161 | if (!keys) |
| 5162 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5163 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5164 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5165 | goto error; |
| 5166 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5167 | PyErr_Format(PyExc_TypeError, |
| 5168 | "env.keys() or env.values() is not a list"); |
| 5169 | goto error; |
| 5170 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5171 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5172 | for (pos = 0; pos < i; pos++) { |
| 5173 | key = PyList_GetItem(keys, pos); |
| 5174 | val = PyList_GetItem(vals, pos); |
| 5175 | if (!key || !val) |
| 5176 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5177 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5178 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5179 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 5180 | goto error; |
| 5181 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 5182 | Py_DECREF(key2); |
| 5183 | goto error; |
| 5184 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5185 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 5186 | defining hidden environment variables. */ |
| 5187 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 5188 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 5189 | { |
| 5190 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5191 | Py_DECREF(key2); |
| 5192 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5193 | goto error; |
| 5194 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5195 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 5196 | #else |
| 5197 | if (!PyUnicode_FSConverter(key, &key2)) |
| 5198 | goto error; |
| 5199 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 5200 | Py_DECREF(key2); |
| 5201 | goto error; |
| 5202 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5203 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 5204 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 5205 | { |
| 5206 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5207 | Py_DECREF(key2); |
| 5208 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5209 | goto error; |
| 5210 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5211 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 5212 | PyBytes_AS_STRING(val2)); |
| 5213 | #endif |
| 5214 | Py_DECREF(key2); |
| 5215 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5216 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5217 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5218 | |
| 5219 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 5220 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5221 | goto error; |
| 5222 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5223 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5224 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5225 | } |
| 5226 | Py_DECREF(vals); |
| 5227 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5228 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5229 | envlist[envc] = 0; |
| 5230 | *envc_ptr = envc; |
| 5231 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5232 | |
| 5233 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5234 | Py_XDECREF(keys); |
| 5235 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5236 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5237 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5238 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5239 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5240 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5241 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5242 | { |
| 5243 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5244 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5245 | if (argvlist == NULL) { |
| 5246 | PyErr_NoMemory(); |
| 5247 | return NULL; |
| 5248 | } |
| 5249 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5250 | PyObject* item = PySequence_ITEM(argv, i); |
| 5251 | if (item == NULL) |
| 5252 | goto fail; |
| 5253 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5254 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5255 | goto fail; |
| 5256 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5257 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5258 | } |
| 5259 | argvlist[*argc] = NULL; |
| 5260 | return argvlist; |
| 5261 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5262 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5263 | free_string_array(argvlist, *argc); |
| 5264 | return NULL; |
| 5265 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5266 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5267 | #endif |
| 5268 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5269 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5270 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5271 | /*[clinic input] |
| 5272 | os.execv |
| 5273 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5274 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5275 | Path of executable file. |
| 5276 | argv: object |
| 5277 | Tuple or list of strings. |
| 5278 | / |
| 5279 | |
| 5280 | Execute an executable path with arguments, replacing current process. |
| 5281 | [clinic start generated code]*/ |
| 5282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5283 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5284 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5285 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5286 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5287 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5288 | Py_ssize_t argc; |
| 5289 | |
| 5290 | /* execv has two arguments: (path, argv), where |
| 5291 | argv is a list or tuple of strings. */ |
| 5292 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5293 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5294 | PyErr_SetString(PyExc_TypeError, |
| 5295 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5296 | return NULL; |
| 5297 | } |
| 5298 | argc = PySequence_Size(argv); |
| 5299 | if (argc < 1) { |
| 5300 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5301 | return NULL; |
| 5302 | } |
| 5303 | |
| 5304 | argvlist = parse_arglist(argv, &argc); |
| 5305 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5306 | return NULL; |
| 5307 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5308 | if (!argvlist[0][0]) { |
| 5309 | PyErr_SetString(PyExc_ValueError, |
| 5310 | "execv() arg 2 first element cannot be empty"); |
| 5311 | free_string_array(argvlist, argc); |
| 5312 | return NULL; |
| 5313 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5314 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5315 | if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5316 | free_string_array(argvlist, argc); |
| 5317 | return NULL; |
| 5318 | } |
| 5319 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5320 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5321 | #ifdef HAVE_WEXECV |
| 5322 | _wexecv(path->wide, argvlist); |
| 5323 | #else |
| 5324 | execv(path->narrow, argvlist); |
| 5325 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5326 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5327 | |
| 5328 | /* If we get here it's definitely an error */ |
| 5329 | |
| 5330 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5331 | return posix_error(); |
| 5332 | } |
| 5333 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5334 | |
| 5335 | /*[clinic input] |
| 5336 | os.execve |
| 5337 | |
| 5338 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5339 | Path of executable file. |
| 5340 | argv: object |
| 5341 | Tuple or list of strings. |
| 5342 | env: object |
| 5343 | Dictionary of strings mapping to strings. |
| 5344 | |
| 5345 | Execute an executable path with arguments, replacing current process. |
| 5346 | [clinic start generated code]*/ |
| 5347 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5348 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5349 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5350 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5351 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5352 | EXECV_CHAR **argvlist = NULL; |
| 5353 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5354 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5355 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5356 | /* execve has three arguments: (path, argv, env), where |
| 5357 | argv is a list or tuple of strings and env is a dictionary |
| 5358 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5359 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5360 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5361 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5362 | "execve: argv must be a tuple or list"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5363 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5364 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5365 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5366 | if (argc < 1) { |
| 5367 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5368 | return NULL; |
| 5369 | } |
| 5370 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5371 | if (!PyMapping_Check(env)) { |
| 5372 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5373 | "execve: environment must be a mapping object"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5374 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5375 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5376 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5377 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5378 | if (argvlist == NULL) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5379 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5380 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5381 | if (!argvlist[0][0]) { |
| 5382 | PyErr_SetString(PyExc_ValueError, |
| 5383 | "execve: argv first element cannot be empty"); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5384 | goto fail_0; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5385 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5386 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5387 | envlist = parse_envlist(env, &envc); |
| 5388 | if (envlist == NULL) |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5389 | goto fail_0; |
| 5390 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5391 | if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5392 | goto fail_1; |
| 5393 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5394 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5395 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5396 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5397 | if (path->fd > -1) |
| 5398 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5399 | else |
| 5400 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5401 | #ifdef HAVE_WEXECV |
| 5402 | _wexecve(path->wide, argvlist, envlist); |
| 5403 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5404 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5405 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5406 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5407 | |
| 5408 | /* If we get here it's definitely an error */ |
| 5409 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5410 | posix_path_error(path); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5411 | fail_1: |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5412 | free_string_array(envlist, envc); |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5413 | fail_0: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5414 | if (argvlist) |
| 5415 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5416 | return NULL; |
| 5417 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5418 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5419 | #endif /* HAVE_EXECV */ |
| 5420 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5421 | #ifdef HAVE_POSIX_SPAWN |
| 5422 | |
| 5423 | enum posix_spawn_file_actions_identifier { |
| 5424 | POSIX_SPAWN_OPEN, |
| 5425 | POSIX_SPAWN_CLOSE, |
| 5426 | POSIX_SPAWN_DUP2 |
| 5427 | }; |
| 5428 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5429 | #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] | 5430 | static int |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5431 | convert_sched_param(PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5432 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5433 | |
| 5434 | static int |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5435 | parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup, |
| 5436 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5437 | PyObject *setsigdef, PyObject *scheduler, |
| 5438 | posix_spawnattr_t *attrp) |
| 5439 | { |
| 5440 | long all_flags = 0; |
| 5441 | |
| 5442 | errno = posix_spawnattr_init(attrp); |
| 5443 | if (errno) { |
| 5444 | posix_error(); |
| 5445 | return -1; |
| 5446 | } |
| 5447 | |
| 5448 | if (setpgroup) { |
| 5449 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5450 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5451 | goto fail; |
| 5452 | } |
| 5453 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5454 | if (errno) { |
| 5455 | posix_error(); |
| 5456 | goto fail; |
| 5457 | } |
| 5458 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5459 | } |
| 5460 | |
| 5461 | if (resetids) { |
| 5462 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5463 | } |
| 5464 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5465 | if (setsid) { |
| 5466 | #ifdef POSIX_SPAWN_SETSID |
| 5467 | all_flags |= POSIX_SPAWN_SETSID; |
| 5468 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5469 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5470 | #else |
| 5471 | argument_unavailable_error(func_name, "setsid"); |
| 5472 | return -1; |
| 5473 | #endif |
| 5474 | } |
| 5475 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5476 | if (setsigmask) { |
| 5477 | sigset_t set; |
| 5478 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5479 | goto fail; |
| 5480 | } |
| 5481 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5482 | if (errno) { |
| 5483 | posix_error(); |
| 5484 | goto fail; |
| 5485 | } |
| 5486 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5487 | } |
| 5488 | |
| 5489 | if (setsigdef) { |
| 5490 | sigset_t set; |
| 5491 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5492 | goto fail; |
| 5493 | } |
| 5494 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5495 | if (errno) { |
| 5496 | posix_error(); |
| 5497 | goto fail; |
| 5498 | } |
| 5499 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5500 | } |
| 5501 | |
| 5502 | if (scheduler) { |
| 5503 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5504 | PyObject *py_schedpolicy; |
| 5505 | struct sched_param schedparam; |
| 5506 | |
| 5507 | if (!PyArg_ParseTuple(scheduler, "OO&" |
| 5508 | ";A scheduler tuple must have two elements", |
| 5509 | &py_schedpolicy, convert_sched_param, &schedparam)) { |
| 5510 | goto fail; |
| 5511 | } |
| 5512 | if (py_schedpolicy != Py_None) { |
| 5513 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5514 | |
| 5515 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5516 | goto fail; |
| 5517 | } |
| 5518 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5519 | if (errno) { |
| 5520 | posix_error(); |
| 5521 | goto fail; |
| 5522 | } |
| 5523 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5524 | } |
| 5525 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5526 | if (errno) { |
| 5527 | posix_error(); |
| 5528 | goto fail; |
| 5529 | } |
| 5530 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5531 | #else |
| 5532 | PyErr_SetString(PyExc_NotImplementedError, |
| 5533 | "The scheduler option is not supported in this system."); |
| 5534 | goto fail; |
| 5535 | #endif |
| 5536 | } |
| 5537 | |
| 5538 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5539 | if (errno) { |
| 5540 | posix_error(); |
| 5541 | goto fail; |
| 5542 | } |
| 5543 | |
| 5544 | return 0; |
| 5545 | |
| 5546 | fail: |
| 5547 | (void)posix_spawnattr_destroy(attrp); |
| 5548 | return -1; |
| 5549 | } |
| 5550 | |
| 5551 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5552 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5553 | posix_spawn_file_actions_t *file_actionsp, |
| 5554 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5555 | { |
| 5556 | PyObject *seq; |
| 5557 | PyObject *file_action = NULL; |
| 5558 | PyObject *tag_obj; |
| 5559 | |
| 5560 | seq = PySequence_Fast(file_actions, |
| 5561 | "file_actions must be a sequence or None"); |
| 5562 | if (seq == NULL) { |
| 5563 | return -1; |
| 5564 | } |
| 5565 | |
| 5566 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5567 | if (errno) { |
| 5568 | posix_error(); |
| 5569 | Py_DECREF(seq); |
| 5570 | return -1; |
| 5571 | } |
| 5572 | |
Zackery Spytz | d52a83a | 2019-06-26 14:54:20 -0600 | [diff] [blame] | 5573 | 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] | 5574 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5575 | Py_INCREF(file_action); |
| 5576 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5577 | PyErr_SetString(PyExc_TypeError, |
| 5578 | "Each file_actions element must be a non-empty tuple"); |
| 5579 | goto fail; |
| 5580 | } |
| 5581 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5582 | if (tag == -1 && PyErr_Occurred()) { |
| 5583 | goto fail; |
| 5584 | } |
| 5585 | |
| 5586 | /* Populate the file_actions object */ |
| 5587 | switch (tag) { |
| 5588 | case POSIX_SPAWN_OPEN: { |
| 5589 | int fd, oflag; |
| 5590 | PyObject *path; |
| 5591 | unsigned long mode; |
| 5592 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5593 | ";A open file_action tuple must have 5 elements", |
| 5594 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5595 | &oflag, &mode)) |
| 5596 | { |
| 5597 | goto fail; |
| 5598 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5599 | if (PyList_Append(temp_buffer, path)) { |
| 5600 | Py_DECREF(path); |
| 5601 | goto fail; |
| 5602 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5603 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5604 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5605 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5606 | if (errno) { |
| 5607 | posix_error(); |
| 5608 | goto fail; |
| 5609 | } |
| 5610 | break; |
| 5611 | } |
| 5612 | case POSIX_SPAWN_CLOSE: { |
| 5613 | int fd; |
| 5614 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5615 | ";A close file_action tuple must have 2 elements", |
| 5616 | &tag_obj, &fd)) |
| 5617 | { |
| 5618 | goto fail; |
| 5619 | } |
| 5620 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5621 | if (errno) { |
| 5622 | posix_error(); |
| 5623 | goto fail; |
| 5624 | } |
| 5625 | break; |
| 5626 | } |
| 5627 | case POSIX_SPAWN_DUP2: { |
| 5628 | int fd1, fd2; |
| 5629 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5630 | ";A dup2 file_action tuple must have 3 elements", |
| 5631 | &tag_obj, &fd1, &fd2)) |
| 5632 | { |
| 5633 | goto fail; |
| 5634 | } |
| 5635 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5636 | fd1, fd2); |
| 5637 | if (errno) { |
| 5638 | posix_error(); |
| 5639 | goto fail; |
| 5640 | } |
| 5641 | break; |
| 5642 | } |
| 5643 | default: { |
| 5644 | PyErr_SetString(PyExc_TypeError, |
| 5645 | "Unknown file_actions identifier"); |
| 5646 | goto fail; |
| 5647 | } |
| 5648 | } |
| 5649 | Py_DECREF(file_action); |
| 5650 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5651 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5652 | Py_DECREF(seq); |
| 5653 | return 0; |
| 5654 | |
| 5655 | fail: |
| 5656 | Py_DECREF(seq); |
| 5657 | Py_DECREF(file_action); |
| 5658 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5659 | return -1; |
| 5660 | } |
| 5661 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5662 | |
| 5663 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5664 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 5665 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5666 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5667 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5668 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5669 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5670 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5671 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5672 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5673 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5674 | posix_spawnattr_t attr; |
| 5675 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5676 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5677 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5678 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5679 | pid_t pid; |
| 5680 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5681 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5682 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5683 | 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] | 5684 | like posix.environ. */ |
| 5685 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5686 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5687 | PyErr_Format(PyExc_TypeError, |
| 5688 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5689 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5690 | } |
| 5691 | argc = PySequence_Size(argv); |
| 5692 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5693 | PyErr_Format(PyExc_ValueError, |
| 5694 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5695 | return NULL; |
| 5696 | } |
| 5697 | |
| 5698 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5699 | PyErr_Format(PyExc_TypeError, |
| 5700 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5701 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5702 | } |
| 5703 | |
| 5704 | argvlist = parse_arglist(argv, &argc); |
| 5705 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5706 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5707 | } |
| 5708 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5709 | PyErr_Format(PyExc_ValueError, |
| 5710 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5711 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5712 | } |
| 5713 | |
| 5714 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5715 | if (envlist == NULL) { |
| 5716 | goto exit; |
| 5717 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5718 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 5719 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5720 | /* There is a bug in old versions of glibc that makes some of the |
| 5721 | * helper functions for manipulating file actions not copy the provided |
| 5722 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5723 | * copy the value of path for some old versions of glibc (<2.20). |
| 5724 | * The use of temp_buffer here is a workaround that keeps the |
| 5725 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5726 | * Check https://bugs.python.org/issue33630 and |
| 5727 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5728 | temp_buffer = PyList_New(0); |
| 5729 | if (!temp_buffer) { |
| 5730 | goto exit; |
| 5731 | } |
| 5732 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5733 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5734 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5735 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5736 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5737 | |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5738 | if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid, |
| 5739 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5740 | goto exit; |
| 5741 | } |
| 5742 | attrp = &attr; |
| 5743 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5744 | if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5745 | goto exit; |
| 5746 | } |
| 5747 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5748 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5749 | #ifdef HAVE_POSIX_SPAWNP |
| 5750 | if (use_posix_spawnp) { |
| 5751 | err_code = posix_spawnp(&pid, path->narrow, |
| 5752 | file_actionsp, attrp, argvlist, envlist); |
| 5753 | } |
| 5754 | else |
| 5755 | #endif /* HAVE_POSIX_SPAWNP */ |
| 5756 | { |
| 5757 | err_code = posix_spawn(&pid, path->narrow, |
| 5758 | file_actionsp, attrp, argvlist, envlist); |
| 5759 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5760 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5761 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5762 | if (err_code) { |
| 5763 | errno = err_code; |
| 5764 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5765 | goto exit; |
| 5766 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5767 | #ifdef _Py_MEMORY_SANITIZER |
| 5768 | __msan_unpoison(&pid, sizeof(pid)); |
| 5769 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5770 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5771 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5772 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5773 | if (file_actionsp) { |
| 5774 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5775 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5776 | if (attrp) { |
| 5777 | (void)posix_spawnattr_destroy(attrp); |
| 5778 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5779 | if (envlist) { |
| 5780 | free_string_array(envlist, envc); |
| 5781 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5782 | if (argvlist) { |
| 5783 | free_string_array(argvlist, argc); |
| 5784 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5785 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5786 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5787 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5788 | |
| 5789 | |
| 5790 | /*[clinic input] |
| 5791 | |
| 5792 | os.posix_spawn |
| 5793 | path: path_t |
| 5794 | Path of executable file. |
| 5795 | argv: object |
| 5796 | Tuple or list of strings. |
| 5797 | env: object |
| 5798 | Dictionary of strings mapping to strings. |
| 5799 | / |
| 5800 | * |
| 5801 | file_actions: object(c_default='NULL') = () |
| 5802 | A sequence of file action tuples. |
| 5803 | setpgroup: object = NULL |
| 5804 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5805 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5806 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 5807 | setsid: bool(accept={int}) = False |
| 5808 | 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] | 5809 | setsigmask: object(c_default='NULL') = () |
| 5810 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5811 | setsigdef: object(c_default='NULL') = () |
| 5812 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5813 | scheduler: object = NULL |
| 5814 | A tuple with the scheduler policy (optional) and parameters. |
| 5815 | |
| 5816 | Execute the program specified by path in a new process. |
| 5817 | [clinic start generated code]*/ |
| 5818 | |
| 5819 | static PyObject * |
| 5820 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5821 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5822 | PyObject *setpgroup, int resetids, int setsid, |
| 5823 | PyObject *setsigmask, PyObject *setsigdef, |
| 5824 | PyObject *scheduler) |
| 5825 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5826 | { |
| 5827 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5828 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5829 | scheduler); |
| 5830 | } |
| 5831 | #endif /* HAVE_POSIX_SPAWN */ |
| 5832 | |
| 5833 | |
| 5834 | |
| 5835 | #ifdef HAVE_POSIX_SPAWNP |
| 5836 | /*[clinic input] |
| 5837 | |
| 5838 | os.posix_spawnp |
| 5839 | path: path_t |
| 5840 | Path of executable file. |
| 5841 | argv: object |
| 5842 | Tuple or list of strings. |
| 5843 | env: object |
| 5844 | Dictionary of strings mapping to strings. |
| 5845 | / |
| 5846 | * |
| 5847 | file_actions: object(c_default='NULL') = () |
| 5848 | A sequence of file action tuples. |
| 5849 | setpgroup: object = NULL |
| 5850 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5851 | resetids: bool(accept={int}) = False |
| 5852 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5853 | setsid: bool(accept={int}) = False |
| 5854 | 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] | 5855 | setsigmask: object(c_default='NULL') = () |
| 5856 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5857 | setsigdef: object(c_default='NULL') = () |
| 5858 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5859 | scheduler: object = NULL |
| 5860 | A tuple with the scheduler policy (optional) and parameters. |
| 5861 | |
| 5862 | Execute the program specified by path in a new process. |
| 5863 | [clinic start generated code]*/ |
| 5864 | |
| 5865 | static PyObject * |
| 5866 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5867 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5868 | PyObject *setpgroup, int resetids, int setsid, |
| 5869 | PyObject *setsigmask, PyObject *setsigdef, |
| 5870 | PyObject *scheduler) |
| 5871 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5872 | { |
| 5873 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5874 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5875 | scheduler); |
| 5876 | } |
| 5877 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5878 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5879 | #ifdef HAVE_RTPSPAWN |
| 5880 | static intptr_t |
| 5881 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], |
| 5882 | const char *envp[]) |
| 5883 | { |
| 5884 | RTP_ID rtpid; |
| 5885 | int status; |
| 5886 | pid_t res; |
| 5887 | int async_err = 0; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5888 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5889 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
| 5890 | uStackSize=0 cannot be used, the default stack size is too small for |
| 5891 | Python. */ |
| 5892 | if (envp) { |
| 5893 | rtpid = rtpSpawn(rtpFileName, argv, envp, |
| 5894 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5895 | } |
| 5896 | else { |
| 5897 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, |
| 5898 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5899 | } |
| 5900 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { |
| 5901 | do { |
| 5902 | res = waitpid((pid_t)rtpid, &status, 0); |
| 5903 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 5904 | |
| 5905 | if (res < 0) |
| 5906 | return RTP_ID_ERROR; |
| 5907 | return ((intptr_t)status); |
| 5908 | } |
| 5909 | return ((intptr_t)rtpid); |
| 5910 | } |
| 5911 | #endif |
| 5912 | |
| 5913 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5914 | /*[clinic input] |
| 5915 | os.spawnv |
| 5916 | |
| 5917 | mode: int |
| 5918 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5919 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5920 | Path of executable file. |
| 5921 | argv: object |
| 5922 | Tuple or list of strings. |
| 5923 | / |
| 5924 | |
| 5925 | Execute the program specified by path in a new process. |
| 5926 | [clinic start generated code]*/ |
| 5927 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5928 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5929 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5930 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5931 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5932 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5933 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5934 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5935 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5936 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5937 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5938 | /* spawnv has three arguments: (mode, path, argv), where |
| 5939 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5940 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5941 | if (PyList_Check(argv)) { |
| 5942 | argc = PyList_Size(argv); |
| 5943 | getitem = PyList_GetItem; |
| 5944 | } |
| 5945 | else if (PyTuple_Check(argv)) { |
| 5946 | argc = PyTuple_Size(argv); |
| 5947 | getitem = PyTuple_GetItem; |
| 5948 | } |
| 5949 | else { |
| 5950 | PyErr_SetString(PyExc_TypeError, |
| 5951 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5952 | return NULL; |
| 5953 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5954 | if (argc == 0) { |
| 5955 | PyErr_SetString(PyExc_ValueError, |
| 5956 | "spawnv() arg 2 cannot be empty"); |
| 5957 | return NULL; |
| 5958 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5959 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5960 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5961 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5962 | return PyErr_NoMemory(); |
| 5963 | } |
| 5964 | for (i = 0; i < argc; i++) { |
| 5965 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5966 | &argvlist[i])) { |
| 5967 | free_string_array(argvlist, i); |
| 5968 | PyErr_SetString( |
| 5969 | PyExc_TypeError, |
| 5970 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5971 | return NULL; |
| 5972 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5973 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5974 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5975 | PyErr_SetString( |
| 5976 | PyExc_ValueError, |
| 5977 | "spawnv() arg 2 first element cannot be empty"); |
| 5978 | return NULL; |
| 5979 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5980 | } |
| 5981 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5982 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5983 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5984 | if (mode == _OLD_P_OVERLAY) |
| 5985 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5986 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5987 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 5988 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 5989 | Py_None) < 0) { |
| 5990 | free_string_array(argvlist, argc); |
| 5991 | return NULL; |
| 5992 | } |
| 5993 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5994 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5995 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5996 | #ifdef HAVE_WSPAWNV |
| 5997 | spawnval = _wspawnv(mode, path->wide, argvlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5998 | #elif defined(HAVE_RTPSPAWN) |
| 5999 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6000 | #else |
| 6001 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 6002 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6003 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6004 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6005 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6006 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6007 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6008 | if (spawnval == -1) |
| 6009 | return posix_error(); |
| 6010 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6011 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6012 | } |
| 6013 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6014 | /*[clinic input] |
| 6015 | os.spawnve |
| 6016 | |
| 6017 | mode: int |
| 6018 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6019 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6020 | Path of executable file. |
| 6021 | argv: object |
| 6022 | Tuple or list of strings. |
| 6023 | env: object |
| 6024 | Dictionary of strings mapping to strings. |
| 6025 | / |
| 6026 | |
| 6027 | Execute the program specified by path in a new process. |
| 6028 | [clinic start generated code]*/ |
| 6029 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6030 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6031 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6032 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6033 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6034 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6035 | EXECV_CHAR **argvlist; |
| 6036 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6037 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 6038 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 6039 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6040 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6041 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6042 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6043 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 6044 | argv is a list or tuple of strings and env is a dictionary |
| 6045 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6046 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6047 | if (PyList_Check(argv)) { |
| 6048 | argc = PyList_Size(argv); |
| 6049 | getitem = PyList_GetItem; |
| 6050 | } |
| 6051 | else if (PyTuple_Check(argv)) { |
| 6052 | argc = PyTuple_Size(argv); |
| 6053 | getitem = PyTuple_GetItem; |
| 6054 | } |
| 6055 | else { |
| 6056 | PyErr_SetString(PyExc_TypeError, |
| 6057 | "spawnve() arg 2 must be a tuple or list"); |
| 6058 | goto fail_0; |
| 6059 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 6060 | if (argc == 0) { |
| 6061 | PyErr_SetString(PyExc_ValueError, |
| 6062 | "spawnve() arg 2 cannot be empty"); |
| 6063 | goto fail_0; |
| 6064 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6065 | if (!PyMapping_Check(env)) { |
| 6066 | PyErr_SetString(PyExc_TypeError, |
| 6067 | "spawnve() arg 3 must be a mapping object"); |
| 6068 | goto fail_0; |
| 6069 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6070 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6071 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6072 | if (argvlist == NULL) { |
| 6073 | PyErr_NoMemory(); |
| 6074 | goto fail_0; |
| 6075 | } |
| 6076 | for (i = 0; i < argc; i++) { |
| 6077 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 6078 | &argvlist[i])) |
| 6079 | { |
| 6080 | lastarg = i; |
| 6081 | goto fail_1; |
| 6082 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6083 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6084 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6085 | PyErr_SetString( |
| 6086 | PyExc_ValueError, |
| 6087 | "spawnv() arg 2 first element cannot be empty"); |
| 6088 | goto fail_1; |
| 6089 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6090 | } |
| 6091 | lastarg = argc; |
| 6092 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6093 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6094 | envlist = parse_envlist(env, &envc); |
| 6095 | if (envlist == NULL) |
| 6096 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6097 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6098 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6099 | if (mode == _OLD_P_OVERLAY) |
| 6100 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6101 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6102 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6103 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6104 | goto fail_2; |
| 6105 | } |
| 6106 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6107 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6108 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6109 | #ifdef HAVE_WSPAWNV |
| 6110 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6111 | #elif defined(HAVE_RTPSPAWN) |
| 6112 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, |
| 6113 | (const char **)envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6114 | #else |
| 6115 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 6116 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6117 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6118 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6119 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6120 | if (spawnval == -1) |
| 6121 | (void) posix_error(); |
| 6122 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6123 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6124 | |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 6125 | fail_2: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6126 | while (--envc >= 0) |
| 6127 | PyMem_DEL(envlist[envc]); |
| 6128 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 6129 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6130 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6131 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6132 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6133 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6134 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6135 | #endif /* HAVE_SPAWNV */ |
| 6136 | |
| 6137 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6138 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6139 | |
| 6140 | /* Helper function to validate arguments. |
| 6141 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 6142 | If obj is non-NULL it must be callable. */ |
| 6143 | static int |
| 6144 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 6145 | { |
| 6146 | if (obj && !PyCallable_Check(obj)) { |
| 6147 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6148 | obj_name, _PyType_Name(Py_TYPE(obj))); |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6149 | return -1; |
| 6150 | } |
| 6151 | return 0; |
| 6152 | } |
| 6153 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6154 | /*[clinic input] |
| 6155 | os.register_at_fork |
| 6156 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6157 | * |
| 6158 | before: object=NULL |
| 6159 | A callable to be called in the parent before the fork() syscall. |
| 6160 | after_in_child: object=NULL |
| 6161 | A callable to be called in the child after fork(). |
| 6162 | after_in_parent: object=NULL |
| 6163 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6164 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6165 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6166 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6167 | 'before' callbacks are called in reverse order. |
| 6168 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6169 | |
| 6170 | [clinic start generated code]*/ |
| 6171 | |
| 6172 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6173 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 6174 | PyObject *after_in_child, PyObject *after_in_parent) |
| 6175 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6176 | { |
| 6177 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6178 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6179 | if (!before && !after_in_child && !after_in_parent) { |
| 6180 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 6181 | return NULL; |
| 6182 | } |
| 6183 | if (check_null_or_callable(before, "before") || |
| 6184 | check_null_or_callable(after_in_child, "after_in_child") || |
| 6185 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6186 | return NULL; |
| 6187 | } |
Victor Stinner | ff4584c | 2020-03-13 18:03:56 +0100 | [diff] [blame] | 6188 | interp = _PyInterpreterState_GET_UNSAFE(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6189 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6190 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6191 | return NULL; |
| 6192 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6193 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6194 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6195 | } |
| 6196 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 6197 | return NULL; |
| 6198 | } |
| 6199 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6200 | } |
| 6201 | #endif /* HAVE_FORK */ |
| 6202 | |
| 6203 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6204 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6205 | /*[clinic input] |
| 6206 | os.fork1 |
| 6207 | |
| 6208 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 6209 | |
| 6210 | Return 0 to child process and PID of child to parent process. |
| 6211 | [clinic start generated code]*/ |
| 6212 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6213 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6214 | os_fork1_impl(PyObject *module) |
| 6215 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6216 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6217 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6218 | |
Victor Stinner | ff4584c | 2020-03-13 18:03:56 +0100 | [diff] [blame] | 6219 | if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6220 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6221 | return NULL; |
| 6222 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6223 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6224 | pid = fork1(); |
| 6225 | if (pid == 0) { |
| 6226 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6227 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6228 | } else { |
| 6229 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6230 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6231 | } |
| 6232 | if (pid == -1) |
| 6233 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6234 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6235 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6236 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6237 | |
| 6238 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6239 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6240 | /*[clinic input] |
| 6241 | os.fork |
| 6242 | |
| 6243 | Fork a child process. |
| 6244 | |
| 6245 | Return 0 to child process and PID of child to parent process. |
| 6246 | [clinic start generated code]*/ |
| 6247 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6248 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6249 | os_fork_impl(PyObject *module) |
| 6250 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6251 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6252 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6253 | |
Victor Stinner | ff4584c | 2020-03-13 18:03:56 +0100 | [diff] [blame] | 6254 | if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6255 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6256 | return NULL; |
| 6257 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6258 | if (PySys_Audit("os.fork", NULL) < 0) { |
| 6259 | return NULL; |
| 6260 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6261 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6262 | pid = fork(); |
| 6263 | if (pid == 0) { |
| 6264 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6265 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6266 | } else { |
| 6267 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6268 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6269 | } |
| 6270 | if (pid == -1) |
| 6271 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6272 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6273 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6274 | #endif /* HAVE_FORK */ |
| 6275 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6276 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6277 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6278 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6279 | /*[clinic input] |
| 6280 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6281 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6282 | policy: int |
| 6283 | |
| 6284 | Get the maximum scheduling priority for policy. |
| 6285 | [clinic start generated code]*/ |
| 6286 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6287 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6288 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 6289 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6290 | { |
| 6291 | int max; |
| 6292 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6293 | max = sched_get_priority_max(policy); |
| 6294 | if (max < 0) |
| 6295 | return posix_error(); |
| 6296 | return PyLong_FromLong(max); |
| 6297 | } |
| 6298 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6299 | |
| 6300 | /*[clinic input] |
| 6301 | os.sched_get_priority_min |
| 6302 | |
| 6303 | policy: int |
| 6304 | |
| 6305 | Get the minimum scheduling priority for policy. |
| 6306 | [clinic start generated code]*/ |
| 6307 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6308 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6309 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 6310 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6311 | { |
| 6312 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6313 | if (min < 0) |
| 6314 | return posix_error(); |
| 6315 | return PyLong_FromLong(min); |
| 6316 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6317 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 6318 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6319 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6320 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 6321 | /*[clinic input] |
| 6322 | os.sched_getscheduler |
| 6323 | pid: pid_t |
| 6324 | / |
| 6325 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6326 | Get the scheduling policy for the process identified by pid. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6327 | |
| 6328 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6329 | [clinic start generated code]*/ |
| 6330 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6331 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6332 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6333 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6334 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6335 | int policy; |
| 6336 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6337 | policy = sched_getscheduler(pid); |
| 6338 | if (policy < 0) |
| 6339 | return posix_error(); |
| 6340 | return PyLong_FromLong(policy); |
| 6341 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6342 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6343 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6344 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6345 | #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] | 6346 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6347 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6348 | |
| 6349 | @classmethod |
| 6350 | os.sched_param.__new__ |
| 6351 | |
| 6352 | sched_priority: object |
| 6353 | A scheduling parameter. |
| 6354 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6355 | Currently has only one field: sched_priority |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6356 | [clinic start generated code]*/ |
| 6357 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6358 | static PyObject * |
| 6359 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6360 | /*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6361 | { |
| 6362 | PyObject *res; |
| 6363 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6364 | res = PyStructSequence_New(type); |
| 6365 | if (!res) |
| 6366 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6367 | Py_INCREF(sched_priority); |
| 6368 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6369 | return res; |
| 6370 | } |
| 6371 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6372 | PyDoc_VAR(os_sched_param__doc__); |
| 6373 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6374 | static PyStructSequence_Field sched_param_fields[] = { |
| 6375 | {"sched_priority", "the scheduling priority"}, |
| 6376 | {0} |
| 6377 | }; |
| 6378 | |
| 6379 | static PyStructSequence_Desc sched_param_desc = { |
| 6380 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6381 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6382 | sched_param_fields, |
| 6383 | 1 |
| 6384 | }; |
| 6385 | |
| 6386 | static int |
| 6387 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 6388 | { |
| 6389 | long priority; |
| 6390 | |
Andy Lester | 5572870 | 2020-03-06 16:53:17 -0600 | [diff] [blame] | 6391 | if (!Py_IS_TYPE(param, (PyTypeObject *)_posixstate_global->SchedParamType)) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6392 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6393 | return 0; |
| 6394 | } |
| 6395 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6396 | if (priority == -1 && PyErr_Occurred()) |
| 6397 | return 0; |
| 6398 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6399 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6400 | return 0; |
| 6401 | } |
| 6402 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6403 | return 1; |
| 6404 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6405 | #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] | 6406 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6407 | |
| 6408 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6409 | /*[clinic input] |
| 6410 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6411 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6412 | pid: pid_t |
| 6413 | policy: int |
| 6414 | param: sched_param |
| 6415 | / |
| 6416 | |
| 6417 | Set the scheduling policy for the process identified by pid. |
| 6418 | |
| 6419 | If pid is 0, the calling process is changed. |
| 6420 | param is an instance of sched_param. |
| 6421 | [clinic start generated code]*/ |
| 6422 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6423 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6424 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6425 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6426 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6427 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6428 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6429 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6430 | ** scheduling policy under Solaris/Illumos, and others. |
| 6431 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6432 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6433 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6434 | return posix_error(); |
| 6435 | Py_RETURN_NONE; |
| 6436 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6437 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6438 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6439 | |
| 6440 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6441 | /*[clinic input] |
| 6442 | os.sched_getparam |
| 6443 | pid: pid_t |
| 6444 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6445 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6446 | Returns scheduling parameters for the process identified by pid. |
| 6447 | |
| 6448 | If pid is 0, returns parameters for the calling process. |
| 6449 | Return value is an instance of sched_param. |
| 6450 | [clinic start generated code]*/ |
| 6451 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6452 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6453 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6454 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6455 | { |
| 6456 | struct sched_param param; |
| 6457 | PyObject *result; |
| 6458 | PyObject *priority; |
| 6459 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6460 | if (sched_getparam(pid, ¶m)) |
| 6461 | return posix_error(); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6462 | PyObject *SchedParamType = _posixstate_global->SchedParamType; |
| 6463 | result = PyStructSequence_New((PyTypeObject *)SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6464 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6465 | return NULL; |
| 6466 | priority = PyLong_FromLong(param.sched_priority); |
| 6467 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6468 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6469 | return NULL; |
| 6470 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6471 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6472 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6473 | } |
| 6474 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6475 | |
| 6476 | /*[clinic input] |
| 6477 | os.sched_setparam |
| 6478 | pid: pid_t |
| 6479 | param: sched_param |
| 6480 | / |
| 6481 | |
| 6482 | Set scheduling parameters for the process identified by pid. |
| 6483 | |
| 6484 | If pid is 0, sets parameters for the calling process. |
| 6485 | param should be an instance of sched_param. |
| 6486 | [clinic start generated code]*/ |
| 6487 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6488 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6489 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6490 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6491 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6492 | { |
| 6493 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6494 | return posix_error(); |
| 6495 | Py_RETURN_NONE; |
| 6496 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6497 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6498 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6499 | |
| 6500 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6501 | /*[clinic input] |
| 6502 | os.sched_rr_get_interval -> double |
| 6503 | pid: pid_t |
| 6504 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6505 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6506 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6507 | |
| 6508 | Value returned is a float. |
| 6509 | [clinic start generated code]*/ |
| 6510 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6511 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6512 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6513 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6514 | { |
| 6515 | struct timespec interval; |
| 6516 | if (sched_rr_get_interval(pid, &interval)) { |
| 6517 | posix_error(); |
| 6518 | return -1.0; |
| 6519 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6520 | #ifdef _Py_MEMORY_SANITIZER |
| 6521 | __msan_unpoison(&interval, sizeof(interval)); |
| 6522 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6523 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6524 | } |
| 6525 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6526 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6527 | |
| 6528 | /*[clinic input] |
| 6529 | os.sched_yield |
| 6530 | |
| 6531 | Voluntarily relinquish the CPU. |
| 6532 | [clinic start generated code]*/ |
| 6533 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6534 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6535 | os_sched_yield_impl(PyObject *module) |
| 6536 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6537 | { |
| 6538 | if (sched_yield()) |
| 6539 | return posix_error(); |
| 6540 | Py_RETURN_NONE; |
| 6541 | } |
| 6542 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6543 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6544 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6545 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6546 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6547 | /*[clinic input] |
| 6548 | os.sched_setaffinity |
| 6549 | pid: pid_t |
| 6550 | mask : object |
| 6551 | / |
| 6552 | |
| 6553 | Set the CPU affinity of the process identified by pid to mask. |
| 6554 | |
| 6555 | mask should be an iterable of integers identifying CPUs. |
| 6556 | [clinic start generated code]*/ |
| 6557 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6558 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6559 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6560 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6561 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6562 | int ncpus; |
| 6563 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6564 | cpu_set_t *cpu_set = NULL; |
| 6565 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6566 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6567 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6568 | if (iterator == NULL) |
| 6569 | return NULL; |
| 6570 | |
| 6571 | ncpus = NCPUS_START; |
| 6572 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6573 | cpu_set = CPU_ALLOC(ncpus); |
| 6574 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6575 | PyErr_NoMemory(); |
| 6576 | goto error; |
| 6577 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6578 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6579 | |
| 6580 | while ((item = PyIter_Next(iterator))) { |
| 6581 | long cpu; |
| 6582 | if (!PyLong_Check(item)) { |
| 6583 | PyErr_Format(PyExc_TypeError, |
| 6584 | "expected an iterator of ints, " |
| 6585 | "but iterator yielded %R", |
| 6586 | Py_TYPE(item)); |
| 6587 | Py_DECREF(item); |
| 6588 | goto error; |
| 6589 | } |
| 6590 | cpu = PyLong_AsLong(item); |
| 6591 | Py_DECREF(item); |
| 6592 | if (cpu < 0) { |
| 6593 | if (!PyErr_Occurred()) |
| 6594 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6595 | goto error; |
| 6596 | } |
| 6597 | if (cpu > INT_MAX - 1) { |
| 6598 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6599 | goto error; |
| 6600 | } |
| 6601 | if (cpu >= ncpus) { |
| 6602 | /* Grow CPU mask to fit the CPU number */ |
| 6603 | int newncpus = ncpus; |
| 6604 | cpu_set_t *newmask; |
| 6605 | size_t newsetsize; |
| 6606 | while (newncpus <= cpu) { |
| 6607 | if (newncpus > INT_MAX / 2) |
| 6608 | newncpus = cpu + 1; |
| 6609 | else |
| 6610 | newncpus = newncpus * 2; |
| 6611 | } |
| 6612 | newmask = CPU_ALLOC(newncpus); |
| 6613 | if (newmask == NULL) { |
| 6614 | PyErr_NoMemory(); |
| 6615 | goto error; |
| 6616 | } |
| 6617 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6618 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6619 | memcpy(newmask, cpu_set, setsize); |
| 6620 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6621 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6622 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6623 | ncpus = newncpus; |
| 6624 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6625 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6626 | } |
Brandt Bucher | 45a30af | 2019-06-27 09:10:57 -0700 | [diff] [blame] | 6627 | if (PyErr_Occurred()) { |
| 6628 | goto error; |
| 6629 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6630 | Py_CLEAR(iterator); |
| 6631 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6632 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6633 | posix_error(); |
| 6634 | goto error; |
| 6635 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6636 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6637 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6638 | |
| 6639 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6640 | if (cpu_set) |
| 6641 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6642 | Py_XDECREF(iterator); |
| 6643 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6644 | } |
| 6645 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6646 | |
| 6647 | /*[clinic input] |
| 6648 | os.sched_getaffinity |
| 6649 | pid: pid_t |
| 6650 | / |
| 6651 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6652 | 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] | 6653 | |
| 6654 | The affinity is returned as a set of CPU identifiers. |
| 6655 | [clinic start generated code]*/ |
| 6656 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6657 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6658 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6659 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6660 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6661 | int cpu, ncpus, count; |
| 6662 | size_t setsize; |
| 6663 | cpu_set_t *mask = NULL; |
| 6664 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6665 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6666 | ncpus = NCPUS_START; |
| 6667 | while (1) { |
| 6668 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6669 | mask = CPU_ALLOC(ncpus); |
| 6670 | if (mask == NULL) |
| 6671 | return PyErr_NoMemory(); |
| 6672 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6673 | break; |
| 6674 | CPU_FREE(mask); |
| 6675 | if (errno != EINVAL) |
| 6676 | return posix_error(); |
| 6677 | if (ncpus > INT_MAX / 2) { |
| 6678 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6679 | "a large enough CPU set"); |
| 6680 | return NULL; |
| 6681 | } |
| 6682 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6683 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6684 | |
| 6685 | res = PySet_New(NULL); |
| 6686 | if (res == NULL) |
| 6687 | goto error; |
| 6688 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6689 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6690 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6691 | --count; |
| 6692 | if (cpu_num == NULL) |
| 6693 | goto error; |
| 6694 | if (PySet_Add(res, cpu_num)) { |
| 6695 | Py_DECREF(cpu_num); |
| 6696 | goto error; |
| 6697 | } |
| 6698 | Py_DECREF(cpu_num); |
| 6699 | } |
| 6700 | } |
| 6701 | CPU_FREE(mask); |
| 6702 | return res; |
| 6703 | |
| 6704 | error: |
| 6705 | if (mask) |
| 6706 | CPU_FREE(mask); |
| 6707 | Py_XDECREF(res); |
| 6708 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6709 | } |
| 6710 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6711 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6712 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6713 | #endif /* HAVE_SCHED_H */ |
| 6714 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6715 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6716 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6717 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6718 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6719 | #define DEV_PTY_FILE "/dev/ptc" |
| 6720 | #define HAVE_DEV_PTMX |
| 6721 | #else |
| 6722 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6723 | #endif |
| 6724 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6725 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6726 | #ifdef HAVE_PTY_H |
| 6727 | #include <pty.h> |
| 6728 | #else |
| 6729 | #ifdef HAVE_LIBUTIL_H |
| 6730 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6731 | #else |
| 6732 | #ifdef HAVE_UTIL_H |
| 6733 | #include <util.h> |
| 6734 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6735 | #endif /* HAVE_LIBUTIL_H */ |
| 6736 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6737 | #ifdef HAVE_STROPTS_H |
| 6738 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6739 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6740 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6741 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6742 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6743 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6744 | /*[clinic input] |
| 6745 | os.openpty |
| 6746 | |
| 6747 | Open a pseudo-terminal. |
| 6748 | |
| 6749 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6750 | for both the master and slave ends. |
| 6751 | [clinic start generated code]*/ |
| 6752 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6753 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6754 | os_openpty_impl(PyObject *module) |
| 6755 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6756 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6757 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6758 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6759 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6760 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6761 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6762 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6763 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6764 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6765 | #endif |
| 6766 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6767 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6768 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6769 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6770 | goto posix_error; |
| 6771 | |
| 6772 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6773 | goto error; |
| 6774 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6775 | goto error; |
| 6776 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6777 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6778 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6779 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6780 | goto posix_error; |
| 6781 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6782 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6783 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6784 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6785 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6786 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6787 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6788 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6789 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6790 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6791 | goto posix_error; |
| 6792 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6793 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6794 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6795 | /* change permission of slave */ |
| 6796 | if (grantpt(master_fd) < 0) { |
| 6797 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6798 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6799 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6800 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6801 | /* unlock slave */ |
| 6802 | if (unlockpt(master_fd) < 0) { |
| 6803 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6804 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6805 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6806 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6807 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6808 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6809 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6810 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6811 | goto posix_error; |
| 6812 | |
| 6813 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6814 | if (slave_fd == -1) |
| 6815 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6816 | |
| 6817 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6818 | goto posix_error; |
| 6819 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6820 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6821 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6822 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6823 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6824 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6825 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6826 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6827 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6828 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6829 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6830 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6831 | posix_error: |
| 6832 | posix_error(); |
| 6833 | error: |
| 6834 | if (master_fd != -1) |
| 6835 | close(master_fd); |
| 6836 | if (slave_fd != -1) |
| 6837 | close(slave_fd); |
| 6838 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6839 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6840 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6841 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6842 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6843 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6844 | /*[clinic input] |
| 6845 | os.forkpty |
| 6846 | |
| 6847 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6848 | |
| 6849 | Returns a tuple of (pid, master_fd). |
| 6850 | Like fork(), return pid of 0 to the child process, |
| 6851 | and pid of child to the parent process. |
| 6852 | To both, return fd of newly opened pseudo-terminal. |
| 6853 | [clinic start generated code]*/ |
| 6854 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6855 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6856 | os_forkpty_impl(PyObject *module) |
| 6857 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6858 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6859 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6860 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6861 | |
Victor Stinner | ff4584c | 2020-03-13 18:03:56 +0100 | [diff] [blame] | 6862 | if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) { |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6863 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6864 | return NULL; |
| 6865 | } |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 6866 | if (PySys_Audit("os.forkpty", NULL) < 0) { |
| 6867 | return NULL; |
| 6868 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6869 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6870 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6871 | if (pid == 0) { |
| 6872 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6873 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6874 | } else { |
| 6875 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6876 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6877 | } |
| 6878 | if (pid == -1) |
| 6879 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6880 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6881 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6882 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6883 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6884 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6885 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6886 | /*[clinic input] |
| 6887 | os.getegid |
| 6888 | |
| 6889 | Return the current process's effective group id. |
| 6890 | [clinic start generated code]*/ |
| 6891 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6892 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6893 | os_getegid_impl(PyObject *module) |
| 6894 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6895 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6896 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6897 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6898 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6899 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6900 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6901 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6902 | /*[clinic input] |
| 6903 | os.geteuid |
| 6904 | |
| 6905 | Return the current process's effective user id. |
| 6906 | [clinic start generated code]*/ |
| 6907 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6908 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6909 | os_geteuid_impl(PyObject *module) |
| 6910 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6911 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6912 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6913 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6914 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6915 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6916 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6917 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6918 | /*[clinic input] |
| 6919 | os.getgid |
| 6920 | |
| 6921 | Return the current process's group id. |
| 6922 | [clinic start generated code]*/ |
| 6923 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6924 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6925 | os_getgid_impl(PyObject *module) |
| 6926 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6927 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6928 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6929 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6930 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6931 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6932 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6933 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6934 | /*[clinic input] |
| 6935 | os.getpid |
| 6936 | |
| 6937 | Return the current process id. |
| 6938 | [clinic start generated code]*/ |
| 6939 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6940 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6941 | os_getpid_impl(PyObject *module) |
| 6942 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6943 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6944 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6945 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6946 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6947 | |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6948 | #ifdef NGROUPS_MAX |
| 6949 | #define MAX_GROUPS NGROUPS_MAX |
| 6950 | #else |
| 6951 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6952 | #define MAX_GROUPS 64 |
| 6953 | #endif |
| 6954 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6955 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6956 | |
| 6957 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6958 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6959 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6960 | Returns a list of groups to which a user belongs.\n\n\ |
| 6961 | user: username to lookup\n\ |
| 6962 | group: base group id of the user"); |
| 6963 | |
| 6964 | static PyObject * |
| 6965 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6966 | { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6967 | const char *user; |
| 6968 | int i, ngroups; |
| 6969 | PyObject *list; |
| 6970 | #ifdef __APPLE__ |
| 6971 | int *groups, basegid; |
| 6972 | #else |
| 6973 | gid_t *groups, basegid; |
| 6974 | #endif |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6975 | |
| 6976 | /* |
| 6977 | * NGROUPS_MAX is defined by POSIX.1 as the maximum |
| 6978 | * number of supplimental groups a users can belong to. |
| 6979 | * We have to increment it by one because |
| 6980 | * getgrouplist() returns both the supplemental groups |
| 6981 | * and the primary group, i.e. all of the groups the |
| 6982 | * user belongs to. |
| 6983 | */ |
| 6984 | ngroups = 1 + MAX_GROUPS; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6985 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6986 | #ifdef __APPLE__ |
| 6987 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6988 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6989 | #else |
| 6990 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6991 | _Py_Gid_Converter, &basegid)) |
| 6992 | return NULL; |
| 6993 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6994 | |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 6995 | while (1) { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6996 | #ifdef __APPLE__ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 6997 | groups = PyMem_New(int, ngroups); |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 6998 | #else |
| 6999 | groups = PyMem_New(gid_t, ngroups); |
| 7000 | #endif |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7001 | if (groups == NULL) { |
| 7002 | return PyErr_NoMemory(); |
| 7003 | } |
Victor Stinner | f5c7cab | 2020-03-24 18:22:10 +0100 | [diff] [blame] | 7004 | |
| 7005 | int old_ngroups = ngroups; |
| 7006 | if (getgrouplist(user, basegid, groups, &ngroups) != -1) { |
| 7007 | /* Success */ |
| 7008 | break; |
| 7009 | } |
| 7010 | |
| 7011 | /* getgrouplist() fails if the group list is too small */ |
| 7012 | PyMem_Free(groups); |
| 7013 | |
| 7014 | if (ngroups > old_ngroups) { |
| 7015 | /* If the group list is too small, the glibc implementation of |
| 7016 | getgrouplist() sets ngroups to the total number of groups and |
| 7017 | returns -1. */ |
| 7018 | } |
| 7019 | else { |
| 7020 | /* Double the group list size */ |
| 7021 | if (ngroups > INT_MAX / 2) { |
| 7022 | return PyErr_NoMemory(); |
| 7023 | } |
| 7024 | ngroups *= 2; |
| 7025 | } |
| 7026 | |
| 7027 | /* Retry getgrouplist() with a larger group list */ |
Victor Stinner | 8ec7370 | 2020-03-23 20:00:57 +0100 | [diff] [blame] | 7028 | } |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7029 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 7030 | #ifdef _Py_MEMORY_SANITIZER |
| 7031 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 7032 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 7033 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 7034 | #endif |
| 7035 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7036 | list = PyList_New(ngroups); |
| 7037 | if (list == NULL) { |
| 7038 | PyMem_Del(groups); |
| 7039 | return NULL; |
| 7040 | } |
| 7041 | |
| 7042 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7043 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7044 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7045 | #else |
| 7046 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 7047 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 7048 | if (o == NULL) { |
| 7049 | Py_DECREF(list); |
| 7050 | PyMem_Del(groups); |
| 7051 | return NULL; |
| 7052 | } |
| 7053 | PyList_SET_ITEM(list, i, o); |
| 7054 | } |
| 7055 | |
| 7056 | PyMem_Del(groups); |
| 7057 | |
| 7058 | return list; |
| 7059 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7060 | #endif /* HAVE_GETGROUPLIST */ |
| 7061 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7062 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7063 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7064 | /*[clinic input] |
| 7065 | os.getgroups |
| 7066 | |
| 7067 | Return list of supplemental group IDs for the process. |
| 7068 | [clinic start generated code]*/ |
| 7069 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7070 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7071 | os_getgroups_impl(PyObject *module) |
| 7072 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7073 | { |
| 7074 | PyObject *result = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7075 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7076 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7077 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7078 | * This is a helper variable to store the intermediate result when |
| 7079 | * that happens. |
| 7080 | * |
| 7081 | * To keep the code readable the OSX behaviour is unconditional, |
| 7082 | * according to the POSIX spec this should be safe on all unix-y |
| 7083 | * systems. |
| 7084 | */ |
| 7085 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7086 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7087 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7088 | #ifdef __APPLE__ |
| 7089 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 7090 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 7091 | * always first call getgroups with length 0 to get the actual number |
| 7092 | * of groups. |
| 7093 | */ |
| 7094 | n = getgroups(0, NULL); |
| 7095 | if (n < 0) { |
| 7096 | return posix_error(); |
| 7097 | } else if (n <= MAX_GROUPS) { |
| 7098 | /* groups will fit in existing array */ |
| 7099 | alt_grouplist = grouplist; |
| 7100 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7101 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7102 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7103 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7104 | } |
| 7105 | } |
| 7106 | |
| 7107 | n = getgroups(n, alt_grouplist); |
| 7108 | if (n == -1) { |
| 7109 | if (alt_grouplist != grouplist) { |
| 7110 | PyMem_Free(alt_grouplist); |
| 7111 | } |
| 7112 | return posix_error(); |
| 7113 | } |
| 7114 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7115 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7116 | if (n < 0) { |
| 7117 | if (errno == EINVAL) { |
| 7118 | n = getgroups(0, NULL); |
| 7119 | if (n == -1) { |
| 7120 | return posix_error(); |
| 7121 | } |
| 7122 | if (n == 0) { |
| 7123 | /* Avoid malloc(0) */ |
| 7124 | alt_grouplist = grouplist; |
| 7125 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7126 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7127 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7128 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7129 | } |
| 7130 | n = getgroups(n, alt_grouplist); |
| 7131 | if (n == -1) { |
| 7132 | PyMem_Free(alt_grouplist); |
| 7133 | return posix_error(); |
| 7134 | } |
| 7135 | } |
| 7136 | } else { |
| 7137 | return posix_error(); |
| 7138 | } |
| 7139 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7140 | #endif |
| 7141 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7142 | result = PyList_New(n); |
| 7143 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7144 | int i; |
| 7145 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7146 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7147 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7148 | Py_DECREF(result); |
| 7149 | result = NULL; |
| 7150 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7151 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7152 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7153 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7154 | } |
| 7155 | |
| 7156 | if (alt_grouplist != grouplist) { |
| 7157 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7158 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7159 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7160 | return result; |
| 7161 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7162 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7163 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7164 | #ifdef HAVE_INITGROUPS |
| 7165 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 7166 | "initgroups(username, gid) -> None\n\n\ |
| 7167 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 7168 | the groups of which the specified username is a member, plus the specified\n\ |
| 7169 | group id."); |
| 7170 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7171 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7172 | static PyObject * |
| 7173 | posix_initgroups(PyObject *self, PyObject *args) |
| 7174 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7175 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7176 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7177 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7178 | #ifdef __APPLE__ |
| 7179 | int gid; |
| 7180 | #else |
| 7181 | gid_t gid; |
| 7182 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7183 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7184 | #ifdef __APPLE__ |
| 7185 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 7186 | PyUnicode_FSConverter, &oname, |
| 7187 | &gid)) |
| 7188 | #else |
| 7189 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 7190 | PyUnicode_FSConverter, &oname, |
| 7191 | _Py_Gid_Converter, &gid)) |
| 7192 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7193 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7194 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7195 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7196 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7197 | Py_DECREF(oname); |
| 7198 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7199 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7200 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7201 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7202 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7203 | #endif /* HAVE_INITGROUPS */ |
| 7204 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7205 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7206 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7207 | /*[clinic input] |
| 7208 | os.getpgid |
| 7209 | |
| 7210 | pid: pid_t |
| 7211 | |
| 7212 | Call the system call getpgid(), and return the result. |
| 7213 | [clinic start generated code]*/ |
| 7214 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7215 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7216 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 7217 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7218 | { |
| 7219 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7220 | if (pgid < 0) |
| 7221 | return posix_error(); |
| 7222 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7223 | } |
| 7224 | #endif /* HAVE_GETPGID */ |
| 7225 | |
| 7226 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7227 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7228 | /*[clinic input] |
| 7229 | os.getpgrp |
| 7230 | |
| 7231 | Return the current process group id. |
| 7232 | [clinic start generated code]*/ |
| 7233 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7234 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7235 | os_getpgrp_impl(PyObject *module) |
| 7236 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7237 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7238 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7239 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7240 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7241 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7242 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7243 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7244 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7245 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7246 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7247 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7248 | /*[clinic input] |
| 7249 | os.setpgrp |
| 7250 | |
| 7251 | Make the current process the leader of its process group. |
| 7252 | [clinic start generated code]*/ |
| 7253 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7254 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7255 | os_setpgrp_impl(PyObject *module) |
| 7256 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7257 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7258 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7259 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7260 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7261 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7262 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7263 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7264 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7265 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7266 | #endif /* HAVE_SETPGRP */ |
| 7267 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7268 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7269 | |
| 7270 | #ifdef MS_WINDOWS |
| 7271 | #include <tlhelp32.h> |
| 7272 | |
| 7273 | static PyObject* |
| 7274 | win32_getppid() |
| 7275 | { |
| 7276 | HANDLE snapshot; |
| 7277 | pid_t mypid; |
| 7278 | PyObject* result = NULL; |
| 7279 | BOOL have_record; |
| 7280 | PROCESSENTRY32 pe; |
| 7281 | |
| 7282 | mypid = getpid(); /* This function never fails */ |
| 7283 | |
| 7284 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 7285 | if (snapshot == INVALID_HANDLE_VALUE) |
| 7286 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 7287 | |
| 7288 | pe.dwSize = sizeof(pe); |
| 7289 | have_record = Process32First(snapshot, &pe); |
| 7290 | while (have_record) { |
| 7291 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 7292 | /* We could cache the ulong value in a static variable. */ |
| 7293 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 7294 | break; |
| 7295 | } |
| 7296 | |
| 7297 | have_record = Process32Next(snapshot, &pe); |
| 7298 | } |
| 7299 | |
| 7300 | /* If our loop exits and our pid was not found (result will be NULL) |
| 7301 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 7302 | * error anyway, so let's raise it. */ |
| 7303 | if (!result) |
| 7304 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7305 | |
| 7306 | CloseHandle(snapshot); |
| 7307 | |
| 7308 | return result; |
| 7309 | } |
| 7310 | #endif /*MS_WINDOWS*/ |
| 7311 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7312 | |
| 7313 | /*[clinic input] |
| 7314 | os.getppid |
| 7315 | |
| 7316 | Return the parent's process id. |
| 7317 | |
| 7318 | If the parent process has already exited, Windows machines will still |
| 7319 | return its id; others systems will return the id of the 'init' process (1). |
| 7320 | [clinic start generated code]*/ |
| 7321 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7322 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7323 | os_getppid_impl(PyObject *module) |
| 7324 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7325 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7326 | #ifdef MS_WINDOWS |
| 7327 | return win32_getppid(); |
| 7328 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7329 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7330 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7331 | } |
| 7332 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7333 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7334 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7335 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7336 | /*[clinic input] |
| 7337 | os.getlogin |
| 7338 | |
| 7339 | Return the actual login name. |
| 7340 | [clinic start generated code]*/ |
| 7341 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7342 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7343 | os_getlogin_impl(PyObject *module) |
| 7344 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7345 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7346 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7347 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7348 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7349 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7350 | |
| 7351 | if (GetUserNameW(user_name, &num_chars)) { |
| 7352 | /* num_chars is the number of unicode chars plus null terminator */ |
| 7353 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7354 | } |
| 7355 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7356 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7357 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7358 | char *name; |
| 7359 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7360 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7361 | errno = 0; |
| 7362 | name = getlogin(); |
| 7363 | if (name == NULL) { |
| 7364 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7365 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7366 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7367 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7368 | } |
| 7369 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7370 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7371 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7372 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7373 | return result; |
| 7374 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7375 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7376 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7377 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7378 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7379 | /*[clinic input] |
| 7380 | os.getuid |
| 7381 | |
| 7382 | Return the current process's user id. |
| 7383 | [clinic start generated code]*/ |
| 7384 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7385 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7386 | os_getuid_impl(PyObject *module) |
| 7387 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7388 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7389 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7390 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7391 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7392 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7393 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7394 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7395 | #define HAVE_KILL |
| 7396 | #endif /* MS_WINDOWS */ |
| 7397 | |
| 7398 | #ifdef HAVE_KILL |
| 7399 | /*[clinic input] |
| 7400 | os.kill |
| 7401 | |
| 7402 | pid: pid_t |
| 7403 | signal: Py_ssize_t |
| 7404 | / |
| 7405 | |
| 7406 | Kill a process with a signal. |
| 7407 | [clinic start generated code]*/ |
| 7408 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7409 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7410 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7411 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7412 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7413 | if (PySys_Audit("os.kill", "in", pid, signal) < 0) { |
| 7414 | return NULL; |
| 7415 | } |
| 7416 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7417 | if (kill(pid, (int)signal) == -1) |
| 7418 | return posix_error(); |
| 7419 | Py_RETURN_NONE; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7420 | #else /* !MS_WINDOWS */ |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7421 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7422 | DWORD sig = (DWORD)signal; |
| 7423 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7424 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7425 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7426 | /* Console processes which share a common console can be sent CTRL+C or |
| 7427 | CTRL+BREAK events, provided they handle said events. */ |
| 7428 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7429 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7430 | err = GetLastError(); |
| 7431 | PyErr_SetFromWindowsErr(err); |
| 7432 | } |
| 7433 | else |
| 7434 | Py_RETURN_NONE; |
| 7435 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7436 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7437 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7438 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7439 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7440 | if (handle == NULL) { |
| 7441 | err = GetLastError(); |
| 7442 | return PyErr_SetFromWindowsErr(err); |
| 7443 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7444 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7445 | if (TerminateProcess(handle, sig) == 0) { |
| 7446 | err = GetLastError(); |
| 7447 | result = PyErr_SetFromWindowsErr(err); |
| 7448 | } else { |
| 7449 | Py_INCREF(Py_None); |
| 7450 | result = Py_None; |
| 7451 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7452 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7453 | CloseHandle(handle); |
| 7454 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7455 | #endif /* !MS_WINDOWS */ |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7456 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7457 | #endif /* HAVE_KILL */ |
| 7458 | |
| 7459 | |
| 7460 | #ifdef HAVE_KILLPG |
| 7461 | /*[clinic input] |
| 7462 | os.killpg |
| 7463 | |
| 7464 | pgid: pid_t |
| 7465 | signal: int |
| 7466 | / |
| 7467 | |
| 7468 | Kill a process group with a signal. |
| 7469 | [clinic start generated code]*/ |
| 7470 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7471 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7472 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7473 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7474 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 7475 | if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) { |
| 7476 | return NULL; |
| 7477 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7478 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7479 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7480 | take the same type. Moreover, pid_t is always at least as wide as |
| 7481 | int (else compilation of this module fails), which is safe. */ |
| 7482 | if (killpg(pgid, signal) == -1) |
| 7483 | return posix_error(); |
| 7484 | Py_RETURN_NONE; |
| 7485 | } |
| 7486 | #endif /* HAVE_KILLPG */ |
| 7487 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7488 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7489 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7490 | #ifdef HAVE_SYS_LOCK_H |
| 7491 | #include <sys/lock.h> |
| 7492 | #endif |
| 7493 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7494 | /*[clinic input] |
| 7495 | os.plock |
| 7496 | op: int |
| 7497 | / |
| 7498 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7499 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7500 | [clinic start generated code]*/ |
| 7501 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7502 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7503 | os_plock_impl(PyObject *module, int op) |
| 7504 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7505 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7506 | if (plock(op) == -1) |
| 7507 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7508 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7509 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7510 | #endif /* HAVE_PLOCK */ |
| 7511 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7512 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7513 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7514 | /*[clinic input] |
| 7515 | os.setuid |
| 7516 | |
| 7517 | uid: uid_t |
| 7518 | / |
| 7519 | |
| 7520 | Set the current process's user id. |
| 7521 | [clinic start generated code]*/ |
| 7522 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7523 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7524 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7525 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7526 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7527 | if (setuid(uid) < 0) |
| 7528 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7529 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7530 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7531 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7532 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7533 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7534 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7535 | /*[clinic input] |
| 7536 | os.seteuid |
| 7537 | |
| 7538 | euid: uid_t |
| 7539 | / |
| 7540 | |
| 7541 | Set the current process's effective user id. |
| 7542 | [clinic start generated code]*/ |
| 7543 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7544 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7545 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7546 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7547 | { |
| 7548 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7549 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7550 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7551 | } |
| 7552 | #endif /* HAVE_SETEUID */ |
| 7553 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7554 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7555 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7556 | /*[clinic input] |
| 7557 | os.setegid |
| 7558 | |
| 7559 | egid: gid_t |
| 7560 | / |
| 7561 | |
| 7562 | Set the current process's effective group id. |
| 7563 | [clinic start generated code]*/ |
| 7564 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7565 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7566 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7567 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7568 | { |
| 7569 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7570 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7571 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7572 | } |
| 7573 | #endif /* HAVE_SETEGID */ |
| 7574 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7575 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7576 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7577 | /*[clinic input] |
| 7578 | os.setreuid |
| 7579 | |
| 7580 | ruid: uid_t |
| 7581 | euid: uid_t |
| 7582 | / |
| 7583 | |
| 7584 | Set the current process's real and effective user ids. |
| 7585 | [clinic start generated code]*/ |
| 7586 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7587 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7588 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7589 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7590 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7591 | if (setreuid(ruid, euid) < 0) { |
| 7592 | return posix_error(); |
| 7593 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7594 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7595 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7596 | } |
| 7597 | #endif /* HAVE_SETREUID */ |
| 7598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7599 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7600 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7601 | /*[clinic input] |
| 7602 | os.setregid |
| 7603 | |
| 7604 | rgid: gid_t |
| 7605 | egid: gid_t |
| 7606 | / |
| 7607 | |
| 7608 | Set the current process's real and effective group ids. |
| 7609 | [clinic start generated code]*/ |
| 7610 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7611 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7612 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7613 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7614 | { |
| 7615 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7616 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7617 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7618 | } |
| 7619 | #endif /* HAVE_SETREGID */ |
| 7620 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7621 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7622 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7623 | /*[clinic input] |
| 7624 | os.setgid |
| 7625 | gid: gid_t |
| 7626 | / |
| 7627 | |
| 7628 | Set the current process's group id. |
| 7629 | [clinic start generated code]*/ |
| 7630 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7631 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7632 | os_setgid_impl(PyObject *module, gid_t gid) |
| 7633 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7634 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7635 | if (setgid(gid) < 0) |
| 7636 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7637 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7638 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7639 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7640 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7641 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7642 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7643 | /*[clinic input] |
| 7644 | os.setgroups |
| 7645 | |
| 7646 | groups: object |
| 7647 | / |
| 7648 | |
| 7649 | Set the groups of the current process to list. |
| 7650 | [clinic start generated code]*/ |
| 7651 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7652 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7653 | os_setgroups(PyObject *module, PyObject *groups) |
| 7654 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7655 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7656 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7657 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7658 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7659 | if (!PySequence_Check(groups)) { |
| 7660 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7661 | return NULL; |
| 7662 | } |
| 7663 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7664 | if (len < 0) { |
| 7665 | return NULL; |
| 7666 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7667 | if (len > MAX_GROUPS) { |
| 7668 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7669 | return NULL; |
| 7670 | } |
| 7671 | for(i = 0; i < len; i++) { |
| 7672 | PyObject *elem; |
| 7673 | elem = PySequence_GetItem(groups, i); |
| 7674 | if (!elem) |
| 7675 | return NULL; |
| 7676 | if (!PyLong_Check(elem)) { |
| 7677 | PyErr_SetString(PyExc_TypeError, |
| 7678 | "groups must be integers"); |
| 7679 | Py_DECREF(elem); |
| 7680 | return NULL; |
| 7681 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7682 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7683 | Py_DECREF(elem); |
| 7684 | return NULL; |
| 7685 | } |
| 7686 | } |
| 7687 | Py_DECREF(elem); |
| 7688 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7689 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7690 | if (setgroups(len, grouplist) < 0) |
| 7691 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7692 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7693 | } |
| 7694 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7695 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7696 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7697 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7698 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7699 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7700 | PyObject *result; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7701 | PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7702 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7703 | if (pid == -1) |
| 7704 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7705 | |
Zackery Spytz | 682107c | 2019-09-09 09:48:32 -0600 | [diff] [blame] | 7706 | // If wait succeeded but no child was ready to report status, ru will not |
| 7707 | // have been populated. |
| 7708 | if (pid == 0) { |
| 7709 | memset(ru, 0, sizeof(*ru)); |
| 7710 | } |
| 7711 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7712 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7713 | if (m == NULL) |
| 7714 | return NULL; |
| 7715 | struct_rusage = PyObject_GetAttr(m, _posixstate_global->struct_rusage); |
| 7716 | Py_DECREF(m); |
| 7717 | if (struct_rusage == NULL) |
| 7718 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7719 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7720 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7721 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
Eddie Elizondo | e4db1f0 | 2019-11-25 19:07:37 -0800 | [diff] [blame] | 7722 | Py_DECREF(struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7723 | if (!result) |
| 7724 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7725 | |
| 7726 | #ifndef doubletime |
| 7727 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7728 | #endif |
| 7729 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7730 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7731 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7732 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7733 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7734 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7735 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7736 | SET_INT(result, 2, ru->ru_maxrss); |
| 7737 | SET_INT(result, 3, ru->ru_ixrss); |
| 7738 | SET_INT(result, 4, ru->ru_idrss); |
| 7739 | SET_INT(result, 5, ru->ru_isrss); |
| 7740 | SET_INT(result, 6, ru->ru_minflt); |
| 7741 | SET_INT(result, 7, ru->ru_majflt); |
| 7742 | SET_INT(result, 8, ru->ru_nswap); |
| 7743 | SET_INT(result, 9, ru->ru_inblock); |
| 7744 | SET_INT(result, 10, ru->ru_oublock); |
| 7745 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7746 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7747 | SET_INT(result, 13, ru->ru_nsignals); |
| 7748 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7749 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7750 | #undef SET_INT |
| 7751 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7752 | if (PyErr_Occurred()) { |
| 7753 | Py_DECREF(result); |
| 7754 | return NULL; |
| 7755 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7756 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7757 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7758 | } |
| 7759 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7760 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7761 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7762 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7763 | /*[clinic input] |
| 7764 | os.wait3 |
| 7765 | |
| 7766 | options: int |
| 7767 | Wait for completion of a child process. |
| 7768 | |
| 7769 | Returns a tuple of information about the child process: |
| 7770 | (pid, status, rusage) |
| 7771 | [clinic start generated code]*/ |
| 7772 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7773 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7774 | os_wait3_impl(PyObject *module, int options) |
| 7775 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7776 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7777 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7778 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7779 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7780 | WAIT_TYPE status; |
| 7781 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7782 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7783 | do { |
| 7784 | Py_BEGIN_ALLOW_THREADS |
| 7785 | pid = wait3(&status, options, &ru); |
| 7786 | Py_END_ALLOW_THREADS |
| 7787 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7788 | if (pid < 0) |
| 7789 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7790 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7791 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7792 | } |
| 7793 | #endif /* HAVE_WAIT3 */ |
| 7794 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7795 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7796 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7797 | /*[clinic input] |
| 7798 | |
| 7799 | os.wait4 |
| 7800 | |
| 7801 | pid: pid_t |
| 7802 | options: int |
| 7803 | |
| 7804 | Wait for completion of a specific child process. |
| 7805 | |
| 7806 | Returns a tuple of information about the child process: |
| 7807 | (pid, status, rusage) |
| 7808 | [clinic start generated code]*/ |
| 7809 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7810 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7811 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7812 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7813 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7814 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7815 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7816 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7817 | WAIT_TYPE status; |
| 7818 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7819 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7820 | do { |
| 7821 | Py_BEGIN_ALLOW_THREADS |
| 7822 | res = wait4(pid, &status, options, &ru); |
| 7823 | Py_END_ALLOW_THREADS |
| 7824 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7825 | if (res < 0) |
| 7826 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7827 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7828 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7829 | } |
| 7830 | #endif /* HAVE_WAIT4 */ |
| 7831 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7832 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7833 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7834 | /*[clinic input] |
| 7835 | os.waitid |
| 7836 | |
| 7837 | idtype: idtype_t |
| 7838 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7839 | id: id_t |
| 7840 | The id to wait on. |
| 7841 | options: int |
| 7842 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7843 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7844 | / |
| 7845 | |
| 7846 | Returns the result of waiting for a process or processes. |
| 7847 | |
| 7848 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7849 | no children in a waitable state. |
| 7850 | [clinic start generated code]*/ |
| 7851 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7852 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7853 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7854 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7855 | { |
| 7856 | PyObject *result; |
| 7857 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7858 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7859 | siginfo_t si; |
| 7860 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7861 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7862 | do { |
| 7863 | Py_BEGIN_ALLOW_THREADS |
| 7864 | res = waitid(idtype, id, &si, options); |
| 7865 | Py_END_ALLOW_THREADS |
| 7866 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7867 | if (res < 0) |
| 7868 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7869 | |
| 7870 | if (si.si_pid == 0) |
| 7871 | Py_RETURN_NONE; |
| 7872 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 7873 | PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7874 | result = PyStructSequence_New((PyTypeObject *)WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7875 | if (!result) |
| 7876 | return NULL; |
| 7877 | |
| 7878 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7879 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7880 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7881 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7882 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7883 | if (PyErr_Occurred()) { |
| 7884 | Py_DECREF(result); |
| 7885 | return NULL; |
| 7886 | } |
| 7887 | |
| 7888 | return result; |
| 7889 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7890 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7891 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7892 | |
| 7893 | #if defined(HAVE_WAITPID) |
| 7894 | /*[clinic input] |
| 7895 | os.waitpid |
| 7896 | pid: pid_t |
| 7897 | options: int |
| 7898 | / |
| 7899 | |
| 7900 | Wait for completion of a given child process. |
| 7901 | |
| 7902 | Returns a tuple of information regarding the child process: |
| 7903 | (pid, status) |
| 7904 | |
| 7905 | The options argument is ignored on Windows. |
| 7906 | [clinic start generated code]*/ |
| 7907 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7908 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7909 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7910 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7911 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7912 | pid_t res; |
| 7913 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7914 | WAIT_TYPE status; |
| 7915 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7916 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7917 | do { |
| 7918 | Py_BEGIN_ALLOW_THREADS |
| 7919 | res = waitpid(pid, &status, options); |
| 7920 | Py_END_ALLOW_THREADS |
| 7921 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7922 | if (res < 0) |
| 7923 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7924 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7925 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7926 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7927 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7928 | /* 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] | 7929 | /*[clinic input] |
| 7930 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7931 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7932 | options: int |
| 7933 | / |
| 7934 | |
| 7935 | Wait for completion of a given process. |
| 7936 | |
| 7937 | Returns a tuple of information regarding the process: |
| 7938 | (pid, status << 8) |
| 7939 | |
| 7940 | The options argument is ignored on Windows. |
| 7941 | [clinic start generated code]*/ |
| 7942 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7943 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7944 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7945 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7946 | { |
| 7947 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7948 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7949 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7950 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7951 | do { |
| 7952 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7953 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7954 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7955 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7956 | Py_END_ALLOW_THREADS |
| 7957 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7958 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7959 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7960 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7961 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7962 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7963 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7964 | #endif |
| 7965 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7966 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7967 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7968 | /*[clinic input] |
| 7969 | os.wait |
| 7970 | |
| 7971 | Wait for completion of a child process. |
| 7972 | |
| 7973 | Returns a tuple of information about the child process: |
| 7974 | (pid, status) |
| 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_wait_impl(PyObject *module) |
| 7979 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7980 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7981 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7982 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7983 | WAIT_TYPE status; |
| 7984 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7985 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7986 | do { |
| 7987 | Py_BEGIN_ALLOW_THREADS |
| 7988 | pid = wait(&status); |
| 7989 | Py_END_ALLOW_THREADS |
| 7990 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7991 | if (pid < 0) |
| 7992 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7993 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7994 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7995 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7996 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7997 | |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 7998 | #if defined(__linux__) && defined(__NR_pidfd_open) |
| 7999 | /*[clinic input] |
| 8000 | os.pidfd_open |
| 8001 | pid: pid_t |
| 8002 | flags: unsigned_int = 0 |
| 8003 | |
| 8004 | Return a file descriptor referring to the process *pid*. |
| 8005 | |
| 8006 | The descriptor can be used to perform process management without races and |
| 8007 | signals. |
| 8008 | [clinic start generated code]*/ |
| 8009 | |
| 8010 | static PyObject * |
| 8011 | os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags) |
| 8012 | /*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/ |
| 8013 | { |
| 8014 | int fd = syscall(__NR_pidfd_open, pid, flags); |
| 8015 | if (fd < 0) { |
| 8016 | return posix_error(); |
| 8017 | } |
| 8018 | return PyLong_FromLong(fd); |
| 8019 | } |
| 8020 | #endif |
| 8021 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8022 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8023 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8024 | /*[clinic input] |
| 8025 | os.readlink |
| 8026 | |
| 8027 | path: path_t |
| 8028 | * |
| 8029 | dir_fd: dir_fd(requires='readlinkat') = None |
| 8030 | |
| 8031 | Return a string representing the path to which the symbolic link points. |
| 8032 | |
| 8033 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8034 | and path should be relative; path will then be relative to that directory. |
| 8035 | |
| 8036 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 8037 | using it will raise a NotImplementedError. |
| 8038 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8039 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8040 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8041 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 8042 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8043 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8044 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 8045 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8046 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8047 | |
| 8048 | Py_BEGIN_ALLOW_THREADS |
| 8049 | #ifdef HAVE_READLINKAT |
| 8050 | if (dir_fd != DEFAULT_DIR_FD) |
| 8051 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 8052 | else |
| 8053 | #endif |
| 8054 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 8055 | Py_END_ALLOW_THREADS |
| 8056 | |
| 8057 | if (length < 0) { |
| 8058 | return path_error(path); |
| 8059 | } |
| 8060 | buffer[length] = '\0'; |
| 8061 | |
| 8062 | if (PyUnicode_Check(path->object)) |
| 8063 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 8064 | else |
| 8065 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8066 | #elif defined(MS_WINDOWS) |
| 8067 | DWORD n_bytes_returned; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8068 | DWORD io_result = 0; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8069 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8070 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 8071 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8072 | PyObject *result = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8073 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8074 | /* First get a handle to the reparse point */ |
| 8075 | Py_BEGIN_ALLOW_THREADS |
| 8076 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8077 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8078 | 0, |
| 8079 | 0, |
| 8080 | 0, |
| 8081 | OPEN_EXISTING, |
| 8082 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 8083 | 0); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8084 | if (reparse_point_handle != INVALID_HANDLE_VALUE) { |
| 8085 | /* New call DeviceIoControl to read the reparse point */ |
| 8086 | io_result = DeviceIoControl( |
| 8087 | reparse_point_handle, |
| 8088 | FSCTL_GET_REPARSE_POINT, |
| 8089 | 0, 0, /* in buffer */ |
| 8090 | target_buffer, sizeof(target_buffer), |
| 8091 | &n_bytes_returned, |
| 8092 | 0 /* we're not using OVERLAPPED_IO */ |
| 8093 | ); |
| 8094 | CloseHandle(reparse_point_handle); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8095 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8096 | Py_END_ALLOW_THREADS |
| 8097 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8098 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8099 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8100 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8101 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8102 | wchar_t *name = NULL; |
| 8103 | Py_ssize_t nameLen = 0; |
| 8104 | if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8105 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8106 | name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 8107 | rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 8108 | nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8109 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8110 | else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) |
| 8111 | { |
| 8112 | name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer + |
| 8113 | rdb->MountPointReparseBuffer.SubstituteNameOffset); |
| 8114 | nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
| 8115 | } |
| 8116 | else |
| 8117 | { |
| 8118 | PyErr_SetString(PyExc_ValueError, "not a symbolic link"); |
| 8119 | } |
| 8120 | if (name) { |
| 8121 | if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) { |
| 8122 | /* Our buffer is mutable, so this is okay */ |
| 8123 | name[1] = L'\\'; |
| 8124 | } |
| 8125 | result = PyUnicode_FromWideChar(name, nameLen); |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8126 | if (result && path->narrow) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8127 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
| 8128 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8129 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8130 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8131 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8132 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8133 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8134 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8135 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8136 | |
| 8137 | #if defined(MS_WINDOWS) |
| 8138 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8139 | /* Remove the last portion of the path - return 0 on success */ |
| 8140 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8141 | _dirnameW(WCHAR *path) |
| 8142 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8143 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8144 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 8145 | if (length == MAX_PATH) { |
| 8146 | return -1; |
| 8147 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8148 | |
| 8149 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8150 | for(ptr = path + length; ptr != path; ptr--) { |
| 8151 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8152 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8153 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8154 | } |
| 8155 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8156 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8157 | } |
| 8158 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8159 | /* Is this path absolute? */ |
| 8160 | static int |
| 8161 | _is_absW(const WCHAR *path) |
| 8162 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8163 | return path[0] == L'\\' || path[0] == L'/' || |
| 8164 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8165 | } |
| 8166 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8167 | /* join root and rest with a backslash - return 0 on success */ |
| 8168 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8169 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 8170 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8171 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8172 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8173 | } |
| 8174 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8175 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 8176 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8177 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8178 | |
| 8179 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 8180 | return -1; |
| 8181 | } |
| 8182 | |
| 8183 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8184 | } |
| 8185 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8186 | /* Return True if the path at src relative to dest is a directory */ |
| 8187 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8188 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8189 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8190 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 8191 | WCHAR dest_parent[MAX_PATH]; |
| 8192 | WCHAR src_resolved[MAX_PATH] = L""; |
| 8193 | |
| 8194 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8195 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 8196 | _dirnameW(dest_parent)) { |
| 8197 | return 0; |
| 8198 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8199 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8200 | if (_joinW(src_resolved, dest_parent, src)) { |
| 8201 | return 0; |
| 8202 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8203 | return ( |
| 8204 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 8205 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 8206 | ); |
| 8207 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8208 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8209 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8210 | |
| 8211 | /*[clinic input] |
| 8212 | os.symlink |
| 8213 | src: path_t |
| 8214 | dst: path_t |
| 8215 | target_is_directory: bool = False |
| 8216 | * |
| 8217 | dir_fd: dir_fd(requires='symlinkat')=None |
| 8218 | |
| 8219 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 8220 | |
| 8221 | Create a symbolic link pointing to src named dst. |
| 8222 | |
| 8223 | target_is_directory is required on Windows if the target is to be |
| 8224 | interpreted as a directory. (On Windows, symlink requires |
| 8225 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 8226 | target_is_directory is ignored on non-Windows platforms. |
| 8227 | |
| 8228 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8229 | and path should be relative; path will then be relative to that directory. |
| 8230 | dir_fd may not be implemented on your platform. |
| 8231 | If it is unavailable, using it will raise a NotImplementedError. |
| 8232 | |
| 8233 | [clinic start generated code]*/ |
| 8234 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8235 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8236 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8237 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8238 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8239 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8240 | #ifdef MS_WINDOWS |
| 8241 | DWORD result; |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8242 | DWORD flags = 0; |
| 8243 | |
| 8244 | /* Assumed true, set to false if detected to not be available. */ |
| 8245 | static int windows_has_symlink_unprivileged_flag = TRUE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8246 | #else |
| 8247 | int result; |
| 8248 | #endif |
| 8249 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 8250 | if (PySys_Audit("os.symlink", "OOi", src->object, dst->object, |
| 8251 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { |
| 8252 | return NULL; |
| 8253 | } |
| 8254 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8255 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8256 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8257 | if (windows_has_symlink_unprivileged_flag) { |
| 8258 | /* Allow non-admin symlinks if system allows it. */ |
| 8259 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 8260 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8261 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8262 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8263 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8264 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
| 8265 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { |
| 8266 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 8267 | } |
| 8268 | |
| 8269 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8270 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8271 | Py_END_ALLOW_THREADS |
| 8272 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8273 | if (windows_has_symlink_unprivileged_flag && !result && |
| 8274 | ERROR_INVALID_PARAMETER == GetLastError()) { |
| 8275 | |
| 8276 | Py_BEGIN_ALLOW_THREADS |
| 8277 | _Py_BEGIN_SUPPRESS_IPH |
| 8278 | /* This error might be caused by |
| 8279 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. |
| 8280 | Try again, and update windows_has_symlink_unprivileged_flag if we |
| 8281 | are successful this time. |
| 8282 | |
| 8283 | NOTE: There is a risk of a race condition here if there are other |
| 8284 | conditions than the flag causing ERROR_INVALID_PARAMETER, and |
| 8285 | another process (or thread) changes that condition in between our |
| 8286 | calls to CreateSymbolicLink. |
| 8287 | */ |
| 8288 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 8289 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
| 8290 | _Py_END_SUPPRESS_IPH |
| 8291 | Py_END_ALLOW_THREADS |
| 8292 | |
| 8293 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { |
| 8294 | windows_has_symlink_unprivileged_flag = FALSE; |
| 8295 | } |
| 8296 | } |
| 8297 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8298 | if (!result) |
| 8299 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8300 | |
| 8301 | #else |
| 8302 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8303 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 8304 | PyErr_SetString(PyExc_ValueError, |
| 8305 | "symlink: src and dst must be the same type"); |
| 8306 | return NULL; |
| 8307 | } |
| 8308 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8309 | Py_BEGIN_ALLOW_THREADS |
| 8310 | #if HAVE_SYMLINKAT |
| 8311 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8312 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8313 | else |
| 8314 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8315 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8316 | Py_END_ALLOW_THREADS |
| 8317 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8318 | if (result) |
| 8319 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8320 | #endif |
| 8321 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8322 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8323 | } |
| 8324 | #endif /* HAVE_SYMLINK */ |
| 8325 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8326 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8327 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8328 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8329 | static PyStructSequence_Field times_result_fields[] = { |
| 8330 | {"user", "user time"}, |
| 8331 | {"system", "system time"}, |
| 8332 | {"children_user", "user time of children"}, |
| 8333 | {"children_system", "system time of children"}, |
| 8334 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 8335 | {NULL} |
| 8336 | }; |
| 8337 | |
| 8338 | PyDoc_STRVAR(times_result__doc__, |
| 8339 | "times_result: Result from os.times().\n\n\ |
| 8340 | This object may be accessed either as a tuple of\n\ |
| 8341 | (user, system, children_user, children_system, elapsed),\n\ |
| 8342 | or via the attributes user, system, children_user, children_system,\n\ |
| 8343 | and elapsed.\n\ |
| 8344 | \n\ |
| 8345 | See os.times for more information."); |
| 8346 | |
| 8347 | static PyStructSequence_Desc times_result_desc = { |
| 8348 | "times_result", /* name */ |
| 8349 | times_result__doc__, /* doc */ |
| 8350 | times_result_fields, |
| 8351 | 5 |
| 8352 | }; |
| 8353 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8354 | #ifdef MS_WINDOWS |
| 8355 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 8356 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8357 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8358 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8359 | |
| 8360 | static PyObject * |
| 8361 | build_times_result(double user, double system, |
| 8362 | double children_user, double children_system, |
| 8363 | double elapsed) |
| 8364 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8365 | PyObject *TimesResultType = _posixstate_global->TimesResultType; |
| 8366 | PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8367 | if (value == NULL) |
| 8368 | return NULL; |
| 8369 | |
| 8370 | #define SET(i, field) \ |
| 8371 | { \ |
| 8372 | PyObject *o = PyFloat_FromDouble(field); \ |
| 8373 | if (!o) { \ |
| 8374 | Py_DECREF(value); \ |
| 8375 | return NULL; \ |
| 8376 | } \ |
| 8377 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 8378 | } \ |
| 8379 | |
| 8380 | SET(0, user); |
| 8381 | SET(1, system); |
| 8382 | SET(2, children_user); |
| 8383 | SET(3, children_system); |
| 8384 | SET(4, elapsed); |
| 8385 | |
| 8386 | #undef SET |
| 8387 | |
| 8388 | return value; |
| 8389 | } |
| 8390 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8391 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8392 | #ifndef MS_WINDOWS |
| 8393 | #define NEED_TICKS_PER_SECOND |
| 8394 | static long ticks_per_second = -1; |
| 8395 | #endif /* MS_WINDOWS */ |
| 8396 | |
| 8397 | /*[clinic input] |
| 8398 | os.times |
| 8399 | |
| 8400 | Return a collection containing process timing information. |
| 8401 | |
| 8402 | The object returned behaves like a named tuple with these fields: |
| 8403 | (utime, stime, cutime, cstime, elapsed_time) |
| 8404 | All fields are floating point numbers. |
| 8405 | [clinic start generated code]*/ |
| 8406 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8407 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8408 | os_times_impl(PyObject *module) |
| 8409 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8410 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8411 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8412 | FILETIME create, exit, kernel, user; |
| 8413 | HANDLE hProc; |
| 8414 | hProc = GetCurrentProcess(); |
| 8415 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8416 | /* The fields of a FILETIME structure are the hi and lo part |
| 8417 | of a 64-bit value expressed in 100 nanosecond units. |
| 8418 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8419 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8420 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8421 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8422 | (double)(user.dwHighDateTime*429.4967296 + |
| 8423 | user.dwLowDateTime*1e-7), |
| 8424 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8425 | kernel.dwLowDateTime*1e-7), |
| 8426 | (double)0, |
| 8427 | (double)0, |
| 8428 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8429 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8430 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8431 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8432 | |
| 8433 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8434 | struct tms t; |
| 8435 | clock_t c; |
| 8436 | errno = 0; |
| 8437 | c = times(&t); |
| 8438 | if (c == (clock_t) -1) |
| 8439 | return posix_error(); |
| 8440 | return build_times_result( |
| 8441 | (double)t.tms_utime / ticks_per_second, |
| 8442 | (double)t.tms_stime / ticks_per_second, |
| 8443 | (double)t.tms_cutime / ticks_per_second, |
| 8444 | (double)t.tms_cstime / ticks_per_second, |
| 8445 | (double)c / ticks_per_second); |
| 8446 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8447 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8448 | #endif /* HAVE_TIMES */ |
| 8449 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8450 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8451 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8452 | /*[clinic input] |
| 8453 | os.getsid |
| 8454 | |
| 8455 | pid: pid_t |
| 8456 | / |
| 8457 | |
| 8458 | Call the system call getsid(pid) and return the result. |
| 8459 | [clinic start generated code]*/ |
| 8460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8461 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8462 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8463 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8464 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8465 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8466 | sid = getsid(pid); |
| 8467 | if (sid < 0) |
| 8468 | return posix_error(); |
| 8469 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8470 | } |
| 8471 | #endif /* HAVE_GETSID */ |
| 8472 | |
| 8473 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8474 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8475 | /*[clinic input] |
| 8476 | os.setsid |
| 8477 | |
| 8478 | Call the system call setsid(). |
| 8479 | [clinic start generated code]*/ |
| 8480 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8481 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8482 | os_setsid_impl(PyObject *module) |
| 8483 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8484 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8485 | if (setsid() < 0) |
| 8486 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8487 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8488 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8489 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8490 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8491 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8492 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8493 | /*[clinic input] |
| 8494 | os.setpgid |
| 8495 | |
| 8496 | pid: pid_t |
| 8497 | pgrp: pid_t |
| 8498 | / |
| 8499 | |
| 8500 | Call the system call setpgid(pid, pgrp). |
| 8501 | [clinic start generated code]*/ |
| 8502 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8503 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8504 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8505 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8506 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8507 | if (setpgid(pid, pgrp) < 0) |
| 8508 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8509 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8510 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8511 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8512 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8513 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8514 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8515 | /*[clinic input] |
| 8516 | os.tcgetpgrp |
| 8517 | |
| 8518 | fd: int |
| 8519 | / |
| 8520 | |
| 8521 | Return the process group associated with the terminal specified by fd. |
| 8522 | [clinic start generated code]*/ |
| 8523 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8524 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8525 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8526 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8527 | { |
| 8528 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8529 | if (pgid < 0) |
| 8530 | return posix_error(); |
| 8531 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8532 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8533 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8534 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8535 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8536 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8537 | /*[clinic input] |
| 8538 | os.tcsetpgrp |
| 8539 | |
| 8540 | fd: int |
| 8541 | pgid: pid_t |
| 8542 | / |
| 8543 | |
| 8544 | Set the process group associated with the terminal specified by fd. |
| 8545 | [clinic start generated code]*/ |
| 8546 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8547 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8548 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8549 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8550 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8551 | if (tcsetpgrp(fd, pgid) < 0) |
| 8552 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8553 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8554 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8555 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8556 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8557 | /* Functions acting on file descriptors */ |
| 8558 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8559 | #ifdef O_CLOEXEC |
| 8560 | extern int _Py_open_cloexec_works; |
| 8561 | #endif |
| 8562 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8563 | |
| 8564 | /*[clinic input] |
| 8565 | os.open -> int |
| 8566 | path: path_t |
| 8567 | flags: int |
| 8568 | mode: int = 0o777 |
| 8569 | * |
| 8570 | dir_fd: dir_fd(requires='openat') = None |
| 8571 | |
| 8572 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8573 | |
| 8574 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8575 | |
| 8576 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8577 | and path should be relative; path will then be relative to that directory. |
| 8578 | dir_fd may not be implemented on your platform. |
| 8579 | If it is unavailable, using it will raise a NotImplementedError. |
| 8580 | [clinic start generated code]*/ |
| 8581 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8582 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8583 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8584 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8585 | { |
| 8586 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8587 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8588 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8589 | #ifdef O_CLOEXEC |
| 8590 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8591 | #elif !defined(MS_WINDOWS) |
| 8592 | int *atomic_flag_works = NULL; |
| 8593 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8594 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8595 | #ifdef MS_WINDOWS |
| 8596 | flags |= O_NOINHERIT; |
| 8597 | #elif defined(O_CLOEXEC) |
| 8598 | flags |= O_CLOEXEC; |
| 8599 | #endif |
| 8600 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 8601 | if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) { |
| 8602 | return -1; |
| 8603 | } |
| 8604 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8605 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8606 | do { |
| 8607 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8608 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8609 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8610 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8611 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8612 | if (dir_fd != DEFAULT_DIR_FD) |
| 8613 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8614 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8615 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8616 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8617 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8618 | Py_END_ALLOW_THREADS |
| 8619 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8620 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8621 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8622 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8623 | if (!async_err) |
| 8624 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8625 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8626 | } |
| 8627 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8628 | #ifndef MS_WINDOWS |
| 8629 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8630 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8631 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8632 | } |
| 8633 | #endif |
| 8634 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8635 | return fd; |
| 8636 | } |
| 8637 | |
| 8638 | |
| 8639 | /*[clinic input] |
| 8640 | os.close |
| 8641 | |
| 8642 | fd: int |
| 8643 | |
| 8644 | Close a file descriptor. |
| 8645 | [clinic start generated code]*/ |
| 8646 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8647 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8648 | os_close_impl(PyObject *module, int fd) |
| 8649 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8650 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8651 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8652 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8653 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8654 | * for more details. |
| 8655 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8656 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8657 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8658 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8659 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8660 | Py_END_ALLOW_THREADS |
| 8661 | if (res < 0) |
| 8662 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8663 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8664 | } |
| 8665 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8666 | |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8667 | #ifdef HAVE_FDWALK |
| 8668 | static int |
| 8669 | _fdwalk_close_func(void *lohi, int fd) |
| 8670 | { |
| 8671 | int lo = ((int *)lohi)[0]; |
| 8672 | int hi = ((int *)lohi)[1]; |
| 8673 | |
| 8674 | if (fd >= hi) |
| 8675 | return 1; |
| 8676 | else if (fd >= lo) |
| 8677 | close(fd); |
| 8678 | return 0; |
| 8679 | } |
| 8680 | #endif /* HAVE_FDWALK */ |
| 8681 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8682 | /*[clinic input] |
| 8683 | os.closerange |
| 8684 | |
| 8685 | fd_low: int |
| 8686 | fd_high: int |
| 8687 | / |
| 8688 | |
| 8689 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8690 | [clinic start generated code]*/ |
| 8691 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8692 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8693 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8694 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8695 | { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8696 | #ifdef HAVE_FDWALK |
| 8697 | int lohi[2]; |
| 8698 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8699 | int i; |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8700 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8701 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8702 | _Py_BEGIN_SUPPRESS_IPH |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8703 | #ifdef HAVE_FDWALK |
| 8704 | lohi[0] = Py_MAX(fd_low, 0); |
| 8705 | lohi[1] = fd_high; |
| 8706 | fdwalk(_fdwalk_close_func, lohi); |
| 8707 | #else |
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 8708 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 8709 | close(i); |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8710 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8711 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8712 | Py_END_ALLOW_THREADS |
| 8713 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8714 | } |
| 8715 | |
| 8716 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8717 | /*[clinic input] |
| 8718 | os.dup -> int |
| 8719 | |
| 8720 | fd: int |
| 8721 | / |
| 8722 | |
| 8723 | Return a duplicate of a file descriptor. |
| 8724 | [clinic start generated code]*/ |
| 8725 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8726 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8727 | os_dup_impl(PyObject *module, int fd) |
| 8728 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8729 | { |
| 8730 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8731 | } |
| 8732 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8733 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8734 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8735 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8736 | fd: int |
| 8737 | fd2: int |
| 8738 | inheritable: bool=True |
| 8739 | |
| 8740 | Duplicate file descriptor. |
| 8741 | [clinic start generated code]*/ |
| 8742 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8743 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8744 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8745 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8746 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8747 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8748 | #if defined(HAVE_DUP3) && \ |
| 8749 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8750 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8751 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8752 | #endif |
| 8753 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8754 | if (fd < 0 || fd2 < 0) { |
| 8755 | posix_error(); |
| 8756 | return -1; |
| 8757 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8758 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8759 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8760 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8761 | * upon close(), and therefore below. |
| 8762 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8763 | #ifdef MS_WINDOWS |
| 8764 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8765 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8766 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8767 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8768 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8769 | if (res < 0) { |
| 8770 | posix_error(); |
| 8771 | return -1; |
| 8772 | } |
| 8773 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8774 | |
| 8775 | /* Character files like console cannot be make non-inheritable */ |
| 8776 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8777 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8778 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8779 | } |
| 8780 | |
| 8781 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8782 | Py_BEGIN_ALLOW_THREADS |
| 8783 | if (!inheritable) |
| 8784 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8785 | else |
| 8786 | res = dup2(fd, fd2); |
| 8787 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8788 | if (res < 0) { |
| 8789 | posix_error(); |
| 8790 | return -1; |
| 8791 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8792 | |
| 8793 | #else |
| 8794 | |
| 8795 | #ifdef HAVE_DUP3 |
| 8796 | if (!inheritable && dup3_works != 0) { |
| 8797 | Py_BEGIN_ALLOW_THREADS |
| 8798 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8799 | Py_END_ALLOW_THREADS |
| 8800 | if (res < 0) { |
| 8801 | if (dup3_works == -1) |
| 8802 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8803 | if (dup3_works) { |
| 8804 | posix_error(); |
| 8805 | return -1; |
| 8806 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8807 | } |
| 8808 | } |
| 8809 | |
| 8810 | if (inheritable || dup3_works == 0) |
| 8811 | { |
| 8812 | #endif |
| 8813 | Py_BEGIN_ALLOW_THREADS |
| 8814 | res = dup2(fd, fd2); |
| 8815 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8816 | if (res < 0) { |
| 8817 | posix_error(); |
| 8818 | return -1; |
| 8819 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8820 | |
| 8821 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8822 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8823 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8824 | } |
| 8825 | #ifdef HAVE_DUP3 |
| 8826 | } |
| 8827 | #endif |
| 8828 | |
| 8829 | #endif |
| 8830 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8831 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8832 | } |
| 8833 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8834 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8835 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8836 | /*[clinic input] |
| 8837 | os.lockf |
| 8838 | |
| 8839 | fd: int |
| 8840 | An open file descriptor. |
| 8841 | command: int |
| 8842 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8843 | length: Py_off_t |
| 8844 | The number of bytes to lock, starting at the current position. |
| 8845 | / |
| 8846 | |
| 8847 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8848 | |
| 8849 | [clinic start generated code]*/ |
| 8850 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8851 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8852 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8853 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8854 | { |
| 8855 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8856 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 8857 | if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) { |
| 8858 | return NULL; |
| 8859 | } |
| 8860 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8861 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8862 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8863 | Py_END_ALLOW_THREADS |
| 8864 | |
| 8865 | if (res < 0) |
| 8866 | return posix_error(); |
| 8867 | |
| 8868 | Py_RETURN_NONE; |
| 8869 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8870 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8871 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8872 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8873 | /*[clinic input] |
| 8874 | os.lseek -> Py_off_t |
| 8875 | |
| 8876 | fd: int |
| 8877 | position: Py_off_t |
| 8878 | how: int |
| 8879 | / |
| 8880 | |
| 8881 | Set the position of a file descriptor. Return the new position. |
| 8882 | |
| 8883 | Return the new cursor position in number of bytes |
| 8884 | relative to the beginning of the file. |
| 8885 | [clinic start generated code]*/ |
| 8886 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8887 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8888 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8889 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8890 | { |
| 8891 | Py_off_t result; |
| 8892 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8893 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8894 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8895 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8896 | case 0: how = SEEK_SET; break; |
| 8897 | case 1: how = SEEK_CUR; break; |
| 8898 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8899 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8900 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8901 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8902 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8903 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8904 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8905 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8906 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8907 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8908 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8909 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8910 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8911 | if (result < 0) |
| 8912 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8913 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8914 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8915 | } |
| 8916 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8917 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8918 | /*[clinic input] |
| 8919 | os.read |
| 8920 | fd: int |
| 8921 | length: Py_ssize_t |
| 8922 | / |
| 8923 | |
| 8924 | Read from a file descriptor. Returns a bytes object. |
| 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_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 8929 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8930 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8931 | Py_ssize_t n; |
| 8932 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8933 | |
| 8934 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8935 | errno = EINVAL; |
| 8936 | return posix_error(); |
| 8937 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8938 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 8939 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8940 | |
| 8941 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8942 | if (buffer == NULL) |
| 8943 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8944 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8945 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8946 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8947 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8948 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8949 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8950 | |
| 8951 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8952 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8953 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8954 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8955 | } |
| 8956 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8957 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8958 | || defined(__APPLE__))) \ |
| 8959 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 8960 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 8961 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8962 | 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] | 8963 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8964 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8965 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8966 | *iov = PyMem_New(struct iovec, cnt); |
| 8967 | if (*iov == NULL) { |
| 8968 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8969 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8970 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8971 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8972 | *buf = PyMem_New(Py_buffer, cnt); |
| 8973 | if (*buf == NULL) { |
| 8974 | PyMem_Del(*iov); |
| 8975 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8976 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8977 | } |
| 8978 | |
| 8979 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8980 | PyObject *item = PySequence_GetItem(seq, i); |
| 8981 | if (item == NULL) |
| 8982 | goto fail; |
| 8983 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8984 | Py_DECREF(item); |
| 8985 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8986 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8987 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8988 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8989 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8990 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8991 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8992 | |
| 8993 | fail: |
| 8994 | PyMem_Del(*iov); |
| 8995 | for (j = 0; j < i; j++) { |
| 8996 | PyBuffer_Release(&(*buf)[j]); |
| 8997 | } |
| 8998 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8999 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9000 | } |
| 9001 | |
| 9002 | static void |
| 9003 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 9004 | { |
| 9005 | int i; |
| 9006 | PyMem_Del(iov); |
| 9007 | for (i = 0; i < cnt; i++) { |
| 9008 | PyBuffer_Release(&buf[i]); |
| 9009 | } |
| 9010 | PyMem_Del(buf); |
| 9011 | } |
| 9012 | #endif |
| 9013 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9014 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9015 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9016 | /*[clinic input] |
| 9017 | os.readv -> Py_ssize_t |
| 9018 | |
| 9019 | fd: int |
| 9020 | buffers: object |
| 9021 | / |
| 9022 | |
| 9023 | Read from a file descriptor fd into an iterable of buffers. |
| 9024 | |
| 9025 | The buffers should be mutable buffers accepting bytes. |
| 9026 | readv will transfer data into each buffer until it is full |
| 9027 | and then move on to the next buffer in the sequence to hold |
| 9028 | the rest of the data. |
| 9029 | |
| 9030 | readv returns the total number of bytes read, |
| 9031 | which may be less than the total capacity of all the buffers. |
| 9032 | [clinic start generated code]*/ |
| 9033 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9034 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9035 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 9036 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9037 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9038 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9039 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9040 | struct iovec *iov; |
| 9041 | Py_buffer *buf; |
| 9042 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9043 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9044 | PyErr_SetString(PyExc_TypeError, |
| 9045 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9046 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9047 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9048 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9049 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9050 | if (cnt < 0) |
| 9051 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9052 | |
| 9053 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 9054 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9055 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9056 | do { |
| 9057 | Py_BEGIN_ALLOW_THREADS |
| 9058 | n = readv(fd, iov, cnt); |
| 9059 | Py_END_ALLOW_THREADS |
| 9060 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9061 | |
| 9062 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9063 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9064 | if (!async_err) |
| 9065 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9066 | return -1; |
| 9067 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9068 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9069 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9070 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9071 | #endif /* HAVE_READV */ |
| 9072 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9073 | |
| 9074 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9075 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9076 | os.pread |
| 9077 | |
| 9078 | fd: int |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9079 | length: Py_ssize_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9080 | offset: Py_off_t |
| 9081 | / |
| 9082 | |
| 9083 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 9084 | |
| 9085 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 9086 | the beginning of the file. The file offset remains unchanged. |
| 9087 | [clinic start generated code]*/ |
| 9088 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9089 | static PyObject * |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 9090 | os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset) |
| 9091 | /*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9092 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9093 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9094 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9095 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9096 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9097 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9098 | errno = EINVAL; |
| 9099 | return posix_error(); |
| 9100 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9101 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9102 | if (buffer == NULL) |
| 9103 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9104 | |
| 9105 | do { |
| 9106 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9107 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9108 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9109 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9110 | Py_END_ALLOW_THREADS |
| 9111 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9112 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9113 | if (n < 0) { |
| 9114 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9115 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9116 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9117 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9118 | _PyBytes_Resize(&buffer, n); |
| 9119 | return buffer; |
| 9120 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9121 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9122 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9123 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 9124 | /*[clinic input] |
| 9125 | os.preadv -> Py_ssize_t |
| 9126 | |
| 9127 | fd: int |
| 9128 | buffers: object |
| 9129 | offset: Py_off_t |
| 9130 | flags: int = 0 |
| 9131 | / |
| 9132 | |
| 9133 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 9134 | |
| 9135 | Combines the functionality of readv() and pread(). As readv(), it will |
| 9136 | transfer data into each buffer until it is full and then move on to the next |
| 9137 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 9138 | specifies the file offset at which the input operation is to be performed. It |
| 9139 | will return the total number of bytes read (which can be less than the total |
| 9140 | capacity of all the objects). |
| 9141 | |
| 9142 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9143 | |
| 9144 | - RWF_HIPRI |
| 9145 | - RWF_NOWAIT |
| 9146 | |
| 9147 | Using non-zero flags requires Linux 4.6 or newer. |
| 9148 | [clinic start generated code]*/ |
| 9149 | |
| 9150 | static Py_ssize_t |
| 9151 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9152 | int flags) |
| 9153 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 9154 | { |
| 9155 | Py_ssize_t cnt, n; |
| 9156 | int async_err = 0; |
| 9157 | struct iovec *iov; |
| 9158 | Py_buffer *buf; |
| 9159 | |
| 9160 | if (!PySequence_Check(buffers)) { |
| 9161 | PyErr_SetString(PyExc_TypeError, |
| 9162 | "preadv2() arg 2 must be a sequence"); |
| 9163 | return -1; |
| 9164 | } |
| 9165 | |
| 9166 | cnt = PySequence_Size(buffers); |
| 9167 | if (cnt < 0) { |
| 9168 | return -1; |
| 9169 | } |
| 9170 | |
| 9171 | #ifndef HAVE_PREADV2 |
| 9172 | if(flags != 0) { |
| 9173 | argument_unavailable_error("preadv2", "flags"); |
| 9174 | return -1; |
| 9175 | } |
| 9176 | #endif |
| 9177 | |
| 9178 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 9179 | return -1; |
| 9180 | } |
| 9181 | #ifdef HAVE_PREADV2 |
| 9182 | do { |
| 9183 | Py_BEGIN_ALLOW_THREADS |
| 9184 | _Py_BEGIN_SUPPRESS_IPH |
| 9185 | n = preadv2(fd, iov, cnt, offset, flags); |
| 9186 | _Py_END_SUPPRESS_IPH |
| 9187 | Py_END_ALLOW_THREADS |
| 9188 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9189 | #else |
| 9190 | do { |
| 9191 | Py_BEGIN_ALLOW_THREADS |
| 9192 | _Py_BEGIN_SUPPRESS_IPH |
| 9193 | n = preadv(fd, iov, cnt, offset); |
| 9194 | _Py_END_SUPPRESS_IPH |
| 9195 | Py_END_ALLOW_THREADS |
| 9196 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9197 | #endif |
| 9198 | |
| 9199 | iov_cleanup(iov, buf, cnt); |
| 9200 | if (n < 0) { |
| 9201 | if (!async_err) { |
| 9202 | posix_error(); |
| 9203 | } |
| 9204 | return -1; |
| 9205 | } |
| 9206 | |
| 9207 | return n; |
| 9208 | } |
| 9209 | #endif /* HAVE_PREADV */ |
| 9210 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9211 | |
| 9212 | /*[clinic input] |
| 9213 | os.write -> Py_ssize_t |
| 9214 | |
| 9215 | fd: int |
| 9216 | data: Py_buffer |
| 9217 | / |
| 9218 | |
| 9219 | Write a bytes object to a file descriptor. |
| 9220 | [clinic start generated code]*/ |
| 9221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9222 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9223 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 9224 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9225 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9226 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9227 | } |
| 9228 | |
| 9229 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9230 | PyDoc_STRVAR(posix_sendfile__doc__, |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9231 | "sendfile(out_fd, in_fd, offset, count) -> byteswritten\n\ |
| 9232 | sendfile(out_fd, in_fd, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9233 | -> byteswritten\n\ |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9234 | Copy count bytes from file descriptor in_fd to file descriptor out_fd."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9235 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9236 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9237 | static PyObject * |
| 9238 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 9239 | { |
| 9240 | int in, out; |
| 9241 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9242 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9243 | off_t offset; |
| 9244 | |
| 9245 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 9246 | #ifndef __APPLE__ |
| 9247 | Py_ssize_t len; |
| 9248 | #endif |
| 9249 | PyObject *headers = NULL, *trailers = NULL; |
| 9250 | Py_buffer *hbuf, *tbuf; |
| 9251 | off_t sbytes; |
| 9252 | struct sf_hdtr sf; |
| 9253 | int flags = 0; |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9254 | static char *keywords[] = {"out_fd", "in_fd", |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 9255 | "offset", "count", |
| 9256 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9257 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 9258 | sf.headers = NULL; |
| 9259 | sf.trailers = NULL; |
| 9260 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9261 | #ifdef __APPLE__ |
| 9262 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9263 | keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9264 | #else |
| 9265 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9266 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9267 | #endif |
| 9268 | &headers, &trailers, &flags)) |
| 9269 | return NULL; |
| 9270 | if (headers != NULL) { |
| 9271 | if (!PySequence_Check(headers)) { |
| 9272 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9273 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9274 | return NULL; |
| 9275 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9276 | Py_ssize_t i = PySequence_Size(headers); |
| 9277 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9278 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9279 | if (i > INT_MAX) { |
| 9280 | PyErr_SetString(PyExc_OverflowError, |
| 9281 | "sendfile() header is too large"); |
| 9282 | return NULL; |
| 9283 | } |
| 9284 | if (i > 0) { |
| 9285 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9286 | if (iov_setup(&(sf.headers), &hbuf, |
| 9287 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9288 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9289 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9290 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 9291 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 9292 | # define OFF_T_MAX 0x7fffffffffffffff |
| 9293 | if (sbytes >= OFF_T_MAX - blen) { |
| 9294 | PyErr_SetString(PyExc_OverflowError, |
| 9295 | "sendfile() header is too large"); |
| 9296 | return NULL; |
| 9297 | } |
| 9298 | sbytes += blen; |
| 9299 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9300 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9301 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9302 | } |
| 9303 | } |
| 9304 | if (trailers != NULL) { |
| 9305 | if (!PySequence_Check(trailers)) { |
| 9306 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9307 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9308 | return NULL; |
| 9309 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9310 | Py_ssize_t i = PySequence_Size(trailers); |
| 9311 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9312 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9313 | if (i > INT_MAX) { |
| 9314 | PyErr_SetString(PyExc_OverflowError, |
| 9315 | "sendfile() trailer is too large"); |
| 9316 | return NULL; |
| 9317 | } |
| 9318 | if (i > 0) { |
| 9319 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9320 | if (iov_setup(&(sf.trailers), &tbuf, |
| 9321 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9322 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9323 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9324 | } |
| 9325 | } |
| 9326 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9327 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9328 | do { |
| 9329 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9330 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9331 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9332 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9333 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9334 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9335 | Py_END_ALLOW_THREADS |
| 9336 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9337 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9338 | |
| 9339 | if (sf.headers != NULL) |
| 9340 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 9341 | if (sf.trailers != NULL) |
| 9342 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 9343 | |
| 9344 | if (ret < 0) { |
| 9345 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 9346 | if (sbytes != 0) { |
| 9347 | // some data has been sent |
| 9348 | goto done; |
| 9349 | } |
| 9350 | else { |
| 9351 | // no data has been sent; upper application is supposed |
| 9352 | // to retry on EAGAIN or EBUSY |
| 9353 | return posix_error(); |
| 9354 | } |
| 9355 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9356 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9357 | } |
| 9358 | goto done; |
| 9359 | |
| 9360 | done: |
| 9361 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 9362 | return Py_BuildValue("l", sbytes); |
| 9363 | #else |
| 9364 | return Py_BuildValue("L", sbytes); |
| 9365 | #endif |
| 9366 | |
| 9367 | #else |
| 9368 | Py_ssize_t count; |
| 9369 | PyObject *offobj; |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9370 | static char *keywords[] = {"out_fd", "in_fd", |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9371 | "offset", "count", NULL}; |
| 9372 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 9373 | keywords, &out, &in, &offobj, &count)) |
| 9374 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9375 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9376 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9377 | do { |
| 9378 | Py_BEGIN_ALLOW_THREADS |
| 9379 | ret = sendfile(out, in, NULL, count); |
| 9380 | Py_END_ALLOW_THREADS |
| 9381 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9382 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9383 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9384 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9385 | } |
| 9386 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9387 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9388 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9389 | |
| 9390 | do { |
| 9391 | Py_BEGIN_ALLOW_THREADS |
| 9392 | ret = sendfile(out, in, &offset, count); |
| 9393 | Py_END_ALLOW_THREADS |
| 9394 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9395 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9396 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9397 | return Py_BuildValue("n", ret); |
| 9398 | #endif |
| 9399 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9400 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9401 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9402 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9403 | #if defined(__APPLE__) |
| 9404 | /*[clinic input] |
| 9405 | os._fcopyfile |
| 9406 | |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9407 | in_fd: int |
| 9408 | out_fd: int |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9409 | flags: int |
| 9410 | / |
| 9411 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9412 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9413 | [clinic start generated code]*/ |
| 9414 | |
| 9415 | static PyObject * |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9416 | os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags) |
| 9417 | /*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9418 | { |
| 9419 | int ret; |
| 9420 | |
| 9421 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9422 | ret = fcopyfile(in_fd, out_fd, NULL, flags); |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9423 | Py_END_ALLOW_THREADS |
| 9424 | if (ret < 0) |
| 9425 | return posix_error(); |
| 9426 | Py_RETURN_NONE; |
| 9427 | } |
| 9428 | #endif |
| 9429 | |
| 9430 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9431 | /*[clinic input] |
| 9432 | os.fstat |
| 9433 | |
| 9434 | fd : int |
| 9435 | |
| 9436 | Perform a stat system call on the given file descriptor. |
| 9437 | |
| 9438 | Like stat(), but for an open file descriptor. |
| 9439 | Equivalent to os.stat(fd). |
| 9440 | [clinic start generated code]*/ |
| 9441 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9442 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9443 | os_fstat_impl(PyObject *module, int fd) |
| 9444 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9445 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9446 | STRUCT_STAT st; |
| 9447 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9448 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9449 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9450 | do { |
| 9451 | Py_BEGIN_ALLOW_THREADS |
| 9452 | res = FSTAT(fd, &st); |
| 9453 | Py_END_ALLOW_THREADS |
| 9454 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9455 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9456 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9457 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9458 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9459 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9460 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9461 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9462 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9463 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9464 | } |
| 9465 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9466 | |
| 9467 | /*[clinic input] |
| 9468 | os.isatty -> bool |
| 9469 | fd: int |
| 9470 | / |
| 9471 | |
| 9472 | Return True if the fd is connected to a terminal. |
| 9473 | |
| 9474 | Return True if the file descriptor is an open file descriptor |
| 9475 | connected to the slave end of a terminal. |
| 9476 | [clinic start generated code]*/ |
| 9477 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9478 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9479 | os_isatty_impl(PyObject *module, int fd) |
| 9480 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9481 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9482 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9483 | _Py_BEGIN_SUPPRESS_IPH |
| 9484 | return_value = isatty(fd); |
| 9485 | _Py_END_SUPPRESS_IPH |
| 9486 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9487 | } |
| 9488 | |
| 9489 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9490 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9491 | /*[clinic input] |
| 9492 | os.pipe |
| 9493 | |
| 9494 | Create a pipe. |
| 9495 | |
| 9496 | Returns a tuple of two file descriptors: |
| 9497 | (read_fd, write_fd) |
| 9498 | [clinic start generated code]*/ |
| 9499 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9500 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9501 | os_pipe_impl(PyObject *module) |
| 9502 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9503 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9504 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9505 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9506 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9507 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9508 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9509 | #else |
| 9510 | int res; |
| 9511 | #endif |
| 9512 | |
| 9513 | #ifdef MS_WINDOWS |
| 9514 | attr.nLength = sizeof(attr); |
| 9515 | attr.lpSecurityDescriptor = NULL; |
| 9516 | attr.bInheritHandle = FALSE; |
| 9517 | |
| 9518 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9519 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9520 | ok = CreatePipe(&read, &write, &attr, 0); |
| 9521 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9522 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 9523 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9524 | if (fds[0] == -1 || fds[1] == -1) { |
| 9525 | CloseHandle(read); |
| 9526 | CloseHandle(write); |
| 9527 | ok = 0; |
| 9528 | } |
| 9529 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9530 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9531 | Py_END_ALLOW_THREADS |
| 9532 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9533 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9534 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9535 | #else |
| 9536 | |
| 9537 | #ifdef HAVE_PIPE2 |
| 9538 | Py_BEGIN_ALLOW_THREADS |
| 9539 | res = pipe2(fds, O_CLOEXEC); |
| 9540 | Py_END_ALLOW_THREADS |
| 9541 | |
| 9542 | if (res != 0 && errno == ENOSYS) |
| 9543 | { |
| 9544 | #endif |
| 9545 | Py_BEGIN_ALLOW_THREADS |
| 9546 | res = pipe(fds); |
| 9547 | Py_END_ALLOW_THREADS |
| 9548 | |
| 9549 | if (res == 0) { |
| 9550 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9551 | close(fds[0]); |
| 9552 | close(fds[1]); |
| 9553 | return NULL; |
| 9554 | } |
| 9555 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9556 | close(fds[0]); |
| 9557 | close(fds[1]); |
| 9558 | return NULL; |
| 9559 | } |
| 9560 | } |
| 9561 | #ifdef HAVE_PIPE2 |
| 9562 | } |
| 9563 | #endif |
| 9564 | |
| 9565 | if (res != 0) |
| 9566 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9567 | #endif /* !MS_WINDOWS */ |
| 9568 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9569 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9570 | #endif /* HAVE_PIPE */ |
| 9571 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9572 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9573 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9574 | /*[clinic input] |
| 9575 | os.pipe2 |
| 9576 | |
| 9577 | flags: int |
| 9578 | / |
| 9579 | |
| 9580 | Create a pipe with flags set atomically. |
| 9581 | |
| 9582 | Returns a tuple of two file descriptors: |
| 9583 | (read_fd, write_fd) |
| 9584 | |
| 9585 | flags can be constructed by ORing together one or more of these values: |
| 9586 | O_NONBLOCK, O_CLOEXEC. |
| 9587 | [clinic start generated code]*/ |
| 9588 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9589 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9590 | os_pipe2_impl(PyObject *module, int flags) |
| 9591 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9592 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9593 | int fds[2]; |
| 9594 | int res; |
| 9595 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9596 | res = pipe2(fds, flags); |
| 9597 | if (res != 0) |
| 9598 | return posix_error(); |
| 9599 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9600 | } |
| 9601 | #endif /* HAVE_PIPE2 */ |
| 9602 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9603 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9604 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9605 | /*[clinic input] |
| 9606 | os.writev -> Py_ssize_t |
| 9607 | fd: int |
| 9608 | buffers: object |
| 9609 | / |
| 9610 | |
| 9611 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9612 | |
| 9613 | Returns the total number of bytes written. |
| 9614 | buffers must be a sequence of bytes-like objects. |
| 9615 | [clinic start generated code]*/ |
| 9616 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9617 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9618 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9619 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9620 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9621 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9622 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9623 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9624 | struct iovec *iov; |
| 9625 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9626 | |
| 9627 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9628 | PyErr_SetString(PyExc_TypeError, |
| 9629 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9630 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9631 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9632 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9633 | if (cnt < 0) |
| 9634 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9635 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9636 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9637 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9638 | } |
| 9639 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9640 | do { |
| 9641 | Py_BEGIN_ALLOW_THREADS |
| 9642 | result = writev(fd, iov, cnt); |
| 9643 | Py_END_ALLOW_THREADS |
| 9644 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9645 | |
| 9646 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9647 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9648 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9649 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9650 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9651 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9652 | #endif /* HAVE_WRITEV */ |
| 9653 | |
| 9654 | |
| 9655 | #ifdef HAVE_PWRITE |
| 9656 | /*[clinic input] |
| 9657 | os.pwrite -> Py_ssize_t |
| 9658 | |
| 9659 | fd: int |
| 9660 | buffer: Py_buffer |
| 9661 | offset: Py_off_t |
| 9662 | / |
| 9663 | |
| 9664 | Write bytes to a file descriptor starting at a particular offset. |
| 9665 | |
| 9666 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9667 | the file. Returns the number of bytes writte. Does not change the |
| 9668 | current file offset. |
| 9669 | [clinic start generated code]*/ |
| 9670 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9671 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9672 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9673 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9674 | { |
| 9675 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9676 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9677 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9678 | do { |
| 9679 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9680 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9681 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9682 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9683 | Py_END_ALLOW_THREADS |
| 9684 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9685 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9686 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9687 | posix_error(); |
| 9688 | return size; |
| 9689 | } |
| 9690 | #endif /* HAVE_PWRITE */ |
| 9691 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9692 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9693 | /*[clinic input] |
| 9694 | os.pwritev -> Py_ssize_t |
| 9695 | |
| 9696 | fd: int |
| 9697 | buffers: object |
| 9698 | offset: Py_off_t |
| 9699 | flags: int = 0 |
| 9700 | / |
| 9701 | |
| 9702 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9703 | |
| 9704 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9705 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9706 | buffer is written before proceeding to second, and so on. The operating system may |
| 9707 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9708 | This function writes the contents of each object to the file descriptor and returns |
| 9709 | the total number of bytes written. |
| 9710 | |
| 9711 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9712 | |
| 9713 | - RWF_DSYNC |
| 9714 | - RWF_SYNC |
| 9715 | |
| 9716 | Using non-zero flags requires Linux 4.7 or newer. |
| 9717 | [clinic start generated code]*/ |
| 9718 | |
| 9719 | static Py_ssize_t |
| 9720 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9721 | int flags) |
| 9722 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/ |
| 9723 | { |
| 9724 | Py_ssize_t cnt; |
| 9725 | Py_ssize_t result; |
| 9726 | int async_err = 0; |
| 9727 | struct iovec *iov; |
| 9728 | Py_buffer *buf; |
| 9729 | |
| 9730 | if (!PySequence_Check(buffers)) { |
| 9731 | PyErr_SetString(PyExc_TypeError, |
| 9732 | "pwritev() arg 2 must be a sequence"); |
| 9733 | return -1; |
| 9734 | } |
| 9735 | |
| 9736 | cnt = PySequence_Size(buffers); |
| 9737 | if (cnt < 0) { |
| 9738 | return -1; |
| 9739 | } |
| 9740 | |
| 9741 | #ifndef HAVE_PWRITEV2 |
| 9742 | if(flags != 0) { |
| 9743 | argument_unavailable_error("pwritev2", "flags"); |
| 9744 | return -1; |
| 9745 | } |
| 9746 | #endif |
| 9747 | |
| 9748 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9749 | return -1; |
| 9750 | } |
| 9751 | #ifdef HAVE_PWRITEV2 |
| 9752 | do { |
| 9753 | Py_BEGIN_ALLOW_THREADS |
| 9754 | _Py_BEGIN_SUPPRESS_IPH |
| 9755 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9756 | _Py_END_SUPPRESS_IPH |
| 9757 | Py_END_ALLOW_THREADS |
| 9758 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9759 | #else |
| 9760 | do { |
| 9761 | Py_BEGIN_ALLOW_THREADS |
| 9762 | _Py_BEGIN_SUPPRESS_IPH |
| 9763 | result = pwritev(fd, iov, cnt, offset); |
| 9764 | _Py_END_SUPPRESS_IPH |
| 9765 | Py_END_ALLOW_THREADS |
| 9766 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9767 | #endif |
| 9768 | |
| 9769 | iov_cleanup(iov, buf, cnt); |
| 9770 | if (result < 0) { |
| 9771 | if (!async_err) { |
| 9772 | posix_error(); |
| 9773 | } |
| 9774 | return -1; |
| 9775 | } |
| 9776 | |
| 9777 | return result; |
| 9778 | } |
| 9779 | #endif /* HAVE_PWRITEV */ |
| 9780 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9781 | #ifdef HAVE_COPY_FILE_RANGE |
| 9782 | /*[clinic input] |
| 9783 | |
| 9784 | os.copy_file_range |
| 9785 | src: int |
| 9786 | Source file descriptor. |
| 9787 | dst: int |
| 9788 | Destination file descriptor. |
| 9789 | count: Py_ssize_t |
| 9790 | Number of bytes to copy. |
| 9791 | offset_src: object = None |
| 9792 | Starting offset in src. |
| 9793 | offset_dst: object = None |
| 9794 | Starting offset in dst. |
| 9795 | |
| 9796 | Copy count bytes from one file descriptor to another. |
| 9797 | |
| 9798 | If offset_src is None, then src is read from the current position; |
| 9799 | respectively for offset_dst. |
| 9800 | [clinic start generated code]*/ |
| 9801 | |
| 9802 | static PyObject * |
| 9803 | os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, |
| 9804 | PyObject *offset_src, PyObject *offset_dst) |
| 9805 | /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ |
| 9806 | { |
| 9807 | off_t offset_src_val, offset_dst_val; |
| 9808 | off_t *p_offset_src = NULL; |
| 9809 | off_t *p_offset_dst = NULL; |
| 9810 | Py_ssize_t ret; |
| 9811 | int async_err = 0; |
| 9812 | /* The flags argument is provided to allow |
| 9813 | * for future extensions and currently must be to 0. */ |
| 9814 | int flags = 0; |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9815 | |
| 9816 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9817 | if (count < 0) { |
| 9818 | PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); |
| 9819 | return NULL; |
| 9820 | } |
| 9821 | |
| 9822 | if (offset_src != Py_None) { |
| 9823 | if (!Py_off_t_converter(offset_src, &offset_src_val)) { |
| 9824 | return NULL; |
| 9825 | } |
| 9826 | p_offset_src = &offset_src_val; |
| 9827 | } |
| 9828 | |
| 9829 | if (offset_dst != Py_None) { |
| 9830 | if (!Py_off_t_converter(offset_dst, &offset_dst_val)) { |
| 9831 | return NULL; |
| 9832 | } |
| 9833 | p_offset_dst = &offset_dst_val; |
| 9834 | } |
| 9835 | |
| 9836 | do { |
| 9837 | Py_BEGIN_ALLOW_THREADS |
| 9838 | ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags); |
| 9839 | Py_END_ALLOW_THREADS |
| 9840 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9841 | |
| 9842 | if (ret < 0) { |
| 9843 | return (!async_err) ? posix_error() : NULL; |
| 9844 | } |
| 9845 | |
| 9846 | return PyLong_FromSsize_t(ret); |
| 9847 | } |
| 9848 | #endif /* HAVE_COPY_FILE_RANGE*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9849 | |
| 9850 | #ifdef HAVE_MKFIFO |
| 9851 | /*[clinic input] |
| 9852 | os.mkfifo |
| 9853 | |
| 9854 | path: path_t |
| 9855 | mode: int=0o666 |
| 9856 | * |
| 9857 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9858 | |
| 9859 | Create a "fifo" (a POSIX named pipe). |
| 9860 | |
| 9861 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9862 | and path should be relative; path will then be relative to that directory. |
| 9863 | dir_fd may not be implemented on your platform. |
| 9864 | If it is unavailable, using it will raise a NotImplementedError. |
| 9865 | [clinic start generated code]*/ |
| 9866 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9867 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9868 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9869 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9870 | { |
| 9871 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9872 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9873 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9874 | do { |
| 9875 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9876 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9877 | if (dir_fd != DEFAULT_DIR_FD) |
| 9878 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9879 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9880 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9881 | result = mkfifo(path->narrow, mode); |
| 9882 | Py_END_ALLOW_THREADS |
| 9883 | } while (result != 0 && errno == EINTR && |
| 9884 | !(async_err = PyErr_CheckSignals())); |
| 9885 | if (result != 0) |
| 9886 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9887 | |
| 9888 | Py_RETURN_NONE; |
| 9889 | } |
| 9890 | #endif /* HAVE_MKFIFO */ |
| 9891 | |
| 9892 | |
| 9893 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9894 | /*[clinic input] |
| 9895 | os.mknod |
| 9896 | |
| 9897 | path: path_t |
| 9898 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9899 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9900 | * |
| 9901 | dir_fd: dir_fd(requires='mknodat')=None |
| 9902 | |
| 9903 | Create a node in the file system. |
| 9904 | |
| 9905 | Create a node in the file system (file, device special file or named pipe) |
| 9906 | at path. mode specifies both the permissions to use and the |
| 9907 | type of node to be created, being combined (bitwise OR) with one of |
| 9908 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 9909 | device defines the newly created device special file (probably using |
| 9910 | os.makedev()). Otherwise device is ignored. |
| 9911 | |
| 9912 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9913 | and path should be relative; path will then be relative to that directory. |
| 9914 | dir_fd may not be implemented on your platform. |
| 9915 | If it is unavailable, using it will raise a NotImplementedError. |
| 9916 | [clinic start generated code]*/ |
| 9917 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9918 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9919 | 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] | 9920 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9921 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9922 | { |
| 9923 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9924 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9925 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9926 | do { |
| 9927 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9928 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9929 | if (dir_fd != DEFAULT_DIR_FD) |
| 9930 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 9931 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9932 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9933 | result = mknod(path->narrow, mode, device); |
| 9934 | Py_END_ALLOW_THREADS |
| 9935 | } while (result != 0 && errno == EINTR && |
| 9936 | !(async_err = PyErr_CheckSignals())); |
| 9937 | if (result != 0) |
| 9938 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9939 | |
| 9940 | Py_RETURN_NONE; |
| 9941 | } |
| 9942 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 9943 | |
| 9944 | |
| 9945 | #ifdef HAVE_DEVICE_MACROS |
| 9946 | /*[clinic input] |
| 9947 | os.major -> unsigned_int |
| 9948 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9949 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9950 | / |
| 9951 | |
| 9952 | Extracts a device major number from a raw device number. |
| 9953 | [clinic start generated code]*/ |
| 9954 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9955 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9956 | os_major_impl(PyObject *module, dev_t device) |
| 9957 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9958 | { |
| 9959 | return major(device); |
| 9960 | } |
| 9961 | |
| 9962 | |
| 9963 | /*[clinic input] |
| 9964 | os.minor -> unsigned_int |
| 9965 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9966 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9967 | / |
| 9968 | |
| 9969 | Extracts a device minor number from a raw device number. |
| 9970 | [clinic start generated code]*/ |
| 9971 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9972 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9973 | os_minor_impl(PyObject *module, dev_t device) |
| 9974 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9975 | { |
| 9976 | return minor(device); |
| 9977 | } |
| 9978 | |
| 9979 | |
| 9980 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9981 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9982 | |
| 9983 | major: int |
| 9984 | minor: int |
| 9985 | / |
| 9986 | |
| 9987 | Composes a raw device number from the major and minor device numbers. |
| 9988 | [clinic start generated code]*/ |
| 9989 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9990 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9991 | os_makedev_impl(PyObject *module, int major, int minor) |
| 9992 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9993 | { |
| 9994 | return makedev(major, minor); |
| 9995 | } |
| 9996 | #endif /* HAVE_DEVICE_MACROS */ |
| 9997 | |
| 9998 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9999 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10000 | /*[clinic input] |
| 10001 | os.ftruncate |
| 10002 | |
| 10003 | fd: int |
| 10004 | length: Py_off_t |
| 10005 | / |
| 10006 | |
| 10007 | Truncate a file, specified by file descriptor, to a specific length. |
| 10008 | [clinic start generated code]*/ |
| 10009 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10010 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10011 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 10012 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10013 | { |
| 10014 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10015 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10016 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10017 | if (PySys_Audit("os.truncate", "in", fd, length) < 0) { |
| 10018 | return NULL; |
| 10019 | } |
| 10020 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10021 | do { |
| 10022 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10023 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10024 | #ifdef MS_WINDOWS |
| 10025 | result = _chsize_s(fd, length); |
| 10026 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10027 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10028 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10029 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10030 | Py_END_ALLOW_THREADS |
| 10031 | } while (result != 0 && errno == EINTR && |
| 10032 | !(async_err = PyErr_CheckSignals())); |
| 10033 | if (result != 0) |
| 10034 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10035 | Py_RETURN_NONE; |
| 10036 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10037 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10038 | |
| 10039 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10040 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10041 | /*[clinic input] |
| 10042 | os.truncate |
| 10043 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 10044 | length: Py_off_t |
| 10045 | |
| 10046 | Truncate a file, specified by path, to a specific length. |
| 10047 | |
| 10048 | On some platforms, path may also be specified as an open file descriptor. |
| 10049 | If this functionality is unavailable, using it raises an exception. |
| 10050 | [clinic start generated code]*/ |
| 10051 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10052 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10053 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 10054 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10055 | { |
| 10056 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10057 | #ifdef MS_WINDOWS |
| 10058 | int fd; |
| 10059 | #endif |
| 10060 | |
| 10061 | if (path->fd != -1) |
| 10062 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10063 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 10064 | if (PySys_Audit("os.truncate", "On", path->object, length) < 0) { |
| 10065 | return NULL; |
| 10066 | } |
| 10067 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10068 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10069 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10070 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 10071 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 10072 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10073 | result = -1; |
| 10074 | else { |
| 10075 | result = _chsize_s(fd, length); |
| 10076 | close(fd); |
| 10077 | if (result < 0) |
| 10078 | errno = result; |
| 10079 | } |
| 10080 | #else |
| 10081 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10082 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 10083 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10084 | Py_END_ALLOW_THREADS |
| 10085 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 10086 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10087 | |
| 10088 | Py_RETURN_NONE; |
| 10089 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 10090 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10091 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10092 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10093 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 10094 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 10095 | defined, which is the case in Python on AIX. AIX bug report: |
| 10096 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 10097 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 10098 | # define POSIX_FADVISE_AIX_BUG |
| 10099 | #endif |
| 10100 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10101 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10102 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10103 | /*[clinic input] |
| 10104 | os.posix_fallocate |
| 10105 | |
| 10106 | fd: int |
| 10107 | offset: Py_off_t |
| 10108 | length: Py_off_t |
| 10109 | / |
| 10110 | |
| 10111 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 10112 | |
| 10113 | Ensure that the file specified by fd encompasses a range of bytes |
| 10114 | starting at offset bytes from the beginning and continuing for length bytes. |
| 10115 | [clinic start generated code]*/ |
| 10116 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10117 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10118 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10119 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10120 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10121 | { |
| 10122 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10123 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10124 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10125 | do { |
| 10126 | Py_BEGIN_ALLOW_THREADS |
| 10127 | result = posix_fallocate(fd, offset, length); |
| 10128 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10129 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10130 | |
| 10131 | if (result == 0) |
| 10132 | Py_RETURN_NONE; |
| 10133 | |
| 10134 | if (async_err) |
| 10135 | return NULL; |
| 10136 | |
| 10137 | errno = result; |
| 10138 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10139 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10140 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10141 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10142 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10143 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10144 | /*[clinic input] |
| 10145 | os.posix_fadvise |
| 10146 | |
| 10147 | fd: int |
| 10148 | offset: Py_off_t |
| 10149 | length: Py_off_t |
| 10150 | advice: int |
| 10151 | / |
| 10152 | |
| 10153 | Announce an intention to access data in a specific pattern. |
| 10154 | |
| 10155 | Announce an intention to access data in a specific pattern, thus allowing |
| 10156 | the kernel to make optimizations. |
| 10157 | The advice applies to the region of the file specified by fd starting at |
| 10158 | offset and continuing for length bytes. |
| 10159 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 10160 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 10161 | POSIX_FADV_DONTNEED. |
| 10162 | [clinic start generated code]*/ |
| 10163 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10164 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10165 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10166 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10167 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10168 | { |
| 10169 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10170 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10171 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10172 | do { |
| 10173 | Py_BEGIN_ALLOW_THREADS |
| 10174 | result = posix_fadvise(fd, offset, length, advice); |
| 10175 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10176 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10177 | |
| 10178 | if (result == 0) |
| 10179 | Py_RETURN_NONE; |
| 10180 | |
| 10181 | if (async_err) |
| 10182 | return NULL; |
| 10183 | |
| 10184 | errno = result; |
| 10185 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10186 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10187 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10188 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10189 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 10190 | #ifdef MS_WINDOWS |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10191 | static PyObject* |
| 10192 | win32_putenv(PyObject *name, PyObject *value) |
| 10193 | { |
| 10194 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 10195 | defining hidden environment variables. */ |
| 10196 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 10197 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 10198 | { |
| 10199 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10200 | return NULL; |
| 10201 | } |
| 10202 | PyObject *unicode; |
| 10203 | if (value != NULL) { |
| 10204 | unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 10205 | } |
| 10206 | else { |
| 10207 | unicode = PyUnicode_FromFormat("%U=", name); |
| 10208 | } |
| 10209 | if (unicode == NULL) { |
| 10210 | return NULL; |
| 10211 | } |
| 10212 | |
| 10213 | Py_ssize_t size; |
| 10214 | /* PyUnicode_AsWideCharString() rejects embedded null characters */ |
| 10215 | wchar_t *env = PyUnicode_AsWideCharString(unicode, &size); |
| 10216 | Py_DECREF(unicode); |
| 10217 | |
| 10218 | if (env == NULL) { |
| 10219 | return NULL; |
| 10220 | } |
| 10221 | if (size > _MAX_ENV) { |
| 10222 | PyErr_Format(PyExc_ValueError, |
| 10223 | "the environment variable is longer than %u characters", |
| 10224 | _MAX_ENV); |
| 10225 | PyMem_Free(env); |
| 10226 | return NULL; |
| 10227 | } |
| 10228 | |
| 10229 | /* _wputenv() and SetEnvironmentVariableW() update the environment in the |
| 10230 | Process Environment Block (PEB). _wputenv() also updates CRT 'environ' |
| 10231 | and '_wenviron' variables, whereas SetEnvironmentVariableW() does not. |
| 10232 | |
| 10233 | Prefer _wputenv() to be compatible with C libraries using CRT |
| 10234 | variables and CRT functions using these variables (ex: getenv()). */ |
| 10235 | int err = _wputenv(env); |
| 10236 | PyMem_Free(env); |
| 10237 | |
| 10238 | if (err) { |
| 10239 | posix_error(); |
| 10240 | return NULL; |
| 10241 | } |
| 10242 | |
| 10243 | Py_RETURN_NONE; |
| 10244 | } |
| 10245 | #endif |
| 10246 | |
| 10247 | |
| 10248 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10249 | /*[clinic input] |
| 10250 | os.putenv |
| 10251 | |
| 10252 | name: unicode |
| 10253 | value: unicode |
| 10254 | / |
| 10255 | |
| 10256 | Change or add an environment variable. |
| 10257 | [clinic start generated code]*/ |
| 10258 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10259 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10260 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10261 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10262 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10263 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10264 | return NULL; |
| 10265 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10266 | return win32_putenv(name, value); |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10267 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10268 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10269 | /*[clinic input] |
| 10270 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10271 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10272 | name: FSConverter |
| 10273 | value: FSConverter |
| 10274 | / |
| 10275 | |
| 10276 | Change or add an environment variable. |
| 10277 | [clinic start generated code]*/ |
| 10278 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10279 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10280 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10281 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10282 | { |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10283 | const char *name_string = PyBytes_AS_STRING(name); |
| 10284 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10285 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10286 | if (strchr(name_string, '=') != NULL) { |
| 10287 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10288 | return NULL; |
| 10289 | } |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10290 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10291 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
| 10292 | return NULL; |
| 10293 | } |
| 10294 | |
Victor Stinner | b477d19 | 2020-01-22 22:48:16 +0100 | [diff] [blame] | 10295 | if (setenv(name_string, value_string, 1)) { |
| 10296 | return posix_error(); |
| 10297 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10298 | Py_RETURN_NONE; |
| 10299 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10300 | #endif /* !defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10301 | |
| 10302 | |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10303 | #ifdef MS_WINDOWS |
| 10304 | /*[clinic input] |
| 10305 | os.unsetenv |
| 10306 | name: unicode |
| 10307 | / |
| 10308 | |
| 10309 | Delete an environment variable. |
| 10310 | [clinic start generated code]*/ |
| 10311 | |
| 10312 | static PyObject * |
| 10313 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10314 | /*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/ |
| 10315 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10316 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10317 | return NULL; |
| 10318 | } |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10319 | return win32_putenv(name, NULL); |
| 10320 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10321 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10322 | /*[clinic input] |
| 10323 | os.unsetenv |
| 10324 | name: FSConverter |
| 10325 | / |
| 10326 | |
| 10327 | Delete an environment variable. |
| 10328 | [clinic start generated code]*/ |
| 10329 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10330 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10331 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10332 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10333 | { |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 10334 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
| 10335 | return NULL; |
| 10336 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10337 | #ifdef HAVE_BROKEN_UNSETENV |
| 10338 | unsetenv(PyBytes_AS_STRING(name)); |
| 10339 | #else |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10340 | int err = unsetenv(PyBytes_AS_STRING(name)); |
| 10341 | if (err) { |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10342 | return posix_error(); |
Victor Stinner | 161e7b3 | 2020-01-24 11:53:44 +0100 | [diff] [blame] | 10343 | } |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10344 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10345 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10346 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10347 | } |
Victor Stinner | b8d1262 | 2020-01-24 14:05:48 +0100 | [diff] [blame] | 10348 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10349 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10350 | |
| 10351 | /*[clinic input] |
| 10352 | os.strerror |
| 10353 | |
| 10354 | code: int |
| 10355 | / |
| 10356 | |
| 10357 | Translate an error code to a message string. |
| 10358 | [clinic start generated code]*/ |
| 10359 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10360 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10361 | os_strerror_impl(PyObject *module, int code) |
| 10362 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10363 | { |
| 10364 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10365 | if (message == NULL) { |
| 10366 | PyErr_SetString(PyExc_ValueError, |
| 10367 | "strerror() argument out of range"); |
| 10368 | return NULL; |
| 10369 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 10370 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10371 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10372 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10373 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10374 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10375 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10376 | /*[clinic input] |
| 10377 | os.WCOREDUMP -> bool |
| 10378 | |
| 10379 | status: int |
| 10380 | / |
| 10381 | |
| 10382 | Return True if the process returning status was dumped to a core file. |
| 10383 | [clinic start generated code]*/ |
| 10384 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10385 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10386 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 10387 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10388 | { |
| 10389 | WAIT_TYPE wait_status; |
| 10390 | WAIT_STATUS_INT(wait_status) = status; |
| 10391 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10392 | } |
| 10393 | #endif /* WCOREDUMP */ |
| 10394 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10395 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10396 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10397 | /*[clinic input] |
| 10398 | os.WIFCONTINUED -> bool |
| 10399 | |
| 10400 | status: int |
| 10401 | |
| 10402 | Return True if a particular process was continued from a job control stop. |
| 10403 | |
| 10404 | Return True if the process returning status was continued from a |
| 10405 | job control stop. |
| 10406 | [clinic start generated code]*/ |
| 10407 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10408 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10409 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 10410 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10411 | { |
| 10412 | WAIT_TYPE wait_status; |
| 10413 | WAIT_STATUS_INT(wait_status) = status; |
| 10414 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10415 | } |
| 10416 | #endif /* WIFCONTINUED */ |
| 10417 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10418 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10419 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10420 | /*[clinic input] |
| 10421 | os.WIFSTOPPED -> bool |
| 10422 | |
| 10423 | status: int |
| 10424 | |
| 10425 | Return True if the process returning status was stopped. |
| 10426 | [clinic start generated code]*/ |
| 10427 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10428 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10429 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 10430 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10431 | { |
| 10432 | WAIT_TYPE wait_status; |
| 10433 | WAIT_STATUS_INT(wait_status) = status; |
| 10434 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10435 | } |
| 10436 | #endif /* WIFSTOPPED */ |
| 10437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10438 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10439 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10440 | /*[clinic input] |
| 10441 | os.WIFSIGNALED -> bool |
| 10442 | |
| 10443 | status: int |
| 10444 | |
| 10445 | Return True if the process returning status was terminated by a signal. |
| 10446 | [clinic start generated code]*/ |
| 10447 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10448 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10449 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 10450 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10451 | { |
| 10452 | WAIT_TYPE wait_status; |
| 10453 | WAIT_STATUS_INT(wait_status) = status; |
| 10454 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10455 | } |
| 10456 | #endif /* WIFSIGNALED */ |
| 10457 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10458 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10459 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10460 | /*[clinic input] |
| 10461 | os.WIFEXITED -> bool |
| 10462 | |
| 10463 | status: int |
| 10464 | |
| 10465 | Return True if the process returning status exited via the exit() system call. |
| 10466 | [clinic start generated code]*/ |
| 10467 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10468 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10469 | os_WIFEXITED_impl(PyObject *module, int status) |
| 10470 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10471 | { |
| 10472 | WAIT_TYPE wait_status; |
| 10473 | WAIT_STATUS_INT(wait_status) = status; |
| 10474 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10475 | } |
| 10476 | #endif /* WIFEXITED */ |
| 10477 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10478 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10479 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10480 | /*[clinic input] |
| 10481 | os.WEXITSTATUS -> int |
| 10482 | |
| 10483 | status: int |
| 10484 | |
| 10485 | Return the process return code from status. |
| 10486 | [clinic start generated code]*/ |
| 10487 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10488 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10489 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 10490 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10491 | { |
| 10492 | WAIT_TYPE wait_status; |
| 10493 | WAIT_STATUS_INT(wait_status) = status; |
| 10494 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10495 | } |
| 10496 | #endif /* WEXITSTATUS */ |
| 10497 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10498 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10499 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10500 | /*[clinic input] |
| 10501 | os.WTERMSIG -> int |
| 10502 | |
| 10503 | status: int |
| 10504 | |
| 10505 | Return the signal that terminated the process that provided the status value. |
| 10506 | [clinic start generated code]*/ |
| 10507 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10508 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10509 | os_WTERMSIG_impl(PyObject *module, int status) |
| 10510 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10511 | { |
| 10512 | WAIT_TYPE wait_status; |
| 10513 | WAIT_STATUS_INT(wait_status) = status; |
| 10514 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10515 | } |
| 10516 | #endif /* WTERMSIG */ |
| 10517 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10518 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10519 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10520 | /*[clinic input] |
| 10521 | os.WSTOPSIG -> int |
| 10522 | |
| 10523 | status: int |
| 10524 | |
| 10525 | Return the signal that stopped the process that provided the status value. |
| 10526 | [clinic start generated code]*/ |
| 10527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10528 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10529 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 10530 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10531 | { |
| 10532 | WAIT_TYPE wait_status; |
| 10533 | WAIT_STATUS_INT(wait_status) = status; |
| 10534 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10535 | } |
| 10536 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10537 | #endif /* HAVE_SYS_WAIT_H */ |
| 10538 | |
| 10539 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10540 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10541 | #ifdef _SCO_DS |
| 10542 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 10543 | needed definitions in sys/statvfs.h */ |
| 10544 | #define _SVID3 |
| 10545 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10546 | #include <sys/statvfs.h> |
| 10547 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10548 | static PyObject* |
| 10549 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 10550 | PyObject *StatVFSResultType = _posixstate_global->StatVFSResultType; |
| 10551 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10552 | if (v == NULL) |
| 10553 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10554 | |
| 10555 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10556 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10557 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10558 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 10559 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 10560 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 10561 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 10562 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 10563 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 10564 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10565 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10566 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10567 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10568 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10569 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10570 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10571 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10572 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10573 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10574 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10575 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10576 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10577 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10578 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10579 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10580 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10581 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10582 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10583 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10584 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 10585 | * (issue #32390). */ |
| 10586 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 10587 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 10588 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10589 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10590 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10591 | if (PyErr_Occurred()) { |
| 10592 | Py_DECREF(v); |
| 10593 | return NULL; |
| 10594 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10595 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10596 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10597 | } |
| 10598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10599 | |
| 10600 | /*[clinic input] |
| 10601 | os.fstatvfs |
| 10602 | fd: int |
| 10603 | / |
| 10604 | |
| 10605 | Perform an fstatvfs system call on the given fd. |
| 10606 | |
| 10607 | Equivalent to statvfs(fd). |
| 10608 | [clinic start generated code]*/ |
| 10609 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10610 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10611 | os_fstatvfs_impl(PyObject *module, int fd) |
| 10612 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10613 | { |
| 10614 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10615 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10616 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10617 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10618 | do { |
| 10619 | Py_BEGIN_ALLOW_THREADS |
| 10620 | result = fstatvfs(fd, &st); |
| 10621 | Py_END_ALLOW_THREADS |
| 10622 | } while (result != 0 && errno == EINTR && |
| 10623 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10624 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10625 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10626 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10627 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10628 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10629 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10630 | |
| 10631 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10632 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10633 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10634 | /*[clinic input] |
| 10635 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10636 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10637 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10638 | |
| 10639 | Perform a statvfs system call on the given path. |
| 10640 | |
| 10641 | path may always be specified as a string. |
| 10642 | On some platforms, path may also be specified as an open file descriptor. |
| 10643 | If this functionality is unavailable, using it raises an exception. |
| 10644 | [clinic start generated code]*/ |
| 10645 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10646 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10647 | os_statvfs_impl(PyObject *module, path_t *path) |
| 10648 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10649 | { |
| 10650 | int result; |
| 10651 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10652 | |
| 10653 | Py_BEGIN_ALLOW_THREADS |
| 10654 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10655 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10656 | #ifdef __APPLE__ |
| 10657 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10658 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10659 | fd_specified("statvfs", path->fd); |
| 10660 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10661 | } |
| 10662 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10663 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10664 | } |
| 10665 | else |
| 10666 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10667 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10668 | Py_END_ALLOW_THREADS |
| 10669 | |
| 10670 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10671 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10672 | } |
| 10673 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10674 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10675 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10676 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10677 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10678 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10679 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10680 | /*[clinic input] |
| 10681 | os._getdiskusage |
| 10682 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10683 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10684 | |
| 10685 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10686 | [clinic start generated code]*/ |
| 10687 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10688 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10689 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10690 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10691 | { |
| 10692 | BOOL retval; |
| 10693 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10694 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10695 | |
| 10696 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10697 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10698 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10699 | if (retval == 0) { |
| 10700 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10701 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10702 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10703 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10704 | if (dir_path == NULL) { |
| 10705 | return PyErr_NoMemory(); |
| 10706 | } |
| 10707 | |
| 10708 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10709 | |
| 10710 | if (_dirnameW(dir_path) != -1) { |
| 10711 | Py_BEGIN_ALLOW_THREADS |
| 10712 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10713 | Py_END_ALLOW_THREADS |
| 10714 | } |
| 10715 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10716 | err = GetLastError(); |
| 10717 | PyMem_Free(dir_path); |
| 10718 | if (retval) { |
| 10719 | goto success; |
| 10720 | } |
| 10721 | } |
| 10722 | return PyErr_SetFromWindowsErr(err); |
| 10723 | } |
| 10724 | |
| 10725 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10726 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10727 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10728 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10729 | |
| 10730 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10731 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10732 | * It maps strings representing configuration variable names to |
| 10733 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10734 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10735 | * rarely-used constants. There are three separate tables that use |
| 10736 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10737 | * |
| 10738 | * This code is always included, even if none of the interfaces that |
| 10739 | * need it are included. The #if hackery needed to avoid it would be |
| 10740 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10741 | */ |
| 10742 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10743 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10744 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10745 | }; |
| 10746 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10747 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10748 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10749 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10750 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10751 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10752 | int value = _PyLong_AsInt(arg); |
| 10753 | if (value == -1 && PyErr_Occurred()) |
| 10754 | return 0; |
| 10755 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10756 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10757 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10758 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10759 | /* look up the value in the table using a binary search */ |
| 10760 | size_t lo = 0; |
| 10761 | size_t mid; |
| 10762 | size_t hi = tablesize; |
| 10763 | int cmp; |
| 10764 | const char *confname; |
| 10765 | if (!PyUnicode_Check(arg)) { |
| 10766 | PyErr_SetString(PyExc_TypeError, |
| 10767 | "configuration names must be strings or integers"); |
| 10768 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10769 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10770 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10771 | if (confname == NULL) |
| 10772 | return 0; |
| 10773 | while (lo < hi) { |
| 10774 | mid = (lo + hi) / 2; |
| 10775 | cmp = strcmp(confname, table[mid].name); |
| 10776 | if (cmp < 0) |
| 10777 | hi = mid; |
| 10778 | else if (cmp > 0) |
| 10779 | lo = mid + 1; |
| 10780 | else { |
| 10781 | *valuep = table[mid].value; |
| 10782 | return 1; |
| 10783 | } |
| 10784 | } |
| 10785 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10786 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10787 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10788 | } |
| 10789 | |
| 10790 | |
| 10791 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10792 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10793 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10794 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10795 | #endif |
| 10796 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10797 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10798 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10799 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10800 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10801 | #endif |
| 10802 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10803 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10804 | #endif |
| 10805 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10806 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10807 | #endif |
| 10808 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10809 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10810 | #endif |
| 10811 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10812 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10813 | #endif |
| 10814 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10815 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10816 | #endif |
| 10817 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10818 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10819 | #endif |
| 10820 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10821 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10822 | #endif |
| 10823 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10824 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10825 | #endif |
| 10826 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10827 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10828 | #endif |
| 10829 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10830 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10831 | #endif |
| 10832 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10833 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10834 | #endif |
| 10835 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10836 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10837 | #endif |
| 10838 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10839 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10840 | #endif |
| 10841 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10842 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10843 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10844 | #ifdef _PC_ACL_ENABLED |
| 10845 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10846 | #endif |
| 10847 | #ifdef _PC_MIN_HOLE_SIZE |
| 10848 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10849 | #endif |
| 10850 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10851 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10852 | #endif |
| 10853 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10854 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10855 | #endif |
| 10856 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10857 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10858 | #endif |
| 10859 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10860 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10861 | #endif |
| 10862 | #ifdef _PC_REC_XFER_ALIGN |
| 10863 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10864 | #endif |
| 10865 | #ifdef _PC_SYMLINK_MAX |
| 10866 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10867 | #endif |
| 10868 | #ifdef _PC_XATTR_ENABLED |
| 10869 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10870 | #endif |
| 10871 | #ifdef _PC_XATTR_EXISTS |
| 10872 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10873 | #endif |
| 10874 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10875 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10876 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10877 | }; |
| 10878 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10879 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10880 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10881 | { |
| 10882 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10883 | sizeof(posix_constants_pathconf) |
| 10884 | / sizeof(struct constdef)); |
| 10885 | } |
| 10886 | #endif |
| 10887 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10888 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10889 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10890 | /*[clinic input] |
| 10891 | os.fpathconf -> long |
| 10892 | |
| 10893 | fd: int |
| 10894 | name: path_confname |
| 10895 | / |
| 10896 | |
| 10897 | Return the configuration limit name for the file descriptor fd. |
| 10898 | |
| 10899 | If there is no limit, return -1. |
| 10900 | [clinic start generated code]*/ |
| 10901 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10902 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10903 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 10904 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10905 | { |
| 10906 | long limit; |
| 10907 | |
| 10908 | errno = 0; |
| 10909 | limit = fpathconf(fd, name); |
| 10910 | if (limit == -1 && errno != 0) |
| 10911 | posix_error(); |
| 10912 | |
| 10913 | return limit; |
| 10914 | } |
| 10915 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10916 | |
| 10917 | |
| 10918 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10919 | /*[clinic input] |
| 10920 | os.pathconf -> long |
| 10921 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 10922 | name: path_confname |
| 10923 | |
| 10924 | Return the configuration limit name for the file or directory path. |
| 10925 | |
| 10926 | If there is no limit, return -1. |
| 10927 | On some platforms, path may also be specified as an open file descriptor. |
| 10928 | If this functionality is unavailable, using it raises an exception. |
| 10929 | [clinic start generated code]*/ |
| 10930 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10931 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10932 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 10933 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10934 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10935 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10936 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10937 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10938 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10939 | if (path->fd != -1) |
| 10940 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10941 | else |
| 10942 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10943 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10944 | if (limit == -1 && errno != 0) { |
| 10945 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 10946 | /* could be a path or name problem */ |
| 10947 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10948 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10949 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10950 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10951 | |
| 10952 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10953 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10954 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10955 | |
| 10956 | #ifdef HAVE_CONFSTR |
| 10957 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10958 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10959 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10960 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10961 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10962 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10963 | #endif |
| 10964 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10965 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10966 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10967 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10968 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10969 | #endif |
| 10970 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10971 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10972 | #endif |
| 10973 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10974 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10975 | #endif |
| 10976 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10977 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10978 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10979 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10980 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10981 | #endif |
| 10982 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10983 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10984 | #endif |
| 10985 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10986 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10987 | #endif |
| 10988 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10989 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10990 | #endif |
| 10991 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10992 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10993 | #endif |
| 10994 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10995 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10996 | #endif |
| 10997 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10998 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10999 | #endif |
| 11000 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11001 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11002 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11003 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11004 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11005 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11006 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11007 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11008 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11009 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11010 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11011 | #endif |
| 11012 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11013 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11014 | #endif |
| 11015 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11016 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11017 | #endif |
| 11018 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11019 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11020 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11021 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11022 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11023 | #endif |
| 11024 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11025 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11026 | #endif |
| 11027 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11028 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11029 | #endif |
| 11030 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11031 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11032 | #endif |
| 11033 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11034 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11035 | #endif |
| 11036 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11037 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11038 | #endif |
| 11039 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11040 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11041 | #endif |
| 11042 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11043 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11044 | #endif |
| 11045 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11046 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11047 | #endif |
| 11048 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11049 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11050 | #endif |
| 11051 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11052 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11053 | #endif |
| 11054 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11055 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11056 | #endif |
| 11057 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11058 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11059 | #endif |
| 11060 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11061 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11062 | #endif |
| 11063 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11064 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11065 | #endif |
| 11066 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11067 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11068 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11069 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11070 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11071 | #endif |
| 11072 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11073 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11074 | #endif |
| 11075 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11076 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11077 | #endif |
| 11078 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11079 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11080 | #endif |
| 11081 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11082 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11083 | #endif |
| 11084 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11085 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11086 | #endif |
| 11087 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11088 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11089 | #endif |
| 11090 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11091 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11092 | #endif |
| 11093 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11094 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11095 | #endif |
| 11096 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11097 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11098 | #endif |
| 11099 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11100 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11101 | #endif |
| 11102 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11103 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11104 | #endif |
| 11105 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11106 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11107 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11108 | }; |
| 11109 | |
| 11110 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11111 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11112 | { |
| 11113 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 11114 | sizeof(posix_constants_confstr) |
| 11115 | / sizeof(struct constdef)); |
| 11116 | } |
| 11117 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11118 | |
| 11119 | /*[clinic input] |
| 11120 | os.confstr |
| 11121 | |
| 11122 | name: confstr_confname |
| 11123 | / |
| 11124 | |
| 11125 | Return a string-valued system configuration variable. |
| 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_confstr_impl(PyObject *module, int name) |
| 11130 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11131 | { |
| 11132 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11133 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11134 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11135 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11136 | errno = 0; |
| 11137 | len = confstr(name, buffer, sizeof(buffer)); |
| 11138 | if (len == 0) { |
| 11139 | if (errno) { |
| 11140 | posix_error(); |
| 11141 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11142 | } |
| 11143 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11144 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11145 | } |
| 11146 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11147 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11148 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11149 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11150 | char *buf = PyMem_Malloc(len); |
| 11151 | if (buf == NULL) |
| 11152 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11153 | len2 = confstr(name, buf, len); |
| 11154 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 11155 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11156 | PyMem_Free(buf); |
| 11157 | } |
| 11158 | else |
| 11159 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11160 | return result; |
| 11161 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11162 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11163 | |
| 11164 | |
| 11165 | #ifdef HAVE_SYSCONF |
| 11166 | static struct constdef posix_constants_sysconf[] = { |
| 11167 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11168 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11169 | #endif |
| 11170 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11171 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11172 | #endif |
| 11173 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11174 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11175 | #endif |
| 11176 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11177 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11178 | #endif |
| 11179 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11180 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11181 | #endif |
| 11182 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11183 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11184 | #endif |
| 11185 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11186 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11187 | #endif |
| 11188 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11189 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11190 | #endif |
| 11191 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11192 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11193 | #endif |
| 11194 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11195 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11196 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11197 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11198 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11199 | #endif |
| 11200 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11201 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11202 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11203 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11204 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11205 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11206 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11207 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11208 | #endif |
| 11209 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11210 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11211 | #endif |
| 11212 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11213 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11214 | #endif |
| 11215 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11216 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11217 | #endif |
| 11218 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11219 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11220 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11221 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11222 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11223 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11224 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11225 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11226 | #endif |
| 11227 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11228 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11229 | #endif |
| 11230 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11231 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11232 | #endif |
| 11233 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11234 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11235 | #endif |
| 11236 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11237 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11238 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11239 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11240 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11241 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11242 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11243 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11244 | #endif |
| 11245 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11246 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11247 | #endif |
| 11248 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11249 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11250 | #endif |
| 11251 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11252 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11253 | #endif |
| 11254 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11255 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11256 | #endif |
| 11257 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11258 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11259 | #endif |
| 11260 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11261 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11262 | #endif |
| 11263 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11264 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11265 | #endif |
| 11266 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11267 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11268 | #endif |
| 11269 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11270 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11271 | #endif |
| 11272 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11273 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11274 | #endif |
| 11275 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11276 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11277 | #endif |
| 11278 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11279 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11280 | #endif |
| 11281 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11282 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11283 | #endif |
| 11284 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11285 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11286 | #endif |
| 11287 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11288 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11289 | #endif |
| 11290 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11291 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11292 | #endif |
| 11293 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11294 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11295 | #endif |
| 11296 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11297 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11298 | #endif |
| 11299 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11300 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11301 | #endif |
| 11302 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11303 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11304 | #endif |
| 11305 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11306 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11307 | #endif |
| 11308 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11309 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11310 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11311 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11312 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11313 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11314 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11315 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11316 | #endif |
| 11317 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11318 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11319 | #endif |
| 11320 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11321 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11322 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11323 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11324 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11325 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11326 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11327 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11328 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11329 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11330 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11331 | #endif |
| 11332 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11333 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11334 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11335 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11336 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11337 | #endif |
| 11338 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11339 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11340 | #endif |
| 11341 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11342 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11343 | #endif |
| 11344 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11345 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11346 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11347 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11348 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11349 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11350 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11351 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11352 | #endif |
| 11353 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11354 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11355 | #endif |
| 11356 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11357 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11358 | #endif |
| 11359 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11360 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11361 | #endif |
| 11362 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11363 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11364 | #endif |
| 11365 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11366 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11367 | #endif |
| 11368 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11369 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11370 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11371 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11372 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11373 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11374 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11375 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11376 | #endif |
| 11377 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11378 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11379 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11380 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11381 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11382 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11383 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11384 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11385 | #endif |
| 11386 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11387 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11388 | #endif |
| 11389 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11390 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11391 | #endif |
| 11392 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11393 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11394 | #endif |
| 11395 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11396 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11397 | #endif |
| 11398 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11399 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11400 | #endif |
| 11401 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11402 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11403 | #endif |
| 11404 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11405 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11406 | #endif |
| 11407 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11408 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11409 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11410 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11411 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11412 | #endif |
| 11413 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11414 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11415 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11416 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11417 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11418 | #endif |
| 11419 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11420 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11421 | #endif |
| 11422 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11423 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11424 | #endif |
| 11425 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11426 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11427 | #endif |
Batuhan Taşkaya | 909f4a3 | 2020-04-05 03:40:49 +0300 | [diff] [blame] | 11428 | #ifdef _SC_AIX_REALMEM |
| 11429 | {"SC_AIX_REALMEM", _SC_AIX_REALMEM}, |
| 11430 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11431 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11432 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11433 | #endif |
| 11434 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11435 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11436 | #endif |
| 11437 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11438 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11439 | #endif |
| 11440 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11441 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11442 | #endif |
| 11443 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11444 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11445 | #endif |
| 11446 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11447 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11448 | #endif |
| 11449 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11450 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11451 | #endif |
| 11452 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11453 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11454 | #endif |
| 11455 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11456 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11457 | #endif |
| 11458 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11459 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11460 | #endif |
| 11461 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11462 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11463 | #endif |
| 11464 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11465 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11466 | #endif |
| 11467 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11468 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11469 | #endif |
| 11470 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11471 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11472 | #endif |
| 11473 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11474 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11475 | #endif |
| 11476 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11477 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11478 | #endif |
| 11479 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11480 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11481 | #endif |
| 11482 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11483 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11484 | #endif |
| 11485 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11486 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11487 | #endif |
| 11488 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11489 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11490 | #endif |
| 11491 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11492 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11493 | #endif |
| 11494 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11495 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11496 | #endif |
| 11497 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11498 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11499 | #endif |
| 11500 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11501 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11502 | #endif |
| 11503 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11504 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11505 | #endif |
| 11506 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11507 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11508 | #endif |
| 11509 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11510 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11511 | #endif |
| 11512 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11513 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11514 | #endif |
| 11515 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11516 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11517 | #endif |
| 11518 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11519 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11520 | #endif |
| 11521 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11522 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11523 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11524 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11525 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11526 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11527 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11528 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11529 | #endif |
| 11530 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11531 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11532 | #endif |
| 11533 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11534 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11535 | #endif |
| 11536 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11537 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11538 | #endif |
| 11539 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11540 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11541 | #endif |
| 11542 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11543 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11544 | #endif |
| 11545 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11546 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11547 | #endif |
| 11548 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11549 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11550 | #endif |
| 11551 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11552 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11553 | #endif |
| 11554 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11555 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11556 | #endif |
| 11557 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11558 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11559 | #endif |
| 11560 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11561 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11562 | #endif |
| 11563 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11564 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11565 | #endif |
| 11566 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11567 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11568 | #endif |
| 11569 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11570 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11571 | #endif |
| 11572 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11573 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11574 | #endif |
| 11575 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11576 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11577 | #endif |
| 11578 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11579 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11580 | #endif |
| 11581 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11582 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11583 | #endif |
| 11584 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11585 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11586 | #endif |
| 11587 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11588 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11589 | #endif |
| 11590 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11591 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11592 | #endif |
| 11593 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11594 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11595 | #endif |
| 11596 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11597 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11598 | #endif |
| 11599 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11600 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11601 | #endif |
| 11602 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11603 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11604 | #endif |
| 11605 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11606 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11607 | #endif |
| 11608 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11609 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11610 | #endif |
| 11611 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11612 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11613 | #endif |
| 11614 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11615 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11616 | #endif |
| 11617 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11618 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11619 | #endif |
| 11620 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11621 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11622 | #endif |
| 11623 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11624 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11625 | #endif |
| 11626 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11627 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11628 | #endif |
| 11629 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11630 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11631 | #endif |
| 11632 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11633 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11634 | #endif |
| 11635 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11636 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11637 | #endif |
| 11638 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11639 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11640 | #endif |
| 11641 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11642 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11643 | #endif |
| 11644 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11645 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11646 | #endif |
| 11647 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11648 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11649 | #endif |
| 11650 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11651 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11652 | #endif |
| 11653 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11654 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11655 | #endif |
| 11656 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11657 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11658 | #endif |
| 11659 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11660 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11661 | #endif |
| 11662 | }; |
| 11663 | |
| 11664 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11665 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11666 | { |
| 11667 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11668 | sizeof(posix_constants_sysconf) |
| 11669 | / sizeof(struct constdef)); |
| 11670 | } |
| 11671 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11672 | |
| 11673 | /*[clinic input] |
| 11674 | os.sysconf -> long |
| 11675 | name: sysconf_confname |
| 11676 | / |
| 11677 | |
| 11678 | Return an integer-valued system configuration variable. |
| 11679 | [clinic start generated code]*/ |
| 11680 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11681 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11682 | os_sysconf_impl(PyObject *module, int name) |
| 11683 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11684 | { |
| 11685 | long value; |
| 11686 | |
| 11687 | errno = 0; |
| 11688 | value = sysconf(name); |
| 11689 | if (value == -1 && errno != 0) |
| 11690 | posix_error(); |
| 11691 | return value; |
| 11692 | } |
| 11693 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11694 | |
| 11695 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11696 | /* 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] | 11697 | * 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] | 11698 | * the exported dictionaries that are used to publish information about the |
| 11699 | * names available on the host platform. |
| 11700 | * |
| 11701 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11702 | * when used, even for platforms we're not able to test on. It also makes |
| 11703 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11704 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11705 | |
| 11706 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11707 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11708 | { |
| 11709 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11710 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11711 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11712 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11713 | |
| 11714 | return strcmp(c1->name, c2->name); |
| 11715 | } |
| 11716 | |
| 11717 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11718 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11719 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11720 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11721 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11722 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11723 | |
| 11724 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11725 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11726 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11727 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11728 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11729 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11730 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11731 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11732 | Py_XDECREF(o); |
| 11733 | Py_DECREF(d); |
| 11734 | return -1; |
| 11735 | } |
| 11736 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11737 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11738 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11739 | } |
| 11740 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11741 | /* Return -1 on failure, 0 on success. */ |
| 11742 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11743 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11744 | { |
| 11745 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11746 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11747 | sizeof(posix_constants_pathconf) |
| 11748 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11749 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11750 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11751 | #endif |
| 11752 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11753 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11754 | sizeof(posix_constants_confstr) |
| 11755 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11756 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11757 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11758 | #endif |
| 11759 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11760 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11761 | sizeof(posix_constants_sysconf) |
| 11762 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11763 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11764 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11765 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11766 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11767 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11768 | |
| 11769 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11770 | /*[clinic input] |
| 11771 | os.abort |
| 11772 | |
| 11773 | Abort the interpreter immediately. |
| 11774 | |
| 11775 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11776 | on the hosting operating system. This function never returns. |
| 11777 | [clinic start generated code]*/ |
| 11778 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11779 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11780 | os_abort_impl(PyObject *module) |
| 11781 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11782 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11783 | abort(); |
| 11784 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11785 | #ifndef __clang__ |
| 11786 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11787 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11788 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11789 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11790 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11791 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11792 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11793 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11794 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11795 | /* Grab ShellExecute dynamically from shell32 */ |
| 11796 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11797 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11798 | LPCWSTR, INT); |
| 11799 | static int |
| 11800 | check_ShellExecute() |
| 11801 | { |
| 11802 | HINSTANCE hShell32; |
| 11803 | |
| 11804 | /* only recheck */ |
| 11805 | if (-1 == has_ShellExecute) { |
| 11806 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11807 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11808 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11809 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11810 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11811 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11812 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11813 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11814 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11815 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11816 | } else { |
| 11817 | has_ShellExecute = 0; |
| 11818 | } |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 11819 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11820 | } |
| 11821 | return has_ShellExecute; |
| 11822 | } |
| 11823 | |
| 11824 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11825 | /*[clinic input] |
| 11826 | os.startfile |
| 11827 | filepath: path_t |
| 11828 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11829 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11830 | Start a file with its associated application. |
| 11831 | |
| 11832 | When "operation" is not specified or "open", this acts like |
| 11833 | double-clicking the file in Explorer, or giving the file name as an |
| 11834 | argument to the DOS "start" command: the file is opened with whatever |
| 11835 | application (if any) its extension is associated. |
| 11836 | When another "operation" is given, it specifies what should be done with |
| 11837 | the file. A typical operation is "print". |
| 11838 | |
| 11839 | startfile returns as soon as the associated application is launched. |
| 11840 | There is no option to wait for the application to close, and no way |
| 11841 | to retrieve the application's exit status. |
| 11842 | |
| 11843 | The filepath is relative to the current directory. If you want to use |
| 11844 | an absolute path, make sure the first character is not a slash ("/"); |
| 11845 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11846 | [clinic start generated code]*/ |
| 11847 | |
| 11848 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11849 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11850 | const Py_UNICODE *operation) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 11851 | /*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11852 | { |
| 11853 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11854 | |
| 11855 | if(!check_ShellExecute()) { |
| 11856 | /* If the OS doesn't have ShellExecute, return a |
| 11857 | NotImplementedError. */ |
| 11858 | return PyErr_Format(PyExc_NotImplementedError, |
| 11859 | "startfile not available on this platform"); |
| 11860 | } |
| 11861 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 11862 | if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) { |
Saiyang Gou | 95f6001 | 2020-02-04 16:15:00 -0800 | [diff] [blame] | 11863 | return NULL; |
| 11864 | } |
| 11865 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11866 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11867 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11868 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11869 | Py_END_ALLOW_THREADS |
| 11870 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11871 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11872 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11873 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11874 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11875 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11876 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11877 | #endif /* MS_WINDOWS */ |
| 11878 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11879 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11880 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11881 | /*[clinic input] |
| 11882 | os.getloadavg |
| 11883 | |
| 11884 | Return average recent system load information. |
| 11885 | |
| 11886 | Return the number of processes in the system run queue averaged over |
| 11887 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11888 | Raises OSError if the load average was unobtainable. |
| 11889 | [clinic start generated code]*/ |
| 11890 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11891 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11892 | os_getloadavg_impl(PyObject *module) |
| 11893 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11894 | { |
| 11895 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11896 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11897 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11898 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11899 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11900 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11901 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11902 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11903 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11904 | |
| 11905 | /*[clinic input] |
| 11906 | os.device_encoding |
| 11907 | fd: int |
| 11908 | |
| 11909 | Return a string describing the encoding of a terminal's file descriptor. |
| 11910 | |
| 11911 | The file descriptor must be attached to a terminal. |
| 11912 | If the device is not a terminal, return None. |
| 11913 | [clinic start generated code]*/ |
| 11914 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11915 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11916 | os_device_encoding_impl(PyObject *module, int fd) |
| 11917 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11918 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11919 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 11920 | } |
| 11921 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11922 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11923 | #ifdef HAVE_SETRESUID |
| 11924 | /*[clinic input] |
| 11925 | os.setresuid |
| 11926 | |
| 11927 | ruid: uid_t |
| 11928 | euid: uid_t |
| 11929 | suid: uid_t |
| 11930 | / |
| 11931 | |
| 11932 | Set the current process's real, effective, and saved user ids. |
| 11933 | [clinic start generated code]*/ |
| 11934 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11935 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11936 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 11937 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11938 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11939 | if (setresuid(ruid, euid, suid) < 0) |
| 11940 | return posix_error(); |
| 11941 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11942 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11943 | #endif /* HAVE_SETRESUID */ |
| 11944 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11945 | |
| 11946 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11947 | /*[clinic input] |
| 11948 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11949 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11950 | rgid: gid_t |
| 11951 | egid: gid_t |
| 11952 | sgid: gid_t |
| 11953 | / |
| 11954 | |
| 11955 | Set the current process's real, effective, and saved group ids. |
| 11956 | [clinic start generated code]*/ |
| 11957 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11958 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11959 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 11960 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11961 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11962 | if (setresgid(rgid, egid, sgid) < 0) |
| 11963 | return posix_error(); |
| 11964 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11965 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11966 | #endif /* HAVE_SETRESGID */ |
| 11967 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11968 | |
| 11969 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11970 | /*[clinic input] |
| 11971 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11972 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11973 | Return a tuple of the current process's real, effective, and saved user ids. |
| 11974 | [clinic start generated code]*/ |
| 11975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11976 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11977 | os_getresuid_impl(PyObject *module) |
| 11978 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11979 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11980 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11981 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 11982 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11983 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 11984 | _PyLong_FromUid(euid), |
| 11985 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11986 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11987 | #endif /* HAVE_GETRESUID */ |
| 11988 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11989 | |
| 11990 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11991 | /*[clinic input] |
| 11992 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11993 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11994 | Return a tuple of the current process's real, effective, and saved group ids. |
| 11995 | [clinic start generated code]*/ |
| 11996 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11997 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11998 | os_getresgid_impl(PyObject *module) |
| 11999 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12000 | { |
| 12001 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12002 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 12003 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 12004 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 12005 | _PyLong_FromGid(egid), |
| 12006 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12007 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12008 | #endif /* HAVE_GETRESGID */ |
| 12009 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 12010 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12011 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12012 | /*[clinic input] |
| 12013 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12014 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12015 | path: path_t(allow_fd=True) |
| 12016 | attribute: path_t |
| 12017 | * |
| 12018 | follow_symlinks: bool = True |
| 12019 | |
| 12020 | Return the value of extended attribute attribute on path. |
| 12021 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12022 | 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] | 12023 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12024 | link, getxattr will examine the symbolic link itself instead of the file |
| 12025 | the link points to. |
| 12026 | |
| 12027 | [clinic start generated code]*/ |
| 12028 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12029 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12030 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12031 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12032 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12033 | { |
| 12034 | Py_ssize_t i; |
| 12035 | PyObject *buffer = NULL; |
| 12036 | |
| 12037 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 12038 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12039 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12040 | if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) { |
| 12041 | return NULL; |
| 12042 | } |
| 12043 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12044 | for (i = 0; ; i++) { |
| 12045 | void *ptr; |
| 12046 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12047 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12048 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12049 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12050 | path_error(path); |
| 12051 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12052 | } |
| 12053 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 12054 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12055 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12056 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12057 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12058 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12059 | if (path->fd >= 0) |
| 12060 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12061 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12062 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12063 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12064 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12065 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12066 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12067 | if (result < 0) { |
| 12068 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12069 | if (errno == ERANGE) |
| 12070 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12071 | path_error(path); |
| 12072 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12073 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12074 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12075 | if (result != buffer_size) { |
| 12076 | /* Can only shrink. */ |
| 12077 | _PyBytes_Resize(&buffer, result); |
| 12078 | } |
| 12079 | break; |
| 12080 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12081 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12082 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12083 | } |
| 12084 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12085 | |
| 12086 | /*[clinic input] |
| 12087 | os.setxattr |
| 12088 | |
| 12089 | path: path_t(allow_fd=True) |
| 12090 | attribute: path_t |
| 12091 | value: Py_buffer |
| 12092 | flags: int = 0 |
| 12093 | * |
| 12094 | follow_symlinks: bool = True |
| 12095 | |
| 12096 | Set extended attribute attribute on path to value. |
| 12097 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12098 | 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] | 12099 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12100 | link, setxattr will modify the symbolic link itself instead of the file |
| 12101 | the link points to. |
| 12102 | |
| 12103 | [clinic start generated code]*/ |
| 12104 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12105 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12106 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12107 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12108 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12109 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12110 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12112 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12113 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12114 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12115 | if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object, |
| 12116 | value->buf, value->len, flags) < 0) { |
| 12117 | return NULL; |
| 12118 | } |
| 12119 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12120 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12121 | if (path->fd > -1) |
| 12122 | result = fsetxattr(path->fd, attribute->narrow, |
| 12123 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12124 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12125 | result = setxattr(path->narrow, attribute->narrow, |
| 12126 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12127 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12128 | result = lsetxattr(path->narrow, attribute->narrow, |
| 12129 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12130 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12131 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12132 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12133 | path_error(path); |
| 12134 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12135 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12136 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12137 | Py_RETURN_NONE; |
| 12138 | } |
| 12139 | |
| 12140 | |
| 12141 | /*[clinic input] |
| 12142 | os.removexattr |
| 12143 | |
| 12144 | path: path_t(allow_fd=True) |
| 12145 | attribute: path_t |
| 12146 | * |
| 12147 | follow_symlinks: bool = True |
| 12148 | |
| 12149 | Remove extended attribute attribute on path. |
| 12150 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12151 | 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] | 12152 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12153 | link, removexattr will modify the symbolic link itself instead of the file |
| 12154 | the link points to. |
| 12155 | |
| 12156 | [clinic start generated code]*/ |
| 12157 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12158 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12159 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12160 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12161 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12162 | { |
| 12163 | ssize_t result; |
| 12164 | |
| 12165 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 12166 | return NULL; |
| 12167 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12168 | if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) { |
| 12169 | return NULL; |
| 12170 | } |
| 12171 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12172 | Py_BEGIN_ALLOW_THREADS; |
| 12173 | if (path->fd > -1) |
| 12174 | result = fremovexattr(path->fd, attribute->narrow); |
| 12175 | else if (follow_symlinks) |
| 12176 | result = removexattr(path->narrow, attribute->narrow); |
| 12177 | else |
| 12178 | result = lremovexattr(path->narrow, attribute->narrow); |
| 12179 | Py_END_ALLOW_THREADS; |
| 12180 | |
| 12181 | if (result) { |
| 12182 | return path_error(path); |
| 12183 | } |
| 12184 | |
| 12185 | Py_RETURN_NONE; |
| 12186 | } |
| 12187 | |
| 12188 | |
| 12189 | /*[clinic input] |
| 12190 | os.listxattr |
| 12191 | |
| 12192 | path: path_t(allow_fd=True, nullable=True) = None |
| 12193 | * |
| 12194 | follow_symlinks: bool = True |
| 12195 | |
| 12196 | Return a list of extended attributes on path. |
| 12197 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12198 | 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] | 12199 | if path is None, listxattr will examine the current directory. |
| 12200 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12201 | link, listxattr will examine the symbolic link itself instead of the file |
| 12202 | the link points to. |
| 12203 | [clinic start generated code]*/ |
| 12204 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12205 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12206 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12207 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12208 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12209 | Py_ssize_t i; |
| 12210 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12211 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12212 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12213 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12214 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12215 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12216 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 12217 | if (PySys_Audit("os.listxattr", "(O)", |
| 12218 | path->object ? path->object : Py_None) < 0) { |
| 12219 | return NULL; |
| 12220 | } |
| 12221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12222 | name = path->narrow ? path->narrow : "."; |
| 12223 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12224 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12225 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12226 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12227 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12228 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12229 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 12230 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12231 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12232 | break; |
| 12233 | } |
| 12234 | buffer = PyMem_MALLOC(buffer_size); |
| 12235 | if (!buffer) { |
| 12236 | PyErr_NoMemory(); |
| 12237 | break; |
| 12238 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12239 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12240 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12241 | if (path->fd > -1) |
| 12242 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12243 | else if (follow_symlinks) |
| 12244 | length = listxattr(name, buffer, buffer_size); |
| 12245 | else |
| 12246 | length = llistxattr(name, buffer, buffer_size); |
| 12247 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12248 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12249 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12250 | if (errno == ERANGE) { |
| 12251 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 12252 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12253 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12254 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12255 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12256 | break; |
| 12257 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12258 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12259 | result = PyList_New(0); |
| 12260 | if (!result) { |
| 12261 | goto exit; |
| 12262 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12263 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12264 | end = buffer + length; |
| 12265 | for (trace = start = buffer; trace != end; trace++) { |
| 12266 | if (!*trace) { |
| 12267 | int error; |
| 12268 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 12269 | trace - start); |
| 12270 | if (!attribute) { |
| 12271 | Py_DECREF(result); |
| 12272 | result = NULL; |
| 12273 | goto exit; |
| 12274 | } |
| 12275 | error = PyList_Append(result, attribute); |
| 12276 | Py_DECREF(attribute); |
| 12277 | if (error) { |
| 12278 | Py_DECREF(result); |
| 12279 | result = NULL; |
| 12280 | goto exit; |
| 12281 | } |
| 12282 | start = trace + 1; |
| 12283 | } |
| 12284 | } |
| 12285 | break; |
| 12286 | } |
| 12287 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12288 | if (buffer) |
| 12289 | PyMem_FREE(buffer); |
| 12290 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12291 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12292 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12293 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12294 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12295 | /*[clinic input] |
| 12296 | os.urandom |
| 12297 | |
| 12298 | size: Py_ssize_t |
| 12299 | / |
| 12300 | |
| 12301 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 12302 | [clinic start generated code]*/ |
| 12303 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12304 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12305 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 12306 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12307 | { |
| 12308 | PyObject *bytes; |
| 12309 | int result; |
| 12310 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12311 | if (size < 0) |
| 12312 | return PyErr_Format(PyExc_ValueError, |
| 12313 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12314 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 12315 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12316 | return NULL; |
| 12317 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 12318 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12319 | if (result == -1) { |
| 12320 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12321 | return NULL; |
| 12322 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12323 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12324 | } |
| 12325 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 12326 | #ifdef HAVE_MEMFD_CREATE |
| 12327 | /*[clinic input] |
| 12328 | os.memfd_create |
| 12329 | |
| 12330 | name: FSConverter |
| 12331 | flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC |
| 12332 | |
| 12333 | [clinic start generated code]*/ |
| 12334 | |
| 12335 | static PyObject * |
| 12336 | os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags) |
| 12337 | /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/ |
| 12338 | { |
| 12339 | int fd; |
| 12340 | const char *bytes = PyBytes_AS_STRING(name); |
| 12341 | Py_BEGIN_ALLOW_THREADS |
| 12342 | fd = memfd_create(bytes, flags); |
| 12343 | Py_END_ALLOW_THREADS |
| 12344 | if (fd == -1) { |
| 12345 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12346 | } |
| 12347 | return PyLong_FromLong(fd); |
| 12348 | } |
| 12349 | #endif |
| 12350 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12351 | /* Terminal size querying */ |
| 12352 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12353 | PyDoc_STRVAR(TerminalSize_docstring, |
| 12354 | "A tuple of (columns, lines) for holding terminal window size"); |
| 12355 | |
| 12356 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 12357 | {"columns", "width of the terminal window in characters"}, |
| 12358 | {"lines", "height of the terminal window in characters"}, |
| 12359 | {NULL, NULL} |
| 12360 | }; |
| 12361 | |
| 12362 | static PyStructSequence_Desc TerminalSize_desc = { |
| 12363 | "os.terminal_size", |
| 12364 | TerminalSize_docstring, |
| 12365 | TerminalSize_fields, |
| 12366 | 2, |
| 12367 | }; |
| 12368 | |
| 12369 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12370 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12371 | PyDoc_STRVAR(termsize__doc__, |
| 12372 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 12373 | "\n" \ |
| 12374 | "The optional argument fd (default standard output) specifies\n" \ |
| 12375 | "which file descriptor should be queried.\n" \ |
| 12376 | "\n" \ |
| 12377 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 12378 | "is thrown.\n" \ |
| 12379 | "\n" \ |
| 12380 | "This function will only be defined if an implementation is\n" \ |
| 12381 | "available for this system.\n" \ |
| 12382 | "\n" \ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 12383 | "shutil.get_terminal_size is the high-level function which should\n" \ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12384 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 12385 | |
| 12386 | static PyObject* |
| 12387 | get_terminal_size(PyObject *self, PyObject *args) |
| 12388 | { |
| 12389 | int columns, lines; |
| 12390 | PyObject *termsize; |
| 12391 | |
| 12392 | int fd = fileno(stdout); |
| 12393 | /* Under some conditions stdout may not be connected and |
| 12394 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 12395 | * GUI apps don't have valid standard streams by default. |
| 12396 | * |
| 12397 | * If this happens, and the optional fd argument is not present, |
| 12398 | * the ioctl below will fail returning EBADF. This is what we want. |
| 12399 | */ |
| 12400 | |
| 12401 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 12402 | return NULL; |
| 12403 | |
| 12404 | #ifdef TERMSIZE_USE_IOCTL |
| 12405 | { |
| 12406 | struct winsize w; |
| 12407 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 12408 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12409 | columns = w.ws_col; |
| 12410 | lines = w.ws_row; |
| 12411 | } |
| 12412 | #endif /* TERMSIZE_USE_IOCTL */ |
| 12413 | |
| 12414 | #ifdef TERMSIZE_USE_CONIO |
| 12415 | { |
| 12416 | DWORD nhandle; |
| 12417 | HANDLE handle; |
| 12418 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 12419 | switch (fd) { |
| 12420 | case 0: nhandle = STD_INPUT_HANDLE; |
| 12421 | break; |
| 12422 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 12423 | break; |
| 12424 | case 2: nhandle = STD_ERROR_HANDLE; |
| 12425 | break; |
| 12426 | default: |
| 12427 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 12428 | } |
| 12429 | handle = GetStdHandle(nhandle); |
| 12430 | if (handle == NULL) |
| 12431 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 12432 | if (handle == INVALID_HANDLE_VALUE) |
| 12433 | return PyErr_SetFromWindowsErr(0); |
| 12434 | |
| 12435 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 12436 | return PyErr_SetFromWindowsErr(0); |
| 12437 | |
| 12438 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 12439 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 12440 | } |
| 12441 | #endif /* TERMSIZE_USE_CONIO */ |
| 12442 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 12443 | PyObject *TerminalSizeType = get_posix_state(self)->TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12444 | termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12445 | if (termsize == NULL) |
| 12446 | return NULL; |
| 12447 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 12448 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 12449 | if (PyErr_Occurred()) { |
| 12450 | Py_DECREF(termsize); |
| 12451 | return NULL; |
| 12452 | } |
| 12453 | return termsize; |
| 12454 | } |
| 12455 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 12456 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12457 | |
| 12458 | /*[clinic input] |
| 12459 | os.cpu_count |
| 12460 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 12461 | Return the number of CPUs in the system; return None if indeterminable. |
| 12462 | |
| 12463 | This number is not equivalent to the number of CPUs the current process can |
| 12464 | use. The number of usable CPUs can be obtained with |
| 12465 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12466 | [clinic start generated code]*/ |
| 12467 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12468 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12469 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12470 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12471 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12472 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12473 | #ifdef MS_WINDOWS |
Steve Dower | aa92927 | 2019-09-11 16:15:39 +0100 | [diff] [blame] | 12474 | ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12475 | #elif defined(__hpux) |
| 12476 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 12477 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 12478 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12479 | #elif defined(__DragonFly__) || \ |
| 12480 | defined(__OpenBSD__) || \ |
| 12481 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12482 | defined(__NetBSD__) || \ |
| 12483 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12484 | int mib[2]; |
| 12485 | size_t len = sizeof(ncpu); |
| 12486 | mib[0] = CTL_HW; |
| 12487 | mib[1] = HW_NCPU; |
| 12488 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 12489 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12490 | #endif |
| 12491 | if (ncpu >= 1) |
| 12492 | return PyLong_FromLong(ncpu); |
| 12493 | else |
| 12494 | Py_RETURN_NONE; |
| 12495 | } |
| 12496 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12497 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12498 | /*[clinic input] |
| 12499 | os.get_inheritable -> bool |
| 12500 | |
| 12501 | fd: int |
| 12502 | / |
| 12503 | |
| 12504 | Get the close-on-exe flag of the specified file descriptor. |
| 12505 | [clinic start generated code]*/ |
| 12506 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12507 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12508 | os_get_inheritable_impl(PyObject *module, int fd) |
| 12509 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12510 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12511 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12512 | _Py_BEGIN_SUPPRESS_IPH |
| 12513 | return_value = _Py_get_inheritable(fd); |
| 12514 | _Py_END_SUPPRESS_IPH |
| 12515 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12516 | } |
| 12517 | |
| 12518 | |
| 12519 | /*[clinic input] |
| 12520 | os.set_inheritable |
| 12521 | fd: int |
| 12522 | inheritable: int |
| 12523 | / |
| 12524 | |
| 12525 | Set the inheritable flag of the specified file descriptor. |
| 12526 | [clinic start generated code]*/ |
| 12527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12528 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12529 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 12530 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12531 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12532 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12533 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12534 | _Py_BEGIN_SUPPRESS_IPH |
| 12535 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 12536 | _Py_END_SUPPRESS_IPH |
| 12537 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12538 | return NULL; |
| 12539 | Py_RETURN_NONE; |
| 12540 | } |
| 12541 | |
| 12542 | |
| 12543 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12544 | /*[clinic input] |
| 12545 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12546 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12547 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12548 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12549 | Get the close-on-exe flag of the specified file descriptor. |
| 12550 | [clinic start generated code]*/ |
| 12551 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12552 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12553 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12554 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12555 | { |
| 12556 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12557 | |
| 12558 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 12559 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12560 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12561 | } |
| 12562 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12563 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12564 | } |
| 12565 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12566 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12567 | /*[clinic input] |
| 12568 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12569 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12570 | inheritable: bool |
| 12571 | / |
| 12572 | |
| 12573 | Set the inheritable flag of the specified handle. |
| 12574 | [clinic start generated code]*/ |
| 12575 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12576 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12577 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12578 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12579 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12580 | { |
| 12581 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12582 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 12583 | PyErr_SetFromWindowsErr(0); |
| 12584 | return NULL; |
| 12585 | } |
| 12586 | Py_RETURN_NONE; |
| 12587 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12588 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12589 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12590 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12591 | /*[clinic input] |
| 12592 | os.get_blocking -> bool |
| 12593 | fd: int |
| 12594 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12595 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12596 | Get the blocking mode of the file descriptor. |
| 12597 | |
| 12598 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 12599 | [clinic start generated code]*/ |
| 12600 | |
| 12601 | static int |
| 12602 | os_get_blocking_impl(PyObject *module, int fd) |
| 12603 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12604 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12605 | int blocking; |
| 12606 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12607 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12608 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12609 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12610 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12611 | } |
| 12612 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12613 | /*[clinic input] |
| 12614 | os.set_blocking |
| 12615 | fd: int |
| 12616 | blocking: bool(accept={int}) |
| 12617 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12618 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12619 | Set the blocking mode of the specified file descriptor. |
| 12620 | |
| 12621 | Set the O_NONBLOCK flag if blocking is False, |
| 12622 | clear the O_NONBLOCK flag otherwise. |
| 12623 | [clinic start generated code]*/ |
| 12624 | |
| 12625 | static PyObject * |
| 12626 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 12627 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12628 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12629 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12630 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12631 | _Py_BEGIN_SUPPRESS_IPH |
| 12632 | result = _Py_set_blocking(fd, blocking); |
| 12633 | _Py_END_SUPPRESS_IPH |
| 12634 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12635 | return NULL; |
| 12636 | Py_RETURN_NONE; |
| 12637 | } |
| 12638 | #endif /* !MS_WINDOWS */ |
| 12639 | |
| 12640 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12641 | /*[clinic input] |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12642 | class os.DirEntry "DirEntry *" "DirEntryType" |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12643 | [clinic start generated code]*/ |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12644 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12645 | |
| 12646 | typedef struct { |
| 12647 | PyObject_HEAD |
| 12648 | PyObject *name; |
| 12649 | PyObject *path; |
| 12650 | PyObject *stat; |
| 12651 | PyObject *lstat; |
| 12652 | #ifdef MS_WINDOWS |
| 12653 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12654 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12655 | int got_file_index; |
| 12656 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12657 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12658 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12659 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12660 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12661 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12662 | #endif |
| 12663 | } DirEntry; |
| 12664 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12665 | static PyObject * |
| 12666 | _disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 12667 | { |
| 12668 | PyErr_Format(PyExc_TypeError, |
| 12669 | "cannot create '%.100s' instances", _PyType_Name(type)); |
| 12670 | return NULL; |
| 12671 | } |
| 12672 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12673 | static void |
| 12674 | DirEntry_dealloc(DirEntry *entry) |
| 12675 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12676 | PyTypeObject *tp = Py_TYPE(entry); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12677 | Py_XDECREF(entry->name); |
| 12678 | Py_XDECREF(entry->path); |
| 12679 | Py_XDECREF(entry->stat); |
| 12680 | Py_XDECREF(entry->lstat); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12681 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 12682 | free_func(entry); |
| 12683 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12684 | } |
| 12685 | |
| 12686 | /* Forward reference */ |
| 12687 | static int |
| 12688 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 12689 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12690 | /*[clinic input] |
| 12691 | os.DirEntry.is_symlink -> bool |
| 12692 | |
| 12693 | Return True if the entry is a symbolic link; cached per entry. |
| 12694 | [clinic start generated code]*/ |
| 12695 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12696 | static int |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12697 | os_DirEntry_is_symlink_impl(DirEntry *self) |
| 12698 | /*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12699 | { |
| 12700 | #ifdef MS_WINDOWS |
| 12701 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12702 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12703 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12704 | if (self->d_type != DT_UNKNOWN) |
| 12705 | return self->d_type == DT_LNK; |
| 12706 | else |
| 12707 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12708 | #else |
| 12709 | /* POSIX without d_type */ |
| 12710 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12711 | #endif |
| 12712 | } |
| 12713 | |
| 12714 | static PyObject * |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12715 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 12716 | { |
| 12717 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12718 | STRUCT_STAT st; |
| 12719 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12720 | |
| 12721 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12722 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12723 | return NULL; |
| 12724 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12725 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12726 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12727 | return NULL; |
| 12728 | const char *path = PyBytes_AS_STRING(ub); |
| 12729 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12730 | #ifdef HAVE_FSTATAT |
| 12731 | result = fstatat(self->dir_fd, path, &st, |
| 12732 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12733 | #else |
| 12734 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12735 | return NULL; |
| 12736 | #endif /* HAVE_FSTATAT */ |
| 12737 | } |
| 12738 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12739 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12740 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12741 | if (follow_symlinks) |
| 12742 | result = STAT(path, &st); |
| 12743 | else |
| 12744 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12745 | } |
| 12746 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12747 | |
| 12748 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12749 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12750 | |
| 12751 | return _pystat_fromstructstat(&st); |
| 12752 | } |
| 12753 | |
| 12754 | static PyObject * |
| 12755 | DirEntry_get_lstat(DirEntry *self) |
| 12756 | { |
| 12757 | if (!self->lstat) { |
| 12758 | #ifdef MS_WINDOWS |
| 12759 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 12760 | #else /* POSIX */ |
| 12761 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 12762 | #endif |
| 12763 | } |
| 12764 | Py_XINCREF(self->lstat); |
| 12765 | return self->lstat; |
| 12766 | } |
| 12767 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12768 | /*[clinic input] |
| 12769 | os.DirEntry.stat |
| 12770 | * |
| 12771 | follow_symlinks: bool = True |
| 12772 | |
| 12773 | Return stat_result object for the entry; cached per entry. |
| 12774 | [clinic start generated code]*/ |
| 12775 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12776 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12777 | os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks) |
| 12778 | /*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12779 | { |
| 12780 | if (!follow_symlinks) |
| 12781 | return DirEntry_get_lstat(self); |
| 12782 | |
| 12783 | if (!self->stat) { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12784 | int result = os_DirEntry_is_symlink_impl(self); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12785 | if (result == -1) |
| 12786 | return NULL; |
| 12787 | else if (result) |
| 12788 | self->stat = DirEntry_fetch_stat(self, 1); |
| 12789 | else |
| 12790 | self->stat = DirEntry_get_lstat(self); |
| 12791 | } |
| 12792 | |
| 12793 | Py_XINCREF(self->stat); |
| 12794 | return self->stat; |
| 12795 | } |
| 12796 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12797 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12798 | static int |
| 12799 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 12800 | { |
| 12801 | PyObject *stat = NULL; |
| 12802 | PyObject *st_mode = NULL; |
| 12803 | long mode; |
| 12804 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12805 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12806 | int is_symlink; |
| 12807 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12808 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12809 | #ifdef MS_WINDOWS |
| 12810 | unsigned long dir_bits; |
| 12811 | #endif |
| 12812 | |
| 12813 | #ifdef MS_WINDOWS |
| 12814 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12815 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12816 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12817 | is_symlink = self->d_type == DT_LNK; |
| 12818 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12819 | #endif |
| 12820 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12821 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12822 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12823 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12824 | stat = os_DirEntry_stat_impl(self, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12825 | if (!stat) { |
| 12826 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12827 | /* If file doesn't exist (anymore), then return False |
| 12828 | (i.e., say it's not a file/directory) */ |
| 12829 | PyErr_Clear(); |
| 12830 | return 0; |
| 12831 | } |
| 12832 | goto error; |
| 12833 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12834 | st_mode = PyObject_GetAttr(stat, _posixstate_global->st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12835 | if (!st_mode) |
| 12836 | goto error; |
| 12837 | |
| 12838 | mode = PyLong_AsLong(st_mode); |
| 12839 | if (mode == -1 && PyErr_Occurred()) |
| 12840 | goto error; |
| 12841 | Py_CLEAR(st_mode); |
| 12842 | Py_CLEAR(stat); |
| 12843 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12844 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12845 | } |
| 12846 | else if (is_symlink) { |
| 12847 | assert(mode_bits != S_IFLNK); |
| 12848 | result = 0; |
| 12849 | } |
| 12850 | else { |
| 12851 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 12852 | #ifdef MS_WINDOWS |
| 12853 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 12854 | if (mode_bits == S_IFDIR) |
| 12855 | result = dir_bits != 0; |
| 12856 | else |
| 12857 | result = dir_bits == 0; |
| 12858 | #else /* POSIX */ |
| 12859 | if (mode_bits == S_IFDIR) |
| 12860 | result = self->d_type == DT_DIR; |
| 12861 | else |
| 12862 | result = self->d_type == DT_REG; |
| 12863 | #endif |
| 12864 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12865 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12866 | |
| 12867 | return result; |
| 12868 | |
| 12869 | error: |
| 12870 | Py_XDECREF(st_mode); |
| 12871 | Py_XDECREF(stat); |
| 12872 | return -1; |
| 12873 | } |
| 12874 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12875 | /*[clinic input] |
| 12876 | os.DirEntry.is_dir -> bool |
| 12877 | * |
| 12878 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12879 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12880 | Return True if the entry is a directory; cached per entry. |
| 12881 | [clinic start generated code]*/ |
| 12882 | |
| 12883 | static int |
| 12884 | os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks) |
| 12885 | /*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/ |
| 12886 | { |
| 12887 | return DirEntry_test_mode(self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12888 | } |
| 12889 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12890 | /*[clinic input] |
| 12891 | os.DirEntry.is_file -> bool |
| 12892 | * |
| 12893 | follow_symlinks: bool = True |
| 12894 | |
| 12895 | Return True if the entry is a file; cached per entry. |
| 12896 | [clinic start generated code]*/ |
| 12897 | |
| 12898 | static int |
| 12899 | os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks) |
| 12900 | /*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12901 | { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12902 | return DirEntry_test_mode(self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12903 | } |
| 12904 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12905 | /*[clinic input] |
| 12906 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12907 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12908 | Return inode of the entry; cached per entry. |
| 12909 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12910 | |
| 12911 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12912 | os_DirEntry_inode_impl(DirEntry *self) |
| 12913 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12914 | { |
| 12915 | #ifdef MS_WINDOWS |
| 12916 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12917 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12918 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12919 | STRUCT_STAT stat; |
| 12920 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12921 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12922 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12923 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12924 | path = PyUnicode_AsUnicode(unicode); |
| 12925 | result = LSTAT(path, &stat); |
| 12926 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12927 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12928 | if (result != 0) |
| 12929 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12930 | |
| 12931 | self->win32_file_index = stat.st_ino; |
| 12932 | self->got_file_index = 1; |
| 12933 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12934 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 12935 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12936 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 12937 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 12938 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12939 | #endif |
| 12940 | } |
| 12941 | |
| 12942 | static PyObject * |
| 12943 | DirEntry_repr(DirEntry *self) |
| 12944 | { |
| 12945 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 12946 | } |
| 12947 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12948 | /*[clinic input] |
| 12949 | os.DirEntry.__fspath__ |
| 12950 | |
| 12951 | Returns the path for the entry. |
| 12952 | [clinic start generated code]*/ |
| 12953 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12954 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12955 | os_DirEntry___fspath___impl(DirEntry *self) |
| 12956 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12957 | { |
| 12958 | Py_INCREF(self->path); |
| 12959 | return self->path; |
| 12960 | } |
| 12961 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12962 | static PyMemberDef DirEntry_members[] = { |
| 12963 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 12964 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 12965 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 12966 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 12967 | {NULL} |
| 12968 | }; |
| 12969 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12970 | #include "clinic/posixmodule.c.h" |
| 12971 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12972 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12973 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 12974 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 12975 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 12976 | OS_DIRENTRY_STAT_METHODDEF |
| 12977 | OS_DIRENTRY_INODE_METHODDEF |
| 12978 | OS_DIRENTRY___FSPATH___METHODDEF |
Batuhan Taşkaya | f9dd51e | 2020-04-08 00:37:19 +0300 | [diff] [blame] | 12979 | {"__class_getitem__", (PyCFunction)Py_GenericAlias, |
| 12980 | METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12981 | {NULL} |
| 12982 | }; |
| 12983 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12984 | static PyType_Slot DirEntryType_slots[] = { |
| 12985 | {Py_tp_new, _disabled_new}, |
| 12986 | {Py_tp_dealloc, DirEntry_dealloc}, |
| 12987 | {Py_tp_repr, DirEntry_repr}, |
| 12988 | {Py_tp_methods, DirEntry_methods}, |
| 12989 | {Py_tp_members, DirEntry_members}, |
| 12990 | {0, 0}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12991 | }; |
| 12992 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12993 | static PyType_Spec DirEntryType_spec = { |
| 12994 | MODNAME ".DirEntry", |
| 12995 | sizeof(DirEntry), |
| 12996 | 0, |
| 12997 | Py_TPFLAGS_DEFAULT, |
| 12998 | DirEntryType_slots |
| 12999 | }; |
| 13000 | |
| 13001 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13002 | #ifdef MS_WINDOWS |
| 13003 | |
| 13004 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 13005 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13006 | { |
| 13007 | Py_ssize_t path_len; |
| 13008 | Py_ssize_t size; |
| 13009 | wchar_t *result; |
| 13010 | wchar_t ch; |
| 13011 | |
| 13012 | if (!path_wide) { /* Default arg: "." */ |
| 13013 | path_wide = L"."; |
| 13014 | path_len = 1; |
| 13015 | } |
| 13016 | else { |
| 13017 | path_len = wcslen(path_wide); |
| 13018 | } |
| 13019 | |
| 13020 | /* The +1's are for the path separator and the NUL */ |
| 13021 | size = path_len + 1 + wcslen(filename) + 1; |
| 13022 | result = PyMem_New(wchar_t, size); |
| 13023 | if (!result) { |
| 13024 | PyErr_NoMemory(); |
| 13025 | return NULL; |
| 13026 | } |
| 13027 | wcscpy(result, path_wide); |
| 13028 | if (path_len > 0) { |
| 13029 | ch = result[path_len - 1]; |
| 13030 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 13031 | result[path_len++] = SEP; |
| 13032 | wcscpy(result + path_len, filename); |
| 13033 | } |
| 13034 | return result; |
| 13035 | } |
| 13036 | |
| 13037 | static PyObject * |
| 13038 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 13039 | { |
| 13040 | DirEntry *entry; |
| 13041 | BY_HANDLE_FILE_INFORMATION file_info; |
| 13042 | ULONG reparse_tag; |
| 13043 | wchar_t *joined_path; |
| 13044 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13045 | PyObject *DirEntryType = _posixstate_global->DirEntryType; |
| 13046 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13047 | if (!entry) |
| 13048 | return NULL; |
| 13049 | entry->name = NULL; |
| 13050 | entry->path = NULL; |
| 13051 | entry->stat = NULL; |
| 13052 | entry->lstat = NULL; |
| 13053 | entry->got_file_index = 0; |
| 13054 | |
| 13055 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 13056 | if (!entry->name) |
| 13057 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13058 | if (path->narrow) { |
| 13059 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 13060 | if (!entry->name) |
| 13061 | goto error; |
| 13062 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13063 | |
| 13064 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 13065 | if (!joined_path) |
| 13066 | goto error; |
| 13067 | |
| 13068 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 13069 | PyMem_Free(joined_path); |
| 13070 | if (!entry->path) |
| 13071 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13072 | if (path->narrow) { |
| 13073 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 13074 | if (!entry->path) |
| 13075 | goto error; |
| 13076 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13077 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13078 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13079 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 13080 | |
| 13081 | return (PyObject *)entry; |
| 13082 | |
| 13083 | error: |
| 13084 | Py_DECREF(entry); |
| 13085 | return NULL; |
| 13086 | } |
| 13087 | |
| 13088 | #else /* POSIX */ |
| 13089 | |
| 13090 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 13091 | 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] | 13092 | { |
| 13093 | Py_ssize_t path_len; |
| 13094 | Py_ssize_t size; |
| 13095 | char *result; |
| 13096 | |
| 13097 | if (!path_narrow) { /* Default arg: "." */ |
| 13098 | path_narrow = "."; |
| 13099 | path_len = 1; |
| 13100 | } |
| 13101 | else { |
| 13102 | path_len = strlen(path_narrow); |
| 13103 | } |
| 13104 | |
| 13105 | if (filename_len == -1) |
| 13106 | filename_len = strlen(filename); |
| 13107 | |
| 13108 | /* The +1's are for the path separator and the NUL */ |
| 13109 | size = path_len + 1 + filename_len + 1; |
| 13110 | result = PyMem_New(char, size); |
| 13111 | if (!result) { |
| 13112 | PyErr_NoMemory(); |
| 13113 | return NULL; |
| 13114 | } |
| 13115 | strcpy(result, path_narrow); |
| 13116 | if (path_len > 0 && result[path_len - 1] != '/') |
| 13117 | result[path_len++] = '/'; |
| 13118 | strcpy(result + path_len, filename); |
| 13119 | return result; |
| 13120 | } |
| 13121 | |
| 13122 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 13123 | DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13124 | ino_t d_ino |
| 13125 | #ifdef HAVE_DIRENT_D_TYPE |
| 13126 | , unsigned char d_type |
| 13127 | #endif |
| 13128 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13129 | { |
| 13130 | DirEntry *entry; |
| 13131 | char *joined_path; |
| 13132 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13133 | PyObject *DirEntryType = _posixstate_global->DirEntryType; |
| 13134 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13135 | if (!entry) |
| 13136 | return NULL; |
| 13137 | entry->name = NULL; |
| 13138 | entry->path = NULL; |
| 13139 | entry->stat = NULL; |
| 13140 | entry->lstat = NULL; |
| 13141 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13142 | if (path->fd != -1) { |
| 13143 | entry->dir_fd = path->fd; |
| 13144 | joined_path = NULL; |
| 13145 | } |
| 13146 | else { |
| 13147 | entry->dir_fd = DEFAULT_DIR_FD; |
| 13148 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 13149 | if (!joined_path) |
| 13150 | goto error; |
| 13151 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13152 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 13153 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13154 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13155 | if (joined_path) |
| 13156 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13157 | } |
| 13158 | else { |
| 13159 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13160 | if (joined_path) |
| 13161 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13162 | } |
| 13163 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13164 | if (!entry->name) |
| 13165 | goto error; |
| 13166 | |
| 13167 | if (path->fd != -1) { |
| 13168 | entry->path = entry->name; |
| 13169 | Py_INCREF(entry->path); |
| 13170 | } |
| 13171 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13172 | goto error; |
| 13173 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13174 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13175 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13176 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13177 | entry->d_ino = d_ino; |
| 13178 | |
| 13179 | return (PyObject *)entry; |
| 13180 | |
| 13181 | error: |
| 13182 | Py_XDECREF(entry); |
| 13183 | return NULL; |
| 13184 | } |
| 13185 | |
| 13186 | #endif |
| 13187 | |
| 13188 | |
| 13189 | typedef struct { |
| 13190 | PyObject_HEAD |
| 13191 | path_t path; |
| 13192 | #ifdef MS_WINDOWS |
| 13193 | HANDLE handle; |
| 13194 | WIN32_FIND_DATAW file_data; |
| 13195 | int first_time; |
| 13196 | #else /* POSIX */ |
| 13197 | DIR *dirp; |
| 13198 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13199 | #ifdef HAVE_FDOPENDIR |
| 13200 | int fd; |
| 13201 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13202 | } ScandirIterator; |
| 13203 | |
| 13204 | #ifdef MS_WINDOWS |
| 13205 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13206 | static int |
| 13207 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13208 | { |
| 13209 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 13210 | } |
| 13211 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13212 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13213 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13214 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13215 | HANDLE handle = iterator->handle; |
| 13216 | |
| 13217 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13218 | return; |
| 13219 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13220 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13221 | Py_BEGIN_ALLOW_THREADS |
| 13222 | FindClose(handle); |
| 13223 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13224 | } |
| 13225 | |
| 13226 | static PyObject * |
| 13227 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13228 | { |
| 13229 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 13230 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13231 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13232 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13233 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13234 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13235 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13236 | |
| 13237 | while (1) { |
| 13238 | if (!iterator->first_time) { |
| 13239 | Py_BEGIN_ALLOW_THREADS |
| 13240 | success = FindNextFileW(iterator->handle, file_data); |
| 13241 | Py_END_ALLOW_THREADS |
| 13242 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13243 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13244 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13245 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13246 | break; |
| 13247 | } |
| 13248 | } |
| 13249 | iterator->first_time = 0; |
| 13250 | |
| 13251 | /* Skip over . and .. */ |
| 13252 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13253 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 13254 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 13255 | if (!entry) |
| 13256 | break; |
| 13257 | return entry; |
| 13258 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13259 | |
| 13260 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13261 | } |
| 13262 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13263 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13264 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13265 | return NULL; |
| 13266 | } |
| 13267 | |
| 13268 | #else /* POSIX */ |
| 13269 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13270 | static int |
| 13271 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13272 | { |
| 13273 | return !iterator->dirp; |
| 13274 | } |
| 13275 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13276 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13277 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13278 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13279 | DIR *dirp = iterator->dirp; |
| 13280 | |
| 13281 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13282 | return; |
| 13283 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13284 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13285 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13286 | #ifdef HAVE_FDOPENDIR |
| 13287 | if (iterator->path.fd != -1) |
| 13288 | rewinddir(dirp); |
| 13289 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13290 | closedir(dirp); |
| 13291 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13292 | return; |
| 13293 | } |
| 13294 | |
| 13295 | static PyObject * |
| 13296 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13297 | { |
| 13298 | struct dirent *direntp; |
| 13299 | Py_ssize_t name_len; |
| 13300 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13301 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13302 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13303 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13304 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13305 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13306 | |
| 13307 | while (1) { |
| 13308 | errno = 0; |
| 13309 | Py_BEGIN_ALLOW_THREADS |
| 13310 | direntp = readdir(iterator->dirp); |
| 13311 | Py_END_ALLOW_THREADS |
| 13312 | |
| 13313 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13314 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13315 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13316 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13317 | break; |
| 13318 | } |
| 13319 | |
| 13320 | /* Skip over . and .. */ |
| 13321 | name_len = NAMLEN(direntp); |
| 13322 | is_dot = direntp->d_name[0] == '.' && |
| 13323 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 13324 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13325 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13326 | name_len, direntp->d_ino |
| 13327 | #ifdef HAVE_DIRENT_D_TYPE |
| 13328 | , direntp->d_type |
| 13329 | #endif |
| 13330 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13331 | if (!entry) |
| 13332 | break; |
| 13333 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13334 | } |
| 13335 | |
| 13336 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13337 | } |
| 13338 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13339 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13340 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13341 | return NULL; |
| 13342 | } |
| 13343 | |
| 13344 | #endif |
| 13345 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13346 | static PyObject * |
| 13347 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 13348 | { |
| 13349 | ScandirIterator_closedir(self); |
| 13350 | Py_RETURN_NONE; |
| 13351 | } |
| 13352 | |
| 13353 | static PyObject * |
| 13354 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 13355 | { |
| 13356 | Py_INCREF(self); |
| 13357 | return self; |
| 13358 | } |
| 13359 | |
| 13360 | static PyObject * |
| 13361 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 13362 | { |
| 13363 | ScandirIterator_closedir(self); |
| 13364 | Py_RETURN_NONE; |
| 13365 | } |
| 13366 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13367 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13368 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 13369 | { |
| 13370 | PyObject *error_type, *error_value, *error_traceback; |
| 13371 | |
| 13372 | /* Save the current exception, if any. */ |
| 13373 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 13374 | |
| 13375 | if (!ScandirIterator_is_closed(iterator)) { |
| 13376 | ScandirIterator_closedir(iterator); |
| 13377 | |
| 13378 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 13379 | "unclosed scandir iterator %R", iterator)) { |
| 13380 | /* Spurious errors can appear at shutdown */ |
| 13381 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 13382 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 13383 | } |
| 13384 | } |
| 13385 | } |
| 13386 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13387 | path_cleanup(&iterator->path); |
| 13388 | |
| 13389 | /* Restore the saved exception. */ |
| 13390 | PyErr_Restore(error_type, error_value, error_traceback); |
| 13391 | } |
| 13392 | |
| 13393 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13394 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 13395 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13396 | PyTypeObject *tp = Py_TYPE(iterator); |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13397 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 13398 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13399 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13400 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 13401 | free_func(iterator); |
| 13402 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13403 | } |
| 13404 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13405 | static PyMethodDef ScandirIterator_methods[] = { |
| 13406 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 13407 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 13408 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 13409 | {NULL} |
| 13410 | }; |
| 13411 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13412 | static PyType_Slot ScandirIteratorType_slots[] = { |
| 13413 | {Py_tp_new, _disabled_new}, |
| 13414 | {Py_tp_dealloc, ScandirIterator_dealloc}, |
| 13415 | {Py_tp_finalize, ScandirIterator_finalize}, |
| 13416 | {Py_tp_iter, PyObject_SelfIter}, |
| 13417 | {Py_tp_iternext, ScandirIterator_iternext}, |
| 13418 | {Py_tp_methods, ScandirIterator_methods}, |
| 13419 | {0, 0}, |
| 13420 | }; |
| 13421 | |
| 13422 | static PyType_Spec ScandirIteratorType_spec = { |
| 13423 | MODNAME ".ScandirIterator", |
| 13424 | sizeof(ScandirIterator), |
| 13425 | 0, |
| 13426 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 13427 | ScandirIteratorType_slots |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13428 | }; |
| 13429 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13430 | /*[clinic input] |
| 13431 | os.scandir |
| 13432 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13433 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13434 | |
| 13435 | Return an iterator of DirEntry objects for given path. |
| 13436 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13437 | 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] | 13438 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 13439 | all other circumstances they will be str. |
| 13440 | |
| 13441 | If path is None, uses the path='.'. |
| 13442 | [clinic start generated code]*/ |
| 13443 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13444 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13445 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13446 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13447 | { |
| 13448 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13449 | #ifdef MS_WINDOWS |
| 13450 | wchar_t *path_strW; |
| 13451 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13452 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13453 | #ifdef HAVE_FDOPENDIR |
| 13454 | int fd = -1; |
| 13455 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13456 | #endif |
| 13457 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 13458 | if (PySys_Audit("os.scandir", "O", |
| 13459 | path->object ? path->object : Py_None) < 0) { |
| 13460 | return NULL; |
| 13461 | } |
| 13462 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 13463 | PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13464 | iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13465 | if (!iterator) |
| 13466 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13467 | |
| 13468 | #ifdef MS_WINDOWS |
| 13469 | iterator->handle = INVALID_HANDLE_VALUE; |
| 13470 | #else |
| 13471 | iterator->dirp = NULL; |
| 13472 | #endif |
| 13473 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13474 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 13475 | /* Move the ownership to iterator->path */ |
| 13476 | path->object = NULL; |
| 13477 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13478 | |
| 13479 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13480 | iterator->first_time = 1; |
| 13481 | |
| 13482 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 13483 | if (!path_strW) |
| 13484 | goto error; |
| 13485 | |
| 13486 | Py_BEGIN_ALLOW_THREADS |
| 13487 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 13488 | Py_END_ALLOW_THREADS |
| 13489 | |
| 13490 | PyMem_Free(path_strW); |
| 13491 | |
| 13492 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 13493 | path_error(&iterator->path); |
| 13494 | goto error; |
| 13495 | } |
| 13496 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13497 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13498 | #ifdef HAVE_FDOPENDIR |
| 13499 | if (path->fd != -1) { |
| 13500 | /* closedir() closes the FD, so we duplicate it */ |
| 13501 | fd = _Py_dup(path->fd); |
| 13502 | if (fd == -1) |
| 13503 | goto error; |
| 13504 | |
| 13505 | Py_BEGIN_ALLOW_THREADS |
| 13506 | iterator->dirp = fdopendir(fd); |
| 13507 | Py_END_ALLOW_THREADS |
| 13508 | } |
| 13509 | else |
| 13510 | #endif |
| 13511 | { |
| 13512 | if (iterator->path.narrow) |
| 13513 | path_str = iterator->path.narrow; |
| 13514 | else |
| 13515 | path_str = "."; |
| 13516 | |
| 13517 | Py_BEGIN_ALLOW_THREADS |
| 13518 | iterator->dirp = opendir(path_str); |
| 13519 | Py_END_ALLOW_THREADS |
| 13520 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13521 | |
| 13522 | if (!iterator->dirp) { |
| 13523 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13524 | #ifdef HAVE_FDOPENDIR |
| 13525 | if (fd != -1) { |
| 13526 | Py_BEGIN_ALLOW_THREADS |
| 13527 | close(fd); |
| 13528 | Py_END_ALLOW_THREADS |
| 13529 | } |
| 13530 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13531 | goto error; |
| 13532 | } |
| 13533 | #endif |
| 13534 | |
| 13535 | return (PyObject *)iterator; |
| 13536 | |
| 13537 | error: |
| 13538 | Py_DECREF(iterator); |
| 13539 | return NULL; |
| 13540 | } |
| 13541 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13542 | /* |
| 13543 | Return the file system path representation of the object. |
| 13544 | |
| 13545 | If the object is str or bytes, then allow it to pass through with |
| 13546 | an incremented refcount. If the object defines __fspath__(), then |
| 13547 | return the result of that method. All other types raise a TypeError. |
| 13548 | */ |
| 13549 | PyObject * |
| 13550 | PyOS_FSPath(PyObject *path) |
| 13551 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13552 | /* For error message reasons, this function is manually inlined in |
| 13553 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13554 | PyObject *func = NULL; |
| 13555 | PyObject *path_repr = NULL; |
| 13556 | |
| 13557 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 13558 | Py_INCREF(path); |
| 13559 | return path; |
| 13560 | } |
| 13561 | |
| 13562 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 13563 | if (NULL == func) { |
| 13564 | return PyErr_Format(PyExc_TypeError, |
| 13565 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13566 | "not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13567 | _PyType_Name(Py_TYPE(path))); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13568 | } |
| 13569 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13570 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13571 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13572 | if (NULL == path_repr) { |
| 13573 | return NULL; |
| 13574 | } |
| 13575 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13576 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 13577 | PyErr_Format(PyExc_TypeError, |
| 13578 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13579 | "not %.200s", _PyType_Name(Py_TYPE(path)), |
| 13580 | _PyType_Name(Py_TYPE(path_repr))); |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13581 | Py_DECREF(path_repr); |
| 13582 | return NULL; |
| 13583 | } |
| 13584 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13585 | return path_repr; |
| 13586 | } |
| 13587 | |
| 13588 | /*[clinic input] |
| 13589 | os.fspath |
| 13590 | |
| 13591 | path: object |
| 13592 | |
| 13593 | Return the file system path representation of the object. |
| 13594 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13595 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 13596 | object defines __fspath__(), then return the result of that method. All other |
| 13597 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13598 | [clinic start generated code]*/ |
| 13599 | |
| 13600 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13601 | os_fspath_impl(PyObject *module, PyObject *path) |
| 13602 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13603 | { |
| 13604 | return PyOS_FSPath(path); |
| 13605 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13606 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13607 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13608 | /*[clinic input] |
| 13609 | os.getrandom |
| 13610 | |
| 13611 | size: Py_ssize_t |
| 13612 | flags: int=0 |
| 13613 | |
| 13614 | Obtain a series of random bytes. |
| 13615 | [clinic start generated code]*/ |
| 13616 | |
| 13617 | static PyObject * |
| 13618 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 13619 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 13620 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13621 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13622 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13623 | |
| 13624 | if (size < 0) { |
| 13625 | errno = EINVAL; |
| 13626 | return posix_error(); |
| 13627 | } |
| 13628 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13629 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13630 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13631 | PyErr_NoMemory(); |
| 13632 | return NULL; |
| 13633 | } |
| 13634 | |
| 13635 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13636 | n = syscall(SYS_getrandom, |
| 13637 | PyBytes_AS_STRING(bytes), |
| 13638 | PyBytes_GET_SIZE(bytes), |
| 13639 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13640 | if (n < 0 && errno == EINTR) { |
| 13641 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13642 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13643 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13644 | |
| 13645 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13646 | continue; |
| 13647 | } |
| 13648 | break; |
| 13649 | } |
| 13650 | |
| 13651 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13652 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13653 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13654 | } |
| 13655 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13656 | if (n != size) { |
| 13657 | _PyBytes_Resize(&bytes, n); |
| 13658 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13659 | |
| 13660 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13661 | |
| 13662 | error: |
| 13663 | Py_DECREF(bytes); |
| 13664 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13665 | } |
| 13666 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13667 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13668 | #ifdef MS_WINDOWS |
| 13669 | /* bpo-36085: Helper functions for managing DLL search directories |
| 13670 | * on win32 |
| 13671 | */ |
| 13672 | |
| 13673 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); |
| 13674 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); |
| 13675 | |
| 13676 | /*[clinic input] |
| 13677 | os._add_dll_directory |
| 13678 | |
| 13679 | path: path_t |
| 13680 | |
| 13681 | Add a path to the DLL search path. |
| 13682 | |
| 13683 | This search path is used when resolving dependencies for imported |
| 13684 | extension modules (the module itself is resolved through sys.path), |
| 13685 | and also by ctypes. |
| 13686 | |
| 13687 | Returns an opaque value that may be passed to os.remove_dll_directory |
| 13688 | to remove this directory from the search path. |
| 13689 | [clinic start generated code]*/ |
| 13690 | |
| 13691 | static PyObject * |
| 13692 | os__add_dll_directory_impl(PyObject *module, path_t *path) |
| 13693 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ |
| 13694 | { |
| 13695 | HMODULE hKernel32; |
| 13696 | PAddDllDirectory AddDllDirectory; |
| 13697 | DLL_DIRECTORY_COOKIE cookie = 0; |
| 13698 | DWORD err = 0; |
| 13699 | |
Saiyang Gou | 7514f4f | 2020-02-12 23:47:42 -0800 | [diff] [blame] | 13700 | if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) { |
| 13701 | return NULL; |
| 13702 | } |
| 13703 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13704 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13705 | infrequent operation, just do it each time. Kernel32 is always |
| 13706 | loaded. */ |
| 13707 | Py_BEGIN_ALLOW_THREADS |
| 13708 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13709 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( |
| 13710 | hKernel32, "AddDllDirectory")) || |
| 13711 | !(cookie = (*AddDllDirectory)(path->wide))) { |
| 13712 | err = GetLastError(); |
| 13713 | } |
| 13714 | Py_END_ALLOW_THREADS |
| 13715 | |
| 13716 | if (err) { |
| 13717 | return win32_error_object_err("add_dll_directory", |
| 13718 | path->object, err); |
| 13719 | } |
| 13720 | |
| 13721 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); |
| 13722 | } |
| 13723 | |
| 13724 | /*[clinic input] |
| 13725 | os._remove_dll_directory |
| 13726 | |
| 13727 | cookie: object |
| 13728 | |
| 13729 | Removes a path from the DLL search path. |
| 13730 | |
| 13731 | The parameter is an opaque value that was returned from |
| 13732 | os.add_dll_directory. You can only remove directories that you added |
| 13733 | yourself. |
| 13734 | [clinic start generated code]*/ |
| 13735 | |
| 13736 | static PyObject * |
| 13737 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) |
| 13738 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ |
| 13739 | { |
| 13740 | HMODULE hKernel32; |
| 13741 | PRemoveDllDirectory RemoveDllDirectory; |
| 13742 | DLL_DIRECTORY_COOKIE cookieValue; |
| 13743 | DWORD err = 0; |
| 13744 | |
| 13745 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { |
| 13746 | PyErr_SetString(PyExc_TypeError, |
| 13747 | "Provided cookie was not returned from os.add_dll_directory"); |
| 13748 | return NULL; |
| 13749 | } |
| 13750 | |
| 13751 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( |
| 13752 | cookie, "DLL directory cookie"); |
| 13753 | |
| 13754 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13755 | infrequent operation, just do it each time. Kernel32 is always |
| 13756 | loaded. */ |
| 13757 | Py_BEGIN_ALLOW_THREADS |
| 13758 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13759 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( |
| 13760 | hKernel32, "RemoveDllDirectory")) || |
| 13761 | !(*RemoveDllDirectory)(cookieValue)) { |
| 13762 | err = GetLastError(); |
| 13763 | } |
| 13764 | Py_END_ALLOW_THREADS |
| 13765 | |
| 13766 | if (err) { |
| 13767 | return win32_error_object_err("remove_dll_directory", |
| 13768 | NULL, err); |
| 13769 | } |
| 13770 | |
| 13771 | if (PyCapsule_SetName(cookie, NULL)) { |
| 13772 | return NULL; |
| 13773 | } |
| 13774 | |
| 13775 | Py_RETURN_NONE; |
| 13776 | } |
| 13777 | |
| 13778 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13779 | |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 13780 | |
| 13781 | /* Only check if WIFEXITED is available: expect that it comes |
| 13782 | with WEXITSTATUS, WIFSIGNALED, etc. |
| 13783 | |
| 13784 | os.waitstatus_to_exitcode() is implemented in C and not in Python, so |
| 13785 | subprocess can safely call it during late Python finalization without |
| 13786 | risking that used os attributes were set to None by _PyImport_Cleanup(). */ |
| 13787 | #if defined(WIFEXITED) || defined(MS_WINDOWS) |
| 13788 | /*[clinic input] |
| 13789 | os.waitstatus_to_exitcode |
| 13790 | |
| 13791 | status: int |
| 13792 | |
| 13793 | Convert a wait status to an exit code. |
| 13794 | |
| 13795 | On Unix: |
| 13796 | |
| 13797 | * If WIFEXITED(status) is true, return WEXITSTATUS(status). |
| 13798 | * If WIFSIGNALED(status) is true, return -WTERMSIG(status). |
| 13799 | * Otherwise, raise a ValueError. |
| 13800 | |
| 13801 | On Windows, return status shifted right by 8 bits. |
| 13802 | |
| 13803 | On Unix, if the process is being traced or if waitpid() was called with |
| 13804 | WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true. |
| 13805 | This function must not be called if WIFSTOPPED(status) is true. |
| 13806 | [clinic start generated code]*/ |
| 13807 | |
| 13808 | static PyObject * |
| 13809 | os_waitstatus_to_exitcode_impl(PyObject *module, int status) |
| 13810 | /*[clinic end generated code: output=c7c2265731f79b7a input=edfa5ca5006276fb]*/ |
| 13811 | { |
| 13812 | #ifndef MS_WINDOWS |
| 13813 | WAIT_TYPE wait_status; |
| 13814 | WAIT_STATUS_INT(wait_status) = status; |
| 13815 | int exitcode; |
| 13816 | if (WIFEXITED(wait_status)) { |
| 13817 | exitcode = WEXITSTATUS(wait_status); |
| 13818 | /* Sanity check to provide warranty on the function behavior. |
| 13819 | It should not occur in practice */ |
| 13820 | if (exitcode < 0) { |
| 13821 | PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode); |
| 13822 | return NULL; |
| 13823 | } |
| 13824 | } |
| 13825 | else if (WIFSIGNALED(wait_status)) { |
| 13826 | int signum = WTERMSIG(wait_status); |
| 13827 | /* Sanity check to provide warranty on the function behavior. |
| 13828 | It should not occurs in practice */ |
| 13829 | if (signum <= 0) { |
| 13830 | PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum); |
| 13831 | return NULL; |
| 13832 | } |
| 13833 | exitcode = -signum; |
| 13834 | } else if (WIFSTOPPED(wait_status)) { |
| 13835 | /* Status only received if the process is being traced |
| 13836 | or if waitpid() was called with WUNTRACED option. */ |
| 13837 | int signum = WSTOPSIG(wait_status); |
| 13838 | PyErr_Format(PyExc_ValueError, |
| 13839 | "process stopped by delivery of signal %i", |
| 13840 | signum); |
| 13841 | return NULL; |
| 13842 | } |
| 13843 | else { |
| 13844 | PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status); |
| 13845 | return NULL; |
| 13846 | } |
| 13847 | return PyLong_FromLong(exitcode); |
| 13848 | #else |
| 13849 | /* Windows implementation: see os.waitpid() implementation |
| 13850 | which uses _cwait(). */ |
| 13851 | int exitcode = (status >> 8); |
| 13852 | return PyLong_FromLong(exitcode); |
| 13853 | #endif |
| 13854 | } |
| 13855 | #endif |
| 13856 | |
| 13857 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13858 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13859 | |
| 13860 | OS_STAT_METHODDEF |
| 13861 | OS_ACCESS_METHODDEF |
| 13862 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13863 | OS_CHDIR_METHODDEF |
| 13864 | OS_CHFLAGS_METHODDEF |
| 13865 | OS_CHMOD_METHODDEF |
| 13866 | OS_FCHMOD_METHODDEF |
| 13867 | OS_LCHMOD_METHODDEF |
| 13868 | OS_CHOWN_METHODDEF |
| 13869 | OS_FCHOWN_METHODDEF |
| 13870 | OS_LCHOWN_METHODDEF |
| 13871 | OS_LCHFLAGS_METHODDEF |
| 13872 | OS_CHROOT_METHODDEF |
| 13873 | OS_CTERMID_METHODDEF |
| 13874 | OS_GETCWD_METHODDEF |
| 13875 | OS_GETCWDB_METHODDEF |
| 13876 | OS_LINK_METHODDEF |
| 13877 | OS_LISTDIR_METHODDEF |
| 13878 | OS_LSTAT_METHODDEF |
| 13879 | OS_MKDIR_METHODDEF |
| 13880 | OS_NICE_METHODDEF |
| 13881 | OS_GETPRIORITY_METHODDEF |
| 13882 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13883 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 13884 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13885 | OS_READLINK_METHODDEF |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 13886 | OS_COPY_FILE_RANGE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13887 | OS_RENAME_METHODDEF |
| 13888 | OS_REPLACE_METHODDEF |
| 13889 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13890 | OS_SYMLINK_METHODDEF |
| 13891 | OS_SYSTEM_METHODDEF |
| 13892 | OS_UMASK_METHODDEF |
| 13893 | OS_UNAME_METHODDEF |
| 13894 | OS_UNLINK_METHODDEF |
| 13895 | OS_REMOVE_METHODDEF |
| 13896 | OS_UTIME_METHODDEF |
| 13897 | OS_TIMES_METHODDEF |
| 13898 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13899 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13900 | OS_EXECV_METHODDEF |
| 13901 | OS_EXECVE_METHODDEF |
| 13902 | OS_SPAWNV_METHODDEF |
| 13903 | OS_SPAWNVE_METHODDEF |
| 13904 | OS_FORK1_METHODDEF |
| 13905 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 13906 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13907 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 13908 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 13909 | OS_SCHED_GETPARAM_METHODDEF |
| 13910 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 13911 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 13912 | OS_SCHED_SETPARAM_METHODDEF |
| 13913 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 13914 | OS_SCHED_YIELD_METHODDEF |
| 13915 | OS_SCHED_SETAFFINITY_METHODDEF |
| 13916 | OS_SCHED_GETAFFINITY_METHODDEF |
| 13917 | OS_OPENPTY_METHODDEF |
| 13918 | OS_FORKPTY_METHODDEF |
| 13919 | OS_GETEGID_METHODDEF |
| 13920 | OS_GETEUID_METHODDEF |
| 13921 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 13922 | #ifdef HAVE_GETGROUPLIST |
| 13923 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 13924 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13925 | OS_GETGROUPS_METHODDEF |
| 13926 | OS_GETPID_METHODDEF |
| 13927 | OS_GETPGRP_METHODDEF |
| 13928 | OS_GETPPID_METHODDEF |
| 13929 | OS_GETUID_METHODDEF |
| 13930 | OS_GETLOGIN_METHODDEF |
| 13931 | OS_KILL_METHODDEF |
| 13932 | OS_KILLPG_METHODDEF |
| 13933 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13934 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13935 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13936 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13937 | OS_SETUID_METHODDEF |
| 13938 | OS_SETEUID_METHODDEF |
| 13939 | OS_SETREUID_METHODDEF |
| 13940 | OS_SETGID_METHODDEF |
| 13941 | OS_SETEGID_METHODDEF |
| 13942 | OS_SETREGID_METHODDEF |
| 13943 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13944 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13945 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13946 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13947 | OS_GETPGID_METHODDEF |
| 13948 | OS_SETPGRP_METHODDEF |
| 13949 | OS_WAIT_METHODDEF |
| 13950 | OS_WAIT3_METHODDEF |
| 13951 | OS_WAIT4_METHODDEF |
| 13952 | OS_WAITID_METHODDEF |
| 13953 | OS_WAITPID_METHODDEF |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 13954 | OS_PIDFD_OPEN_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13955 | OS_GETSID_METHODDEF |
| 13956 | OS_SETSID_METHODDEF |
| 13957 | OS_SETPGID_METHODDEF |
| 13958 | OS_TCGETPGRP_METHODDEF |
| 13959 | OS_TCSETPGRP_METHODDEF |
| 13960 | OS_OPEN_METHODDEF |
| 13961 | OS_CLOSE_METHODDEF |
| 13962 | OS_CLOSERANGE_METHODDEF |
| 13963 | OS_DEVICE_ENCODING_METHODDEF |
| 13964 | OS_DUP_METHODDEF |
| 13965 | OS_DUP2_METHODDEF |
| 13966 | OS_LOCKF_METHODDEF |
| 13967 | OS_LSEEK_METHODDEF |
| 13968 | OS_READ_METHODDEF |
| 13969 | OS_READV_METHODDEF |
| 13970 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13971 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13972 | OS_WRITE_METHODDEF |
| 13973 | OS_WRITEV_METHODDEF |
| 13974 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13975 | OS_PWRITEV_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13976 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 13977 | {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13978 | posix_sendfile__doc__}, |
| 13979 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13980 | OS_FSTAT_METHODDEF |
| 13981 | OS_ISATTY_METHODDEF |
| 13982 | OS_PIPE_METHODDEF |
| 13983 | OS_PIPE2_METHODDEF |
| 13984 | OS_MKFIFO_METHODDEF |
| 13985 | OS_MKNOD_METHODDEF |
| 13986 | OS_MAJOR_METHODDEF |
| 13987 | OS_MINOR_METHODDEF |
| 13988 | OS_MAKEDEV_METHODDEF |
| 13989 | OS_FTRUNCATE_METHODDEF |
| 13990 | OS_TRUNCATE_METHODDEF |
| 13991 | OS_POSIX_FALLOCATE_METHODDEF |
| 13992 | OS_POSIX_FADVISE_METHODDEF |
| 13993 | OS_PUTENV_METHODDEF |
| 13994 | OS_UNSETENV_METHODDEF |
| 13995 | OS_STRERROR_METHODDEF |
| 13996 | OS_FCHDIR_METHODDEF |
| 13997 | OS_FSYNC_METHODDEF |
| 13998 | OS_SYNC_METHODDEF |
| 13999 | OS_FDATASYNC_METHODDEF |
| 14000 | OS_WCOREDUMP_METHODDEF |
| 14001 | OS_WIFCONTINUED_METHODDEF |
| 14002 | OS_WIFSTOPPED_METHODDEF |
| 14003 | OS_WIFSIGNALED_METHODDEF |
| 14004 | OS_WIFEXITED_METHODDEF |
| 14005 | OS_WEXITSTATUS_METHODDEF |
| 14006 | OS_WTERMSIG_METHODDEF |
| 14007 | OS_WSTOPSIG_METHODDEF |
| 14008 | OS_FSTATVFS_METHODDEF |
| 14009 | OS_STATVFS_METHODDEF |
| 14010 | OS_CONFSTR_METHODDEF |
| 14011 | OS_SYSCONF_METHODDEF |
| 14012 | OS_FPATHCONF_METHODDEF |
| 14013 | OS_PATHCONF_METHODDEF |
| 14014 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 14015 | OS__GETFULLPATHNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14016 | OS__GETDISKUSAGE_METHODDEF |
| 14017 | OS__GETFINALPATHNAME_METHODDEF |
| 14018 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 14019 | OS_GETLOADAVG_METHODDEF |
| 14020 | OS_URANDOM_METHODDEF |
| 14021 | OS_SETRESUID_METHODDEF |
| 14022 | OS_SETRESGID_METHODDEF |
| 14023 | OS_GETRESUID_METHODDEF |
| 14024 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 14025 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14026 | OS_GETXATTR_METHODDEF |
| 14027 | OS_SETXATTR_METHODDEF |
| 14028 | OS_REMOVEXATTR_METHODDEF |
| 14029 | OS_LISTXATTR_METHODDEF |
| 14030 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14031 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 14032 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 14033 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14034 | OS_CPU_COUNT_METHODDEF |
| 14035 | OS_GET_INHERITABLE_METHODDEF |
| 14036 | OS_SET_INHERITABLE_METHODDEF |
| 14037 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 14038 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 14039 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 14040 | OS_GET_BLOCKING_METHODDEF |
| 14041 | OS_SET_BLOCKING_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 14042 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 14043 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 14044 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14045 | OS_GETRANDOM_METHODDEF |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14046 | OS_MEMFD_CREATE_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14047 | #ifdef MS_WINDOWS |
| 14048 | OS__ADD_DLL_DIRECTORY_METHODDEF |
| 14049 | OS__REMOVE_DLL_DIRECTORY_METHODDEF |
| 14050 | #endif |
Victor Stinner | 65a796e | 2020-04-01 18:49:29 +0200 | [diff] [blame] | 14051 | OS_WAITSTATUS_TO_EXITCODE_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14052 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14053 | }; |
| 14054 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14055 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14056 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14057 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14058 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14059 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14060 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14061 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14062 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14063 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14064 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14065 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14066 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 14067 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14068 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14069 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14070 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14071 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14072 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14073 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14074 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14075 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14076 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14077 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14078 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14079 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14080 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14081 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14082 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14083 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 14084 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14085 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14086 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14087 | #endif |
| 14088 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14089 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14090 | #endif |
| 14091 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14092 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14093 | #endif |
| 14094 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14095 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14096 | #endif |
| 14097 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14098 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14099 | #endif |
| 14100 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14101 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14102 | #endif |
| 14103 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14104 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14105 | #endif |
| 14106 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14107 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14108 | #endif |
| 14109 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14110 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14111 | #endif |
| 14112 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14113 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14114 | #endif |
| 14115 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14116 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14117 | #endif |
| 14118 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14119 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14120 | #endif |
| 14121 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14122 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14123 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14124 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14125 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14126 | #endif |
| 14127 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14128 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 14129 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14130 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14131 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14132 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14133 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14134 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14135 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14136 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14137 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14138 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14139 | #endif |
| 14140 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14141 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 14142 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 14143 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14144 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14145 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14146 | #endif |
| 14147 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14148 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14149 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14150 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14151 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14152 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14153 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14154 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14155 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 14156 | #ifdef O_TMPFILE |
| 14157 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 14158 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14159 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14160 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14161 | #endif |
| 14162 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14163 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14164 | #endif |
| 14165 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14166 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14167 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14168 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14169 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14170 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14171 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14172 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14173 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14174 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14175 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14176 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14177 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14178 | #endif |
| 14179 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14180 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14181 | #endif |
| 14182 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14183 | /* MS Windows */ |
| 14184 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14185 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14186 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14187 | #endif |
| 14188 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14189 | /* Optimize for short life (keep in memory). */ |
| 14190 | /* 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] | 14191 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14192 | #endif |
| 14193 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14194 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14195 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14196 | #endif |
| 14197 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14198 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14199 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14200 | #endif |
| 14201 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14202 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14203 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14204 | #endif |
| 14205 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14206 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14207 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14208 | /* Send a SIGIO signal whenever input or output |
| 14209 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14210 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14211 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14212 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14213 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14214 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14215 | #endif |
| 14216 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14217 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14218 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14219 | #endif |
| 14220 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14221 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14222 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14223 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14224 | #ifdef O_NOLINKS |
| 14225 | /* 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] | 14226 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14227 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14228 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14229 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14230 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14231 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 14232 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14233 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14234 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14235 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14236 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14237 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14238 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14239 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14240 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14241 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14242 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14243 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14244 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14245 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14246 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14247 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14248 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14249 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14250 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14251 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14252 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14253 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14254 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14255 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14256 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14257 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14258 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14259 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14260 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14261 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14262 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14263 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14264 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14265 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14266 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14267 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14268 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14269 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14270 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14271 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14272 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14273 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14274 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14275 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14276 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14277 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14278 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14279 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14280 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14281 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14282 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14283 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14284 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14285 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 14286 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14287 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14288 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14289 | #endif /* ST_RDONLY */ |
| 14290 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14291 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14292 | #endif /* ST_NOSUID */ |
| 14293 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 14294 | /* GNU extensions */ |
| 14295 | #ifdef ST_NODEV |
| 14296 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 14297 | #endif /* ST_NODEV */ |
| 14298 | #ifdef ST_NOEXEC |
| 14299 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 14300 | #endif /* ST_NOEXEC */ |
| 14301 | #ifdef ST_SYNCHRONOUS |
| 14302 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 14303 | #endif /* ST_SYNCHRONOUS */ |
| 14304 | #ifdef ST_MANDLOCK |
| 14305 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 14306 | #endif /* ST_MANDLOCK */ |
| 14307 | #ifdef ST_WRITE |
| 14308 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 14309 | #endif /* ST_WRITE */ |
| 14310 | #ifdef ST_APPEND |
| 14311 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 14312 | #endif /* ST_APPEND */ |
| 14313 | #ifdef ST_NOATIME |
| 14314 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 14315 | #endif /* ST_NOATIME */ |
| 14316 | #ifdef ST_NODIRATIME |
| 14317 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 14318 | #endif /* ST_NODIRATIME */ |
| 14319 | #ifdef ST_RELATIME |
| 14320 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 14321 | #endif /* ST_RELATIME */ |
| 14322 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14323 | /* FreeBSD sendfile() constants */ |
| 14324 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14325 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14326 | #endif |
| 14327 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14328 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14329 | #endif |
| 14330 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14331 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14332 | #endif |
| 14333 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14334 | /* constants for posix_fadvise */ |
| 14335 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14336 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14337 | #endif |
| 14338 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14339 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14340 | #endif |
| 14341 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14342 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14343 | #endif |
| 14344 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14345 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14346 | #endif |
| 14347 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14348 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14349 | #endif |
| 14350 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14351 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14352 | #endif |
| 14353 | |
| 14354 | /* constants for waitid */ |
| 14355 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14356 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 14357 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 14358 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 14359 | #ifdef P_PIDFD |
| 14360 | if (PyModule_AddIntMacro(m, P_PIDFD)) return -1; |
| 14361 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14362 | #endif |
| 14363 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14364 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14365 | #endif |
| 14366 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14367 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14368 | #endif |
| 14369 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14370 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14371 | #endif |
| 14372 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14373 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14374 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14375 | #ifdef CLD_KILLED |
| 14376 | if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1; |
| 14377 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14378 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14379 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14380 | #endif |
| 14381 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14382 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14383 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14384 | #ifdef CLD_STOPPED |
| 14385 | if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1; |
| 14386 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14387 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14388 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14389 | #endif |
| 14390 | |
| 14391 | /* constants for lockf */ |
| 14392 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14393 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14394 | #endif |
| 14395 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14396 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14397 | #endif |
| 14398 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14399 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14400 | #endif |
| 14401 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14402 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14403 | #endif |
| 14404 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14405 | #ifdef RWF_DSYNC |
| 14406 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 14407 | #endif |
| 14408 | #ifdef RWF_HIPRI |
| 14409 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 14410 | #endif |
| 14411 | #ifdef RWF_SYNC |
| 14412 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 14413 | #endif |
| 14414 | #ifdef RWF_NOWAIT |
| 14415 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 14416 | #endif |
| 14417 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14418 | /* constants for posix_spawn */ |
| 14419 | #ifdef HAVE_POSIX_SPAWN |
| 14420 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 14421 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 14422 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 14423 | #endif |
| 14424 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14425 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14426 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 14427 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14428 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14429 | #endif |
| 14430 | #ifdef HAVE_SPAWNV |
| 14431 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14432 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 14433 | #endif |
| 14434 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14435 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14436 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14437 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14438 | #endif |
| 14439 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14440 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14441 | #endif |
| 14442 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14443 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14444 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14445 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 14446 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14447 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14448 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14449 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14450 | #endif |
| 14451 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14452 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14453 | #endif |
| 14454 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14455 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14456 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14457 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14458 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14459 | #endif |
| 14460 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14461 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14462 | #endif |
| 14463 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14464 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14465 | #endif |
| 14466 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14467 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14468 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14469 | #endif |
| 14470 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 14471 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14472 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 14473 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 14474 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 14475 | #endif |
| 14476 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14477 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14478 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14479 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14480 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14481 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14482 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14483 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14484 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14485 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14486 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14487 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14488 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14489 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14490 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14491 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14492 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14493 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14494 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14495 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14496 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14497 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 14498 | #if HAVE_DECL_RTLD_MEMBER |
| 14499 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 14500 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14501 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14502 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 14503 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 14504 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 14505 | #endif |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14506 | #ifdef HAVE_MEMFD_CREATE |
| 14507 | if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1; |
| 14508 | if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1; |
| 14509 | #ifdef MFD_HUGETLB |
| 14510 | if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14511 | #endif |
| 14512 | #ifdef MFD_HUGE_SHIFT |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14513 | if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14514 | #endif |
| 14515 | #ifdef MFD_HUGE_MASK |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14516 | if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14517 | #endif |
| 14518 | #ifdef MFD_HUGE_64KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14519 | if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14520 | #endif |
| 14521 | #ifdef MFD_HUGE_512KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14522 | if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14523 | #endif |
| 14524 | #ifdef MFD_HUGE_1MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14525 | if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14526 | #endif |
| 14527 | #ifdef MFD_HUGE_2MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14528 | if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14529 | #endif |
| 14530 | #ifdef MFD_HUGE_8MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14531 | if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14532 | #endif |
| 14533 | #ifdef MFD_HUGE_16MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14534 | if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14535 | #endif |
| 14536 | #ifdef MFD_HUGE_32MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14537 | if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14538 | #endif |
| 14539 | #ifdef MFD_HUGE_256MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14540 | if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14541 | #endif |
| 14542 | #ifdef MFD_HUGE_512MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14543 | if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14544 | #endif |
| 14545 | #ifdef MFD_HUGE_1GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14546 | if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14547 | #endif |
| 14548 | #ifdef MFD_HUGE_2GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14549 | if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14550 | #endif |
| 14551 | #ifdef MFD_HUGE_16GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14552 | if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1; |
| 14553 | #endif |
| 14554 | #endif |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14555 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14556 | #if defined(__APPLE__) |
| 14557 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 14558 | #endif |
| 14559 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14560 | #ifdef MS_WINDOWS |
| 14561 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; |
| 14562 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; |
| 14563 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; |
| 14564 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; |
| 14565 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; |
| 14566 | #endif |
| 14567 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14568 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14569 | } |
| 14570 | |
| 14571 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14572 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14573 | PyModuleDef_HEAD_INIT, |
| 14574 | MODNAME, |
| 14575 | posix__doc__, |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14576 | sizeof(_posixstate), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14577 | posix_methods, |
| 14578 | NULL, |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14579 | _posix_traverse, |
| 14580 | _posix_clear, |
| 14581 | _posix_free, |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14582 | }; |
| 14583 | |
| 14584 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14585 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14586 | |
| 14587 | #ifdef HAVE_FACCESSAT |
| 14588 | "HAVE_FACCESSAT", |
| 14589 | #endif |
| 14590 | |
| 14591 | #ifdef HAVE_FCHDIR |
| 14592 | "HAVE_FCHDIR", |
| 14593 | #endif |
| 14594 | |
| 14595 | #ifdef HAVE_FCHMOD |
| 14596 | "HAVE_FCHMOD", |
| 14597 | #endif |
| 14598 | |
| 14599 | #ifdef HAVE_FCHMODAT |
| 14600 | "HAVE_FCHMODAT", |
| 14601 | #endif |
| 14602 | |
| 14603 | #ifdef HAVE_FCHOWN |
| 14604 | "HAVE_FCHOWN", |
| 14605 | #endif |
| 14606 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 14607 | #ifdef HAVE_FCHOWNAT |
| 14608 | "HAVE_FCHOWNAT", |
| 14609 | #endif |
| 14610 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14611 | #ifdef HAVE_FEXECVE |
| 14612 | "HAVE_FEXECVE", |
| 14613 | #endif |
| 14614 | |
| 14615 | #ifdef HAVE_FDOPENDIR |
| 14616 | "HAVE_FDOPENDIR", |
| 14617 | #endif |
| 14618 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14619 | #ifdef HAVE_FPATHCONF |
| 14620 | "HAVE_FPATHCONF", |
| 14621 | #endif |
| 14622 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14623 | #ifdef HAVE_FSTATAT |
| 14624 | "HAVE_FSTATAT", |
| 14625 | #endif |
| 14626 | |
| 14627 | #ifdef HAVE_FSTATVFS |
| 14628 | "HAVE_FSTATVFS", |
| 14629 | #endif |
| 14630 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 14631 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14632 | "HAVE_FTRUNCATE", |
| 14633 | #endif |
| 14634 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14635 | #ifdef HAVE_FUTIMENS |
| 14636 | "HAVE_FUTIMENS", |
| 14637 | #endif |
| 14638 | |
| 14639 | #ifdef HAVE_FUTIMES |
| 14640 | "HAVE_FUTIMES", |
| 14641 | #endif |
| 14642 | |
| 14643 | #ifdef HAVE_FUTIMESAT |
| 14644 | "HAVE_FUTIMESAT", |
| 14645 | #endif |
| 14646 | |
| 14647 | #ifdef HAVE_LINKAT |
| 14648 | "HAVE_LINKAT", |
| 14649 | #endif |
| 14650 | |
| 14651 | #ifdef HAVE_LCHFLAGS |
| 14652 | "HAVE_LCHFLAGS", |
| 14653 | #endif |
| 14654 | |
| 14655 | #ifdef HAVE_LCHMOD |
| 14656 | "HAVE_LCHMOD", |
| 14657 | #endif |
| 14658 | |
| 14659 | #ifdef HAVE_LCHOWN |
| 14660 | "HAVE_LCHOWN", |
| 14661 | #endif |
| 14662 | |
| 14663 | #ifdef HAVE_LSTAT |
| 14664 | "HAVE_LSTAT", |
| 14665 | #endif |
| 14666 | |
| 14667 | #ifdef HAVE_LUTIMES |
| 14668 | "HAVE_LUTIMES", |
| 14669 | #endif |
| 14670 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14671 | #ifdef HAVE_MEMFD_CREATE |
| 14672 | "HAVE_MEMFD_CREATE", |
| 14673 | #endif |
| 14674 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14675 | #ifdef HAVE_MKDIRAT |
| 14676 | "HAVE_MKDIRAT", |
| 14677 | #endif |
| 14678 | |
| 14679 | #ifdef HAVE_MKFIFOAT |
| 14680 | "HAVE_MKFIFOAT", |
| 14681 | #endif |
| 14682 | |
| 14683 | #ifdef HAVE_MKNODAT |
| 14684 | "HAVE_MKNODAT", |
| 14685 | #endif |
| 14686 | |
| 14687 | #ifdef HAVE_OPENAT |
| 14688 | "HAVE_OPENAT", |
| 14689 | #endif |
| 14690 | |
| 14691 | #ifdef HAVE_READLINKAT |
| 14692 | "HAVE_READLINKAT", |
| 14693 | #endif |
| 14694 | |
| 14695 | #ifdef HAVE_RENAMEAT |
| 14696 | "HAVE_RENAMEAT", |
| 14697 | #endif |
| 14698 | |
| 14699 | #ifdef HAVE_SYMLINKAT |
| 14700 | "HAVE_SYMLINKAT", |
| 14701 | #endif |
| 14702 | |
| 14703 | #ifdef HAVE_UNLINKAT |
| 14704 | "HAVE_UNLINKAT", |
| 14705 | #endif |
| 14706 | |
| 14707 | #ifdef HAVE_UTIMENSAT |
| 14708 | "HAVE_UTIMENSAT", |
| 14709 | #endif |
| 14710 | |
| 14711 | #ifdef MS_WINDOWS |
| 14712 | "MS_WINDOWS", |
| 14713 | #endif |
| 14714 | |
| 14715 | NULL |
| 14716 | }; |
| 14717 | |
| 14718 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 14719 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 14720 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14721 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14722 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14723 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14724 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14725 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14726 | m = PyState_FindModule(&posixmodule); |
| 14727 | if (m != NULL) { |
| 14728 | Py_INCREF(m); |
| 14729 | return m; |
| 14730 | } |
| 14731 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14732 | m = PyModule_Create(&posixmodule); |
| 14733 | if (m == NULL) |
| 14734 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14735 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14736 | /* Initialize environ dictionary */ |
| 14737 | v = convertenviron(); |
| 14738 | Py_XINCREF(v); |
| 14739 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 14740 | return NULL; |
| 14741 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14742 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14743 | if (all_ins(m)) |
| 14744 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14745 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14746 | if (setup_confname_tables(m)) |
| 14747 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14748 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14749 | Py_INCREF(PyExc_OSError); |
| 14750 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 14751 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14752 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14753 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 14754 | PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc); |
| 14755 | if (WaitidResultType == NULL) { |
| 14756 | return NULL; |
| 14757 | } |
| 14758 | Py_INCREF(WaitidResultType); |
| 14759 | PyModule_AddObject(m, "waitid_result", WaitidResultType); |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14760 | get_posix_state(m)->WaitidResultType = WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14761 | #endif |
| 14762 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14763 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
| 14764 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 14765 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 14766 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 14767 | PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc); |
| 14768 | if (StatResultType == NULL) { |
| 14769 | return NULL; |
| 14770 | } |
| 14771 | Py_INCREF(StatResultType); |
| 14772 | PyModule_AddObject(m, "stat_result", StatResultType); |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14773 | get_posix_state(m)->StatResultType = StatResultType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14774 | structseq_new = ((PyTypeObject *)StatResultType)->tp_new; |
| 14775 | ((PyTypeObject *)StatResultType)->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14776 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14777 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
| 14778 | PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc); |
| 14779 | if (StatVFSResultType == NULL) { |
| 14780 | return NULL; |
| 14781 | } |
| 14782 | Py_INCREF(StatVFSResultType); |
| 14783 | PyModule_AddObject(m, "statvfs_result", StatVFSResultType); |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14784 | get_posix_state(m)->StatVFSResultType = StatVFSResultType; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14785 | #ifdef NEED_TICKS_PER_SECOND |
| 14786 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14787 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14788 | # elif defined(HZ) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14789 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14790 | # else |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14791 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14792 | # endif |
| 14793 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14794 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14795 | #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] | 14796 | sched_param_desc.name = MODNAME ".sched_param"; |
| 14797 | PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc); |
| 14798 | if (SchedParamType == NULL) { |
| 14799 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14800 | } |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14801 | Py_INCREF(SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14802 | PyModule_AddObject(m, "sched_param", SchedParamType); |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14803 | get_posix_state(m)->SchedParamType = SchedParamType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14804 | ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param; |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14805 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14806 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14807 | /* initialize TerminalSize_info */ |
| 14808 | PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc); |
| 14809 | if (TerminalSizeType == NULL) { |
| 14810 | return NULL; |
| 14811 | } |
| 14812 | Py_INCREF(TerminalSizeType); |
| 14813 | PyModule_AddObject(m, "terminal_size", TerminalSizeType); |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14814 | get_posix_state(m)->TerminalSizeType = TerminalSizeType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14815 | |
| 14816 | /* initialize scandir types */ |
| 14817 | PyObject *ScandirIteratorType = PyType_FromSpec(&ScandirIteratorType_spec); |
| 14818 | if (ScandirIteratorType == NULL) { |
| 14819 | return NULL; |
| 14820 | } |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14821 | get_posix_state(m)->ScandirIteratorType = ScandirIteratorType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14822 | |
| 14823 | PyObject *DirEntryType = PyType_FromSpec(&DirEntryType_spec); |
| 14824 | if (DirEntryType == NULL) { |
| 14825 | return NULL; |
| 14826 | } |
| 14827 | Py_INCREF(DirEntryType); |
| 14828 | PyModule_AddObject(m, "DirEntry", DirEntryType); |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14829 | get_posix_state(m)->DirEntryType = DirEntryType; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14830 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14831 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14832 | PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(×_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14833 | if (TimesResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14834 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14835 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14836 | Py_INCREF(TimesResultType); |
| 14837 | PyModule_AddObject(m, "times_result", TimesResultType); |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14838 | get_posix_state(m)->TimesResultType = TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14839 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14840 | PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14841 | if (UnameResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14842 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14843 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14844 | Py_INCREF(UnameResultType); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14845 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14846 | get_posix_state(m)->UnameResultType = (PyObject *)UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14847 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14848 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14849 | /* |
| 14850 | * Step 2 of weak-linking support on Mac OS X. |
| 14851 | * |
| 14852 | * The code below removes functions that are not available on the |
| 14853 | * currently active platform. |
| 14854 | * |
| 14855 | * This block allow one to use a python binary that was build on |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14856 | * OSX 10.4 on OSX 10.3, without losing access to new APIs on |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14857 | * OSX 10.4. |
| 14858 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14859 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14860 | if (fstatvfs == NULL) { |
| 14861 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 14862 | return NULL; |
| 14863 | } |
| 14864 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14865 | #endif /* HAVE_FSTATVFS */ |
| 14866 | |
| 14867 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14868 | if (statvfs == NULL) { |
| 14869 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 14870 | return NULL; |
| 14871 | } |
| 14872 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14873 | #endif /* HAVE_STATVFS */ |
| 14874 | |
| 14875 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14876 | if (lchown == NULL) { |
| 14877 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 14878 | return NULL; |
| 14879 | } |
| 14880 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14881 | #endif /* HAVE_LCHOWN */ |
| 14882 | |
| 14883 | |
| 14884 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14885 | |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14886 | if ((get_posix_state(m)->billion = PyLong_FromLong(1000000000)) == NULL) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14887 | return NULL; |
| 14888 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14889 | get_posix_state(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage"); |
| 14890 | if (get_posix_state(m)->struct_rusage == NULL) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14891 | return NULL; |
| 14892 | #endif |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 14893 | get_posix_state(m)->st_mode = PyUnicode_InternFromString("st_mode"); |
| 14894 | if (get_posix_state(m)->st_mode == NULL) |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 14895 | return NULL; |
| 14896 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14897 | /* suppress "function not used" warnings */ |
| 14898 | { |
| 14899 | int ignored; |
| 14900 | fd_specified("", -1); |
| 14901 | follow_symlinks_specified("", 1); |
| 14902 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 14903 | dir_fd_converter(Py_None, &ignored); |
| 14904 | dir_fd_unavailable(Py_None, &ignored); |
| 14905 | } |
| 14906 | |
| 14907 | /* |
| 14908 | * provide list of locally available functions |
| 14909 | * so os.py can populate support_* lists |
| 14910 | */ |
| 14911 | list = PyList_New(0); |
| 14912 | if (!list) |
| 14913 | return NULL; |
| 14914 | for (trace = have_functions; *trace; trace++) { |
| 14915 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 14916 | if (!unicode) |
| 14917 | return NULL; |
| 14918 | if (PyList_Append(list, unicode)) |
| 14919 | return NULL; |
| 14920 | Py_DECREF(unicode); |
| 14921 | } |
| 14922 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 14923 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14924 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14925 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14926 | |
| 14927 | #ifdef __cplusplus |
| 14928 | } |
| 14929 | #endif |