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 | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 454 | run_at_forkers(_PyInterpreterState_Get()->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 | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 465 | run_at_forkers(_PyInterpreterState_Get()->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 | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 479 | run_at_forkers(_PyInterpreterState_Get()->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 | } |
| 494 | #endif |
| 495 | |
| 496 | /* Legacy wrapper */ |
| 497 | void |
| 498 | PyOS_AfterFork(void) |
| 499 | { |
| 500 | #ifdef HAVE_FORK |
| 501 | PyOS_AfterFork_Child(); |
| 502 | #endif |
| 503 | } |
| 504 | |
| 505 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 506 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 507 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 508 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
| 509 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 510 | ULONG, struct _Py_stat_struct *); |
| 511 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 512 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 513 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 514 | #ifndef MS_WINDOWS |
| 515 | PyObject * |
| 516 | _PyLong_FromUid(uid_t uid) |
| 517 | { |
| 518 | if (uid == (uid_t)-1) |
| 519 | return PyLong_FromLong(-1); |
| 520 | return PyLong_FromUnsignedLong(uid); |
| 521 | } |
| 522 | |
| 523 | PyObject * |
| 524 | _PyLong_FromGid(gid_t gid) |
| 525 | { |
| 526 | if (gid == (gid_t)-1) |
| 527 | return PyLong_FromLong(-1); |
| 528 | return PyLong_FromUnsignedLong(gid); |
| 529 | } |
| 530 | |
| 531 | int |
| 532 | _Py_Uid_Converter(PyObject *obj, void *p) |
| 533 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 534 | uid_t uid; |
| 535 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 536 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 537 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 538 | unsigned long uresult; |
| 539 | |
| 540 | index = PyNumber_Index(obj); |
| 541 | if (index == NULL) { |
| 542 | PyErr_Format(PyExc_TypeError, |
| 543 | "uid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 544 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 545 | return 0; |
| 546 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 547 | |
| 548 | /* |
| 549 | * Handling uid_t is complicated for two reasons: |
| 550 | * * Although uid_t is (always?) unsigned, it still |
| 551 | * accepts -1. |
| 552 | * * We don't know its size in advance--it may be |
| 553 | * bigger than an int, or it may be smaller than |
| 554 | * a long. |
| 555 | * |
| 556 | * So a bit of defensive programming is in order. |
| 557 | * Start with interpreting the value passed |
| 558 | * in as a signed long and see if it works. |
| 559 | */ |
| 560 | |
| 561 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 562 | |
| 563 | if (!overflow) { |
| 564 | uid = (uid_t)result; |
| 565 | |
| 566 | if (result == -1) { |
| 567 | if (PyErr_Occurred()) |
| 568 | goto fail; |
| 569 | /* It's a legitimate -1, we're done. */ |
| 570 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 571 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 572 | |
| 573 | /* Any other negative number is disallowed. */ |
| 574 | if (result < 0) |
| 575 | goto underflow; |
| 576 | |
| 577 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 578 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 579 | (long)uid != result) |
| 580 | goto underflow; |
| 581 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 582 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 583 | |
| 584 | if (overflow < 0) |
| 585 | goto underflow; |
| 586 | |
| 587 | /* |
| 588 | * Okay, the value overflowed a signed long. If it |
| 589 | * fits in an *unsigned* long, it may still be okay, |
| 590 | * as uid_t may be unsigned long on this platform. |
| 591 | */ |
| 592 | uresult = PyLong_AsUnsignedLong(index); |
| 593 | if (PyErr_Occurred()) { |
| 594 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 595 | goto overflow; |
| 596 | goto fail; |
| 597 | } |
| 598 | |
| 599 | uid = (uid_t)uresult; |
| 600 | |
| 601 | /* |
| 602 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, |
| 603 | * but this value would get interpreted as (uid_t)-1 by chown |
| 604 | * and its siblings. That's not what the user meant! So we |
| 605 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 606 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 607 | */ |
| 608 | if (uid == (uid_t)-1) |
| 609 | goto overflow; |
| 610 | |
| 611 | /* Ensure the value wasn't truncated. */ |
| 612 | if (sizeof(uid_t) < sizeof(long) && |
| 613 | (unsigned long)uid != uresult) |
| 614 | goto overflow; |
| 615 | /* fallthrough */ |
| 616 | |
| 617 | success: |
| 618 | Py_DECREF(index); |
| 619 | *(uid_t *)p = uid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 620 | return 1; |
| 621 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 622 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 623 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 624 | "uid is less than minimum"); |
| 625 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 626 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 627 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 628 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 629 | "uid is greater than maximum"); |
| 630 | /* fallthrough */ |
| 631 | |
| 632 | fail: |
| 633 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | int |
| 638 | _Py_Gid_Converter(PyObject *obj, void *p) |
| 639 | { |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 640 | gid_t gid; |
| 641 | PyObject *index; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 642 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 643 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 644 | unsigned long uresult; |
| 645 | |
| 646 | index = PyNumber_Index(obj); |
| 647 | if (index == NULL) { |
| 648 | PyErr_Format(PyExc_TypeError, |
| 649 | "gid should be integer, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 650 | _PyType_Name(Py_TYPE(obj))); |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 651 | return 0; |
| 652 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 653 | |
| 654 | /* |
| 655 | * Handling gid_t is complicated for two reasons: |
| 656 | * * Although gid_t is (always?) unsigned, it still |
| 657 | * accepts -1. |
| 658 | * * We don't know its size in advance--it may be |
| 659 | * bigger than an int, or it may be smaller than |
| 660 | * a long. |
| 661 | * |
| 662 | * So a bit of defensive programming is in order. |
| 663 | * Start with interpreting the value passed |
| 664 | * in as a signed long and see if it works. |
| 665 | */ |
| 666 | |
| 667 | result = PyLong_AsLongAndOverflow(index, &overflow); |
| 668 | |
| 669 | if (!overflow) { |
| 670 | gid = (gid_t)result; |
| 671 | |
| 672 | if (result == -1) { |
| 673 | if (PyErr_Occurred()) |
| 674 | goto fail; |
| 675 | /* It's a legitimate -1, we're done. */ |
| 676 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 677 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 678 | |
| 679 | /* Any other negative number is disallowed. */ |
| 680 | if (result < 0) { |
| 681 | goto underflow; |
| 682 | } |
| 683 | |
| 684 | /* Ensure the value wasn't truncated. */ |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 685 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 686 | (long)gid != result) |
| 687 | goto underflow; |
| 688 | goto success; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 689 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 690 | |
| 691 | if (overflow < 0) |
| 692 | goto underflow; |
| 693 | |
| 694 | /* |
| 695 | * Okay, the value overflowed a signed long. If it |
| 696 | * fits in an *unsigned* long, it may still be okay, |
| 697 | * as gid_t may be unsigned long on this platform. |
| 698 | */ |
| 699 | uresult = PyLong_AsUnsignedLong(index); |
| 700 | if (PyErr_Occurred()) { |
| 701 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 702 | goto overflow; |
| 703 | goto fail; |
| 704 | } |
| 705 | |
| 706 | gid = (gid_t)uresult; |
| 707 | |
| 708 | /* |
| 709 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, |
| 710 | * but this value would get interpreted as (gid_t)-1 by chown |
| 711 | * and its siblings. That's not what the user meant! So we |
| 712 | * throw an overflow exception instead. (We already |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 713 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 714 | */ |
| 715 | if (gid == (gid_t)-1) |
| 716 | goto overflow; |
| 717 | |
| 718 | /* Ensure the value wasn't truncated. */ |
| 719 | if (sizeof(gid_t) < sizeof(long) && |
| 720 | (unsigned long)gid != uresult) |
| 721 | goto overflow; |
| 722 | /* fallthrough */ |
| 723 | |
| 724 | success: |
| 725 | Py_DECREF(index); |
| 726 | *(gid_t *)p = gid; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 727 | return 1; |
| 728 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 729 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 730 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 731 | "gid is less than minimum"); |
| 732 | goto fail; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 733 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 734 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 735 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 736 | "gid is greater than maximum"); |
| 737 | /* fallthrough */ |
| 738 | |
| 739 | fail: |
| 740 | Py_DECREF(index); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 741 | return 0; |
| 742 | } |
| 743 | #endif /* MS_WINDOWS */ |
| 744 | |
| 745 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 746 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 747 | |
| 748 | |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 749 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 750 | static int |
| 751 | _Py_Dev_Converter(PyObject *obj, void *p) |
| 752 | { |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 753 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 754 | if (PyErr_Occurred()) |
| 755 | return 0; |
| 756 | return 1; |
| 757 | } |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 758 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 759 | |
| 760 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 761 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 762 | /* |
| 763 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); |
| 764 | * without the int cast, the value gets interpreted as uint (4291925331), |
| 765 | * which doesn't play nicely with all the initializer lines in this file that |
| 766 | * look like this: |
| 767 | * int dir_fd = DEFAULT_DIR_FD; |
| 768 | */ |
| 769 | #define DEFAULT_DIR_FD (int)AT_FDCWD |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 770 | #else |
| 771 | #define DEFAULT_DIR_FD (-100) |
| 772 | #endif |
| 773 | |
| 774 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 775 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 776 | { |
| 777 | int overflow; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 778 | long long_value; |
| 779 | |
| 780 | PyObject *index = PyNumber_Index(o); |
| 781 | if (index == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 782 | return 0; |
| 783 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 784 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 785 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 786 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
| 787 | Py_DECREF(index); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 788 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 789 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 790 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 791 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 792 | return 0; |
| 793 | } |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 794 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 795 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 796 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 797 | return 0; |
| 798 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 799 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 800 | *p = (int)long_value; |
| 801 | return 1; |
| 802 | } |
| 803 | |
| 804 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 805 | dir_fd_converter(PyObject *o, void *p) |
| 806 | { |
| 807 | if (o == Py_None) { |
| 808 | *(int *)p = DEFAULT_DIR_FD; |
| 809 | return 1; |
| 810 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 811 | else if (PyIndex_Check(o)) { |
| 812 | return _fd_converter(o, (int *)p); |
| 813 | } |
| 814 | else { |
| 815 | PyErr_Format(PyExc_TypeError, |
| 816 | "argument should be integer or None, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 817 | _PyType_Name(Py_TYPE(o))); |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 818 | return 0; |
| 819 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 822 | #ifdef HAVE_PUTENV |
| 823 | # define PY_PUTENV_DICT |
| 824 | #endif |
| 825 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 826 | typedef struct { |
| 827 | PyObject *billion; |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 828 | #ifdef PY_PUTENV_DICT |
| 829 | /* putenv() and _wputenv() requires that the caller manages the environment |
| 830 | variable memory. Use a Python dictionary for that: name => env, where |
| 831 | env is a string like "name=value". On Windows, dict keys and values are |
| 832 | Unicode strings. On Unix, they are bytes strings. */ |
| 833 | PyObject *putenv_dict; |
| 834 | #endif |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 835 | PyObject *DirEntryType; |
| 836 | PyObject *ScandirIteratorType; |
| 837 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 838 | PyObject *SchedParamType; |
| 839 | #endif |
| 840 | PyObject *StatResultType; |
| 841 | PyObject *StatVFSResultType; |
| 842 | PyObject *TerminalSizeType; |
| 843 | PyObject *TimesResultType; |
| 844 | PyObject *UnameResultType; |
| 845 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 846 | PyObject *WaitidResultType; |
| 847 | #endif |
| 848 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 849 | PyObject *struct_rusage; |
| 850 | #endif |
| 851 | PyObject *st_mode; |
| 852 | } _posixstate; |
| 853 | |
| 854 | static struct PyModuleDef posixmodule; |
| 855 | |
| 856 | #define _posixstate(o) ((_posixstate *)PyModule_GetState(o)) |
| 857 | #define _posixstate_global ((_posixstate *)PyModule_GetState(PyState_FindModule(&posixmodule))) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 858 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 859 | /* |
| 860 | * A PyArg_ParseTuple "converter" function |
| 861 | * that handles filesystem paths in the manner |
| 862 | * preferred by the os module. |
| 863 | * |
| 864 | * path_converter accepts (Unicode) strings and their |
| 865 | * subclasses, and bytes and their subclasses. What |
| 866 | * it does with the argument depends on the platform: |
| 867 | * |
| 868 | * * On Windows, if we get a (Unicode) string we |
| 869 | * extract the wchar_t * and return it; if we get |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 870 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 871 | * |
| 872 | * * On all other platforms, strings are encoded |
| 873 | * to bytes using PyUnicode_FSConverter, then we |
| 874 | * extract the char * from the bytes object and |
| 875 | * return that. |
| 876 | * |
| 877 | * path_converter also optionally accepts signed |
| 878 | * integers (representing open file descriptors) instead |
| 879 | * of path strings. |
| 880 | * |
| 881 | * Input fields: |
| 882 | * path.nullable |
| 883 | * If nonzero, the path is permitted to be None. |
| 884 | * path.allow_fd |
| 885 | * If nonzero, the path is permitted to be a file handle |
| 886 | * (a signed int) instead of a string. |
| 887 | * path.function_name |
| 888 | * If non-NULL, path_converter will use that as the name |
| 889 | * of the function in error messages. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 890 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 891 | * path.argument_name |
| 892 | * If non-NULL, path_converter will use that as the name |
| 893 | * of the parameter in error messages. |
| 894 | * (If path.argument_name is NULL it uses "path".) |
| 895 | * |
| 896 | * Output fields: |
| 897 | * path.wide |
| 898 | * Points to the path if it was expressed as Unicode |
| 899 | * and was not encoded. (Only used on Windows.) |
| 900 | * path.narrow |
| 901 | * Points to the path if it was expressed as bytes, |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 902 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 903 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 904 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 905 | * path.fd |
| 906 | * Contains a file descriptor if path.accept_fd was true |
| 907 | * and the caller provided a signed integer instead of any |
| 908 | * sort of string. |
| 909 | * |
| 910 | * WARNING: if your "path" parameter is optional, and is |
| 911 | * unspecified, path_converter will never get called. |
| 912 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 |
| 913 | * yourself! |
| 914 | * path.length |
| 915 | * The length of the path in characters, if specified as |
| 916 | * a string. |
| 917 | * path.object |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 918 | * The original object passed in (if get a PathLike object, |
| 919 | * the result of PyOS_FSPath() is treated as the original object). |
| 920 | * Own a reference to the object. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 921 | * path.cleanup |
| 922 | * For internal use only. May point to a temporary object. |
| 923 | * (Pay no attention to the man behind the curtain.) |
| 924 | * |
| 925 | * At most one of path.wide or path.narrow will be non-NULL. |
| 926 | * If path was None and path.nullable was set, |
| 927 | * or if path was an integer and path.allow_fd was set, |
| 928 | * both path.wide and path.narrow will be NULL |
| 929 | * and path.length will be 0. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 930 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 931 | * path_converter takes care to not write to the path_t |
| 932 | * unless it's successful. However it must reset the |
| 933 | * "cleanup" field each time it's called. |
| 934 | * |
| 935 | * Use as follows: |
| 936 | * path_t path; |
| 937 | * memset(&path, 0, sizeof(path)); |
| 938 | * PyArg_ParseTuple(args, "O&", path_converter, &path); |
| 939 | * // ... use values from path ... |
| 940 | * path_cleanup(&path); |
| 941 | * |
| 942 | * (Note that if PyArg_Parse fails you don't need to call |
| 943 | * path_cleanup(). However it is safe to do so.) |
| 944 | */ |
| 945 | typedef struct { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 946 | const char *function_name; |
| 947 | const char *argument_name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 948 | int nullable; |
| 949 | int allow_fd; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 950 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 951 | #ifdef MS_WINDOWS |
| 952 | BOOL narrow; |
| 953 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 954 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 955 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 956 | int fd; |
| 957 | Py_ssize_t length; |
| 958 | PyObject *object; |
| 959 | PyObject *cleanup; |
| 960 | } path_t; |
| 961 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 962 | #ifdef MS_WINDOWS |
| 963 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 964 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} |
| 965 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 966 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
| 967 | {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] | 968 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 969 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 970 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 971 | path_cleanup(path_t *path) |
| 972 | { |
| 973 | Py_CLEAR(path->object); |
| 974 | Py_CLEAR(path->cleanup); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | static int |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 978 | path_converter(PyObject *o, void *p) |
| 979 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 980 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 981 | PyObject *bytes = NULL; |
| 982 | Py_ssize_t length = 0; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 983 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 984 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 985 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 986 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 987 | const wchar_t *wide; |
| 988 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 989 | |
| 990 | #define FORMAT_EXCEPTION(exc, fmt) \ |
| 991 | PyErr_Format(exc, "%s%s" fmt, \ |
| 992 | path->function_name ? path->function_name : "", \ |
| 993 | path->function_name ? ": " : "", \ |
| 994 | path->argument_name ? path->argument_name : "path") |
| 995 | |
| 996 | /* Py_CLEANUP_SUPPORTED support */ |
| 997 | if (o == NULL) { |
| 998 | path_cleanup(path); |
| 999 | return 1; |
| 1000 | } |
| 1001 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1002 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1003 | path->object = path->cleanup = NULL; |
| 1004 | /* path->object owns a reference to the original object */ |
| 1005 | Py_INCREF(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1006 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1007 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1008 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1009 | #ifdef MS_WINDOWS |
| 1010 | path->narrow = FALSE; |
| 1011 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1012 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1013 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1014 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1015 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1018 | /* Only call this here so that we don't treat the return value of |
| 1019 | os.fspath() as an fd or buffer. */ |
| 1020 | is_index = path->allow_fd && PyIndex_Check(o); |
| 1021 | is_buffer = PyObject_CheckBuffer(o); |
| 1022 | is_bytes = PyBytes_Check(o); |
| 1023 | is_unicode = PyUnicode_Check(o); |
| 1024 | |
| 1025 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { |
| 1026 | /* Inline PyOS_FSPath() for better error messages. */ |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1027 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1028 | |
| 1029 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); |
| 1030 | if (NULL == func) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1031 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1032 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1033 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1034 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1035 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1036 | goto error_exit; |
| 1037 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1038 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1039 | is_unicode = 1; |
| 1040 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1041 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1042 | is_bytes = 1; |
| 1043 | } |
| 1044 | else { |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1045 | PyErr_Format(PyExc_TypeError, |
| 1046 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1047 | "not %.200s", _PyType_Name(Py_TYPE(o)), |
| 1048 | _PyType_Name(Py_TYPE(res))); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1049 | Py_DECREF(res); |
| 1050 | goto error_exit; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1051 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1052 | |
| 1053 | /* still owns a reference to the original object */ |
| 1054 | Py_DECREF(o); |
| 1055 | o = res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | if (is_unicode) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1059 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1060 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1061 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1062 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1063 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1064 | if (length > 32767) { |
| 1065 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1066 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1067 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1068 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1069 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1070 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1071 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1072 | |
| 1073 | path->wide = wide; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1074 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1075 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1076 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1077 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1078 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1079 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1080 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1081 | #endif |
| 1082 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1083 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1084 | bytes = o; |
| 1085 | Py_INCREF(bytes); |
| 1086 | } |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1087 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1088 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1089 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1090 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1091 | "%s%s%s should be %s, not %.200s", |
| 1092 | path->function_name ? path->function_name : "", |
| 1093 | path->function_name ? ": " : "", |
| 1094 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1095 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1096 | "integer or None" : |
| 1097 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1098 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1099 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1100 | _PyType_Name(Py_TYPE(o)))) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1101 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1102 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1103 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1104 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1105 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1106 | } |
| 1107 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1108 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1109 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1110 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1111 | } |
| 1112 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1113 | #ifdef MS_WINDOWS |
| 1114 | path->narrow = FALSE; |
| 1115 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1116 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1117 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1118 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1119 | } |
| 1120 | else { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1121 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1122 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
| 1123 | path->function_name ? path->function_name : "", |
| 1124 | path->function_name ? ": " : "", |
| 1125 | path->argument_name ? path->argument_name : "path", |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1126 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
| 1127 | "integer or None" : |
| 1128 | path->allow_fd ? "string, bytes, os.PathLike or integer" : |
| 1129 | path->nullable ? "string, bytes, os.PathLike or None" : |
| 1130 | "string, bytes or os.PathLike", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 1131 | _PyType_Name(Py_TYPE(o))); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1132 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1135 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1136 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1137 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1138 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1139 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1142 | #ifdef MS_WINDOWS |
| 1143 | wo = PyUnicode_DecodeFSDefaultAndSize( |
| 1144 | narrow, |
| 1145 | length |
| 1146 | ); |
| 1147 | if (!wo) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1148 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1151 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1152 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1153 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1154 | } |
| 1155 | if (length > 32767) { |
| 1156 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1157 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1158 | } |
| 1159 | if (wcslen(wide) != length) { |
| 1160 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1161 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1162 | } |
| 1163 | path->wide = wide; |
| 1164 | path->narrow = TRUE; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1165 | path->cleanup = wo; |
| 1166 | Py_DECREF(bytes); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1167 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1168 | path->wide = NULL; |
| 1169 | path->narrow = narrow; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1170 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1171 | /* Still a reference owned by path->object, don't have to |
| 1172 | worry about path->narrow is used after free. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1173 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1174 | } |
| 1175 | else { |
| 1176 | path->cleanup = bytes; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1177 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1178 | #endif |
| 1179 | path->fd = -1; |
| 1180 | |
| 1181 | success_exit: |
| 1182 | path->length = length; |
| 1183 | path->object = o; |
| 1184 | return Py_CLEANUP_SUPPORTED; |
| 1185 | |
| 1186 | error_exit: |
| 1187 | Py_XDECREF(o); |
| 1188 | Py_XDECREF(bytes); |
| 1189 | #ifdef MS_WINDOWS |
| 1190 | Py_XDECREF(wo); |
| 1191 | #endif |
| 1192 | return 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1196 | argument_unavailable_error(const char *function_name, const char *argument_name) |
| 1197 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1198 | PyErr_Format(PyExc_NotImplementedError, |
| 1199 | "%s%s%s unavailable on this platform", |
| 1200 | (function_name != NULL) ? function_name : "", |
| 1201 | (function_name != NULL) ? ": ": "", |
| 1202 | argument_name); |
| 1203 | } |
| 1204 | |
| 1205 | static int |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1206 | dir_fd_unavailable(PyObject *o, void *p) |
| 1207 | { |
| 1208 | int dir_fd; |
| 1209 | if (!dir_fd_converter(o, &dir_fd)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1210 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1211 | if (dir_fd != DEFAULT_DIR_FD) { |
| 1212 | argument_unavailable_error(NULL, "dir_fd"); |
| 1213 | return 0; |
| 1214 | } |
| 1215 | *(int *)p = dir_fd; |
| 1216 | return 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1220 | fd_specified(const char *function_name, int fd) |
| 1221 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1222 | if (fd == -1) |
| 1223 | return 0; |
| 1224 | |
| 1225 | argument_unavailable_error(function_name, "fd"); |
| 1226 | return 1; |
| 1227 | } |
| 1228 | |
| 1229 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1230 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
| 1231 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1232 | if (follow_symlinks) |
| 1233 | return 0; |
| 1234 | |
| 1235 | argument_unavailable_error(function_name, "follow_symlinks"); |
| 1236 | return 1; |
| 1237 | } |
| 1238 | |
| 1239 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1240 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
| 1241 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1242 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
| 1243 | #ifndef MS_WINDOWS |
| 1244 | && !path->narrow |
| 1245 | #endif |
| 1246 | ) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1247 | PyErr_Format(PyExc_ValueError, |
| 1248 | "%s: can't specify dir_fd without matching path", |
| 1249 | function_name); |
| 1250 | return 1; |
| 1251 | } |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1256 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
| 1257 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1258 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
| 1259 | PyErr_Format(PyExc_ValueError, |
| 1260 | "%s: can't specify both dir_fd and fd", |
| 1261 | function_name); |
| 1262 | return 1; |
| 1263 | } |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1268 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
| 1269 | int follow_symlinks) |
| 1270 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1271 | if ((fd > 0) && (!follow_symlinks)) { |
| 1272 | PyErr_Format(PyExc_ValueError, |
| 1273 | "%s: cannot use fd and follow_symlinks together", |
| 1274 | function_name); |
| 1275 | return 1; |
| 1276 | } |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
| 1280 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1281 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
| 1282 | int follow_symlinks) |
| 1283 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1284 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
| 1285 | PyErr_Format(PyExc_ValueError, |
| 1286 | "%s: cannot use dir_fd and follow_symlinks together", |
| 1287 | function_name); |
| 1288 | return 1; |
| 1289 | } |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1293 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1294 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1295 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1296 | typedef off_t Py_off_t; |
| 1297 | #endif |
| 1298 | |
| 1299 | static int |
| 1300 | Py_off_t_converter(PyObject *arg, void *addr) |
| 1301 | { |
| 1302 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1303 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); |
| 1304 | #else |
| 1305 | *((Py_off_t *)addr) = PyLong_AsLong(arg); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1306 | #endif |
| 1307 | if (PyErr_Occurred()) |
| 1308 | return 0; |
| 1309 | return 1; |
| 1310 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1311 | |
| 1312 | static PyObject * |
| 1313 | PyLong_FromPy_off_t(Py_off_t offset) |
| 1314 | { |
| 1315 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1316 | return PyLong_FromLongLong(offset); |
| 1317 | #else |
| 1318 | return PyLong_FromLong(offset); |
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1319 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1320 | } |
| 1321 | |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1322 | #ifdef HAVE_SIGSET_T |
| 1323 | /* Convert an iterable of integers to a sigset. |
| 1324 | Return 1 on success, return 0 and raise an exception on error. */ |
| 1325 | int |
| 1326 | _Py_Sigset_Converter(PyObject *obj, void *addr) |
| 1327 | { |
| 1328 | sigset_t *mask = (sigset_t *)addr; |
| 1329 | PyObject *iterator, *item; |
| 1330 | long signum; |
| 1331 | int overflow; |
| 1332 | |
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1333 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
| 1334 | if (sigemptyset(mask) < (0)) { |
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1335 | /* Probably only if mask == NULL. */ |
| 1336 | PyErr_SetFromErrno(PyExc_OSError); |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | iterator = PyObject_GetIter(obj); |
| 1341 | if (iterator == NULL) { |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | while ((item = PyIter_Next(iterator)) != NULL) { |
| 1346 | signum = PyLong_AsLongAndOverflow(item, &overflow); |
| 1347 | Py_DECREF(item); |
| 1348 | if (signum <= 0 || signum >= NSIG) { |
| 1349 | if (overflow || signum != -1 || !PyErr_Occurred()) { |
| 1350 | PyErr_Format(PyExc_ValueError, |
| 1351 | "signal number %ld out of range", signum); |
| 1352 | } |
| 1353 | goto error; |
| 1354 | } |
| 1355 | if (sigaddset(mask, (int)signum)) { |
| 1356 | if (errno != EINVAL) { |
| 1357 | /* Probably impossible */ |
| 1358 | PyErr_SetFromErrno(PyExc_OSError); |
| 1359 | goto error; |
| 1360 | } |
| 1361 | /* For backwards compatibility, allow idioms such as |
| 1362 | * `range(1, NSIG)` but warn about invalid signal numbers |
| 1363 | */ |
| 1364 | const char msg[] = |
| 1365 | "invalid signal number %ld, please use valid_signals()"; |
| 1366 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { |
| 1367 | goto error; |
| 1368 | } |
| 1369 | } |
| 1370 | } |
| 1371 | if (!PyErr_Occurred()) { |
| 1372 | Py_DECREF(iterator); |
| 1373 | return 1; |
| 1374 | } |
| 1375 | |
| 1376 | error: |
| 1377 | Py_DECREF(iterator); |
| 1378 | return 0; |
| 1379 | } |
| 1380 | #endif /* HAVE_SIGSET_T */ |
| 1381 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1382 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1383 | |
| 1384 | static int |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1385 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1386 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1387 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1388 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1389 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1390 | |
| 1391 | if (0 == DeviceIoControl( |
| 1392 | reparse_point_handle, |
| 1393 | FSCTL_GET_REPARSE_POINT, |
| 1394 | NULL, 0, /* in buffer */ |
| 1395 | target_buffer, sizeof(target_buffer), |
| 1396 | &n_bytes_returned, |
| 1397 | NULL)) /* we're not using OVERLAPPED_IO */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1398 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1399 | |
| 1400 | if (reparse_tag) |
| 1401 | *reparse_tag = rdb->ReparseTag; |
| 1402 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1403 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1404 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1405 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1406 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1407 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1408 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1409 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1410 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1411 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
| 1412 | ** man environ(7). |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1413 | */ |
| 1414 | #include <crt_externs.h> |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1415 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1416 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1417 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1418 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1419 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1420 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1421 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1422 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1423 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1424 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1425 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1426 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1427 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1428 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1429 | d = PyDict_New(); |
| 1430 | if (d == NULL) |
| 1431 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1432 | #ifdef MS_WINDOWS |
| 1433 | /* _wenviron must be initialized in this way if the program is started |
| 1434 | through main() instead of wmain(). */ |
| 1435 | _wgetenv(L""); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1436 | e = _wenviron; |
Benoit Hudson | 723f71a | 2019-12-06 14:15:03 -0500 | [diff] [blame] | 1437 | #elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
| 1438 | /* environ is not accessible as an extern in a shared object on OSX; use |
| 1439 | _NSGetEnviron to resolve it. The value changes if you add environment |
| 1440 | variables between calls to Py_Initialize, so don't cache the value. */ |
| 1441 | e = *_NSGetEnviron(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1442 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1443 | e = environ; |
| 1444 | #endif |
| 1445 | if (e == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1446 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1447 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1448 | PyObject *k; |
| 1449 | PyObject *v; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1450 | #ifdef MS_WINDOWS |
| 1451 | const wchar_t *p = wcschr(*e, L'='); |
| 1452 | #else |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1453 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1454 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1455 | if (p == NULL) |
| 1456 | continue; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1457 | #ifdef MS_WINDOWS |
| 1458 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); |
| 1459 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1460 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1461 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1462 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1463 | Py_DECREF(d); |
| 1464 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1465 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1466 | #ifdef MS_WINDOWS |
| 1467 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); |
| 1468 | #else |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1469 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1470 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1471 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1472 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1473 | Py_DECREF(d); |
| 1474 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1475 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1476 | if (PyDict_GetItemWithError(d, k) == NULL) { |
| 1477 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { |
| 1478 | Py_DECREF(v); |
| 1479 | Py_DECREF(k); |
| 1480 | Py_DECREF(d); |
| 1481 | return NULL; |
| 1482 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1483 | } |
| 1484 | Py_DECREF(k); |
| 1485 | Py_DECREF(v); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1486 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1487 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1490 | /* Set a POSIX-specific error from errno, and return NULL */ |
| 1491 | |
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1492 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1493 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1494 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1495 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1496 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1497 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1498 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1499 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1500 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1501 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1502 | /* XXX We should pass the function name along in the future. |
| 1503 | (winreg.c also wants to pass the function name.) |
| 1504 | This would however require an additional param to the |
| 1505 | Windows error object, which is non-trivial. |
| 1506 | */ |
| 1507 | errno = GetLastError(); |
| 1508 | if (filename) |
| 1509 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); |
| 1510 | else |
| 1511 | return PyErr_SetFromWindowsErr(errno); |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1512 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1513 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1514 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1515 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1516 | { |
| 1517 | /* XXX - see win32_error for comments on 'function' */ |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1518 | if (filename) |
| 1519 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1520 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1521 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1522 | filename); |
| 1523 | else |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1524 | return PyErr_SetFromWindowsErr(err); |
| 1525 | } |
| 1526 | |
| 1527 | static PyObject * |
| 1528 | win32_error_object(const char* function, PyObject* filename) |
| 1529 | { |
| 1530 | errno = GetLastError(); |
| 1531 | return win32_error_object_err(function, filename, errno); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1534 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1535 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1536 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1537 | posix_path_object_error(PyObject *path) |
| 1538 | { |
| 1539 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
| 1540 | } |
| 1541 | |
| 1542 | static PyObject * |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1543 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1544 | { |
| 1545 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1546 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 1547 | PyExc_OSError, 0, path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1548 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1549 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1550 | #endif |
| 1551 | } |
| 1552 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1553 | static PyObject * |
| 1554 | path_object_error2(PyObject *path, PyObject *path2) |
| 1555 | { |
| 1556 | #ifdef MS_WINDOWS |
| 1557 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( |
| 1558 | PyExc_OSError, 0, path, path2); |
| 1559 | #else |
| 1560 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); |
| 1561 | #endif |
| 1562 | } |
| 1563 | |
| 1564 | static PyObject * |
| 1565 | path_error(path_t *path) |
| 1566 | { |
| 1567 | return path_object_error(path->object); |
| 1568 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1569 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1570 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1571 | posix_path_error(path_t *path) |
| 1572 | { |
| 1573 | return posix_path_object_error(path->object); |
| 1574 | } |
| 1575 | |
| 1576 | static PyObject * |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1577 | path_error2(path_t *path, path_t *path2) |
| 1578 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1579 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1583 | /* POSIX generic methods */ |
| 1584 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1585 | static int |
| 1586 | fildes_converter(PyObject *o, void *p) |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1587 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1588 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1589 | int *pointer = (int *)p; |
| 1590 | fd = PyObject_AsFileDescriptor(o); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1591 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1592 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1593 | *pointer = fd; |
| 1594 | return 1; |
| 1595 | } |
| 1596 | |
| 1597 | static PyObject * |
| 1598 | posix_fildes_fd(int fd, int (*func)(int)) |
| 1599 | { |
| 1600 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1601 | int async_err = 0; |
| 1602 | |
| 1603 | do { |
| 1604 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1605 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1606 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1607 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1608 | Py_END_ALLOW_THREADS |
| 1609 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 1610 | if (res != 0) |
| 1611 | return (!async_err) ? posix_error() : NULL; |
| 1612 | Py_RETURN_NONE; |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1613 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1614 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1615 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1616 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1617 | /* This is a reimplementation of the C library's chdir function, |
| 1618 | but one that produces Win32 errors instead of DOS error codes. |
| 1619 | chdir is essentially a wrapper around SetCurrentDirectory; however, |
| 1620 | it also needs to set "magic" environment variables indicating |
| 1621 | the per-drive current directory, which are of the form =<drive>: */ |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1622 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1623 | win32_wchdir(LPCWSTR path) |
| 1624 | { |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1625 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1626 | int result; |
| 1627 | wchar_t env[4] = L"=x:"; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1628 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1629 | if(!SetCurrentDirectoryW(path)) |
| 1630 | return FALSE; |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1631 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1632 | if (!result) |
| 1633 | return FALSE; |
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1634 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1635 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1636 | if (!new_path) { |
| 1637 | SetLastError(ERROR_OUTOFMEMORY); |
| 1638 | return FALSE; |
| 1639 | } |
| 1640 | result = GetCurrentDirectoryW(result, new_path); |
| 1641 | if (!result) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1642 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1643 | return FALSE; |
| 1644 | } |
| 1645 | } |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1646 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
| 1647 | wcsncmp(new_path, L"//", 2) == 0); |
| 1648 | if (!is_unc_like_path) { |
| 1649 | env[1] = new_path[0]; |
| 1650 | result = SetEnvironmentVariableW(env, new_path); |
| 1651 | } |
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1652 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1653 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1654 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1655 | } |
| 1656 | #endif |
| 1657 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1658 | #ifdef MS_WINDOWS |
| 1659 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: |
| 1660 | - time stamps are restricted to second resolution |
| 1661 | - file modification times suffer from forth-and-back conversions between |
| 1662 | UTC and local time |
| 1663 | Therefore, we implement our own stat, based on the Win32 API directly. |
| 1664 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1665 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1666 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1667 | #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1668 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1669 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1670 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
| 1671 | BY_HANDLE_FILE_INFORMATION *info, |
| 1672 | ULONG *reparse_tag) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1673 | { |
| 1674 | memset(info, 0, sizeof(*info)); |
| 1675 | info->dwFileAttributes = pFileData->dwFileAttributes; |
| 1676 | info->ftCreationTime = pFileData->ftCreationTime; |
| 1677 | info->ftLastAccessTime = pFileData->ftLastAccessTime; |
| 1678 | info->ftLastWriteTime = pFileData->ftLastWriteTime; |
| 1679 | info->nFileSizeHigh = pFileData->nFileSizeHigh; |
| 1680 | info->nFileSizeLow = pFileData->nFileSizeLow; |
| 1681 | /* info->nNumberOfLinks = 1; */ |
| 1682 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 1683 | *reparse_tag = pFileData->dwReserved0; |
| 1684 | else |
| 1685 | *reparse_tag = 0; |
| 1686 | } |
| 1687 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1688 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1689 | 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] | 1690 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1691 | HANDLE hFindFile; |
| 1692 | WIN32_FIND_DATAW FileData; |
| 1693 | hFindFile = FindFirstFileW(pszFile, &FileData); |
| 1694 | if (hFindFile == INVALID_HANDLE_VALUE) |
| 1695 | return FALSE; |
| 1696 | FindClose(hFindFile); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1697 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1698 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1701 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1702 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1703 | BOOL traverse) |
| 1704 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1705 | HANDLE hFile; |
| 1706 | BY_HANDLE_FILE_INFORMATION fileInfo; |
| 1707 | FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 }; |
| 1708 | DWORD fileType, error; |
| 1709 | BOOL isUnhandledTag = FALSE; |
| 1710 | int retval = 0; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1711 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1712 | DWORD access = FILE_READ_ATTRIBUTES; |
| 1713 | DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */ |
| 1714 | if (!traverse) { |
| 1715 | flags |= FILE_FLAG_OPEN_REPARSE_POINT; |
| 1716 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1717 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1718 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1719 | if (hFile == INVALID_HANDLE_VALUE) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1720 | /* Either the path doesn't exist, or the caller lacks access. */ |
| 1721 | error = GetLastError(); |
| 1722 | switch (error) { |
| 1723 | case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */ |
| 1724 | case ERROR_SHARING_VIOLATION: /* It's a paging file. */ |
| 1725 | /* Try reading the parent directory. */ |
| 1726 | if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) { |
| 1727 | /* Cannot read the parent directory. */ |
| 1728 | SetLastError(error); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1729 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1730 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1731 | if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1732 | if (traverse || |
| 1733 | !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1734 | /* The stat call has to traverse but cannot, so fail. */ |
| 1735 | SetLastError(error); |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1736 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1737 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1738 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1739 | break; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1740 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1741 | case ERROR_INVALID_PARAMETER: |
| 1742 | /* \\.\con requires read or write access. */ |
| 1743 | hFile = CreateFileW(path, access | GENERIC_READ, |
| 1744 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 1745 | OPEN_EXISTING, flags, NULL); |
| 1746 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1747 | SetLastError(error); |
| 1748 | return -1; |
| 1749 | } |
| 1750 | break; |
| 1751 | |
| 1752 | case ERROR_CANT_ACCESS_FILE: |
| 1753 | /* bpo37834: open unhandled reparse points if traverse fails. */ |
| 1754 | if (traverse) { |
| 1755 | traverse = FALSE; |
| 1756 | isUnhandledTag = TRUE; |
| 1757 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, |
| 1758 | flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 1759 | } |
| 1760 | if (hFile == INVALID_HANDLE_VALUE) { |
| 1761 | SetLastError(error); |
| 1762 | return -1; |
| 1763 | } |
| 1764 | break; |
| 1765 | |
| 1766 | default: |
| 1767 | return -1; |
| 1768 | } |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1769 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1770 | |
| 1771 | if (hFile != INVALID_HANDLE_VALUE) { |
| 1772 | /* Handle types other than files on disk. */ |
| 1773 | fileType = GetFileType(hFile); |
| 1774 | if (fileType != FILE_TYPE_DISK) { |
| 1775 | if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) { |
| 1776 | retval = -1; |
| 1777 | goto cleanup; |
| 1778 | } |
| 1779 | DWORD fileAttributes = GetFileAttributesW(path); |
| 1780 | memset(result, 0, sizeof(*result)); |
| 1781 | if (fileAttributes != INVALID_FILE_ATTRIBUTES && |
| 1782 | fileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 1783 | /* \\.\pipe\ or \\.\mailslot\ */ |
| 1784 | result->st_mode = _S_IFDIR; |
| 1785 | } else if (fileType == FILE_TYPE_CHAR) { |
| 1786 | /* \\.\nul */ |
| 1787 | result->st_mode = _S_IFCHR; |
| 1788 | } else if (fileType == FILE_TYPE_PIPE) { |
| 1789 | /* \\.\pipe\spam */ |
| 1790 | result->st_mode = _S_IFIFO; |
| 1791 | } |
| 1792 | /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */ |
| 1793 | goto cleanup; |
| 1794 | } |
| 1795 | |
| 1796 | /* Query the reparse tag, and traverse a non-link. */ |
| 1797 | if (!traverse) { |
| 1798 | if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, |
| 1799 | &tagInfo, sizeof(tagInfo))) { |
| 1800 | /* Allow devices that do not support FileAttributeTagInfo. */ |
| 1801 | switch (GetLastError()) { |
| 1802 | case ERROR_INVALID_PARAMETER: |
| 1803 | case ERROR_INVALID_FUNCTION: |
| 1804 | case ERROR_NOT_SUPPORTED: |
| 1805 | tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL; |
| 1806 | tagInfo.ReparseTag = 0; |
| 1807 | break; |
| 1808 | default: |
| 1809 | retval = -1; |
| 1810 | goto cleanup; |
| 1811 | } |
| 1812 | } else if (tagInfo.FileAttributes & |
| 1813 | FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1814 | if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { |
| 1815 | if (isUnhandledTag) { |
| 1816 | /* Traversing previously failed for either this link |
| 1817 | or its target. */ |
| 1818 | SetLastError(ERROR_CANT_ACCESS_FILE); |
| 1819 | retval = -1; |
| 1820 | goto cleanup; |
| 1821 | } |
| 1822 | /* Traverse a non-link, but not if traversing already failed |
| 1823 | for an unhandled tag. */ |
| 1824 | } else if (!isUnhandledTag) { |
| 1825 | CloseHandle(hFile); |
| 1826 | return win32_xstat_impl(path, result, TRUE); |
| 1827 | } |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | if (!GetFileInformationByHandle(hFile, &fileInfo)) { |
| 1832 | switch (GetLastError()) { |
| 1833 | case ERROR_INVALID_PARAMETER: |
| 1834 | case ERROR_INVALID_FUNCTION: |
| 1835 | case ERROR_NOT_SUPPORTED: |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1836 | /* Volumes and physical disks are block devices, e.g. |
| 1837 | \\.\C: and \\.\PhysicalDrive0. */ |
| 1838 | memset(result, 0, sizeof(*result)); |
| 1839 | result->st_mode = 0x6000; /* S_IFBLK */ |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1840 | goto cleanup; |
| 1841 | } |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1842 | retval = -1; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1843 | goto cleanup; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result); |
| 1848 | |
| 1849 | if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1850 | /* Fix the file execute permissions. This hack sets S_IEXEC if |
| 1851 | the filename has an extension that is commonly used by files |
| 1852 | that CreateProcessW can execute. A real implementation calls |
| 1853 | GetSecurityInfo, OpenThreadToken/OpenProcessToken, and |
| 1854 | AccessCheck to check for generic read, write, and execute |
| 1855 | access. */ |
| 1856 | const wchar_t *fileExtension = wcsrchr(path, '.'); |
| 1857 | if (fileExtension) { |
| 1858 | if (_wcsicmp(fileExtension, L".exe") == 0 || |
| 1859 | _wcsicmp(fileExtension, L".bat") == 0 || |
| 1860 | _wcsicmp(fileExtension, L".cmd") == 0 || |
| 1861 | _wcsicmp(fileExtension, L".com") == 0) { |
| 1862 | result->st_mode |= 0111; |
| 1863 | } |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | cleanup: |
| 1868 | if (hFile != INVALID_HANDLE_VALUE) { |
Steve Dower | 772ec0f | 2019-09-04 14:42:54 -0700 | [diff] [blame] | 1869 | /* Preserve last error if we are failing */ |
| 1870 | error = retval ? GetLastError() : 0; |
| 1871 | if (!CloseHandle(hFile)) { |
| 1872 | retval = -1; |
| 1873 | } else if (retval) { |
| 1874 | /* Restore last error */ |
| 1875 | SetLastError(error); |
| 1876 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | return retval; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1883 | 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] | 1884 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1885 | /* Protocol violation: we explicitly clear errno, instead of |
| 1886 | setting it to a POSIX error. Callers should use GetLastError. */ |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1887 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1888 | errno = 0; |
| 1889 | return code; |
| 1890 | } |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1891 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1892 | |
| 1893 | In Posix, stat automatically traverses symlinks and returns the stat |
| 1894 | structure for the target. In Windows, the equivalent GetFileAttributes by |
| 1895 | default does not traverse symlinks and instead returns attributes for |
| 1896 | the symlink. |
| 1897 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1898 | Instead, we will open the file (which *does* traverse symlinks by default) |
| 1899 | and GetFileInformationByHandle(). */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1900 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1901 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1902 | 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] | 1903 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1904 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1907 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1908 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1909 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1910 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1913 | #endif /* MS_WINDOWS */ |
| 1914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1915 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1916 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1917 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1918 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1919 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
| 1920 | \n\ |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1921 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
| 1922 | or st_flags, they are available as attributes only.\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1923 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1924 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1925 | |
| 1926 | static PyStructSequence_Field stat_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1927 | {"st_mode", "protection bits"}, |
| 1928 | {"st_ino", "inode"}, |
| 1929 | {"st_dev", "device"}, |
| 1930 | {"st_nlink", "number of hard links"}, |
| 1931 | {"st_uid", "user ID of owner"}, |
| 1932 | {"st_gid", "group ID of owner"}, |
| 1933 | {"st_size", "total size, in bytes"}, |
| 1934 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ |
| 1935 | {NULL, "integer time of last access"}, |
| 1936 | {NULL, "integer time of last modification"}, |
| 1937 | {NULL, "integer time of last change"}, |
| 1938 | {"st_atime", "time of last access"}, |
| 1939 | {"st_mtime", "time of last modification"}, |
| 1940 | {"st_ctime", "time of last change"}, |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1941 | {"st_atime_ns", "time of last access in nanoseconds"}, |
| 1942 | {"st_mtime_ns", "time of last modification in nanoseconds"}, |
| 1943 | {"st_ctime_ns", "time of last change in nanoseconds"}, |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1944 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1945 | {"st_blksize", "blocksize for filesystem I/O"}, |
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_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1948 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1949 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1950 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1951 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1952 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1953 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1954 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1955 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1956 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1957 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1958 | #endif |
| 1959 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1960 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1961 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1962 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 1963 | {"st_file_attributes", "Windows file attribute bits"}, |
| 1964 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1965 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 1966 | {"st_fstype", "Type of filesystem"}, |
| 1967 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 1968 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 1969 | {"st_reparse_tag", "Windows reparse tag"}, |
| 1970 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1971 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1972 | }; |
| 1973 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1974 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1975 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1976 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1977 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1978 | #endif |
| 1979 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1980 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1981 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
| 1982 | #else |
| 1983 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX |
| 1984 | #endif |
| 1985 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1986 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1987 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
| 1988 | #else |
| 1989 | #define ST_RDEV_IDX ST_BLOCKS_IDX |
| 1990 | #endif |
| 1991 | |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1992 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
| 1993 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) |
| 1994 | #else |
| 1995 | #define ST_FLAGS_IDX ST_RDEV_IDX |
| 1996 | #endif |
| 1997 | |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1998 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1999 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2000 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 2001 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2002 | #endif |
| 2003 | |
| 2004 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
| 2005 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) |
| 2006 | #else |
| 2007 | #define ST_BIRTHTIME_IDX ST_GEN_IDX |
| 2008 | #endif |
| 2009 | |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2010 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2011 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) |
| 2012 | #else |
| 2013 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX |
| 2014 | #endif |
| 2015 | |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2016 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2017 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) |
| 2018 | #else |
| 2019 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX |
| 2020 | #endif |
| 2021 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2022 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2023 | #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1) |
| 2024 | #else |
| 2025 | #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX |
| 2026 | #endif |
| 2027 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2028 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2029 | "stat_result", /* name */ |
| 2030 | stat_result__doc__, /* doc */ |
| 2031 | stat_result_fields, |
| 2032 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2033 | }; |
| 2034 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2035 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2036 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
| 2037 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 2038 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 2039 | 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] | 2040 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2041 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2042 | |
| 2043 | static PyStructSequence_Field statvfs_result_fields[] = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2044 | {"f_bsize", }, |
| 2045 | {"f_frsize", }, |
| 2046 | {"f_blocks", }, |
| 2047 | {"f_bfree", }, |
| 2048 | {"f_bavail", }, |
| 2049 | {"f_files", }, |
| 2050 | {"f_ffree", }, |
| 2051 | {"f_favail", }, |
| 2052 | {"f_flag", }, |
| 2053 | {"f_namemax",}, |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 2054 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2055 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2056 | }; |
| 2057 | |
| 2058 | static PyStructSequence_Desc statvfs_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2059 | "statvfs_result", /* name */ |
| 2060 | statvfs_result__doc__, /* doc */ |
| 2061 | statvfs_result_fields, |
| 2062 | 10 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2063 | }; |
| 2064 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2065 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2066 | PyDoc_STRVAR(waitid_result__doc__, |
| 2067 | "waitid_result: Result from waitid.\n\n\ |
| 2068 | This object may be accessed either as a tuple of\n\ |
| 2069 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ |
| 2070 | or via the attributes si_pid, si_uid, and so on.\n\ |
| 2071 | \n\ |
| 2072 | See os.waitid for more information."); |
| 2073 | |
| 2074 | static PyStructSequence_Field waitid_result_fields[] = { |
| 2075 | {"si_pid", }, |
| 2076 | {"si_uid", }, |
| 2077 | {"si_signo", }, |
| 2078 | {"si_status", }, |
| 2079 | {"si_code", }, |
| 2080 | {0} |
| 2081 | }; |
| 2082 | |
| 2083 | static PyStructSequence_Desc waitid_result_desc = { |
| 2084 | "waitid_result", /* name */ |
| 2085 | waitid_result__doc__, /* doc */ |
| 2086 | waitid_result_fields, |
| 2087 | 5 |
| 2088 | }; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2089 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2090 | static newfunc structseq_new; |
| 2091 | |
| 2092 | static PyObject * |
| 2093 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2094 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2095 | PyStructSequence *result; |
| 2096 | int i; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2097 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2098 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
| 2099 | if (!result) |
| 2100 | return NULL; |
| 2101 | /* If we have been initialized from a tuple, |
| 2102 | st_?time might be set to None. Initialize it |
| 2103 | from the int slots. */ |
| 2104 | for (i = 7; i <= 9; i++) { |
| 2105 | if (result->ob_item[i+3] == Py_None) { |
| 2106 | Py_DECREF(Py_None); |
| 2107 | Py_INCREF(result->ob_item[i]); |
| 2108 | result->ob_item[i+3] = result->ob_item[i]; |
| 2109 | } |
| 2110 | } |
| 2111 | return (PyObject*)result; |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2114 | static int |
| 2115 | _posix_clear(PyObject *module) |
| 2116 | { |
| 2117 | Py_CLEAR(_posixstate(module)->billion); |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 2118 | #ifdef PY_PUTENV_DICT |
| 2119 | Py_CLEAR(_posixstate(module)->putenv_dict); |
| 2120 | #endif |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2121 | Py_CLEAR(_posixstate(module)->DirEntryType); |
| 2122 | Py_CLEAR(_posixstate(module)->ScandirIteratorType); |
| 2123 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 2124 | Py_CLEAR(_posixstate(module)->SchedParamType); |
| 2125 | #endif |
| 2126 | Py_CLEAR(_posixstate(module)->StatResultType); |
| 2127 | Py_CLEAR(_posixstate(module)->StatVFSResultType); |
| 2128 | Py_CLEAR(_posixstate(module)->TerminalSizeType); |
| 2129 | Py_CLEAR(_posixstate(module)->TimesResultType); |
| 2130 | Py_CLEAR(_posixstate(module)->UnameResultType); |
| 2131 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2132 | Py_CLEAR(_posixstate(module)->WaitidResultType); |
| 2133 | #endif |
| 2134 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 2135 | Py_CLEAR(_posixstate(module)->struct_rusage); |
| 2136 | #endif |
| 2137 | Py_CLEAR(_posixstate(module)->st_mode); |
| 2138 | return 0; |
| 2139 | } |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2140 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2141 | static int |
| 2142 | _posix_traverse(PyObject *module, visitproc visit, void *arg) |
| 2143 | { |
| 2144 | Py_VISIT(_posixstate(module)->billion); |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 2145 | #ifdef PY_PUTENV_DICT |
| 2146 | Py_VISIT(_posixstate(module)->putenv_dict); |
| 2147 | #endif |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2148 | Py_VISIT(_posixstate(module)->DirEntryType); |
| 2149 | Py_VISIT(_posixstate(module)->ScandirIteratorType); |
| 2150 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
| 2151 | Py_VISIT(_posixstate(module)->SchedParamType); |
| 2152 | #endif |
| 2153 | Py_VISIT(_posixstate(module)->StatResultType); |
| 2154 | Py_VISIT(_posixstate(module)->StatVFSResultType); |
| 2155 | Py_VISIT(_posixstate(module)->TerminalSizeType); |
| 2156 | Py_VISIT(_posixstate(module)->TimesResultType); |
| 2157 | Py_VISIT(_posixstate(module)->UnameResultType); |
| 2158 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 2159 | Py_VISIT(_posixstate(module)->WaitidResultType); |
| 2160 | #endif |
| 2161 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 2162 | Py_VISIT(_posixstate(module)->struct_rusage); |
| 2163 | #endif |
| 2164 | Py_VISIT(_posixstate(module)->st_mode); |
| 2165 | return 0; |
| 2166 | } |
| 2167 | |
| 2168 | static void |
| 2169 | _posix_free(void *module) |
| 2170 | { |
| 2171 | _posix_clear((PyObject *)module); |
| 2172 | } |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2173 | |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2174 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2175 | 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] | 2176 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2177 | PyObject *s = _PyLong_FromTime_t(sec); |
| 2178 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); |
| 2179 | PyObject *s_in_ns = NULL; |
| 2180 | PyObject *ns_total = NULL; |
| 2181 | PyObject *float_s = NULL; |
| 2182 | |
| 2183 | if (!(s && ns_fractional)) |
| 2184 | goto exit; |
| 2185 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2186 | s_in_ns = PyNumber_Multiply(s, _posixstate_global->billion); |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2187 | if (!s_in_ns) |
| 2188 | goto exit; |
| 2189 | |
| 2190 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); |
| 2191 | if (!ns_total) |
| 2192 | goto exit; |
| 2193 | |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2194 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
| 2195 | if (!float_s) { |
| 2196 | goto exit; |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | PyStructSequence_SET_ITEM(v, index, s); |
| 2200 | PyStructSequence_SET_ITEM(v, index+3, float_s); |
| 2201 | PyStructSequence_SET_ITEM(v, index+6, ns_total); |
| 2202 | s = NULL; |
| 2203 | float_s = NULL; |
| 2204 | ns_total = NULL; |
| 2205 | exit: |
| 2206 | Py_XDECREF(s); |
| 2207 | Py_XDECREF(ns_fractional); |
| 2208 | Py_XDECREF(s_in_ns); |
| 2209 | Py_XDECREF(ns_total); |
| 2210 | Py_XDECREF(float_s); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2213 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2214 | (used by posix_stat() and posix_fstat()) */ |
| 2215 | static PyObject* |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2216 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2217 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2218 | unsigned long ansec, mnsec, cnsec; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 2219 | PyObject *StatResultType = _posixstate_global->StatResultType; |
| 2220 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2221 | if (v == NULL) |
| 2222 | return NULL; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2223 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2224 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2225 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2226 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2227 | #ifdef MS_WINDOWS |
| 2228 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2229 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2230 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2231 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2232 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2233 | #if defined(MS_WINDOWS) |
| 2234 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); |
| 2235 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); |
| 2236 | #else |
| 2237 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); |
| 2238 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); |
| 2239 | #endif |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2240 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
| 2241 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); |
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2242 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2243 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2244 | ansec = st->st_atim.tv_nsec; |
| 2245 | mnsec = st->st_mtim.tv_nsec; |
| 2246 | cnsec = st->st_ctim.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2247 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2248 | ansec = st->st_atimespec.tv_nsec; |
| 2249 | mnsec = st->st_mtimespec.tv_nsec; |
| 2250 | cnsec = st->st_ctimespec.tv_nsec; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2251 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2252 | ansec = st->st_atime_nsec; |
| 2253 | mnsec = st->st_mtime_nsec; |
| 2254 | cnsec = st->st_ctime_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2255 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2256 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2257 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2258 | fill_time(v, 7, st->st_atime, ansec); |
| 2259 | fill_time(v, 8, st->st_mtime, mnsec); |
| 2260 | fill_time(v, 9, st->st_ctime, cnsec); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2261 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2262 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2263 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
| 2264 | PyLong_FromLong((long)st->st_blksize)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2265 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2266 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2267 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
| 2268 | PyLong_FromLong((long)st->st_blocks)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2269 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2270 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2271 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
| 2272 | PyLong_FromLong((long)st->st_rdev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2273 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2274 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2275 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
| 2276 | PyLong_FromLong((long)st->st_gen)); |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2277 | #endif |
| 2278 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2279 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2280 | PyObject *val; |
| 2281 | unsigned long bsec,bnsec; |
| 2282 | bsec = (long)st->st_birthtime; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2283 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2284 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2285 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2286 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2287 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2288 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2289 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
| 2290 | val); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2291 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2292 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2293 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2294 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
| 2295 | PyLong_FromLong((long)st->st_flags)); |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2296 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2297 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
| 2298 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, |
| 2299 | PyLong_FromUnsignedLong(st->st_file_attributes)); |
| 2300 | #endif |
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2301 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
| 2302 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, |
| 2303 | PyUnicode_FromString(st->st_fstype)); |
| 2304 | #endif |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 2305 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
| 2306 | PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX, |
| 2307 | PyLong_FromUnsignedLong(st->st_reparse_tag)); |
| 2308 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2309 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2310 | if (PyErr_Occurred()) { |
| 2311 | Py_DECREF(v); |
| 2312 | return NULL; |
| 2313 | } |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2314 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2315 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2318 | /* POSIX methods */ |
| 2319 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2320 | |
| 2321 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2322 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2323 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2324 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2325 | STRUCT_STAT st; |
| 2326 | int result; |
| 2327 | |
| 2328 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) |
| 2329 | if (follow_symlinks_specified(function_name, follow_symlinks)) |
| 2330 | return NULL; |
| 2331 | #endif |
| 2332 | |
| 2333 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || |
| 2334 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || |
| 2335 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) |
| 2336 | return NULL; |
| 2337 | |
| 2338 | Py_BEGIN_ALLOW_THREADS |
| 2339 | if (path->fd != -1) |
| 2340 | result = FSTAT(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2341 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2342 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2343 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2344 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2345 | result = win32_lstat(path->wide, &st); |
| 2346 | #else |
| 2347 | else |
| 2348 | #if defined(HAVE_LSTAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2349 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
| 2350 | result = LSTAT(path->narrow, &st); |
| 2351 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2352 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2353 | #ifdef HAVE_FSTATAT |
| 2354 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) |
| 2355 | result = fstatat(dir_fd, path->narrow, &st, |
| 2356 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 2357 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2358 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2359 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2360 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2361 | Py_END_ALLOW_THREADS |
| 2362 | |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2363 | if (result != 0) { |
| 2364 | return path_error(path); |
| 2365 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2366 | |
| 2367 | return _pystat_fromstructstat(&st); |
| 2368 | } |
| 2369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2370 | /*[python input] |
| 2371 | |
| 2372 | for s in """ |
| 2373 | |
| 2374 | FACCESSAT |
| 2375 | FCHMODAT |
| 2376 | FCHOWNAT |
| 2377 | FSTATAT |
| 2378 | LINKAT |
| 2379 | MKDIRAT |
| 2380 | MKFIFOAT |
| 2381 | MKNODAT |
| 2382 | OPENAT |
| 2383 | READLINKAT |
| 2384 | SYMLINKAT |
| 2385 | UNLINKAT |
| 2386 | |
| 2387 | """.strip().split(): |
| 2388 | s = s.strip() |
| 2389 | print(""" |
| 2390 | #ifdef HAVE_{s} |
| 2391 | #define {s}_DIR_FD_CONVERTER dir_fd_converter |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2392 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2393 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2394 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2395 | """.rstrip().format(s=s)) |
| 2396 | |
| 2397 | for s in """ |
| 2398 | |
| 2399 | FCHDIR |
| 2400 | FCHMOD |
| 2401 | FCHOWN |
| 2402 | FDOPENDIR |
| 2403 | FEXECVE |
| 2404 | FPATHCONF |
| 2405 | FSTATVFS |
| 2406 | FTRUNCATE |
| 2407 | |
| 2408 | """.strip().split(): |
| 2409 | s = s.strip() |
| 2410 | print(""" |
| 2411 | #ifdef HAVE_{s} |
| 2412 | #define PATH_HAVE_{s} 1 |
| 2413 | #else |
| 2414 | #define PATH_HAVE_{s} 0 |
| 2415 | #endif |
| 2416 | |
| 2417 | """.rstrip().format(s=s)) |
| 2418 | [python start generated code]*/ |
| 2419 | |
| 2420 | #ifdef HAVE_FACCESSAT |
| 2421 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter |
| 2422 | #else |
| 2423 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2424 | #endif |
| 2425 | |
| 2426 | #ifdef HAVE_FCHMODAT |
| 2427 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2428 | #else |
| 2429 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2430 | #endif |
| 2431 | |
| 2432 | #ifdef HAVE_FCHOWNAT |
| 2433 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter |
| 2434 | #else |
| 2435 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2436 | #endif |
| 2437 | |
| 2438 | #ifdef HAVE_FSTATAT |
| 2439 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter |
| 2440 | #else |
| 2441 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2442 | #endif |
| 2443 | |
| 2444 | #ifdef HAVE_LINKAT |
| 2445 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2446 | #else |
| 2447 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2448 | #endif |
| 2449 | |
| 2450 | #ifdef HAVE_MKDIRAT |
| 2451 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter |
| 2452 | #else |
| 2453 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2454 | #endif |
| 2455 | |
| 2456 | #ifdef HAVE_MKFIFOAT |
| 2457 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter |
| 2458 | #else |
| 2459 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2460 | #endif |
| 2461 | |
| 2462 | #ifdef HAVE_MKNODAT |
| 2463 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter |
| 2464 | #else |
| 2465 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2466 | #endif |
| 2467 | |
| 2468 | #ifdef HAVE_OPENAT |
| 2469 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter |
| 2470 | #else |
| 2471 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2472 | #endif |
| 2473 | |
| 2474 | #ifdef HAVE_READLINKAT |
| 2475 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2476 | #else |
| 2477 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2478 | #endif |
| 2479 | |
| 2480 | #ifdef HAVE_SYMLINKAT |
| 2481 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2482 | #else |
| 2483 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2484 | #endif |
| 2485 | |
| 2486 | #ifdef HAVE_UNLINKAT |
| 2487 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter |
| 2488 | #else |
| 2489 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable |
| 2490 | #endif |
| 2491 | |
| 2492 | #ifdef HAVE_FCHDIR |
| 2493 | #define PATH_HAVE_FCHDIR 1 |
| 2494 | #else |
| 2495 | #define PATH_HAVE_FCHDIR 0 |
| 2496 | #endif |
| 2497 | |
| 2498 | #ifdef HAVE_FCHMOD |
| 2499 | #define PATH_HAVE_FCHMOD 1 |
| 2500 | #else |
| 2501 | #define PATH_HAVE_FCHMOD 0 |
| 2502 | #endif |
| 2503 | |
| 2504 | #ifdef HAVE_FCHOWN |
| 2505 | #define PATH_HAVE_FCHOWN 1 |
| 2506 | #else |
| 2507 | #define PATH_HAVE_FCHOWN 0 |
| 2508 | #endif |
| 2509 | |
| 2510 | #ifdef HAVE_FDOPENDIR |
| 2511 | #define PATH_HAVE_FDOPENDIR 1 |
| 2512 | #else |
| 2513 | #define PATH_HAVE_FDOPENDIR 0 |
| 2514 | #endif |
| 2515 | |
| 2516 | #ifdef HAVE_FEXECVE |
| 2517 | #define PATH_HAVE_FEXECVE 1 |
| 2518 | #else |
| 2519 | #define PATH_HAVE_FEXECVE 0 |
| 2520 | #endif |
| 2521 | |
| 2522 | #ifdef HAVE_FPATHCONF |
| 2523 | #define PATH_HAVE_FPATHCONF 1 |
| 2524 | #else |
| 2525 | #define PATH_HAVE_FPATHCONF 0 |
| 2526 | #endif |
| 2527 | |
| 2528 | #ifdef HAVE_FSTATVFS |
| 2529 | #define PATH_HAVE_FSTATVFS 1 |
| 2530 | #else |
| 2531 | #define PATH_HAVE_FSTATVFS 0 |
| 2532 | #endif |
| 2533 | |
| 2534 | #ifdef HAVE_FTRUNCATE |
| 2535 | #define PATH_HAVE_FTRUNCATE 1 |
| 2536 | #else |
| 2537 | #define PATH_HAVE_FTRUNCATE 0 |
| 2538 | #endif |
| 2539 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2540 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2541 | #ifdef MS_WINDOWS |
| 2542 | #undef PATH_HAVE_FTRUNCATE |
| 2543 | #define PATH_HAVE_FTRUNCATE 1 |
| 2544 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2545 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2546 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2547 | |
| 2548 | class path_t_converter(CConverter): |
| 2549 | |
| 2550 | type = "path_t" |
| 2551 | impl_by_reference = True |
| 2552 | parse_by_reference = True |
| 2553 | |
| 2554 | converter = 'path_converter' |
| 2555 | |
| 2556 | def converter_init(self, *, allow_fd=False, nullable=False): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2557 | # right now path_t doesn't support default values. |
| 2558 | # to support a default value, you'll need to override initialize(). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2559 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2560 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2561 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2562 | if self.c_default not in (None, 'Py_None'): |
| 2563 | 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] | 2564 | |
| 2565 | self.nullable = nullable |
| 2566 | self.allow_fd = allow_fd |
| 2567 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2568 | def pre_render(self): |
| 2569 | def strify(value): |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2570 | if isinstance(value, str): |
| 2571 | return value |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2572 | return str(int(bool(value))) |
| 2573 | |
| 2574 | # add self.py_name here when merging with posixmodule conversion |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2575 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2576 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2577 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2578 | strify(self.nullable), |
| 2579 | strify(self.allow_fd), |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2580 | ) |
| 2581 | |
| 2582 | def cleanup(self): |
| 2583 | return "path_cleanup(&" + self.name + ");\n" |
| 2584 | |
| 2585 | |
| 2586 | class dir_fd_converter(CConverter): |
| 2587 | type = 'int' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2588 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2589 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2590 | if self.default in (unspecified, None): |
| 2591 | self.c_default = 'DEFAULT_DIR_FD' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2592 | if isinstance(requires, str): |
| 2593 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' |
| 2594 | else: |
| 2595 | self.converter = 'dir_fd_converter' |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2596 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2597 | class fildes_converter(CConverter): |
| 2598 | type = 'int' |
| 2599 | converter = 'fildes_converter' |
| 2600 | |
| 2601 | class uid_t_converter(CConverter): |
| 2602 | type = "uid_t" |
| 2603 | converter = '_Py_Uid_Converter' |
| 2604 | |
| 2605 | class gid_t_converter(CConverter): |
| 2606 | type = "gid_t" |
| 2607 | converter = '_Py_Gid_Converter' |
| 2608 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2609 | class dev_t_converter(CConverter): |
| 2610 | type = 'dev_t' |
| 2611 | converter = '_Py_Dev_Converter' |
| 2612 | |
| 2613 | class dev_t_return_converter(unsigned_long_return_converter): |
| 2614 | type = 'dev_t' |
| 2615 | conversion_fn = '_PyLong_FromDev' |
| 2616 | unsigned_cast = '(dev_t)' |
| 2617 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2618 | class FSConverter_converter(CConverter): |
| 2619 | type = 'PyObject *' |
| 2620 | converter = 'PyUnicode_FSConverter' |
| 2621 | def converter_init(self): |
| 2622 | if self.default is not unspecified: |
| 2623 | fail("FSConverter_converter does not support default values") |
| 2624 | self.c_default = 'NULL' |
| 2625 | |
| 2626 | def cleanup(self): |
| 2627 | return "Py_XDECREF(" + self.name + ");\n" |
| 2628 | |
| 2629 | class pid_t_converter(CConverter): |
| 2630 | type = 'pid_t' |
| 2631 | format_unit = '" _Py_PARSE_PID "' |
| 2632 | |
| 2633 | class idtype_t_converter(int_converter): |
| 2634 | type = 'idtype_t' |
| 2635 | |
| 2636 | class id_t_converter(CConverter): |
| 2637 | type = 'id_t' |
| 2638 | format_unit = '" _Py_PARSE_PID "' |
| 2639 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2640 | class intptr_t_converter(CConverter): |
| 2641 | type = 'intptr_t' |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2642 | format_unit = '" _Py_PARSE_INTPTR "' |
| 2643 | |
| 2644 | class Py_off_t_converter(CConverter): |
| 2645 | type = 'Py_off_t' |
| 2646 | converter = 'Py_off_t_converter' |
| 2647 | |
| 2648 | class Py_off_t_return_converter(long_return_converter): |
| 2649 | type = 'Py_off_t' |
| 2650 | conversion_fn = 'PyLong_FromPy_off_t' |
| 2651 | |
| 2652 | class path_confname_converter(CConverter): |
| 2653 | type="int" |
| 2654 | converter="conv_path_confname" |
| 2655 | |
| 2656 | class confstr_confname_converter(path_confname_converter): |
| 2657 | converter='conv_confstr_confname' |
| 2658 | |
| 2659 | class sysconf_confname_converter(path_confname_converter): |
| 2660 | converter="conv_sysconf_confname" |
| 2661 | |
| 2662 | class sched_param_converter(CConverter): |
| 2663 | type = 'struct sched_param' |
| 2664 | converter = 'convert_sched_param' |
| 2665 | impl_by_reference = True; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2666 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2667 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2668 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2669 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2670 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2671 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2672 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2673 | |
| 2674 | path : path_t(allow_fd=True) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2675 | 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] | 2676 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2677 | |
| 2678 | * |
| 2679 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2680 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2681 | If not None, it should be a file descriptor open to a directory, |
| 2682 | and path should be a relative string; path will then be relative to |
| 2683 | that directory. |
| 2684 | |
| 2685 | follow_symlinks: bool = True |
| 2686 | If False, and the last element of the path is a symbolic link, |
| 2687 | stat will examine the symbolic link itself instead of the file |
| 2688 | the link points to. |
| 2689 | |
| 2690 | Perform a stat system call on the given path. |
| 2691 | |
| 2692 | dir_fd and follow_symlinks may not be implemented |
| 2693 | on your platform. If they are unavailable, using them will raise a |
| 2694 | NotImplementedError. |
| 2695 | |
| 2696 | It's an error to use dir_fd or follow_symlinks when specifying path as |
| 2697 | an open file descriptor. |
| 2698 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2699 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2700 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2701 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2702 | 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] | 2703 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2704 | { |
| 2705 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2706 | } |
| 2707 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2708 | |
| 2709 | /*[clinic input] |
| 2710 | os.lstat |
| 2711 | |
| 2712 | path : path_t |
| 2713 | |
| 2714 | * |
| 2715 | |
| 2716 | dir_fd : dir_fd(requires='fstatat') = None |
| 2717 | |
| 2718 | Perform a stat system call on the given path, without following symbolic links. |
| 2719 | |
| 2720 | Like stat(), but do not follow symbolic links. |
| 2721 | Equivalent to stat(path, follow_symlinks=False). |
| 2722 | [clinic start generated code]*/ |
| 2723 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2724 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2725 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
| 2726 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2727 | { |
| 2728 | int follow_symlinks = 0; |
| 2729 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2730 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2731 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2732 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2733 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2734 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2735 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2736 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2737 | 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] | 2738 | |
| 2739 | mode: int |
| 2740 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2741 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2742 | |
| 2743 | * |
| 2744 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2745 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2746 | If not None, it should be a file descriptor open to a directory, |
| 2747 | and path should be relative; path will then be relative to that |
| 2748 | directory. |
| 2749 | |
| 2750 | effective_ids: bool = False |
| 2751 | If True, access will use the effective uid/gid instead of |
| 2752 | the real uid/gid. |
| 2753 | |
| 2754 | follow_symlinks: bool = True |
| 2755 | If False, and the last element of the path is a symbolic link, |
| 2756 | access will examine the symbolic link itself instead of the file |
| 2757 | the link points to. |
| 2758 | |
| 2759 | Use the real uid/gid to test for access to a path. |
| 2760 | |
| 2761 | {parameters} |
| 2762 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2763 | on your platform. If they are unavailable, using them will raise a |
| 2764 | NotImplementedError. |
| 2765 | |
| 2766 | Note that most operations will use the effective uid/gid, therefore this |
| 2767 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2768 | has the specified access to the path. |
| 2769 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2770 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2771 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2772 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2773 | 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] | 2774 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2775 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2776 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2777 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2778 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2779 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2780 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2781 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2782 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2783 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2784 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2785 | #ifndef HAVE_FACCESSAT |
| 2786 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2787 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2788 | |
| 2789 | if (effective_ids) { |
| 2790 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2791 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2792 | } |
| 2793 | #endif |
| 2794 | |
| 2795 | #ifdef MS_WINDOWS |
| 2796 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2797 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2798 | Py_END_ALLOW_THREADS |
| 2799 | |
| 2800 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2801 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2802 | * * we didn't get a -1, and |
| 2803 | * * write access wasn't requested, |
| 2804 | * * or the file isn't read-only, |
| 2805 | * * or it's a directory. |
| 2806 | * (Directories cannot be read-only on Windows.) |
| 2807 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2808 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2809 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2810 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2811 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2812 | #else |
| 2813 | |
| 2814 | Py_BEGIN_ALLOW_THREADS |
| 2815 | #ifdef HAVE_FACCESSAT |
| 2816 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2817 | effective_ids || |
| 2818 | !follow_symlinks) { |
| 2819 | int flags = 0; |
| 2820 | if (!follow_symlinks) |
| 2821 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2822 | if (effective_ids) |
| 2823 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2824 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2825 | } |
| 2826 | else |
| 2827 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2828 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2829 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2830 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2831 | #endif |
| 2832 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2833 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2836 | #ifndef F_OK |
| 2837 | #define F_OK 0 |
| 2838 | #endif |
| 2839 | #ifndef R_OK |
| 2840 | #define R_OK 4 |
| 2841 | #endif |
| 2842 | #ifndef W_OK |
| 2843 | #define W_OK 2 |
| 2844 | #endif |
| 2845 | #ifndef X_OK |
| 2846 | #define X_OK 1 |
| 2847 | #endif |
| 2848 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2849 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2850 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2851 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2852 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2853 | |
| 2854 | fd: int |
| 2855 | Integer file descriptor handle. |
| 2856 | |
| 2857 | / |
| 2858 | |
| 2859 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2860 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2861 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2862 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2863 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2864 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2865 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2866 | |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2867 | long size = sysconf(_SC_TTY_NAME_MAX); |
| 2868 | if (size == -1) { |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2869 | return posix_error(); |
| 2870 | } |
Antonio Gutierrez | 594e2ed | 2019-10-09 04:19:48 +0200 | [diff] [blame] | 2871 | char *buffer = (char *)PyMem_RawMalloc(size); |
| 2872 | if (buffer == NULL) { |
| 2873 | return PyErr_NoMemory(); |
| 2874 | } |
| 2875 | int ret = ttyname_r(fd, buffer, size); |
| 2876 | if (ret != 0) { |
| 2877 | PyMem_RawFree(buffer); |
| 2878 | errno = ret; |
| 2879 | return posix_error(); |
| 2880 | } |
| 2881 | PyObject *res = PyUnicode_DecodeFSDefault(buffer); |
| 2882 | PyMem_RawFree(buffer); |
| 2883 | return res; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2884 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2885 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2886 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2887 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2888 | /*[clinic input] |
| 2889 | os.ctermid |
| 2890 | |
| 2891 | Return the name of the controlling terminal for this process. |
| 2892 | [clinic start generated code]*/ |
| 2893 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2894 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2895 | os_ctermid_impl(PyObject *module) |
| 2896 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2897 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2898 | char *ret; |
| 2899 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2900 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2901 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2902 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2903 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2904 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2905 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2906 | if (ret == NULL) |
| 2907 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2908 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2909 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2910 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2911 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2912 | |
| 2913 | /*[clinic input] |
| 2914 | os.chdir |
| 2915 | |
| 2916 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2917 | |
| 2918 | Change the current working directory to the specified path. |
| 2919 | |
| 2920 | path may always be specified as a string. |
| 2921 | On some platforms, path may also be specified as an open file descriptor. |
| 2922 | If this functionality is unavailable, using it raises an exception. |
| 2923 | [clinic start generated code]*/ |
| 2924 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2925 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2926 | os_chdir_impl(PyObject *module, path_t *path) |
| 2927 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2928 | { |
| 2929 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2930 | |
| 2931 | Py_BEGIN_ALLOW_THREADS |
| 2932 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2933 | /* on unix, success = 0, on windows, success = !0 */ |
| 2934 | result = !win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2935 | #else |
| 2936 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2937 | if (path->fd != -1) |
| 2938 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2939 | else |
| 2940 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2941 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2942 | #endif |
| 2943 | Py_END_ALLOW_THREADS |
| 2944 | |
| 2945 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2946 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2947 | } |
| 2948 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2949 | Py_RETURN_NONE; |
| 2950 | } |
| 2951 | |
| 2952 | |
| 2953 | #ifdef HAVE_FCHDIR |
| 2954 | /*[clinic input] |
| 2955 | os.fchdir |
| 2956 | |
| 2957 | fd: fildes |
| 2958 | |
| 2959 | Change to the directory of the given file descriptor. |
| 2960 | |
| 2961 | fd must be opened on a directory, not a file. |
| 2962 | Equivalent to os.chdir(fd). |
| 2963 | |
| 2964 | [clinic start generated code]*/ |
| 2965 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2966 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2967 | os_fchdir_impl(PyObject *module, int fd) |
| 2968 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2969 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2970 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2971 | } |
| 2972 | #endif /* HAVE_FCHDIR */ |
| 2973 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2974 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2975 | /*[clinic input] |
| 2976 | os.chmod |
| 2977 | |
| 2978 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2979 | 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] | 2980 | On some platforms, path may also be specified as an open file descriptor. |
| 2981 | If this functionality is unavailable, using it raises an exception. |
| 2982 | |
| 2983 | mode: int |
| 2984 | Operating-system mode bitfield. |
| 2985 | |
| 2986 | * |
| 2987 | |
| 2988 | dir_fd : dir_fd(requires='fchmodat') = None |
| 2989 | If not None, it should be a file descriptor open to a directory, |
| 2990 | and path should be relative; path will then be relative to that |
| 2991 | directory. |
| 2992 | |
| 2993 | follow_symlinks: bool = True |
| 2994 | If False, and the last element of the path is a symbolic link, |
| 2995 | chmod will modify the symbolic link itself instead of the file |
| 2996 | the link points to. |
| 2997 | |
| 2998 | Change the access permissions of a file. |
| 2999 | |
| 3000 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3001 | an open file descriptor. |
| 3002 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3003 | If they are unavailable, using them will raise a NotImplementedError. |
| 3004 | |
| 3005 | [clinic start generated code]*/ |
| 3006 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3007 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3008 | 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] | 3009 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3010 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3011 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3012 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3013 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3014 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3015 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3016 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3017 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3018 | #ifdef HAVE_FCHMODAT |
| 3019 | int fchmodat_nofollow_unsupported = 0; |
| 3020 | #endif |
| 3021 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3022 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 3023 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3024 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3025 | #endif |
| 3026 | |
| 3027 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3028 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3029 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 3030 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3031 | result = 0; |
| 3032 | else { |
| 3033 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3034 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 3035 | else |
| 3036 | attr |= FILE_ATTRIBUTE_READONLY; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3037 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3038 | } |
| 3039 | Py_END_ALLOW_THREADS |
| 3040 | |
| 3041 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3042 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3043 | } |
| 3044 | #else /* MS_WINDOWS */ |
| 3045 | Py_BEGIN_ALLOW_THREADS |
| 3046 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3047 | if (path->fd != -1) |
| 3048 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3049 | else |
| 3050 | #endif |
| 3051 | #ifdef HAVE_LCHMOD |
| 3052 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3053 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3054 | else |
| 3055 | #endif |
| 3056 | #ifdef HAVE_FCHMODAT |
| 3057 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 3058 | /* |
| 3059 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 3060 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3061 | * and then says it isn't implemented yet. |
| 3062 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3063 | * |
| 3064 | * Once it is supported, os.chmod will automatically |
| 3065 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 3066 | * Until then, we need to be careful what exception we raise. |
| 3067 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3068 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3069 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3070 | /* |
| 3071 | * But wait! We can't throw the exception without allowing threads, |
| 3072 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 3073 | */ |
| 3074 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3075 | result && |
| 3076 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 3077 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3078 | } |
| 3079 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3080 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3081 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3082 | Py_END_ALLOW_THREADS |
| 3083 | |
| 3084 | if (result) { |
| 3085 | #ifdef HAVE_FCHMODAT |
| 3086 | if (fchmodat_nofollow_unsupported) { |
| 3087 | if (dir_fd != DEFAULT_DIR_FD) |
| 3088 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3089 | dir_fd, follow_symlinks); |
| 3090 | else |
| 3091 | follow_symlinks_specified("chmod", follow_symlinks); |
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 3092 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3093 | } |
| 3094 | else |
| 3095 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3096 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3097 | } |
| 3098 | #endif |
| 3099 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3100 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3103 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3104 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3105 | /*[clinic input] |
| 3106 | os.fchmod |
| 3107 | |
| 3108 | fd: int |
| 3109 | mode: int |
| 3110 | |
| 3111 | Change the access permissions of the file given by file descriptor fd. |
| 3112 | |
| 3113 | Equivalent to os.chmod(fd, mode). |
| 3114 | [clinic start generated code]*/ |
| 3115 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3116 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3117 | os_fchmod_impl(PyObject *module, int fd, int mode) |
| 3118 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3119 | { |
| 3120 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3121 | int async_err = 0; |
| 3122 | |
| 3123 | do { |
| 3124 | Py_BEGIN_ALLOW_THREADS |
| 3125 | res = fchmod(fd, mode); |
| 3126 | Py_END_ALLOW_THREADS |
| 3127 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3128 | if (res != 0) |
| 3129 | return (!async_err) ? posix_error() : NULL; |
| 3130 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3131 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3132 | } |
| 3133 | #endif /* HAVE_FCHMOD */ |
| 3134 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3135 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3136 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3137 | /*[clinic input] |
| 3138 | os.lchmod |
| 3139 | |
| 3140 | path: path_t |
| 3141 | mode: int |
| 3142 | |
| 3143 | Change the access permissions of a file, without following symbolic links. |
| 3144 | |
| 3145 | If path is a symlink, this affects the link itself rather than the target. |
| 3146 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 3147 | [clinic start generated code]*/ |
| 3148 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3149 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3150 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
| 3151 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3152 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3153 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3154 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3155 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3156 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3157 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3158 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3159 | return NULL; |
| 3160 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3161 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3162 | } |
| 3163 | #endif /* HAVE_LCHMOD */ |
| 3164 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3165 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3166 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3167 | /*[clinic input] |
| 3168 | os.chflags |
| 3169 | |
| 3170 | path: path_t |
| 3171 | flags: unsigned_long(bitwise=True) |
| 3172 | follow_symlinks: bool=True |
| 3173 | |
| 3174 | Set file flags. |
| 3175 | |
| 3176 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3177 | link, chflags will change flags on the symbolic link itself instead of the |
| 3178 | file the link points to. |
| 3179 | follow_symlinks may not be implemented on your platform. If it is |
| 3180 | unavailable, using it will raise a NotImplementedError. |
| 3181 | |
| 3182 | [clinic start generated code]*/ |
| 3183 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3184 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3185 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3186 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3187 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3188 | { |
| 3189 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3190 | |
| 3191 | #ifndef HAVE_LCHFLAGS |
| 3192 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3193 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3194 | #endif |
| 3195 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3196 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3197 | #ifdef HAVE_LCHFLAGS |
| 3198 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3199 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3200 | else |
| 3201 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3202 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3203 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3204 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3205 | if (result) |
| 3206 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3207 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3208 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3209 | } |
| 3210 | #endif /* HAVE_CHFLAGS */ |
| 3211 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3212 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3213 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3214 | /*[clinic input] |
| 3215 | os.lchflags |
| 3216 | |
| 3217 | path: path_t |
| 3218 | flags: unsigned_long(bitwise=True) |
| 3219 | |
| 3220 | Set file flags. |
| 3221 | |
| 3222 | This function will not follow symbolic links. |
| 3223 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3224 | [clinic start generated code]*/ |
| 3225 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3226 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3227 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
| 3228 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3229 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3230 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3231 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3232 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3233 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3234 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3235 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3236 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3237 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3238 | } |
| 3239 | #endif /* HAVE_LCHFLAGS */ |
| 3240 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3241 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3242 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3243 | /*[clinic input] |
| 3244 | os.chroot |
| 3245 | path: path_t |
| 3246 | |
| 3247 | Change root directory to path. |
| 3248 | |
| 3249 | [clinic start generated code]*/ |
| 3250 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3251 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3252 | os_chroot_impl(PyObject *module, path_t *path) |
| 3253 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3254 | { |
| 3255 | int res; |
| 3256 | Py_BEGIN_ALLOW_THREADS |
| 3257 | res = chroot(path->narrow); |
| 3258 | Py_END_ALLOW_THREADS |
| 3259 | if (res < 0) |
| 3260 | return path_error(path); |
| 3261 | Py_RETURN_NONE; |
| 3262 | } |
| 3263 | #endif /* HAVE_CHROOT */ |
| 3264 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3265 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3266 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3267 | /*[clinic input] |
| 3268 | os.fsync |
| 3269 | |
| 3270 | fd: fildes |
| 3271 | |
| 3272 | Force write of fd to disk. |
| 3273 | [clinic start generated code]*/ |
| 3274 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3275 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3276 | os_fsync_impl(PyObject *module, int fd) |
| 3277 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3278 | { |
| 3279 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3280 | } |
| 3281 | #endif /* HAVE_FSYNC */ |
| 3282 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3283 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3284 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3285 | /*[clinic input] |
| 3286 | os.sync |
| 3287 | |
| 3288 | Force write of everything to disk. |
| 3289 | [clinic start generated code]*/ |
| 3290 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3291 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3292 | os_sync_impl(PyObject *module) |
| 3293 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3294 | { |
| 3295 | Py_BEGIN_ALLOW_THREADS |
| 3296 | sync(); |
| 3297 | Py_END_ALLOW_THREADS |
| 3298 | Py_RETURN_NONE; |
| 3299 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3300 | #endif /* HAVE_SYNC */ |
| 3301 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3302 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3303 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3304 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3305 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3306 | #endif |
| 3307 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3308 | /*[clinic input] |
| 3309 | os.fdatasync |
| 3310 | |
| 3311 | fd: fildes |
| 3312 | |
| 3313 | Force write of fd to disk without forcing update of metadata. |
| 3314 | [clinic start generated code]*/ |
| 3315 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3316 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3317 | os_fdatasync_impl(PyObject *module, int fd) |
| 3318 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3319 | { |
| 3320 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3321 | } |
| 3322 | #endif /* HAVE_FDATASYNC */ |
| 3323 | |
| 3324 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3325 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3326 | /*[clinic input] |
| 3327 | os.chown |
| 3328 | |
| 3329 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3330 | 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] | 3331 | |
| 3332 | uid: uid_t |
| 3333 | |
| 3334 | gid: gid_t |
| 3335 | |
| 3336 | * |
| 3337 | |
| 3338 | dir_fd : dir_fd(requires='fchownat') = None |
| 3339 | If not None, it should be a file descriptor open to a directory, |
| 3340 | and path should be relative; path will then be relative to that |
| 3341 | directory. |
| 3342 | |
| 3343 | follow_symlinks: bool = True |
| 3344 | If False, and the last element of the path is a symbolic link, |
| 3345 | stat will examine the symbolic link itself instead of the file |
| 3346 | the link points to. |
| 3347 | |
| 3348 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3349 | |
| 3350 | path may always be specified as a string. |
| 3351 | On some platforms, path may also be specified as an open file descriptor. |
| 3352 | If this functionality is unavailable, using it raises an exception. |
| 3353 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3354 | and path should be relative; path will then be relative to that directory. |
| 3355 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3356 | link, chown will modify the symbolic link itself instead of the file the |
| 3357 | link points to. |
| 3358 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3359 | an open file descriptor. |
| 3360 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3361 | If they are unavailable, using them will raise a NotImplementedError. |
| 3362 | |
| 3363 | [clinic start generated code]*/ |
| 3364 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3365 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3366 | 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] | 3367 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3368 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3369 | { |
| 3370 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3371 | |
| 3372 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3373 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3374 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3375 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3376 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3377 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3378 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3379 | |
| 3380 | #ifdef __APPLE__ |
| 3381 | /* |
| 3382 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3383 | * (But we still have an lchown symbol because of weak-linking.) |
| 3384 | * It doesn't have fchownat either. So there's no possibility |
| 3385 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3386 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3387 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3388 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3389 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3390 | } |
| 3391 | #endif |
| 3392 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3393 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3394 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3395 | if (path->fd != -1) |
| 3396 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3397 | else |
| 3398 | #endif |
| 3399 | #ifdef HAVE_LCHOWN |
| 3400 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3401 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3402 | else |
| 3403 | #endif |
| 3404 | #ifdef HAVE_FCHOWNAT |
| 3405 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3406 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3407 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3408 | else |
| 3409 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3410 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3411 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3413 | if (result) |
| 3414 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3415 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3416 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3417 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3418 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3419 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3420 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3421 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3422 | /*[clinic input] |
| 3423 | os.fchown |
| 3424 | |
| 3425 | fd: int |
| 3426 | uid: uid_t |
| 3427 | gid: gid_t |
| 3428 | |
| 3429 | Change the owner and group id of the file specified by file descriptor. |
| 3430 | |
| 3431 | Equivalent to os.chown(fd, uid, gid). |
| 3432 | |
| 3433 | [clinic start generated code]*/ |
| 3434 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3435 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3436 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
| 3437 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3438 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3439 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3440 | int async_err = 0; |
| 3441 | |
| 3442 | do { |
| 3443 | Py_BEGIN_ALLOW_THREADS |
| 3444 | res = fchown(fd, uid, gid); |
| 3445 | Py_END_ALLOW_THREADS |
| 3446 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3447 | if (res != 0) |
| 3448 | return (!async_err) ? posix_error() : NULL; |
| 3449 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3450 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3451 | } |
| 3452 | #endif /* HAVE_FCHOWN */ |
| 3453 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3454 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3455 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3456 | /*[clinic input] |
| 3457 | os.lchown |
| 3458 | |
| 3459 | path : path_t |
| 3460 | uid: uid_t |
| 3461 | gid: gid_t |
| 3462 | |
| 3463 | Change the owner and group id of path to the numeric uid and gid. |
| 3464 | |
| 3465 | This function will not follow symbolic links. |
| 3466 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3467 | [clinic start generated code]*/ |
| 3468 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3469 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3470 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
| 3471 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3472 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3473 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3474 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3475 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3476 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3477 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3478 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3479 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3480 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3481 | } |
| 3482 | #endif /* HAVE_LCHOWN */ |
| 3483 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3484 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3485 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3486 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3487 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3488 | #ifdef MS_WINDOWS |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3489 | wchar_t wbuf[MAXPATHLEN]; |
| 3490 | wchar_t *wbuf2 = wbuf; |
| 3491 | DWORD len; |
| 3492 | |
| 3493 | Py_BEGIN_ALLOW_THREADS |
| 3494 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
| 3495 | /* If the buffer is large enough, len does not include the |
| 3496 | terminating \0. If the buffer is too small, len includes |
| 3497 | the space needed for the terminator. */ |
| 3498 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | ec3e20a | 2019-06-28 18:01:59 +0200 | [diff] [blame] | 3499 | if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3500 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3501 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3502 | else { |
| 3503 | wbuf2 = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3504 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3505 | if (wbuf2) { |
| 3506 | len = GetCurrentDirectoryW(len, wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3507 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3508 | } |
| 3509 | Py_END_ALLOW_THREADS |
| 3510 | |
| 3511 | if (!wbuf2) { |
| 3512 | PyErr_NoMemory(); |
| 3513 | return NULL; |
| 3514 | } |
| 3515 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3516 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3517 | PyMem_RawFree(wbuf2); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3518 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3519 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3520 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3521 | PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len); |
| 3522 | if (wbuf2 != wbuf) { |
| 3523 | PyMem_RawFree(wbuf2); |
| 3524 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3525 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3526 | if (use_bytes) { |
| 3527 | if (resobj == NULL) { |
| 3528 | return NULL; |
| 3529 | } |
| 3530 | Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj)); |
| 3531 | } |
| 3532 | |
| 3533 | return resobj; |
| 3534 | #else |
| 3535 | const size_t chunk = 1024; |
| 3536 | |
| 3537 | char *buf = NULL; |
| 3538 | char *cwd = NULL; |
| 3539 | size_t buflen = 0; |
| 3540 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3541 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3542 | do { |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3543 | char *newbuf; |
| 3544 | if (buflen <= PY_SSIZE_T_MAX - chunk) { |
| 3545 | buflen += chunk; |
| 3546 | newbuf = PyMem_RawRealloc(buf, buflen); |
| 3547 | } |
| 3548 | else { |
| 3549 | newbuf = NULL; |
| 3550 | } |
| 3551 | if (newbuf == NULL) { |
| 3552 | PyMem_RawFree(buf); |
| 3553 | buf = NULL; |
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3554 | break; |
| 3555 | } |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3556 | buf = newbuf; |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3557 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3558 | cwd = getcwd(buf, buflen); |
| 3559 | } while (cwd == NULL && errno == ERANGE); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3560 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3561 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3562 | if (buf == NULL) { |
| 3563 | return PyErr_NoMemory(); |
| 3564 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3565 | if (cwd == NULL) { |
| 3566 | PyMem_RawFree(buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3567 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3568 | } |
| 3569 | |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3570 | PyObject *obj; |
| 3571 | if (use_bytes) { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3572 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3573 | } |
| 3574 | else { |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3575 | obj = PyUnicode_DecodeFSDefault(buf); |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3576 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3577 | PyMem_RawFree(buf); |
| 3578 | |
| 3579 | return obj; |
Victor Stinner | 689830e | 2019-06-26 17:31:12 +0200 | [diff] [blame] | 3580 | #endif /* !MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3581 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3582 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3583 | |
| 3584 | /*[clinic input] |
| 3585 | os.getcwd |
| 3586 | |
| 3587 | Return a unicode string representing the current working directory. |
| 3588 | [clinic start generated code]*/ |
| 3589 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3590 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3591 | os_getcwd_impl(PyObject *module) |
| 3592 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3593 | { |
| 3594 | return posix_getcwd(0); |
| 3595 | } |
| 3596 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3597 | |
| 3598 | /*[clinic input] |
| 3599 | os.getcwdb |
| 3600 | |
| 3601 | Return a bytes string representing the current working directory. |
| 3602 | [clinic start generated code]*/ |
| 3603 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3604 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3605 | os_getcwdb_impl(PyObject *module) |
| 3606 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3607 | { |
| 3608 | return posix_getcwd(1); |
| 3609 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3610 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3611 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3612 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 3613 | #define HAVE_LINK 1 |
| 3614 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3615 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3616 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3617 | /*[clinic input] |
| 3618 | |
| 3619 | os.link |
| 3620 | |
| 3621 | src : path_t |
| 3622 | dst : path_t |
| 3623 | * |
| 3624 | src_dir_fd : dir_fd = None |
| 3625 | dst_dir_fd : dir_fd = None |
| 3626 | follow_symlinks: bool = True |
| 3627 | |
| 3628 | Create a hard link to a file. |
| 3629 | |
| 3630 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 3631 | descriptor open to a directory, and the respective path string (src or dst) |
| 3632 | should be relative; the path will then be relative to that directory. |
| 3633 | If follow_symlinks is False, and the last element of src is a symbolic |
| 3634 | link, link will create a link to the symbolic link itself instead of the |
| 3635 | file the link points to. |
| 3636 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 3637 | platform. If they are unavailable, using them will raise a |
| 3638 | NotImplementedError. |
| 3639 | [clinic start generated code]*/ |
| 3640 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3641 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3642 | 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] | 3643 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3644 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3645 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3646 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3647 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3648 | #else |
| 3649 | int result; |
| 3650 | #endif |
| 3651 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3652 | #ifndef HAVE_LINKAT |
| 3653 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 3654 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3655 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3656 | } |
| 3657 | #endif |
| 3658 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3659 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3660 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3661 | PyErr_SetString(PyExc_NotImplementedError, |
| 3662 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3663 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3664 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3665 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3666 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3667 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3668 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3669 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3670 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3671 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3672 | if (!result) |
| 3673 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3674 | #else |
| 3675 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3676 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3677 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 3678 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 3679 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3680 | result = linkat(src_dir_fd, src->narrow, |
| 3681 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3682 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 3683 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3684 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3685 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3686 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3687 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3688 | if (result) |
| 3689 | return path_error2(src, dst); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3690 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3691 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3692 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3693 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3694 | #endif |
| 3695 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3696 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3697 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3698 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3699 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3700 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3701 | PyObject *v; |
| 3702 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 3703 | BOOL result; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3704 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3705 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3706 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3707 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3708 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3709 | WIN32_FIND_DATAW wFileData; |
| 3710 | const wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3711 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3712 | if (!path->wide) { /* Default arg: "." */ |
| 3713 | po_wchars = L"."; |
| 3714 | len = 1; |
| 3715 | } else { |
| 3716 | po_wchars = path->wide; |
| 3717 | len = wcslen(path->wide); |
| 3718 | } |
| 3719 | /* The +5 is so we can append "\\*.*\0" */ |
| 3720 | wnamebuf = PyMem_New(wchar_t, len + 5); |
| 3721 | if (!wnamebuf) { |
| 3722 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3723 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3724 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3725 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3726 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3727 | wchar_t wch = wnamebuf[len-1]; |
| 3728 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 3729 | wnamebuf[len++] = SEP; |
| 3730 | wcscpy(wnamebuf + len, L"*.*"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3731 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3732 | if ((list = PyList_New(0)) == NULL) { |
| 3733 | goto exit; |
| 3734 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3735 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3736 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3737 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3738 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 3739 | int error = GetLastError(); |
| 3740 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3741 | goto exit; |
| 3742 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3743 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3744 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3745 | } |
| 3746 | do { |
| 3747 | /* Skip over . and .. */ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3748 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 3749 | wcscmp(wFileData.cFileName, L"..") != 0) { |
| 3750 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 3751 | wcslen(wFileData.cFileName)); |
| 3752 | if (path->narrow && v) { |
| 3753 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); |
| 3754 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3755 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3756 | Py_DECREF(list); |
| 3757 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3758 | break; |
| 3759 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3760 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3761 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3762 | Py_DECREF(list); |
| 3763 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3764 | break; |
| 3765 | } |
| 3766 | Py_DECREF(v); |
| 3767 | } |
| 3768 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3769 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3770 | Py_END_ALLOW_THREADS |
| 3771 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 3772 | it got to the end of the directory. */ |
| 3773 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3774 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3775 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3776 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3777 | } |
| 3778 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3779 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3780 | exit: |
| 3781 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 3782 | if (FindClose(hFindFile) == FALSE) { |
| 3783 | if (list != NULL) { |
| 3784 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3785 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3786 | } |
| 3787 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3788 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3789 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3790 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3791 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3792 | } /* end of _listdir_windows_no_opendir */ |
| 3793 | |
| 3794 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 3795 | |
| 3796 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3797 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3798 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3799 | PyObject *v; |
| 3800 | DIR *dirp = NULL; |
| 3801 | struct dirent *ep; |
| 3802 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3803 | #ifdef HAVE_FDOPENDIR |
| 3804 | int fd = -1; |
| 3805 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3806 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3807 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3808 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3809 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3810 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3811 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3812 | if (fd == -1) |
| 3813 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3814 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3815 | return_str = 1; |
| 3816 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3817 | Py_BEGIN_ALLOW_THREADS |
| 3818 | dirp = fdopendir(fd); |
| 3819 | Py_END_ALLOW_THREADS |
| 3820 | } |
| 3821 | else |
| 3822 | #endif |
| 3823 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3824 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3825 | if (path->narrow) { |
| 3826 | name = path->narrow; |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3827 | /* only return bytes if they specified a bytes-like object */ |
| 3828 | return_str = !PyObject_CheckBuffer(path->object); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3829 | } |
| 3830 | else { |
| 3831 | name = "."; |
| 3832 | return_str = 1; |
| 3833 | } |
| 3834 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3835 | Py_BEGIN_ALLOW_THREADS |
| 3836 | dirp = opendir(name); |
| 3837 | Py_END_ALLOW_THREADS |
| 3838 | } |
| 3839 | |
| 3840 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3841 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3842 | #ifdef HAVE_FDOPENDIR |
| 3843 | if (fd != -1) { |
| 3844 | Py_BEGIN_ALLOW_THREADS |
| 3845 | close(fd); |
| 3846 | Py_END_ALLOW_THREADS |
| 3847 | } |
| 3848 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3849 | goto exit; |
| 3850 | } |
| 3851 | if ((list = PyList_New(0)) == NULL) { |
| 3852 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3853 | } |
| 3854 | for (;;) { |
| 3855 | errno = 0; |
| 3856 | Py_BEGIN_ALLOW_THREADS |
| 3857 | ep = readdir(dirp); |
| 3858 | Py_END_ALLOW_THREADS |
| 3859 | if (ep == NULL) { |
| 3860 | if (errno == 0) { |
| 3861 | break; |
| 3862 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3863 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3864 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3865 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3866 | } |
| 3867 | } |
| 3868 | if (ep->d_name[0] == '.' && |
| 3869 | (NAMLEN(ep) == 1 || |
| 3870 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 3871 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3872 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3873 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 3874 | else |
| 3875 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3876 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3877 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3878 | break; |
| 3879 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3880 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3881 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3882 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3883 | break; |
| 3884 | } |
| 3885 | Py_DECREF(v); |
| 3886 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3887 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3888 | exit: |
| 3889 | if (dirp != NULL) { |
| 3890 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3891 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3892 | if (fd > -1) |
| 3893 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3894 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3895 | closedir(dirp); |
| 3896 | Py_END_ALLOW_THREADS |
| 3897 | } |
| 3898 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3899 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3900 | } /* end of _posix_listdir */ |
| 3901 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3902 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3903 | |
| 3904 | /*[clinic input] |
| 3905 | os.listdir |
| 3906 | |
| 3907 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 3908 | |
| 3909 | Return a list containing the names of the files in the directory. |
| 3910 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3911 | 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] | 3912 | the filenames returned will also be bytes; in all other circumstances |
| 3913 | the filenames returned will be str. |
| 3914 | If path is None, uses the path='.'. |
| 3915 | On some platforms, path may also be specified as an open file descriptor;\ |
| 3916 | the file descriptor must refer to a directory. |
| 3917 | If this functionality is unavailable, using it raises NotImplementedError. |
| 3918 | |
| 3919 | The list is in arbitrary order. It does not include the special |
| 3920 | entries '.' and '..' even if they are present in the directory. |
| 3921 | |
| 3922 | |
| 3923 | [clinic start generated code]*/ |
| 3924 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3925 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3926 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3927 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3928 | { |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 3929 | if (PySys_Audit("os.listdir", "O", |
| 3930 | path->object ? path->object : Py_None) < 0) { |
| 3931 | return NULL; |
| 3932 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3933 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 3934 | return _listdir_windows_no_opendir(path, NULL); |
| 3935 | #else |
| 3936 | return _posix_listdir(path, NULL); |
| 3937 | #endif |
| 3938 | } |
| 3939 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3940 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3941 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3942 | /*[clinic input] |
| 3943 | os._getfullpathname |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3944 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3945 | path: path_t |
| 3946 | / |
| 3947 | |
| 3948 | [clinic start generated code]*/ |
| 3949 | |
| 3950 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3951 | os__getfullpathname_impl(PyObject *module, path_t *path) |
| 3952 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3953 | { |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3954 | wchar_t *abspath; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3955 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3956 | /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */ |
| 3957 | if (_Py_abspath(path->wide, &abspath) < 0) { |
| 3958 | return win32_error_object("GetFullPathNameW", path->object); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3959 | } |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 3960 | if (abspath == NULL) { |
| 3961 | return PyErr_NoMemory(); |
| 3962 | } |
| 3963 | |
| 3964 | PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath)); |
| 3965 | PyMem_RawFree(abspath); |
| 3966 | if (str == NULL) { |
| 3967 | return NULL; |
| 3968 | } |
| 3969 | if (path->narrow) { |
| 3970 | Py_SETREF(str, PyUnicode_EncodeFSDefault(str)); |
| 3971 | } |
| 3972 | return str; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3973 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3974 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3975 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3976 | /*[clinic input] |
| 3977 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3978 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3979 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3980 | / |
| 3981 | |
| 3982 | A helper function for samepath on windows. |
| 3983 | [clinic start generated code]*/ |
| 3984 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3985 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3986 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
| 3987 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3988 | { |
| 3989 | HANDLE hFile; |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3990 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
| 3991 | int buf_size = Py_ARRAY_LENGTH(buf); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3992 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3993 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3994 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3995 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3996 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3997 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3998 | 0, /* desired access */ |
| 3999 | 0, /* share mode */ |
| 4000 | NULL, /* security attributes */ |
| 4001 | OPEN_EXISTING, |
| 4002 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 4003 | FILE_FLAG_BACKUP_SEMANTICS, |
| 4004 | NULL); |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4005 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4006 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4007 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4008 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4009 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4010 | |
| 4011 | /* We have a good handle to the target, use it to determine the |
| 4012 | target path name. */ |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4013 | while (1) { |
| 4014 | Py_BEGIN_ALLOW_THREADS |
| 4015 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 4016 | buf_size, VOLUME_NAME_DOS); |
| 4017 | Py_END_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4018 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4019 | if (!result_length) { |
| 4020 | result = win32_error_object("GetFinalPathNameByHandleW", |
| 4021 | path->object); |
| 4022 | goto cleanup; |
| 4023 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4024 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4025 | if (result_length < buf_size) { |
| 4026 | break; |
| 4027 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4028 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4029 | wchar_t *tmp; |
| 4030 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, |
| 4031 | result_length * sizeof(*tmp)); |
| 4032 | if (!tmp) { |
| 4033 | result = PyErr_NoMemory(); |
| 4034 | goto cleanup; |
| 4035 | } |
| 4036 | |
| 4037 | buf_size = result_length; |
| 4038 | target_path = tmp; |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4039 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4040 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4041 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4042 | if (result && path->narrow) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4043 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 4044 | } |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4045 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 4046 | cleanup: |
| 4047 | if (target_path != buf) { |
| 4048 | PyMem_Free(target_path); |
| 4049 | } |
| 4050 | CloseHandle(hFile); |
| 4051 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4052 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 4053 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4054 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4055 | /*[clinic input] |
| 4056 | os._getvolumepathname |
| 4057 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4058 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4059 | |
| 4060 | A helper function for ismount on Win32. |
| 4061 | [clinic start generated code]*/ |
| 4062 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4063 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4064 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
| 4065 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4066 | { |
| 4067 | PyObject *result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4068 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4069 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4070 | BOOL ret; |
| 4071 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4072 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4073 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4074 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 4075 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4076 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4077 | return NULL; |
| 4078 | } |
| 4079 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4080 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4081 | if (mountpath == NULL) |
| 4082 | return PyErr_NoMemory(); |
| 4083 | |
| 4084 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4085 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4086 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4087 | Py_END_ALLOW_THREADS |
| 4088 | |
| 4089 | if (!ret) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4090 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4091 | goto exit; |
| 4092 | } |
| 4093 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4094 | if (path->narrow) |
| 4095 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4096 | |
| 4097 | exit: |
| 4098 | PyMem_Free(mountpath); |
| 4099 | return result; |
| 4100 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4101 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4102 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4103 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4104 | |
| 4105 | /*[clinic input] |
| 4106 | os.mkdir |
| 4107 | |
| 4108 | path : path_t |
| 4109 | |
| 4110 | mode: int = 0o777 |
| 4111 | |
| 4112 | * |
| 4113 | |
| 4114 | dir_fd : dir_fd(requires='mkdirat') = None |
| 4115 | |
| 4116 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4117 | |
| 4118 | Create a directory. |
| 4119 | |
| 4120 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4121 | and path should be relative; path will then be relative to that directory. |
| 4122 | dir_fd may not be implemented on your platform. |
| 4123 | If it is unavailable, using it will raise a NotImplementedError. |
| 4124 | |
| 4125 | The mode argument is ignored on Windows. |
| 4126 | [clinic start generated code]*/ |
| 4127 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4128 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4129 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 4130 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4131 | { |
| 4132 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4133 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4134 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4135 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4136 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4137 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4138 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4139 | if (!result) |
| 4140 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4141 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4142 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4143 | #if HAVE_MKDIRAT |
| 4144 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4145 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4146 | else |
| 4147 | #endif |
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4148 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4149 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4150 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4151 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4152 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4153 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4154 | if (result < 0) |
| 4155 | return path_error(path); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4156 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4157 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4158 | } |
| 4159 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4160 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4161 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4162 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4163 | #include <sys/resource.h> |
| 4164 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4165 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4166 | |
| 4167 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4168 | /*[clinic input] |
| 4169 | os.nice |
| 4170 | |
| 4171 | increment: int |
| 4172 | / |
| 4173 | |
| 4174 | Add increment to the priority of process and return the new priority. |
| 4175 | [clinic start generated code]*/ |
| 4176 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4177 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4178 | os_nice_impl(PyObject *module, int increment) |
| 4179 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4180 | { |
| 4181 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4182 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4183 | /* There are two flavours of 'nice': one that returns the new |
| 4184 | priority (as required by almost all standards out there) and the |
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4185 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4186 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4187 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4188 | If we are of the nice family that returns the new priority, we |
| 4189 | need to clear errno before the call, and check if errno is filled |
| 4190 | before calling posix_error() on a returnvalue of -1, because the |
| 4191 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4192 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4193 | errno = 0; |
| 4194 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4195 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4196 | if (value == 0) |
| 4197 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4198 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4199 | if (value == -1 && errno != 0) |
| 4200 | /* either nice() or getpriority() returned an error */ |
| 4201 | return posix_error(); |
| 4202 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4203 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4204 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4205 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4206 | |
| 4207 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4208 | /*[clinic input] |
| 4209 | os.getpriority |
| 4210 | |
| 4211 | which: int |
| 4212 | who: int |
| 4213 | |
| 4214 | Return program scheduling 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_getpriority_impl(PyObject *module, int which, int who) |
| 4219 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4220 | { |
| 4221 | int retval; |
| 4222 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4223 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4224 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4225 | if (errno != 0) |
| 4226 | return posix_error(); |
| 4227 | return PyLong_FromLong((long)retval); |
| 4228 | } |
| 4229 | #endif /* HAVE_GETPRIORITY */ |
| 4230 | |
| 4231 | |
| 4232 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4233 | /*[clinic input] |
| 4234 | os.setpriority |
| 4235 | |
| 4236 | which: int |
| 4237 | who: int |
| 4238 | priority: int |
| 4239 | |
| 4240 | Set program scheduling priority. |
| 4241 | [clinic start generated code]*/ |
| 4242 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4243 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4244 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
| 4245 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4246 | { |
| 4247 | int retval; |
| 4248 | |
| 4249 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4250 | if (retval == -1) |
| 4251 | return posix_error(); |
| 4252 | Py_RETURN_NONE; |
| 4253 | } |
| 4254 | #endif /* HAVE_SETPRIORITY */ |
| 4255 | |
| 4256 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4257 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4258 | 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] | 4259 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4260 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4261 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4262 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4263 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4264 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4265 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4266 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4267 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4268 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4269 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4270 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 4271 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 4272 | #ifndef HAVE_RENAMEAT |
| 4273 | if (dir_fd_specified) { |
| 4274 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4275 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4276 | } |
| 4277 | #endif |
| 4278 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4279 | #ifdef MS_WINDOWS |
| 4280 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4281 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4282 | Py_END_ALLOW_THREADS |
| 4283 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4284 | if (!result) |
| 4285 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4286 | |
| 4287 | #else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4288 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 4289 | PyErr_Format(PyExc_ValueError, |
| 4290 | "%s: src and dst must be the same type", function_name); |
| 4291 | return NULL; |
| 4292 | } |
| 4293 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4294 | Py_BEGIN_ALLOW_THREADS |
| 4295 | #ifdef HAVE_RENAMEAT |
| 4296 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4297 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4298 | else |
| 4299 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4300 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4301 | Py_END_ALLOW_THREADS |
| 4302 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4303 | if (result) |
| 4304 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4305 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4306 | Py_RETURN_NONE; |
| 4307 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4308 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4309 | |
| 4310 | /*[clinic input] |
| 4311 | os.rename |
| 4312 | |
| 4313 | src : path_t |
| 4314 | dst : path_t |
| 4315 | * |
| 4316 | src_dir_fd : dir_fd = None |
| 4317 | dst_dir_fd : dir_fd = None |
| 4318 | |
| 4319 | Rename a file or directory. |
| 4320 | |
| 4321 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4322 | descriptor open to a directory, and the respective path string (src or dst) |
| 4323 | should be relative; the path will then be relative to that directory. |
| 4324 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 4325 | If they are unavailable, using them will raise a NotImplementedError. |
| 4326 | [clinic start generated code]*/ |
| 4327 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4328 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4329 | 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] | 4330 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4331 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4332 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4333 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4334 | } |
| 4335 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4336 | |
| 4337 | /*[clinic input] |
| 4338 | os.replace = os.rename |
| 4339 | |
| 4340 | Rename a file or directory, overwriting the destination. |
| 4341 | |
| 4342 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4343 | descriptor open to a directory, and the respective path string (src or dst) |
| 4344 | should be relative; the path will then be relative to that directory. |
| 4345 | 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] | 4346 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4347 | [clinic start generated code]*/ |
| 4348 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4349 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4350 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
| 4351 | int dst_dir_fd) |
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4352 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4353 | { |
| 4354 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 4355 | } |
| 4356 | |
| 4357 | |
| 4358 | /*[clinic input] |
| 4359 | os.rmdir |
| 4360 | |
| 4361 | path: path_t |
| 4362 | * |
| 4363 | dir_fd: dir_fd(requires='unlinkat') = None |
| 4364 | |
| 4365 | Remove a directory. |
| 4366 | |
| 4367 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4368 | and path should be relative; path will then be relative to that directory. |
| 4369 | dir_fd may not be implemented on your platform. |
| 4370 | If it is unavailable, using it will raise a NotImplementedError. |
| 4371 | [clinic start generated code]*/ |
| 4372 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4373 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4374 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
| 4375 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4376 | { |
| 4377 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4378 | |
| 4379 | Py_BEGIN_ALLOW_THREADS |
| 4380 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4381 | /* Windows, success=1, UNIX, success=0 */ |
| 4382 | result = !RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4383 | #else |
| 4384 | #ifdef HAVE_UNLINKAT |
| 4385 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4386 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4387 | else |
| 4388 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4389 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4390 | #endif |
| 4391 | Py_END_ALLOW_THREADS |
| 4392 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4393 | if (result) |
| 4394 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4396 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4399 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4400 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4401 | #ifdef MS_WINDOWS |
| 4402 | /*[clinic input] |
| 4403 | os.system -> long |
| 4404 | |
| 4405 | command: Py_UNICODE |
| 4406 | |
| 4407 | Execute the command in a subshell. |
| 4408 | [clinic start generated code]*/ |
| 4409 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4410 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4411 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
| 4412 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4413 | { |
| 4414 | long result; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4415 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4416 | if (PySys_Audit("os.system", "(u)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4417 | return -1; |
| 4418 | } |
| 4419 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4420 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4421 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4422 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4423 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4424 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4425 | return result; |
| 4426 | } |
| 4427 | #else /* MS_WINDOWS */ |
| 4428 | /*[clinic input] |
| 4429 | os.system -> long |
| 4430 | |
| 4431 | command: FSConverter |
| 4432 | |
| 4433 | Execute the command in a subshell. |
| 4434 | [clinic start generated code]*/ |
| 4435 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4436 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4437 | os_system_impl(PyObject *module, PyObject *command) |
| 4438 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4439 | { |
| 4440 | long result; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4441 | const char *bytes = PyBytes_AsString(command); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4442 | |
Steve Dower | fbe3c76 | 2019-10-18 00:52:15 -0700 | [diff] [blame] | 4443 | if (PySys_Audit("os.system", "(O)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4444 | return -1; |
| 4445 | } |
| 4446 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4447 | Py_BEGIN_ALLOW_THREADS |
| 4448 | result = system(bytes); |
| 4449 | Py_END_ALLOW_THREADS |
| 4450 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4451 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4452 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4453 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4454 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4455 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4456 | /*[clinic input] |
| 4457 | os.umask |
| 4458 | |
| 4459 | mask: int |
| 4460 | / |
| 4461 | |
| 4462 | Set the current numeric umask and return the previous umask. |
| 4463 | [clinic start generated code]*/ |
| 4464 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4465 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4466 | os_umask_impl(PyObject *module, int mask) |
| 4467 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4468 | { |
| 4469 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4470 | if (i < 0) |
| 4471 | return posix_error(); |
| 4472 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4475 | #ifdef MS_WINDOWS |
| 4476 | |
| 4477 | /* override the default DeleteFileW behavior so that directory |
| 4478 | symlinks can be removed with this function, the same as with |
| 4479 | Unix symlinks */ |
| 4480 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 4481 | { |
| 4482 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 4483 | WIN32_FIND_DATAW find_data; |
| 4484 | HANDLE find_data_handle; |
| 4485 | int is_directory = 0; |
| 4486 | int is_link = 0; |
| 4487 | |
| 4488 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 4489 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4490 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4491 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 4492 | it is a symlink */ |
| 4493 | if(is_directory && |
| 4494 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 4495 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 4496 | |
| 4497 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4498 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 4499 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 4500 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 4501 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4502 | FindClose(find_data_handle); |
| 4503 | } |
| 4504 | } |
| 4505 | } |
| 4506 | |
| 4507 | if (is_directory && is_link) |
| 4508 | return RemoveDirectoryW(lpFileName); |
| 4509 | |
| 4510 | return DeleteFileW(lpFileName); |
| 4511 | } |
| 4512 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4513 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4514 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4515 | /*[clinic input] |
| 4516 | os.unlink |
| 4517 | |
| 4518 | path: path_t |
| 4519 | * |
| 4520 | dir_fd: dir_fd(requires='unlinkat')=None |
| 4521 | |
| 4522 | Remove a file (same as remove()). |
| 4523 | |
| 4524 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4525 | and path should be relative; path will then be relative to that directory. |
| 4526 | dir_fd may not be implemented on your platform. |
| 4527 | If it is unavailable, using it will raise a NotImplementedError. |
| 4528 | |
| 4529 | [clinic start generated code]*/ |
| 4530 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4531 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4532 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 4533 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4534 | { |
| 4535 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4536 | |
| 4537 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4538 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4539 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4540 | /* Windows, success=1, UNIX, success=0 */ |
| 4541 | result = !Py_DeleteFileW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4542 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4543 | #ifdef HAVE_UNLINKAT |
| 4544 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4545 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4546 | else |
| 4547 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4548 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4549 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4550 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4551 | Py_END_ALLOW_THREADS |
| 4552 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4553 | if (result) |
| 4554 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4555 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4556 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4559 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4560 | /*[clinic input] |
| 4561 | os.remove = os.unlink |
| 4562 | |
| 4563 | Remove a file (same as unlink()). |
| 4564 | |
| 4565 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4566 | and path should be relative; path will then be relative to that directory. |
| 4567 | dir_fd may not be implemented on your platform. |
| 4568 | If it is unavailable, using it will raise a NotImplementedError. |
| 4569 | [clinic start generated code]*/ |
| 4570 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4571 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4572 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
| 4573 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4574 | { |
| 4575 | return os_unlink_impl(module, path, dir_fd); |
| 4576 | } |
| 4577 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4578 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4579 | static PyStructSequence_Field uname_result_fields[] = { |
| 4580 | {"sysname", "operating system name"}, |
| 4581 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 4582 | {"release", "operating system release"}, |
| 4583 | {"version", "operating system version"}, |
| 4584 | {"machine", "hardware identifier"}, |
| 4585 | {NULL} |
| 4586 | }; |
| 4587 | |
| 4588 | PyDoc_STRVAR(uname_result__doc__, |
| 4589 | "uname_result: Result from os.uname().\n\n\ |
| 4590 | This object may be accessed either as a tuple of\n\ |
| 4591 | (sysname, nodename, release, version, machine),\n\ |
| 4592 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 4593 | \n\ |
| 4594 | See os.uname for more information."); |
| 4595 | |
| 4596 | static PyStructSequence_Desc uname_result_desc = { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4597 | MODNAME ".uname_result", /* name */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4598 | uname_result__doc__, /* doc */ |
| 4599 | uname_result_fields, |
| 4600 | 5 |
| 4601 | }; |
| 4602 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4603 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4604 | /*[clinic input] |
| 4605 | os.uname |
| 4606 | |
| 4607 | Return an object identifying the current operating system. |
| 4608 | |
| 4609 | The object behaves like a named tuple with the following fields: |
| 4610 | (sysname, nodename, release, version, machine) |
| 4611 | |
| 4612 | [clinic start generated code]*/ |
| 4613 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4614 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4615 | os_uname_impl(PyObject *module) |
| 4616 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4617 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4618 | struct utsname u; |
| 4619 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4620 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4621 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4622 | Py_BEGIN_ALLOW_THREADS |
| 4623 | res = uname(&u); |
| 4624 | Py_END_ALLOW_THREADS |
| 4625 | if (res < 0) |
| 4626 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4627 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4628 | PyObject *UnameResultType = _posixstate(module)->UnameResultType; |
| 4629 | value = PyStructSequence_New((PyTypeObject *)UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4630 | if (value == NULL) |
| 4631 | return NULL; |
| 4632 | |
| 4633 | #define SET(i, field) \ |
| 4634 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4635 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4636 | if (!o) { \ |
| 4637 | Py_DECREF(value); \ |
| 4638 | return NULL; \ |
| 4639 | } \ |
| 4640 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 4641 | } \ |
| 4642 | |
| 4643 | SET(0, u.sysname); |
| 4644 | SET(1, u.nodename); |
| 4645 | SET(2, u.release); |
| 4646 | SET(3, u.version); |
| 4647 | SET(4, u.machine); |
| 4648 | |
| 4649 | #undef SET |
| 4650 | |
| 4651 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4652 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4653 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4654 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4655 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4656 | |
| 4657 | typedef struct { |
| 4658 | int now; |
| 4659 | time_t atime_s; |
| 4660 | long atime_ns; |
| 4661 | time_t mtime_s; |
| 4662 | long mtime_ns; |
| 4663 | } utime_t; |
| 4664 | |
| 4665 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4666 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4667 | * they also intentionally leak the declaration of a pointer named "time" |
| 4668 | */ |
| 4669 | #define UTIME_TO_TIMESPEC \ |
| 4670 | struct timespec ts[2]; \ |
| 4671 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4672 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4673 | time = NULL; \ |
| 4674 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4675 | ts[0].tv_sec = ut->atime_s; \ |
| 4676 | ts[0].tv_nsec = ut->atime_ns; \ |
| 4677 | ts[1].tv_sec = ut->mtime_s; \ |
| 4678 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4679 | time = ts; \ |
| 4680 | } \ |
| 4681 | |
| 4682 | #define UTIME_TO_TIMEVAL \ |
| 4683 | struct timeval tv[2]; \ |
| 4684 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4685 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4686 | time = NULL; \ |
| 4687 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4688 | tv[0].tv_sec = ut->atime_s; \ |
| 4689 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 4690 | tv[1].tv_sec = ut->mtime_s; \ |
| 4691 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4692 | time = tv; \ |
| 4693 | } \ |
| 4694 | |
| 4695 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4696 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4697 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4698 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4699 | time = NULL; \ |
| 4700 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4701 | u.actime = ut->atime_s; \ |
| 4702 | u.modtime = ut->mtime_s; \ |
| 4703 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | #define UTIME_TO_TIME_T \ |
| 4707 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4708 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4709 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4710 | time = NULL; \ |
| 4711 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4712 | timet[0] = ut->atime_s; \ |
| 4713 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4714 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4715 | } \ |
| 4716 | |
| 4717 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4718 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4719 | |
| 4720 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4721 | 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] | 4722 | { |
| 4723 | #ifdef HAVE_UTIMENSAT |
| 4724 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 4725 | UTIME_TO_TIMESPEC; |
| 4726 | return utimensat(dir_fd, path, time, flags); |
| 4727 | #elif defined(HAVE_FUTIMESAT) |
| 4728 | UTIME_TO_TIMEVAL; |
| 4729 | /* |
| 4730 | * follow_symlinks will never be false here; |
| 4731 | * we only allow !follow_symlinks and dir_fd together |
| 4732 | * if we have utimensat() |
| 4733 | */ |
| 4734 | assert(follow_symlinks); |
| 4735 | return futimesat(dir_fd, path, time); |
| 4736 | #endif |
| 4737 | } |
| 4738 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4739 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 4740 | #else |
| 4741 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4742 | #endif |
| 4743 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4744 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4745 | |
| 4746 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4747 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4748 | { |
| 4749 | #ifdef HAVE_FUTIMENS |
| 4750 | UTIME_TO_TIMESPEC; |
| 4751 | return futimens(fd, time); |
| 4752 | #else |
| 4753 | UTIME_TO_TIMEVAL; |
| 4754 | return futimes(fd, time); |
| 4755 | #endif |
| 4756 | } |
| 4757 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4758 | #define PATH_UTIME_HAVE_FD 1 |
| 4759 | #else |
| 4760 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4761 | #endif |
| 4762 | |
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4763 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
| 4764 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 4765 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4766 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4767 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4768 | |
| 4769 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4770 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4771 | { |
| 4772 | #ifdef HAVE_UTIMENSAT |
| 4773 | UTIME_TO_TIMESPEC; |
| 4774 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 4775 | #else |
| 4776 | UTIME_TO_TIMEVAL; |
| 4777 | return lutimes(path, time); |
| 4778 | #endif |
| 4779 | } |
| 4780 | |
| 4781 | #endif |
| 4782 | |
| 4783 | #ifndef MS_WINDOWS |
| 4784 | |
| 4785 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4786 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4787 | { |
| 4788 | #ifdef HAVE_UTIMENSAT |
| 4789 | UTIME_TO_TIMESPEC; |
| 4790 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 4791 | #elif defined(HAVE_UTIMES) |
| 4792 | UTIME_TO_TIMEVAL; |
| 4793 | return utimes(path, time); |
| 4794 | #elif defined(HAVE_UTIME_H) |
| 4795 | UTIME_TO_UTIMBUF; |
| 4796 | return utime(path, time); |
| 4797 | #else |
| 4798 | UTIME_TO_TIME_T; |
| 4799 | return utime(path, time); |
| 4800 | #endif |
| 4801 | } |
| 4802 | |
| 4803 | #endif |
| 4804 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4805 | static int |
| 4806 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 4807 | { |
| 4808 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4809 | PyObject *divmod; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4810 | divmod = PyNumber_Divmod(py_long, _posixstate_global->billion); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4811 | if (!divmod) |
| 4812 | goto exit; |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4813 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
| 4814 | PyErr_Format(PyExc_TypeError, |
| 4815 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 4816 | _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod))); |
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4817 | goto exit; |
| 4818 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4819 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 4820 | if ((*s == -1) && PyErr_Occurred()) |
| 4821 | goto exit; |
| 4822 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4823 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4824 | goto exit; |
| 4825 | |
| 4826 | result = 1; |
| 4827 | exit: |
| 4828 | Py_XDECREF(divmod); |
| 4829 | return result; |
| 4830 | } |
| 4831 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4832 | |
| 4833 | /*[clinic input] |
| 4834 | os.utime |
| 4835 | |
| 4836 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4837 | times: object = None |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4838 | * |
| 4839 | ns: object = NULL |
| 4840 | dir_fd: dir_fd(requires='futimensat') = None |
| 4841 | follow_symlinks: bool=True |
| 4842 | |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4843 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4844 | |
| 4845 | Set the access and modified time of path. |
| 4846 | |
| 4847 | path may always be specified as a string. |
| 4848 | On some platforms, path may also be specified as an open file descriptor. |
| 4849 | If this functionality is unavailable, using it raises an exception. |
| 4850 | |
| 4851 | If times is not None, it must be a tuple (atime, mtime); |
| 4852 | atime and mtime should be expressed as float seconds since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4853 | 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] | 4854 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 4855 | since the epoch. |
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4856 | 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] | 4857 | Specifying tuples for both times and ns is an error. |
| 4858 | |
| 4859 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4860 | and path should be relative; path will then be relative to that directory. |
| 4861 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 4862 | link, utime will modify the symbolic link itself instead of the file the |
| 4863 | link points to. |
| 4864 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 4865 | as an open file descriptor. |
| 4866 | dir_fd and follow_symlinks may not be available on your platform. |
| 4867 | If they are unavailable, using them will raise a NotImplementedError. |
| 4868 | |
| 4869 | [clinic start generated code]*/ |
| 4870 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4871 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4872 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
| 4873 | int dir_fd, int follow_symlinks) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4874 | /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4875 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4876 | #ifdef MS_WINDOWS |
| 4877 | HANDLE hFile; |
| 4878 | FILETIME atime, mtime; |
| 4879 | #else |
| 4880 | int result; |
| 4881 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4882 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4883 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4884 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4885 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4886 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4887 | if (times != Py_None && ns) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4888 | PyErr_SetString(PyExc_ValueError, |
| 4889 | "utime: you may specify either 'times'" |
| 4890 | " or 'ns' but not both"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4891 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4892 | } |
| 4893 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4894 | if (times != Py_None) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4895 | time_t a_sec, m_sec; |
| 4896 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4897 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4898 | PyErr_SetString(PyExc_TypeError, |
| 4899 | "utime: 'times' must be either" |
| 4900 | " a tuple of two ints or None"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4901 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4902 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4903 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4904 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4905 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4906 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4907 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4908 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4909 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4910 | utime.atime_s = a_sec; |
| 4911 | utime.atime_ns = a_nsec; |
| 4912 | utime.mtime_s = m_sec; |
| 4913 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4914 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4915 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4916 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4917 | PyErr_SetString(PyExc_TypeError, |
| 4918 | "utime: 'ns' must be a tuple of two ints"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4919 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4920 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4921 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4922 | 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] | 4923 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4924 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4925 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4926 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4927 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4928 | } |
| 4929 | else { |
| 4930 | /* times and ns are both None/unspecified. use "now". */ |
| 4931 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4932 | } |
| 4933 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4934 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4935 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4936 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4937 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4938 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4939 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 4940 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 4941 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4942 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4943 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4944 | #if !defined(HAVE_UTIMENSAT) |
| 4945 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4946 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4947 | "utime: cannot use dir_fd and follow_symlinks " |
| 4948 | "together on this platform"); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4949 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4950 | } |
| 4951 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4952 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4953 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4954 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4955 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
| 4956 | NULL, OPEN_EXISTING, |
| 4957 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4958 | Py_END_ALLOW_THREADS |
| 4959 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4960 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4961 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4962 | } |
| 4963 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4964 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4965 | GetSystemTimeAsFileTime(&mtime); |
| 4966 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4967 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4968 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4969 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 4970 | _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] | 4971 | } |
| 4972 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 4973 | /* Avoid putting the file name into the error here, |
| 4974 | as that may confuse the user into believing that |
| 4975 | something is wrong with the file, when it also |
| 4976 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4977 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4978 | CloseHandle(hFile); |
| 4979 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4980 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4981 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4982 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4983 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4984 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4985 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4986 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4987 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4988 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4989 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4990 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4991 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4992 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4993 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4994 | else |
| 4995 | #endif |
| 4996 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4997 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4998 | if (path->fd != -1) |
| 4999 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5000 | else |
| 5001 | #endif |
| 5002 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5003 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5004 | |
| 5005 | Py_END_ALLOW_THREADS |
| 5006 | |
| 5007 | if (result < 0) { |
| 5008 | /* see previous comment about not putting filename in error here */ |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5009 | posix_error(); |
| 5010 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5011 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5012 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 5013 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5014 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 5015 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 5018 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5019 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5020 | |
| 5021 | /*[clinic input] |
| 5022 | os._exit |
| 5023 | |
| 5024 | status: int |
| 5025 | |
| 5026 | Exit to the system with specified status, without normal exit processing. |
| 5027 | [clinic start generated code]*/ |
| 5028 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5029 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5030 | os__exit_impl(PyObject *module, int status) |
| 5031 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5032 | { |
| 5033 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5034 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5037 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5038 | #define EXECV_CHAR wchar_t |
| 5039 | #else |
| 5040 | #define EXECV_CHAR char |
| 5041 | #endif |
| 5042 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5043 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5044 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5045 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5046 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5047 | Py_ssize_t i; |
| 5048 | for (i = 0; i < count; i++) |
| 5049 | PyMem_Free(array[i]); |
| 5050 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5051 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5052 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5053 | static int |
| 5054 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5055 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5056 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5057 | PyObject *ub; |
| 5058 | int result = 0; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5059 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5060 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5061 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5062 | *out = PyUnicode_AsWideCharString(ub, &size); |
| 5063 | if (*out) |
| 5064 | result = 1; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5065 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5066 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5067 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5068 | size = PyBytes_GET_SIZE(ub); |
| 5069 | *out = PyMem_Malloc(size + 1); |
| 5070 | if (*out) { |
| 5071 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); |
| 5072 | result = 1; |
| 5073 | } else |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5074 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5075 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5076 | Py_DECREF(ub); |
| 5077 | return result; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5078 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5079 | #endif |
| 5080 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5081 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5082 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5083 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 5084 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5085 | Py_ssize_t i, pos, envc; |
| 5086 | PyObject *keys=NULL, *vals=NULL; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5087 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5088 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5089 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5090 | i = PyMapping_Size(env); |
| 5091 | if (i < 0) |
| 5092 | return NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5093 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5094 | if (envlist == NULL) { |
| 5095 | PyErr_NoMemory(); |
| 5096 | return NULL; |
| 5097 | } |
| 5098 | envc = 0; |
| 5099 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5100 | if (!keys) |
| 5101 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5102 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5103 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5104 | goto error; |
| 5105 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 5106 | PyErr_Format(PyExc_TypeError, |
| 5107 | "env.keys() or env.values() is not a list"); |
| 5108 | goto error; |
| 5109 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5110 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5111 | for (pos = 0; pos < i; pos++) { |
| 5112 | key = PyList_GetItem(keys, pos); |
| 5113 | val = PyList_GetItem(vals, pos); |
| 5114 | if (!key || !val) |
| 5115 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5116 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5117 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
| 5118 | if (!PyUnicode_FSDecoder(key, &key2)) |
| 5119 | goto error; |
| 5120 | if (!PyUnicode_FSDecoder(val, &val2)) { |
| 5121 | Py_DECREF(key2); |
| 5122 | goto error; |
| 5123 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5124 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 5125 | defining hidden environment variables. */ |
| 5126 | if (PyUnicode_GET_LENGTH(key2) == 0 || |
| 5127 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) |
| 5128 | { |
| 5129 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5130 | Py_DECREF(key2); |
| 5131 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5132 | goto error; |
| 5133 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5134 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
| 5135 | #else |
| 5136 | if (!PyUnicode_FSConverter(key, &key2)) |
| 5137 | goto error; |
| 5138 | if (!PyUnicode_FSConverter(val, &val2)) { |
| 5139 | Py_DECREF(key2); |
| 5140 | goto error; |
| 5141 | } |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5142 | if (PyBytes_GET_SIZE(key2) == 0 || |
| 5143 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) |
| 5144 | { |
| 5145 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5146 | Py_DECREF(key2); |
| 5147 | Py_DECREF(val2); |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5148 | goto error; |
| 5149 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5150 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
| 5151 | PyBytes_AS_STRING(val2)); |
| 5152 | #endif |
| 5153 | Py_DECREF(key2); |
| 5154 | Py_DECREF(val2); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5155 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5156 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5157 | |
| 5158 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { |
| 5159 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5160 | goto error; |
| 5161 | } |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5162 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5163 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5164 | } |
| 5165 | Py_DECREF(vals); |
| 5166 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5167 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5168 | envlist[envc] = 0; |
| 5169 | *envc_ptr = envc; |
| 5170 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5171 | |
| 5172 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5173 | Py_XDECREF(keys); |
| 5174 | Py_XDECREF(vals); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5175 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5176 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5177 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5178 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5179 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5180 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 5181 | { |
| 5182 | int i; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5183 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5184 | if (argvlist == NULL) { |
| 5185 | PyErr_NoMemory(); |
| 5186 | return NULL; |
| 5187 | } |
| 5188 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5189 | PyObject* item = PySequence_ITEM(argv, i); |
| 5190 | if (item == NULL) |
| 5191 | goto fail; |
| 5192 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 5193 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5194 | goto fail; |
| 5195 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5196 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5197 | } |
| 5198 | argvlist[*argc] = NULL; |
| 5199 | return argvlist; |
| 5200 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5201 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5202 | free_string_array(argvlist, *argc); |
| 5203 | return NULL; |
| 5204 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5205 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5206 | #endif |
| 5207 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5208 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5209 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5210 | /*[clinic input] |
| 5211 | os.execv |
| 5212 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5213 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5214 | Path of executable file. |
| 5215 | argv: object |
| 5216 | Tuple or list of strings. |
| 5217 | / |
| 5218 | |
| 5219 | Execute an executable path with arguments, replacing current process. |
| 5220 | [clinic start generated code]*/ |
| 5221 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5222 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5223 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
| 5224 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5225 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5226 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5227 | Py_ssize_t argc; |
| 5228 | |
| 5229 | /* execv has two arguments: (path, argv), where |
| 5230 | argv is a list or tuple of strings. */ |
| 5231 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5232 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 5233 | PyErr_SetString(PyExc_TypeError, |
| 5234 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5235 | return NULL; |
| 5236 | } |
| 5237 | argc = PySequence_Size(argv); |
| 5238 | if (argc < 1) { |
| 5239 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5240 | return NULL; |
| 5241 | } |
| 5242 | |
| 5243 | argvlist = parse_arglist(argv, &argc); |
| 5244 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5245 | return NULL; |
| 5246 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5247 | if (!argvlist[0][0]) { |
| 5248 | PyErr_SetString(PyExc_ValueError, |
| 5249 | "execv() arg 2 first element cannot be empty"); |
| 5250 | free_string_array(argvlist, argc); |
| 5251 | return NULL; |
| 5252 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5253 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5254 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5255 | #ifdef HAVE_WEXECV |
| 5256 | _wexecv(path->wide, argvlist); |
| 5257 | #else |
| 5258 | execv(path->narrow, argvlist); |
| 5259 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5260 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5261 | |
| 5262 | /* If we get here it's definitely an error */ |
| 5263 | |
| 5264 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5265 | return posix_error(); |
| 5266 | } |
| 5267 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5268 | |
| 5269 | /*[clinic input] |
| 5270 | os.execve |
| 5271 | |
| 5272 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 5273 | Path of executable file. |
| 5274 | argv: object |
| 5275 | Tuple or list of strings. |
| 5276 | env: object |
| 5277 | Dictionary of strings mapping to strings. |
| 5278 | |
| 5279 | Execute an executable path with arguments, replacing current process. |
| 5280 | [clinic start generated code]*/ |
| 5281 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5282 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5283 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
| 5284 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5285 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5286 | EXECV_CHAR **argvlist = NULL; |
| 5287 | EXECV_CHAR **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5288 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5289 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5290 | /* execve has three arguments: (path, argv, env), where |
| 5291 | argv is a list or tuple of strings and env is a dictionary |
| 5292 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5293 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5294 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5295 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5296 | "execve: argv must be a tuple or list"); |
| 5297 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5298 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5299 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5300 | if (argc < 1) { |
| 5301 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); |
| 5302 | return NULL; |
| 5303 | } |
| 5304 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5305 | if (!PyMapping_Check(env)) { |
| 5306 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5307 | "execve: environment must be a mapping object"); |
| 5308 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5309 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5310 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5311 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5312 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5313 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5314 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5315 | if (!argvlist[0][0]) { |
| 5316 | PyErr_SetString(PyExc_ValueError, |
| 5317 | "execve: argv first element cannot be empty"); |
| 5318 | goto fail; |
| 5319 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5320 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5321 | envlist = parse_envlist(env, &envc); |
| 5322 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5323 | goto fail; |
| 5324 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5325 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5326 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5327 | if (path->fd > -1) |
| 5328 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5329 | else |
| 5330 | #endif |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5331 | #ifdef HAVE_WEXECV |
| 5332 | _wexecve(path->wide, argvlist, envlist); |
| 5333 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5334 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5335 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5336 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5337 | |
| 5338 | /* If we get here it's definitely an error */ |
| 5339 | |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5340 | posix_path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5341 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5342 | free_string_array(envlist, envc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5343 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5344 | if (argvlist) |
| 5345 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5346 | return NULL; |
| 5347 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5348 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5349 | #endif /* HAVE_EXECV */ |
| 5350 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5351 | #ifdef HAVE_POSIX_SPAWN |
| 5352 | |
| 5353 | enum posix_spawn_file_actions_identifier { |
| 5354 | POSIX_SPAWN_OPEN, |
| 5355 | POSIX_SPAWN_CLOSE, |
| 5356 | POSIX_SPAWN_DUP2 |
| 5357 | }; |
| 5358 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5359 | #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] | 5360 | static int |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5361 | convert_sched_param(PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5362 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5363 | |
| 5364 | static int |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5365 | parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup, |
| 5366 | int resetids, int setsid, PyObject *setsigmask, |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5367 | PyObject *setsigdef, PyObject *scheduler, |
| 5368 | posix_spawnattr_t *attrp) |
| 5369 | { |
| 5370 | long all_flags = 0; |
| 5371 | |
| 5372 | errno = posix_spawnattr_init(attrp); |
| 5373 | if (errno) { |
| 5374 | posix_error(); |
| 5375 | return -1; |
| 5376 | } |
| 5377 | |
| 5378 | if (setpgroup) { |
| 5379 | pid_t pgid = PyLong_AsPid(setpgroup); |
| 5380 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { |
| 5381 | goto fail; |
| 5382 | } |
| 5383 | errno = posix_spawnattr_setpgroup(attrp, pgid); |
| 5384 | if (errno) { |
| 5385 | posix_error(); |
| 5386 | goto fail; |
| 5387 | } |
| 5388 | all_flags |= POSIX_SPAWN_SETPGROUP; |
| 5389 | } |
| 5390 | |
| 5391 | if (resetids) { |
| 5392 | all_flags |= POSIX_SPAWN_RESETIDS; |
| 5393 | } |
| 5394 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5395 | if (setsid) { |
| 5396 | #ifdef POSIX_SPAWN_SETSID |
| 5397 | all_flags |= POSIX_SPAWN_SETSID; |
| 5398 | #elif defined(POSIX_SPAWN_SETSID_NP) |
| 5399 | all_flags |= POSIX_SPAWN_SETSID_NP; |
| 5400 | #else |
| 5401 | argument_unavailable_error(func_name, "setsid"); |
| 5402 | return -1; |
| 5403 | #endif |
| 5404 | } |
| 5405 | |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5406 | if (setsigmask) { |
| 5407 | sigset_t set; |
| 5408 | if (!_Py_Sigset_Converter(setsigmask, &set)) { |
| 5409 | goto fail; |
| 5410 | } |
| 5411 | errno = posix_spawnattr_setsigmask(attrp, &set); |
| 5412 | if (errno) { |
| 5413 | posix_error(); |
| 5414 | goto fail; |
| 5415 | } |
| 5416 | all_flags |= POSIX_SPAWN_SETSIGMASK; |
| 5417 | } |
| 5418 | |
| 5419 | if (setsigdef) { |
| 5420 | sigset_t set; |
| 5421 | if (!_Py_Sigset_Converter(setsigdef, &set)) { |
| 5422 | goto fail; |
| 5423 | } |
| 5424 | errno = posix_spawnattr_setsigdefault(attrp, &set); |
| 5425 | if (errno) { |
| 5426 | posix_error(); |
| 5427 | goto fail; |
| 5428 | } |
| 5429 | all_flags |= POSIX_SPAWN_SETSIGDEF; |
| 5430 | } |
| 5431 | |
| 5432 | if (scheduler) { |
| 5433 | #ifdef POSIX_SPAWN_SETSCHEDULER |
| 5434 | PyObject *py_schedpolicy; |
| 5435 | struct sched_param schedparam; |
| 5436 | |
| 5437 | if (!PyArg_ParseTuple(scheduler, "OO&" |
| 5438 | ";A scheduler tuple must have two elements", |
| 5439 | &py_schedpolicy, convert_sched_param, &schedparam)) { |
| 5440 | goto fail; |
| 5441 | } |
| 5442 | if (py_schedpolicy != Py_None) { |
| 5443 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); |
| 5444 | |
| 5445 | if (schedpolicy == -1 && PyErr_Occurred()) { |
| 5446 | goto fail; |
| 5447 | } |
| 5448 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); |
| 5449 | if (errno) { |
| 5450 | posix_error(); |
| 5451 | goto fail; |
| 5452 | } |
| 5453 | all_flags |= POSIX_SPAWN_SETSCHEDULER; |
| 5454 | } |
| 5455 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); |
| 5456 | if (errno) { |
| 5457 | posix_error(); |
| 5458 | goto fail; |
| 5459 | } |
| 5460 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; |
| 5461 | #else |
| 5462 | PyErr_SetString(PyExc_NotImplementedError, |
| 5463 | "The scheduler option is not supported in this system."); |
| 5464 | goto fail; |
| 5465 | #endif |
| 5466 | } |
| 5467 | |
| 5468 | errno = posix_spawnattr_setflags(attrp, all_flags); |
| 5469 | if (errno) { |
| 5470 | posix_error(); |
| 5471 | goto fail; |
| 5472 | } |
| 5473 | |
| 5474 | return 0; |
| 5475 | |
| 5476 | fail: |
| 5477 | (void)posix_spawnattr_destroy(attrp); |
| 5478 | return -1; |
| 5479 | } |
| 5480 | |
| 5481 | static int |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5482 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5483 | posix_spawn_file_actions_t *file_actionsp, |
| 5484 | PyObject *temp_buffer) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5485 | { |
| 5486 | PyObject *seq; |
| 5487 | PyObject *file_action = NULL; |
| 5488 | PyObject *tag_obj; |
| 5489 | |
| 5490 | seq = PySequence_Fast(file_actions, |
| 5491 | "file_actions must be a sequence or None"); |
| 5492 | if (seq == NULL) { |
| 5493 | return -1; |
| 5494 | } |
| 5495 | |
| 5496 | errno = posix_spawn_file_actions_init(file_actionsp); |
| 5497 | if (errno) { |
| 5498 | posix_error(); |
| 5499 | Py_DECREF(seq); |
| 5500 | return -1; |
| 5501 | } |
| 5502 | |
Zackery Spytz | d52a83a | 2019-06-26 14:54:20 -0600 | [diff] [blame] | 5503 | 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] | 5504 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
| 5505 | Py_INCREF(file_action); |
| 5506 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { |
| 5507 | PyErr_SetString(PyExc_TypeError, |
| 5508 | "Each file_actions element must be a non-empty tuple"); |
| 5509 | goto fail; |
| 5510 | } |
| 5511 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); |
| 5512 | if (tag == -1 && PyErr_Occurred()) { |
| 5513 | goto fail; |
| 5514 | } |
| 5515 | |
| 5516 | /* Populate the file_actions object */ |
| 5517 | switch (tag) { |
| 5518 | case POSIX_SPAWN_OPEN: { |
| 5519 | int fd, oflag; |
| 5520 | PyObject *path; |
| 5521 | unsigned long mode; |
| 5522 | if (!PyArg_ParseTuple(file_action, "OiO&ik" |
| 5523 | ";A open file_action tuple must have 5 elements", |
| 5524 | &tag_obj, &fd, PyUnicode_FSConverter, &path, |
| 5525 | &oflag, &mode)) |
| 5526 | { |
| 5527 | goto fail; |
| 5528 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5529 | if (PyList_Append(temp_buffer, path)) { |
| 5530 | Py_DECREF(path); |
| 5531 | goto fail; |
| 5532 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5533 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
| 5534 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5535 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5536 | if (errno) { |
| 5537 | posix_error(); |
| 5538 | goto fail; |
| 5539 | } |
| 5540 | break; |
| 5541 | } |
| 5542 | case POSIX_SPAWN_CLOSE: { |
| 5543 | int fd; |
| 5544 | if (!PyArg_ParseTuple(file_action, "Oi" |
| 5545 | ";A close file_action tuple must have 2 elements", |
| 5546 | &tag_obj, &fd)) |
| 5547 | { |
| 5548 | goto fail; |
| 5549 | } |
| 5550 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); |
| 5551 | if (errno) { |
| 5552 | posix_error(); |
| 5553 | goto fail; |
| 5554 | } |
| 5555 | break; |
| 5556 | } |
| 5557 | case POSIX_SPAWN_DUP2: { |
| 5558 | int fd1, fd2; |
| 5559 | if (!PyArg_ParseTuple(file_action, "Oii" |
| 5560 | ";A dup2 file_action tuple must have 3 elements", |
| 5561 | &tag_obj, &fd1, &fd2)) |
| 5562 | { |
| 5563 | goto fail; |
| 5564 | } |
| 5565 | errno = posix_spawn_file_actions_adddup2(file_actionsp, |
| 5566 | fd1, fd2); |
| 5567 | if (errno) { |
| 5568 | posix_error(); |
| 5569 | goto fail; |
| 5570 | } |
| 5571 | break; |
| 5572 | } |
| 5573 | default: { |
| 5574 | PyErr_SetString(PyExc_TypeError, |
| 5575 | "Unknown file_actions identifier"); |
| 5576 | goto fail; |
| 5577 | } |
| 5578 | } |
| 5579 | Py_DECREF(file_action); |
| 5580 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5581 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5582 | Py_DECREF(seq); |
| 5583 | return 0; |
| 5584 | |
| 5585 | fail: |
| 5586 | Py_DECREF(seq); |
| 5587 | Py_DECREF(file_action); |
| 5588 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
| 5589 | return -1; |
| 5590 | } |
| 5591 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5592 | |
| 5593 | static PyObject * |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5594 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
| 5595 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5596 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5597 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5598 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5599 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5600 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5601 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5602 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5603 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5604 | posix_spawnattr_t attr; |
| 5605 | posix_spawnattr_t *attrp = NULL; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5606 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5607 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5608 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5609 | pid_t pid; |
| 5610 | int err_code; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5611 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5612 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5613 | 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] | 5614 | like posix.environ. */ |
| 5615 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5616 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5617 | PyErr_Format(PyExc_TypeError, |
| 5618 | "%s: argv must be a tuple or list", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5619 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5620 | } |
| 5621 | argc = PySequence_Size(argv); |
| 5622 | if (argc < 1) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5623 | PyErr_Format(PyExc_ValueError, |
| 5624 | "%s: argv must not be empty", func_name); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5625 | return NULL; |
| 5626 | } |
| 5627 | |
| 5628 | if (!PyMapping_Check(env)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5629 | PyErr_Format(PyExc_TypeError, |
| 5630 | "%s: environment must be a mapping object", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5631 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5632 | } |
| 5633 | |
| 5634 | argvlist = parse_arglist(argv, &argc); |
| 5635 | if (argvlist == NULL) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5636 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5637 | } |
| 5638 | if (!argvlist[0][0]) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5639 | PyErr_Format(PyExc_ValueError, |
| 5640 | "%s: argv first element cannot be empty", func_name); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5641 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5642 | } |
| 5643 | |
| 5644 | envlist = parse_envlist(env, &envc); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5645 | if (envlist == NULL) { |
| 5646 | goto exit; |
| 5647 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5648 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 5649 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5650 | /* There is a bug in old versions of glibc that makes some of the |
| 5651 | * helper functions for manipulating file actions not copy the provided |
| 5652 | * buffers. The problem is that posix_spawn_file_actions_addopen does not |
| 5653 | * copy the value of path for some old versions of glibc (<2.20). |
| 5654 | * The use of temp_buffer here is a workaround that keeps the |
| 5655 | * python objects that own the buffers alive until posix_spawn gets called. |
| 5656 | * Check https://bugs.python.org/issue33630 and |
| 5657 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ |
| 5658 | temp_buffer = PyList_New(0); |
| 5659 | if (!temp_buffer) { |
| 5660 | goto exit; |
| 5661 | } |
| 5662 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5663 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5664 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5665 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5666 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5667 | |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5668 | if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid, |
| 5669 | setsigmask, setsigdef, scheduler, &attr)) { |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5670 | goto exit; |
| 5671 | } |
| 5672 | attrp = &attr; |
| 5673 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5674 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5675 | #ifdef HAVE_POSIX_SPAWNP |
| 5676 | if (use_posix_spawnp) { |
| 5677 | err_code = posix_spawnp(&pid, path->narrow, |
| 5678 | file_actionsp, attrp, argvlist, envlist); |
| 5679 | } |
| 5680 | else |
| 5681 | #endif /* HAVE_POSIX_SPAWNP */ |
| 5682 | { |
| 5683 | err_code = posix_spawn(&pid, path->narrow, |
| 5684 | file_actionsp, attrp, argvlist, envlist); |
| 5685 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5686 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5687 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5688 | if (err_code) { |
| 5689 | errno = err_code; |
| 5690 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5691 | goto exit; |
| 5692 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5693 | #ifdef _Py_MEMORY_SANITIZER |
| 5694 | __msan_unpoison(&pid, sizeof(pid)); |
| 5695 | #endif |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5696 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5697 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5698 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5699 | if (file_actionsp) { |
| 5700 | (void)posix_spawn_file_actions_destroy(file_actionsp); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5701 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5702 | if (attrp) { |
| 5703 | (void)posix_spawnattr_destroy(attrp); |
| 5704 | } |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5705 | if (envlist) { |
| 5706 | free_string_array(envlist, envc); |
| 5707 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5708 | if (argvlist) { |
| 5709 | free_string_array(argvlist, argc); |
| 5710 | } |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5711 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5712 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5713 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5714 | |
| 5715 | |
| 5716 | /*[clinic input] |
| 5717 | |
| 5718 | os.posix_spawn |
| 5719 | path: path_t |
| 5720 | Path of executable file. |
| 5721 | argv: object |
| 5722 | Tuple or list of strings. |
| 5723 | env: object |
| 5724 | Dictionary of strings mapping to strings. |
| 5725 | / |
| 5726 | * |
| 5727 | file_actions: object(c_default='NULL') = () |
| 5728 | A sequence of file action tuples. |
| 5729 | setpgroup: object = NULL |
| 5730 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5731 | resetids: bool(accept={int}) = False |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5732 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
| 5733 | setsid: bool(accept={int}) = False |
| 5734 | 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] | 5735 | setsigmask: object(c_default='NULL') = () |
| 5736 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5737 | setsigdef: object(c_default='NULL') = () |
| 5738 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5739 | scheduler: object = NULL |
| 5740 | A tuple with the scheduler policy (optional) and parameters. |
| 5741 | |
| 5742 | Execute the program specified by path in a new process. |
| 5743 | [clinic start generated code]*/ |
| 5744 | |
| 5745 | static PyObject * |
| 5746 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5747 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5748 | PyObject *setpgroup, int resetids, int setsid, |
| 5749 | PyObject *setsigmask, PyObject *setsigdef, |
| 5750 | PyObject *scheduler) |
| 5751 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5752 | { |
| 5753 | return py_posix_spawn(0, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5754 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5755 | scheduler); |
| 5756 | } |
| 5757 | #endif /* HAVE_POSIX_SPAWN */ |
| 5758 | |
| 5759 | |
| 5760 | |
| 5761 | #ifdef HAVE_POSIX_SPAWNP |
| 5762 | /*[clinic input] |
| 5763 | |
| 5764 | os.posix_spawnp |
| 5765 | path: path_t |
| 5766 | Path of executable file. |
| 5767 | argv: object |
| 5768 | Tuple or list of strings. |
| 5769 | env: object |
| 5770 | Dictionary of strings mapping to strings. |
| 5771 | / |
| 5772 | * |
| 5773 | file_actions: object(c_default='NULL') = () |
| 5774 | A sequence of file action tuples. |
| 5775 | setpgroup: object = NULL |
| 5776 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. |
| 5777 | resetids: bool(accept={int}) = False |
| 5778 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5779 | setsid: bool(accept={int}) = False |
| 5780 | 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] | 5781 | setsigmask: object(c_default='NULL') = () |
| 5782 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. |
| 5783 | setsigdef: object(c_default='NULL') = () |
| 5784 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. |
| 5785 | scheduler: object = NULL |
| 5786 | A tuple with the scheduler policy (optional) and parameters. |
| 5787 | |
| 5788 | Execute the program specified by path in a new process. |
| 5789 | [clinic start generated code]*/ |
| 5790 | |
| 5791 | static PyObject * |
| 5792 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, |
| 5793 | PyObject *env, PyObject *file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5794 | PyObject *setpgroup, int resetids, int setsid, |
| 5795 | PyObject *setsigmask, PyObject *setsigdef, |
| 5796 | PyObject *scheduler) |
| 5797 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5798 | { |
| 5799 | return py_posix_spawn(1, module, path, argv, env, file_actions, |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5800 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5801 | scheduler); |
| 5802 | } |
| 5803 | #endif /* HAVE_POSIX_SPAWNP */ |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5804 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5805 | #ifdef HAVE_RTPSPAWN |
| 5806 | static intptr_t |
| 5807 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], |
| 5808 | const char *envp[]) |
| 5809 | { |
| 5810 | RTP_ID rtpid; |
| 5811 | int status; |
| 5812 | pid_t res; |
| 5813 | int async_err = 0; |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5814 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5815 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
| 5816 | uStackSize=0 cannot be used, the default stack size is too small for |
| 5817 | Python. */ |
| 5818 | if (envp) { |
| 5819 | rtpid = rtpSpawn(rtpFileName, argv, envp, |
| 5820 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5821 | } |
| 5822 | else { |
| 5823 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, |
| 5824 | 100, 0x1000000, 0, VX_FP_TASK); |
| 5825 | } |
| 5826 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { |
| 5827 | do { |
| 5828 | res = waitpid((pid_t)rtpid, &status, 0); |
| 5829 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 5830 | |
| 5831 | if (res < 0) |
| 5832 | return RTP_ID_ERROR; |
| 5833 | return ((intptr_t)status); |
| 5834 | } |
| 5835 | return ((intptr_t)rtpid); |
| 5836 | } |
| 5837 | #endif |
| 5838 | |
| 5839 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5840 | /*[clinic input] |
| 5841 | os.spawnv |
| 5842 | |
| 5843 | mode: int |
| 5844 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5845 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5846 | Path of executable file. |
| 5847 | argv: object |
| 5848 | Tuple or list of strings. |
| 5849 | / |
| 5850 | |
| 5851 | Execute the program specified by path in a new process. |
| 5852 | [clinic start generated code]*/ |
| 5853 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5854 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5855 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
| 5856 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5857 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5858 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5859 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5860 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5861 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5862 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5863 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5864 | /* spawnv has three arguments: (mode, path, argv), where |
| 5865 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5866 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5867 | if (PyList_Check(argv)) { |
| 5868 | argc = PyList_Size(argv); |
| 5869 | getitem = PyList_GetItem; |
| 5870 | } |
| 5871 | else if (PyTuple_Check(argv)) { |
| 5872 | argc = PyTuple_Size(argv); |
| 5873 | getitem = PyTuple_GetItem; |
| 5874 | } |
| 5875 | else { |
| 5876 | PyErr_SetString(PyExc_TypeError, |
| 5877 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5878 | return NULL; |
| 5879 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5880 | if (argc == 0) { |
| 5881 | PyErr_SetString(PyExc_ValueError, |
| 5882 | "spawnv() arg 2 cannot be empty"); |
| 5883 | return NULL; |
| 5884 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5885 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5886 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5887 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5888 | return PyErr_NoMemory(); |
| 5889 | } |
| 5890 | for (i = 0; i < argc; i++) { |
| 5891 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5892 | &argvlist[i])) { |
| 5893 | free_string_array(argvlist, i); |
| 5894 | PyErr_SetString( |
| 5895 | PyExc_TypeError, |
| 5896 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5897 | return NULL; |
| 5898 | } |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5899 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5900 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5901 | PyErr_SetString( |
| 5902 | PyExc_ValueError, |
| 5903 | "spawnv() arg 2 first element cannot be empty"); |
| 5904 | return NULL; |
| 5905 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5906 | } |
| 5907 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5908 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5909 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5910 | if (mode == _OLD_P_OVERLAY) |
| 5911 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5912 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5913 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5914 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5915 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5916 | #ifdef HAVE_WSPAWNV |
| 5917 | spawnval = _wspawnv(mode, path->wide, argvlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5918 | #elif defined(HAVE_RTPSPAWN) |
| 5919 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5920 | #else |
| 5921 | spawnval = _spawnv(mode, path->narrow, argvlist); |
| 5922 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5923 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5924 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5925 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5926 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5927 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5928 | if (spawnval == -1) |
| 5929 | return posix_error(); |
| 5930 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5931 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5932 | } |
| 5933 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5934 | /*[clinic input] |
| 5935 | os.spawnve |
| 5936 | |
| 5937 | mode: int |
| 5938 | Mode of process creation. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5939 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5940 | Path of executable file. |
| 5941 | argv: object |
| 5942 | Tuple or list of strings. |
| 5943 | env: object |
| 5944 | Dictionary of strings mapping to strings. |
| 5945 | / |
| 5946 | |
| 5947 | Execute the program specified by path in a new process. |
| 5948 | [clinic start generated code]*/ |
| 5949 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5950 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5951 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5952 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5953 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5954 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5955 | EXECV_CHAR **argvlist; |
| 5956 | EXECV_CHAR **envlist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5957 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5958 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5959 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5960 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5961 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5962 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5963 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 5964 | argv is a list or tuple of strings and env is a dictionary |
| 5965 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5966 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5967 | if (PyList_Check(argv)) { |
| 5968 | argc = PyList_Size(argv); |
| 5969 | getitem = PyList_GetItem; |
| 5970 | } |
| 5971 | else if (PyTuple_Check(argv)) { |
| 5972 | argc = PyTuple_Size(argv); |
| 5973 | getitem = PyTuple_GetItem; |
| 5974 | } |
| 5975 | else { |
| 5976 | PyErr_SetString(PyExc_TypeError, |
| 5977 | "spawnve() arg 2 must be a tuple or list"); |
| 5978 | goto fail_0; |
| 5979 | } |
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5980 | if (argc == 0) { |
| 5981 | PyErr_SetString(PyExc_ValueError, |
| 5982 | "spawnve() arg 2 cannot be empty"); |
| 5983 | goto fail_0; |
| 5984 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5985 | if (!PyMapping_Check(env)) { |
| 5986 | PyErr_SetString(PyExc_TypeError, |
| 5987 | "spawnve() arg 3 must be a mapping object"); |
| 5988 | goto fail_0; |
| 5989 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5990 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5991 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5992 | if (argvlist == NULL) { |
| 5993 | PyErr_NoMemory(); |
| 5994 | goto fail_0; |
| 5995 | } |
| 5996 | for (i = 0; i < argc; i++) { |
| 5997 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 5998 | &argvlist[i])) |
| 5999 | { |
| 6000 | lastarg = i; |
| 6001 | goto fail_1; |
| 6002 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6003 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6004 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 6005 | PyErr_SetString( |
| 6006 | PyExc_ValueError, |
| 6007 | "spawnv() arg 2 first element cannot be empty"); |
| 6008 | goto fail_1; |
| 6009 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6010 | } |
| 6011 | lastarg = argc; |
| 6012 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6013 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6014 | envlist = parse_envlist(env, &envc); |
| 6015 | if (envlist == NULL) |
| 6016 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6017 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6018 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6019 | if (mode == _OLD_P_OVERLAY) |
| 6020 | mode = _P_OVERLAY; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6021 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6022 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6023 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6024 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6025 | #ifdef HAVE_WSPAWNV |
| 6026 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6027 | #elif defined(HAVE_RTPSPAWN) |
| 6028 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, |
| 6029 | (const char **)envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6030 | #else |
| 6031 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); |
| 6032 | #endif |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6033 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6034 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6035 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6036 | if (spawnval == -1) |
| 6037 | (void) posix_error(); |
| 6038 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6039 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6040 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6041 | while (--envc >= 0) |
| 6042 | PyMem_DEL(envlist[envc]); |
| 6043 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 6044 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6045 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6046 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6047 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6048 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6049 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6050 | #endif /* HAVE_SPAWNV */ |
| 6051 | |
| 6052 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6053 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6054 | |
| 6055 | /* Helper function to validate arguments. |
| 6056 | Returns 0 on success. non-zero on failure with a TypeError raised. |
| 6057 | If obj is non-NULL it must be callable. */ |
| 6058 | static int |
| 6059 | check_null_or_callable(PyObject *obj, const char* obj_name) |
| 6060 | { |
| 6061 | if (obj && !PyCallable_Check(obj)) { |
| 6062 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6063 | obj_name, _PyType_Name(Py_TYPE(obj))); |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6064 | return -1; |
| 6065 | } |
| 6066 | return 0; |
| 6067 | } |
| 6068 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6069 | /*[clinic input] |
| 6070 | os.register_at_fork |
| 6071 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6072 | * |
| 6073 | before: object=NULL |
| 6074 | A callable to be called in the parent before the fork() syscall. |
| 6075 | after_in_child: object=NULL |
| 6076 | A callable to be called in the child after fork(). |
| 6077 | after_in_parent: object=NULL |
| 6078 | A callable to be called in the parent after fork(). |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6079 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6080 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6081 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6082 | 'before' callbacks are called in reverse order. |
| 6083 | 'after_in_child' and 'after_in_parent' callbacks are called in order. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6084 | |
| 6085 | [clinic start generated code]*/ |
| 6086 | |
| 6087 | static PyObject * |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6088 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
| 6089 | PyObject *after_in_child, PyObject *after_in_parent) |
| 6090 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6091 | { |
| 6092 | PyInterpreterState *interp; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6093 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6094 | if (!before && !after_in_child && !after_in_parent) { |
| 6095 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); |
| 6096 | return NULL; |
| 6097 | } |
| 6098 | if (check_null_or_callable(before, "before") || |
| 6099 | check_null_or_callable(after_in_child, "after_in_child") || |
| 6100 | check_null_or_callable(after_in_parent, "after_in_parent")) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6101 | return NULL; |
| 6102 | } |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 6103 | interp = _PyInterpreterState_Get(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6104 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6105 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6106 | return NULL; |
| 6107 | } |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6108 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6109 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6110 | } |
| 6111 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { |
| 6112 | return NULL; |
| 6113 | } |
| 6114 | Py_RETURN_NONE; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6115 | } |
| 6116 | #endif /* HAVE_FORK */ |
| 6117 | |
| 6118 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6119 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6120 | /*[clinic input] |
| 6121 | os.fork1 |
| 6122 | |
| 6123 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 6124 | |
| 6125 | Return 0 to child process and PID of child to parent process. |
| 6126 | [clinic start generated code]*/ |
| 6127 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6128 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6129 | os_fork1_impl(PyObject *module) |
| 6130 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6131 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6132 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6133 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6134 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6135 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6136 | return NULL; |
| 6137 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6138 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6139 | pid = fork1(); |
| 6140 | if (pid == 0) { |
| 6141 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6142 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6143 | } else { |
| 6144 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6145 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6146 | } |
| 6147 | if (pid == -1) |
| 6148 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6149 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6150 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6151 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6152 | |
| 6153 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6154 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6155 | /*[clinic input] |
| 6156 | os.fork |
| 6157 | |
| 6158 | Fork a child process. |
| 6159 | |
| 6160 | Return 0 to child process and PID of child to parent process. |
| 6161 | [clinic start generated code]*/ |
| 6162 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6163 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6164 | os_fork_impl(PyObject *module) |
| 6165 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6166 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6167 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6168 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6169 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6170 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6171 | return NULL; |
| 6172 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6173 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6174 | pid = fork(); |
| 6175 | if (pid == 0) { |
| 6176 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6177 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6178 | } else { |
| 6179 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6180 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6181 | } |
| 6182 | if (pid == -1) |
| 6183 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6184 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6185 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6186 | #endif /* HAVE_FORK */ |
| 6187 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6188 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6189 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6190 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6191 | /*[clinic input] |
| 6192 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6193 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6194 | policy: int |
| 6195 | |
| 6196 | Get the maximum scheduling priority for policy. |
| 6197 | [clinic start generated code]*/ |
| 6198 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6199 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6200 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
| 6201 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6202 | { |
| 6203 | int max; |
| 6204 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6205 | max = sched_get_priority_max(policy); |
| 6206 | if (max < 0) |
| 6207 | return posix_error(); |
| 6208 | return PyLong_FromLong(max); |
| 6209 | } |
| 6210 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6211 | |
| 6212 | /*[clinic input] |
| 6213 | os.sched_get_priority_min |
| 6214 | |
| 6215 | policy: int |
| 6216 | |
| 6217 | Get the minimum scheduling priority for policy. |
| 6218 | [clinic start generated code]*/ |
| 6219 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6220 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6221 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
| 6222 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6223 | { |
| 6224 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6225 | if (min < 0) |
| 6226 | return posix_error(); |
| 6227 | return PyLong_FromLong(min); |
| 6228 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6229 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 6230 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6231 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6232 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 6233 | /*[clinic input] |
| 6234 | os.sched_getscheduler |
| 6235 | pid: pid_t |
| 6236 | / |
| 6237 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6238 | Get the scheduling policy for the process identified by pid. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6239 | |
| 6240 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 6241 | [clinic start generated code]*/ |
| 6242 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6243 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6244 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 6245 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6246 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6247 | int policy; |
| 6248 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6249 | policy = sched_getscheduler(pid); |
| 6250 | if (policy < 0) |
| 6251 | return posix_error(); |
| 6252 | return PyLong_FromLong(policy); |
| 6253 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6254 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6255 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6256 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6257 | #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] | 6258 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6259 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6260 | |
| 6261 | @classmethod |
| 6262 | os.sched_param.__new__ |
| 6263 | |
| 6264 | sched_priority: object |
| 6265 | A scheduling parameter. |
| 6266 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6267 | Currently has only one field: sched_priority |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6268 | [clinic start generated code]*/ |
| 6269 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6270 | static PyObject * |
| 6271 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6272 | /*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6273 | { |
| 6274 | PyObject *res; |
| 6275 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6276 | res = PyStructSequence_New(type); |
| 6277 | if (!res) |
| 6278 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6279 | Py_INCREF(sched_priority); |
| 6280 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6281 | return res; |
| 6282 | } |
| 6283 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6284 | PyDoc_VAR(os_sched_param__doc__); |
| 6285 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6286 | static PyStructSequence_Field sched_param_fields[] = { |
| 6287 | {"sched_priority", "the scheduling priority"}, |
| 6288 | {0} |
| 6289 | }; |
| 6290 | |
| 6291 | static PyStructSequence_Desc sched_param_desc = { |
| 6292 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6293 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6294 | sched_param_fields, |
| 6295 | 1 |
| 6296 | }; |
| 6297 | |
| 6298 | static int |
| 6299 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 6300 | { |
| 6301 | long priority; |
| 6302 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6303 | PyObject *SchedParamType = _posixstate_global->SchedParamType; |
| 6304 | if (Py_TYPE(param) != (PyTypeObject *)SchedParamType) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6305 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 6306 | return 0; |
| 6307 | } |
| 6308 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 6309 | if (priority == -1 && PyErr_Occurred()) |
| 6310 | return 0; |
| 6311 | if (priority > INT_MAX || priority < INT_MIN) { |
| 6312 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 6313 | return 0; |
| 6314 | } |
| 6315 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 6316 | return 1; |
| 6317 | } |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6318 | #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] | 6319 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6320 | |
| 6321 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6322 | /*[clinic input] |
| 6323 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6324 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6325 | pid: pid_t |
| 6326 | policy: int |
| 6327 | param: sched_param |
| 6328 | / |
| 6329 | |
| 6330 | Set the scheduling policy for the process identified by pid. |
| 6331 | |
| 6332 | If pid is 0, the calling process is changed. |
| 6333 | param is an instance of sched_param. |
| 6334 | [clinic start generated code]*/ |
| 6335 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6336 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6337 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6338 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6339 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6340 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6341 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6342 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 6343 | ** scheduling policy under Solaris/Illumos, and others. |
| 6344 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6345 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6346 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6347 | return posix_error(); |
| 6348 | Py_RETURN_NONE; |
| 6349 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6350 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6351 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6352 | |
| 6353 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6354 | /*[clinic input] |
| 6355 | os.sched_getparam |
| 6356 | pid: pid_t |
| 6357 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6358 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6359 | Returns scheduling parameters for the process identified by pid. |
| 6360 | |
| 6361 | If pid is 0, returns parameters for the calling process. |
| 6362 | Return value is an instance of sched_param. |
| 6363 | [clinic start generated code]*/ |
| 6364 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6365 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6366 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
| 6367 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6368 | { |
| 6369 | struct sched_param param; |
| 6370 | PyObject *result; |
| 6371 | PyObject *priority; |
| 6372 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6373 | if (sched_getparam(pid, ¶m)) |
| 6374 | return posix_error(); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 6375 | PyObject *SchedParamType = _posixstate_global->SchedParamType; |
| 6376 | result = PyStructSequence_New((PyTypeObject *)SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6377 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6378 | return NULL; |
| 6379 | priority = PyLong_FromLong(param.sched_priority); |
| 6380 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6381 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6382 | return NULL; |
| 6383 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6384 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 6385 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6386 | } |
| 6387 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6388 | |
| 6389 | /*[clinic input] |
| 6390 | os.sched_setparam |
| 6391 | pid: pid_t |
| 6392 | param: sched_param |
| 6393 | / |
| 6394 | |
| 6395 | Set scheduling parameters for the process identified by pid. |
| 6396 | |
| 6397 | If pid is 0, sets parameters for the calling process. |
| 6398 | param should be an instance of sched_param. |
| 6399 | [clinic start generated code]*/ |
| 6400 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6401 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6402 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6403 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6404 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6405 | { |
| 6406 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6407 | return posix_error(); |
| 6408 | Py_RETURN_NONE; |
| 6409 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6410 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6411 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6412 | |
| 6413 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6414 | /*[clinic input] |
| 6415 | os.sched_rr_get_interval -> double |
| 6416 | pid: pid_t |
| 6417 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6418 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6419 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 6420 | |
| 6421 | Value returned is a float. |
| 6422 | [clinic start generated code]*/ |
| 6423 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6424 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6425 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
| 6426 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6427 | { |
| 6428 | struct timespec interval; |
| 6429 | if (sched_rr_get_interval(pid, &interval)) { |
| 6430 | posix_error(); |
| 6431 | return -1.0; |
| 6432 | } |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6433 | #ifdef _Py_MEMORY_SANITIZER |
| 6434 | __msan_unpoison(&interval, sizeof(interval)); |
| 6435 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6436 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 6437 | } |
| 6438 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6439 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6440 | |
| 6441 | /*[clinic input] |
| 6442 | os.sched_yield |
| 6443 | |
| 6444 | Voluntarily relinquish the CPU. |
| 6445 | [clinic start generated code]*/ |
| 6446 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6447 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6448 | os_sched_yield_impl(PyObject *module) |
| 6449 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6450 | { |
| 6451 | if (sched_yield()) |
| 6452 | return posix_error(); |
| 6453 | Py_RETURN_NONE; |
| 6454 | } |
| 6455 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6456 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6457 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 6458 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6459 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6460 | /*[clinic input] |
| 6461 | os.sched_setaffinity |
| 6462 | pid: pid_t |
| 6463 | mask : object |
| 6464 | / |
| 6465 | |
| 6466 | Set the CPU affinity of the process identified by pid to mask. |
| 6467 | |
| 6468 | mask should be an iterable of integers identifying CPUs. |
| 6469 | [clinic start generated code]*/ |
| 6470 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6471 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6472 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
| 6473 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6474 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6475 | int ncpus; |
| 6476 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6477 | cpu_set_t *cpu_set = NULL; |
| 6478 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6479 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6480 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6481 | if (iterator == NULL) |
| 6482 | return NULL; |
| 6483 | |
| 6484 | ncpus = NCPUS_START; |
| 6485 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6486 | cpu_set = CPU_ALLOC(ncpus); |
| 6487 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6488 | PyErr_NoMemory(); |
| 6489 | goto error; |
| 6490 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6491 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6492 | |
| 6493 | while ((item = PyIter_Next(iterator))) { |
| 6494 | long cpu; |
| 6495 | if (!PyLong_Check(item)) { |
| 6496 | PyErr_Format(PyExc_TypeError, |
| 6497 | "expected an iterator of ints, " |
| 6498 | "but iterator yielded %R", |
| 6499 | Py_TYPE(item)); |
| 6500 | Py_DECREF(item); |
| 6501 | goto error; |
| 6502 | } |
| 6503 | cpu = PyLong_AsLong(item); |
| 6504 | Py_DECREF(item); |
| 6505 | if (cpu < 0) { |
| 6506 | if (!PyErr_Occurred()) |
| 6507 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 6508 | goto error; |
| 6509 | } |
| 6510 | if (cpu > INT_MAX - 1) { |
| 6511 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 6512 | goto error; |
| 6513 | } |
| 6514 | if (cpu >= ncpus) { |
| 6515 | /* Grow CPU mask to fit the CPU number */ |
| 6516 | int newncpus = ncpus; |
| 6517 | cpu_set_t *newmask; |
| 6518 | size_t newsetsize; |
| 6519 | while (newncpus <= cpu) { |
| 6520 | if (newncpus > INT_MAX / 2) |
| 6521 | newncpus = cpu + 1; |
| 6522 | else |
| 6523 | newncpus = newncpus * 2; |
| 6524 | } |
| 6525 | newmask = CPU_ALLOC(newncpus); |
| 6526 | if (newmask == NULL) { |
| 6527 | PyErr_NoMemory(); |
| 6528 | goto error; |
| 6529 | } |
| 6530 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 6531 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6532 | memcpy(newmask, cpu_set, setsize); |
| 6533 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6534 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6535 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6536 | ncpus = newncpus; |
| 6537 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6538 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6539 | } |
Brandt Bucher | 45a30af | 2019-06-27 09:10:57 -0700 | [diff] [blame] | 6540 | if (PyErr_Occurred()) { |
| 6541 | goto error; |
| 6542 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6543 | Py_CLEAR(iterator); |
| 6544 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6545 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6546 | posix_error(); |
| 6547 | goto error; |
| 6548 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6549 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6550 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6551 | |
| 6552 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6553 | if (cpu_set) |
| 6554 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6555 | Py_XDECREF(iterator); |
| 6556 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6557 | } |
| 6558 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6559 | |
| 6560 | /*[clinic input] |
| 6561 | os.sched_getaffinity |
| 6562 | pid: pid_t |
| 6563 | / |
| 6564 | |
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6565 | 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] | 6566 | |
| 6567 | The affinity is returned as a set of CPU identifiers. |
| 6568 | [clinic start generated code]*/ |
| 6569 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6570 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6571 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6572 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6573 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6574 | int cpu, ncpus, count; |
| 6575 | size_t setsize; |
| 6576 | cpu_set_t *mask = NULL; |
| 6577 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6578 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6579 | ncpus = NCPUS_START; |
| 6580 | while (1) { |
| 6581 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 6582 | mask = CPU_ALLOC(ncpus); |
| 6583 | if (mask == NULL) |
| 6584 | return PyErr_NoMemory(); |
| 6585 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 6586 | break; |
| 6587 | CPU_FREE(mask); |
| 6588 | if (errno != EINVAL) |
| 6589 | return posix_error(); |
| 6590 | if (ncpus > INT_MAX / 2) { |
| 6591 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 6592 | "a large enough CPU set"); |
| 6593 | return NULL; |
| 6594 | } |
| 6595 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6596 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6597 | |
| 6598 | res = PySet_New(NULL); |
| 6599 | if (res == NULL) |
| 6600 | goto error; |
| 6601 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 6602 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 6603 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 6604 | --count; |
| 6605 | if (cpu_num == NULL) |
| 6606 | goto error; |
| 6607 | if (PySet_Add(res, cpu_num)) { |
| 6608 | Py_DECREF(cpu_num); |
| 6609 | goto error; |
| 6610 | } |
| 6611 | Py_DECREF(cpu_num); |
| 6612 | } |
| 6613 | } |
| 6614 | CPU_FREE(mask); |
| 6615 | return res; |
| 6616 | |
| 6617 | error: |
| 6618 | if (mask) |
| 6619 | CPU_FREE(mask); |
| 6620 | Py_XDECREF(res); |
| 6621 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6622 | } |
| 6623 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6624 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 6625 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6626 | #endif /* HAVE_SCHED_H */ |
| 6627 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6628 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6629 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6630 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 6631 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6632 | #define DEV_PTY_FILE "/dev/ptc" |
| 6633 | #define HAVE_DEV_PTMX |
| 6634 | #else |
| 6635 | #define DEV_PTY_FILE "/dev/ptmx" |
| 6636 | #endif |
| 6637 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6638 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6639 | #ifdef HAVE_PTY_H |
| 6640 | #include <pty.h> |
| 6641 | #else |
| 6642 | #ifdef HAVE_LIBUTIL_H |
| 6643 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6644 | #else |
| 6645 | #ifdef HAVE_UTIL_H |
| 6646 | #include <util.h> |
| 6647 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6648 | #endif /* HAVE_LIBUTIL_H */ |
| 6649 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6650 | #ifdef HAVE_STROPTS_H |
| 6651 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6652 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6653 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6654 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6655 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6656 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6657 | /*[clinic input] |
| 6658 | os.openpty |
| 6659 | |
| 6660 | Open a pseudo-terminal. |
| 6661 | |
| 6662 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 6663 | for both the master and slave ends. |
| 6664 | [clinic start generated code]*/ |
| 6665 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6666 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6667 | os_openpty_impl(PyObject *module) |
| 6668 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6669 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6670 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6671 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6672 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6673 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6674 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6675 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6676 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6677 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6678 | #endif |
| 6679 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6680 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6681 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6682 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6683 | goto posix_error; |
| 6684 | |
| 6685 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6686 | goto error; |
| 6687 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 6688 | goto error; |
| 6689 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6690 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6691 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 6692 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6693 | goto posix_error; |
| 6694 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6695 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6696 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6697 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6698 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6699 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6700 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6701 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6702 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6703 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6704 | goto posix_error; |
| 6705 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6706 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6707 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6708 | /* change permission of slave */ |
| 6709 | if (grantpt(master_fd) < 0) { |
| 6710 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6711 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6712 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6713 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6714 | /* unlock slave */ |
| 6715 | if (unlockpt(master_fd) < 0) { |
| 6716 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6717 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6718 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6719 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6720 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6721 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6722 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 6723 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6724 | goto posix_error; |
| 6725 | |
| 6726 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6727 | if (slave_fd == -1) |
| 6728 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6729 | |
| 6730 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 6731 | goto posix_error; |
| 6732 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6733 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6734 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 6735 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6736 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6737 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6738 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6739 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6740 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6741 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6742 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6743 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6744 | posix_error: |
| 6745 | posix_error(); |
| 6746 | error: |
| 6747 | if (master_fd != -1) |
| 6748 | close(master_fd); |
| 6749 | if (slave_fd != -1) |
| 6750 | close(slave_fd); |
| 6751 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6752 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6753 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6754 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6755 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6756 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6757 | /*[clinic input] |
| 6758 | os.forkpty |
| 6759 | |
| 6760 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 6761 | |
| 6762 | Returns a tuple of (pid, master_fd). |
| 6763 | Like fork(), return pid of 0 to the child process, |
| 6764 | and pid of child to the parent process. |
| 6765 | To both, return fd of newly opened pseudo-terminal. |
| 6766 | [clinic start generated code]*/ |
| 6767 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6768 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6769 | os_forkpty_impl(PyObject *module) |
| 6770 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6771 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6772 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6773 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6774 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6775 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
| 6776 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); |
| 6777 | return NULL; |
| 6778 | } |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6779 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6780 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 6781 | if (pid == 0) { |
| 6782 | /* child: this clobbers and resets the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6783 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6784 | } else { |
| 6785 | /* parent: release the import lock. */ |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6786 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6787 | } |
| 6788 | if (pid == -1) |
| 6789 | return posix_error(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6790 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6791 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6792 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6793 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6794 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6795 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6796 | /*[clinic input] |
| 6797 | os.getegid |
| 6798 | |
| 6799 | Return the current process's effective group id. |
| 6800 | [clinic start generated code]*/ |
| 6801 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6802 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6803 | os_getegid_impl(PyObject *module) |
| 6804 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6805 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6806 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6807 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6808 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6809 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6810 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6811 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6812 | /*[clinic input] |
| 6813 | os.geteuid |
| 6814 | |
| 6815 | Return the current process's effective user id. |
| 6816 | [clinic start generated code]*/ |
| 6817 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6818 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6819 | os_geteuid_impl(PyObject *module) |
| 6820 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6821 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6822 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6823 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6824 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6825 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6826 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6827 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6828 | /*[clinic input] |
| 6829 | os.getgid |
| 6830 | |
| 6831 | Return the current process's group id. |
| 6832 | [clinic start generated code]*/ |
| 6833 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6834 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6835 | os_getgid_impl(PyObject *module) |
| 6836 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6837 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6838 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6839 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6840 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6841 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6842 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6843 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6844 | /*[clinic input] |
| 6845 | os.getpid |
| 6846 | |
| 6847 | Return the current process id. |
| 6848 | [clinic start generated code]*/ |
| 6849 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6850 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6851 | os_getpid_impl(PyObject *module) |
| 6852 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6853 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6854 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6855 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6856 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6857 | |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6858 | #ifdef NGROUPS_MAX |
| 6859 | #define MAX_GROUPS NGROUPS_MAX |
| 6860 | #else |
| 6861 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 6862 | #define MAX_GROUPS 64 |
| 6863 | #endif |
| 6864 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6865 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6866 | |
| 6867 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6868 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 6869 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 6870 | Returns a list of groups to which a user belongs.\n\n\ |
| 6871 | user: username to lookup\n\ |
| 6872 | group: base group id of the user"); |
| 6873 | |
| 6874 | static PyObject * |
| 6875 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 6876 | { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6877 | const char *user; |
| 6878 | int i, ngroups; |
| 6879 | PyObject *list; |
| 6880 | #ifdef __APPLE__ |
| 6881 | int *groups, basegid; |
| 6882 | #else |
| 6883 | gid_t *groups, basegid; |
| 6884 | #endif |
Jeffrey Kintscher | 8725c83 | 2019-06-13 00:01:29 -0700 | [diff] [blame] | 6885 | |
| 6886 | /* |
| 6887 | * NGROUPS_MAX is defined by POSIX.1 as the maximum |
| 6888 | * number of supplimental groups a users can belong to. |
| 6889 | * We have to increment it by one because |
| 6890 | * getgrouplist() returns both the supplemental groups |
| 6891 | * and the primary group, i.e. all of the groups the |
| 6892 | * user belongs to. |
| 6893 | */ |
| 6894 | ngroups = 1 + MAX_GROUPS; |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6895 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6896 | #ifdef __APPLE__ |
| 6897 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6898 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6899 | #else |
| 6900 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 6901 | _Py_Gid_Converter, &basegid)) |
| 6902 | return NULL; |
| 6903 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6904 | |
| 6905 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6906 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6907 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6908 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6909 | #endif |
| 6910 | if (groups == NULL) |
| 6911 | return PyErr_NoMemory(); |
| 6912 | |
| 6913 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 6914 | PyMem_Del(groups); |
| 6915 | return posix_error(); |
| 6916 | } |
| 6917 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6918 | #ifdef _Py_MEMORY_SANITIZER |
| 6919 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ |
| 6920 | __msan_unpoison(&ngroups, sizeof(ngroups)); |
| 6921 | __msan_unpoison(groups, ngroups*sizeof(*groups)); |
| 6922 | #endif |
| 6923 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6924 | list = PyList_New(ngroups); |
| 6925 | if (list == NULL) { |
| 6926 | PyMem_Del(groups); |
| 6927 | return NULL; |
| 6928 | } |
| 6929 | |
| 6930 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6931 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6932 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6933 | #else |
| 6934 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 6935 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6936 | if (o == NULL) { |
| 6937 | Py_DECREF(list); |
| 6938 | PyMem_Del(groups); |
| 6939 | return NULL; |
| 6940 | } |
| 6941 | PyList_SET_ITEM(list, i, o); |
| 6942 | } |
| 6943 | |
| 6944 | PyMem_Del(groups); |
| 6945 | |
| 6946 | return list; |
| 6947 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6948 | #endif /* HAVE_GETGROUPLIST */ |
| 6949 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6950 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6951 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6952 | /*[clinic input] |
| 6953 | os.getgroups |
| 6954 | |
| 6955 | Return list of supplemental group IDs for the process. |
| 6956 | [clinic start generated code]*/ |
| 6957 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6958 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6959 | os_getgroups_impl(PyObject *module) |
| 6960 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6961 | { |
| 6962 | PyObject *result = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6963 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6964 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6965 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6966 | * This is a helper variable to store the intermediate result when |
| 6967 | * that happens. |
| 6968 | * |
| 6969 | * To keep the code readable the OSX behaviour is unconditional, |
| 6970 | * according to the POSIX spec this should be safe on all unix-y |
| 6971 | * systems. |
| 6972 | */ |
| 6973 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6974 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6975 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6976 | #ifdef __APPLE__ |
| 6977 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 6978 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 6979 | * always first call getgroups with length 0 to get the actual number |
| 6980 | * of groups. |
| 6981 | */ |
| 6982 | n = getgroups(0, NULL); |
| 6983 | if (n < 0) { |
| 6984 | return posix_error(); |
| 6985 | } else if (n <= MAX_GROUPS) { |
| 6986 | /* groups will fit in existing array */ |
| 6987 | alt_grouplist = grouplist; |
| 6988 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 6989 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6990 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 6991 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 6992 | } |
| 6993 | } |
| 6994 | |
| 6995 | n = getgroups(n, alt_grouplist); |
| 6996 | if (n == -1) { |
| 6997 | if (alt_grouplist != grouplist) { |
| 6998 | PyMem_Free(alt_grouplist); |
| 6999 | } |
| 7000 | return posix_error(); |
| 7001 | } |
| 7002 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7003 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7004 | if (n < 0) { |
| 7005 | if (errno == EINVAL) { |
| 7006 | n = getgroups(0, NULL); |
| 7007 | if (n == -1) { |
| 7008 | return posix_error(); |
| 7009 | } |
| 7010 | if (n == 0) { |
| 7011 | /* Avoid malloc(0) */ |
| 7012 | alt_grouplist = grouplist; |
| 7013 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7014 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7015 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7016 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7017 | } |
| 7018 | n = getgroups(n, alt_grouplist); |
| 7019 | if (n == -1) { |
| 7020 | PyMem_Free(alt_grouplist); |
| 7021 | return posix_error(); |
| 7022 | } |
| 7023 | } |
| 7024 | } else { |
| 7025 | return posix_error(); |
| 7026 | } |
| 7027 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7028 | #endif |
| 7029 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7030 | result = PyList_New(n); |
| 7031 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7032 | int i; |
| 7033 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7034 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7035 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7036 | Py_DECREF(result); |
| 7037 | result = NULL; |
| 7038 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7039 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7040 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7041 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7042 | } |
| 7043 | |
| 7044 | if (alt_grouplist != grouplist) { |
| 7045 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7046 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7047 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7048 | return result; |
| 7049 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7050 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7051 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7052 | #ifdef HAVE_INITGROUPS |
| 7053 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 7054 | "initgroups(username, gid) -> None\n\n\ |
| 7055 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 7056 | the groups of which the specified username is a member, plus the specified\n\ |
| 7057 | group id."); |
| 7058 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7059 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7060 | static PyObject * |
| 7061 | posix_initgroups(PyObject *self, PyObject *args) |
| 7062 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7063 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7064 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7065 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7066 | #ifdef __APPLE__ |
| 7067 | int gid; |
| 7068 | #else |
| 7069 | gid_t gid; |
| 7070 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7071 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7072 | #ifdef __APPLE__ |
| 7073 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 7074 | PyUnicode_FSConverter, &oname, |
| 7075 | &gid)) |
| 7076 | #else |
| 7077 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 7078 | PyUnicode_FSConverter, &oname, |
| 7079 | _Py_Gid_Converter, &gid)) |
| 7080 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7081 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7082 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7083 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7084 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7085 | Py_DECREF(oname); |
| 7086 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7087 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7088 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7089 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7090 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7091 | #endif /* HAVE_INITGROUPS */ |
| 7092 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7093 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7094 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7095 | /*[clinic input] |
| 7096 | os.getpgid |
| 7097 | |
| 7098 | pid: pid_t |
| 7099 | |
| 7100 | Call the system call getpgid(), and return the result. |
| 7101 | [clinic start generated code]*/ |
| 7102 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7103 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7104 | os_getpgid_impl(PyObject *module, pid_t pid) |
| 7105 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7106 | { |
| 7107 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7108 | if (pgid < 0) |
| 7109 | return posix_error(); |
| 7110 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7111 | } |
| 7112 | #endif /* HAVE_GETPGID */ |
| 7113 | |
| 7114 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7115 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7116 | /*[clinic input] |
| 7117 | os.getpgrp |
| 7118 | |
| 7119 | Return the current process group id. |
| 7120 | [clinic start generated code]*/ |
| 7121 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7122 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7123 | os_getpgrp_impl(PyObject *module) |
| 7124 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7125 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7126 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7127 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7128 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7129 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7130 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7131 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7132 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7133 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7134 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7135 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7136 | /*[clinic input] |
| 7137 | os.setpgrp |
| 7138 | |
| 7139 | Make the current process the leader of its process group. |
| 7140 | [clinic start generated code]*/ |
| 7141 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7142 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7143 | os_setpgrp_impl(PyObject *module) |
| 7144 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7145 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7146 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7147 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7148 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7149 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7150 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7151 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7152 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7153 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7154 | #endif /* HAVE_SETPGRP */ |
| 7155 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7156 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7157 | |
| 7158 | #ifdef MS_WINDOWS |
| 7159 | #include <tlhelp32.h> |
| 7160 | |
| 7161 | static PyObject* |
| 7162 | win32_getppid() |
| 7163 | { |
| 7164 | HANDLE snapshot; |
| 7165 | pid_t mypid; |
| 7166 | PyObject* result = NULL; |
| 7167 | BOOL have_record; |
| 7168 | PROCESSENTRY32 pe; |
| 7169 | |
| 7170 | mypid = getpid(); /* This function never fails */ |
| 7171 | |
| 7172 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 7173 | if (snapshot == INVALID_HANDLE_VALUE) |
| 7174 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 7175 | |
| 7176 | pe.dwSize = sizeof(pe); |
| 7177 | have_record = Process32First(snapshot, &pe); |
| 7178 | while (have_record) { |
| 7179 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 7180 | /* We could cache the ulong value in a static variable. */ |
| 7181 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 7182 | break; |
| 7183 | } |
| 7184 | |
| 7185 | have_record = Process32Next(snapshot, &pe); |
| 7186 | } |
| 7187 | |
| 7188 | /* If our loop exits and our pid was not found (result will be NULL) |
| 7189 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 7190 | * error anyway, so let's raise it. */ |
| 7191 | if (!result) |
| 7192 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7193 | |
| 7194 | CloseHandle(snapshot); |
| 7195 | |
| 7196 | return result; |
| 7197 | } |
| 7198 | #endif /*MS_WINDOWS*/ |
| 7199 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7200 | |
| 7201 | /*[clinic input] |
| 7202 | os.getppid |
| 7203 | |
| 7204 | Return the parent's process id. |
| 7205 | |
| 7206 | If the parent process has already exited, Windows machines will still |
| 7207 | return its id; others systems will return the id of the 'init' process (1). |
| 7208 | [clinic start generated code]*/ |
| 7209 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7210 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7211 | os_getppid_impl(PyObject *module) |
| 7212 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7213 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7214 | #ifdef MS_WINDOWS |
| 7215 | return win32_getppid(); |
| 7216 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7217 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7218 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7219 | } |
| 7220 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7221 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7222 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7223 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7224 | /*[clinic input] |
| 7225 | os.getlogin |
| 7226 | |
| 7227 | Return the actual login name. |
| 7228 | [clinic start generated code]*/ |
| 7229 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7230 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7231 | os_getlogin_impl(PyObject *module) |
| 7232 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7233 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7234 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7235 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7236 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7237 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7238 | |
| 7239 | if (GetUserNameW(user_name, &num_chars)) { |
| 7240 | /* num_chars is the number of unicode chars plus null terminator */ |
| 7241 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7242 | } |
| 7243 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7244 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 7245 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7246 | char *name; |
| 7247 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7248 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7249 | errno = 0; |
| 7250 | name = getlogin(); |
| 7251 | if (name == NULL) { |
| 7252 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7253 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7254 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7255 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7256 | } |
| 7257 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7258 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7259 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7260 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7261 | return result; |
| 7262 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7263 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7264 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7265 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7266 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7267 | /*[clinic input] |
| 7268 | os.getuid |
| 7269 | |
| 7270 | Return the current process's user id. |
| 7271 | [clinic start generated code]*/ |
| 7272 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7273 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7274 | os_getuid_impl(PyObject *module) |
| 7275 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7276 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7277 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7278 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7279 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7280 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7281 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7282 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7283 | #define HAVE_KILL |
| 7284 | #endif /* MS_WINDOWS */ |
| 7285 | |
| 7286 | #ifdef HAVE_KILL |
| 7287 | /*[clinic input] |
| 7288 | os.kill |
| 7289 | |
| 7290 | pid: pid_t |
| 7291 | signal: Py_ssize_t |
| 7292 | / |
| 7293 | |
| 7294 | Kill a process with a signal. |
| 7295 | [clinic start generated code]*/ |
| 7296 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7297 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7298 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
| 7299 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7300 | #ifndef MS_WINDOWS |
| 7301 | { |
| 7302 | if (kill(pid, (int)signal) == -1) |
| 7303 | return posix_error(); |
| 7304 | Py_RETURN_NONE; |
| 7305 | } |
| 7306 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7307 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7308 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7309 | DWORD sig = (DWORD)signal; |
| 7310 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7311 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7312 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7313 | /* Console processes which share a common console can be sent CTRL+C or |
| 7314 | CTRL+BREAK events, provided they handle said events. */ |
| 7315 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7316 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7317 | err = GetLastError(); |
| 7318 | PyErr_SetFromWindowsErr(err); |
| 7319 | } |
| 7320 | else |
| 7321 | Py_RETURN_NONE; |
| 7322 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7323 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7324 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 7325 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7326 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7327 | if (handle == NULL) { |
| 7328 | err = GetLastError(); |
| 7329 | return PyErr_SetFromWindowsErr(err); |
| 7330 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7331 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7332 | if (TerminateProcess(handle, sig) == 0) { |
| 7333 | err = GetLastError(); |
| 7334 | result = PyErr_SetFromWindowsErr(err); |
| 7335 | } else { |
| 7336 | Py_INCREF(Py_None); |
| 7337 | result = Py_None; |
| 7338 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7339 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7340 | CloseHandle(handle); |
| 7341 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7342 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7343 | #endif /* !MS_WINDOWS */ |
| 7344 | #endif /* HAVE_KILL */ |
| 7345 | |
| 7346 | |
| 7347 | #ifdef HAVE_KILLPG |
| 7348 | /*[clinic input] |
| 7349 | os.killpg |
| 7350 | |
| 7351 | pgid: pid_t |
| 7352 | signal: int |
| 7353 | / |
| 7354 | |
| 7355 | Kill a process group with a signal. |
| 7356 | [clinic start generated code]*/ |
| 7357 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7358 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7359 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
| 7360 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7361 | { |
| 7362 | /* XXX some man pages make the `pgid` parameter an int, others |
| 7363 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 7364 | take the same type. Moreover, pid_t is always at least as wide as |
| 7365 | int (else compilation of this module fails), which is safe. */ |
| 7366 | if (killpg(pgid, signal) == -1) |
| 7367 | return posix_error(); |
| 7368 | Py_RETURN_NONE; |
| 7369 | } |
| 7370 | #endif /* HAVE_KILLPG */ |
| 7371 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7372 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7373 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7374 | #ifdef HAVE_SYS_LOCK_H |
| 7375 | #include <sys/lock.h> |
| 7376 | #endif |
| 7377 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7378 | /*[clinic input] |
| 7379 | os.plock |
| 7380 | op: int |
| 7381 | / |
| 7382 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7383 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7384 | [clinic start generated code]*/ |
| 7385 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7386 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7387 | os_plock_impl(PyObject *module, int op) |
| 7388 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7389 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7390 | if (plock(op) == -1) |
| 7391 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7392 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7393 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7394 | #endif /* HAVE_PLOCK */ |
| 7395 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7396 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7397 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7398 | /*[clinic input] |
| 7399 | os.setuid |
| 7400 | |
| 7401 | uid: uid_t |
| 7402 | / |
| 7403 | |
| 7404 | Set the current process's user id. |
| 7405 | [clinic start generated code]*/ |
| 7406 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7407 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7408 | os_setuid_impl(PyObject *module, uid_t uid) |
| 7409 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7410 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7411 | if (setuid(uid) < 0) |
| 7412 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7413 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7414 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7415 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7416 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7417 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7418 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7419 | /*[clinic input] |
| 7420 | os.seteuid |
| 7421 | |
| 7422 | euid: uid_t |
| 7423 | / |
| 7424 | |
| 7425 | Set the current process's effective user id. |
| 7426 | [clinic start generated code]*/ |
| 7427 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7428 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7429 | os_seteuid_impl(PyObject *module, uid_t euid) |
| 7430 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7431 | { |
| 7432 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7433 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7434 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7435 | } |
| 7436 | #endif /* HAVE_SETEUID */ |
| 7437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7438 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7439 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7440 | /*[clinic input] |
| 7441 | os.setegid |
| 7442 | |
| 7443 | egid: gid_t |
| 7444 | / |
| 7445 | |
| 7446 | Set the current process's effective group id. |
| 7447 | [clinic start generated code]*/ |
| 7448 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7449 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7450 | os_setegid_impl(PyObject *module, gid_t egid) |
| 7451 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7452 | { |
| 7453 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7454 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7455 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7456 | } |
| 7457 | #endif /* HAVE_SETEGID */ |
| 7458 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7459 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7460 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7461 | /*[clinic input] |
| 7462 | os.setreuid |
| 7463 | |
| 7464 | ruid: uid_t |
| 7465 | euid: uid_t |
| 7466 | / |
| 7467 | |
| 7468 | Set the current process's real and effective user ids. |
| 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_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
| 7473 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7474 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7475 | if (setreuid(ruid, euid) < 0) { |
| 7476 | return posix_error(); |
| 7477 | } else { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7478 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7479 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7480 | } |
| 7481 | #endif /* HAVE_SETREUID */ |
| 7482 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7483 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7484 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7485 | /*[clinic input] |
| 7486 | os.setregid |
| 7487 | |
| 7488 | rgid: gid_t |
| 7489 | egid: gid_t |
| 7490 | / |
| 7491 | |
| 7492 | Set the current process's real and effective group ids. |
| 7493 | [clinic start generated code]*/ |
| 7494 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7495 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7496 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
| 7497 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7498 | { |
| 7499 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7500 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7501 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7502 | } |
| 7503 | #endif /* HAVE_SETREGID */ |
| 7504 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7505 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7506 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7507 | /*[clinic input] |
| 7508 | os.setgid |
| 7509 | gid: gid_t |
| 7510 | / |
| 7511 | |
| 7512 | Set the current process's group id. |
| 7513 | [clinic start generated code]*/ |
| 7514 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7515 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7516 | os_setgid_impl(PyObject *module, gid_t gid) |
| 7517 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7518 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7519 | if (setgid(gid) < 0) |
| 7520 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7521 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7522 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7523 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7524 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7525 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7526 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7527 | /*[clinic input] |
| 7528 | os.setgroups |
| 7529 | |
| 7530 | groups: object |
| 7531 | / |
| 7532 | |
| 7533 | Set the groups of the current process to list. |
| 7534 | [clinic start generated code]*/ |
| 7535 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7536 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7537 | os_setgroups(PyObject *module, PyObject *groups) |
| 7538 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7539 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7540 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7541 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7542 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7543 | if (!PySequence_Check(groups)) { |
| 7544 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 7545 | return NULL; |
| 7546 | } |
| 7547 | len = PySequence_Size(groups); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7548 | if (len < 0) { |
| 7549 | return NULL; |
| 7550 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7551 | if (len > MAX_GROUPS) { |
| 7552 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 7553 | return NULL; |
| 7554 | } |
| 7555 | for(i = 0; i < len; i++) { |
| 7556 | PyObject *elem; |
| 7557 | elem = PySequence_GetItem(groups, i); |
| 7558 | if (!elem) |
| 7559 | return NULL; |
| 7560 | if (!PyLong_Check(elem)) { |
| 7561 | PyErr_SetString(PyExc_TypeError, |
| 7562 | "groups must be integers"); |
| 7563 | Py_DECREF(elem); |
| 7564 | return NULL; |
| 7565 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7566 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7567 | Py_DECREF(elem); |
| 7568 | return NULL; |
| 7569 | } |
| 7570 | } |
| 7571 | Py_DECREF(elem); |
| 7572 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7573 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7574 | if (setgroups(len, grouplist) < 0) |
| 7575 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7576 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7577 | } |
| 7578 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7579 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7580 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 7581 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7582 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7583 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7584 | PyObject *result; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7585 | PyObject *struct_rusage; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7586 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7587 | if (pid == -1) |
| 7588 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7589 | |
Zackery Spytz | 682107c | 2019-09-09 09:48:32 -0600 | [diff] [blame] | 7590 | // If wait succeeded but no child was ready to report status, ru will not |
| 7591 | // have been populated. |
| 7592 | if (pid == 0) { |
| 7593 | memset(ru, 0, sizeof(*ru)); |
| 7594 | } |
| 7595 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7596 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 7597 | if (m == NULL) |
| 7598 | return NULL; |
| 7599 | struct_rusage = PyObject_GetAttr(m, _posixstate_global->struct_rusage); |
| 7600 | Py_DECREF(m); |
| 7601 | if (struct_rusage == NULL) |
| 7602 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7603 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7604 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 7605 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
Eddie Elizondo | e4db1f0 | 2019-11-25 19:07:37 -0800 | [diff] [blame] | 7606 | Py_DECREF(struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7607 | if (!result) |
| 7608 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7609 | |
| 7610 | #ifndef doubletime |
| 7611 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 7612 | #endif |
| 7613 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7614 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7615 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7616 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7617 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7618 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7619 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 7620 | SET_INT(result, 2, ru->ru_maxrss); |
| 7621 | SET_INT(result, 3, ru->ru_ixrss); |
| 7622 | SET_INT(result, 4, ru->ru_idrss); |
| 7623 | SET_INT(result, 5, ru->ru_isrss); |
| 7624 | SET_INT(result, 6, ru->ru_minflt); |
| 7625 | SET_INT(result, 7, ru->ru_majflt); |
| 7626 | SET_INT(result, 8, ru->ru_nswap); |
| 7627 | SET_INT(result, 9, ru->ru_inblock); |
| 7628 | SET_INT(result, 10, ru->ru_oublock); |
| 7629 | SET_INT(result, 11, ru->ru_msgsnd); |
| 7630 | SET_INT(result, 12, ru->ru_msgrcv); |
| 7631 | SET_INT(result, 13, ru->ru_nsignals); |
| 7632 | SET_INT(result, 14, ru->ru_nvcsw); |
| 7633 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7634 | #undef SET_INT |
| 7635 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7636 | if (PyErr_Occurred()) { |
| 7637 | Py_DECREF(result); |
| 7638 | return NULL; |
| 7639 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7640 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7641 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7642 | } |
| 7643 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 7644 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7645 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7646 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7647 | /*[clinic input] |
| 7648 | os.wait3 |
| 7649 | |
| 7650 | options: int |
| 7651 | Wait for completion of a child process. |
| 7652 | |
| 7653 | Returns a tuple of information about the child process: |
| 7654 | (pid, status, rusage) |
| 7655 | [clinic start generated code]*/ |
| 7656 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7657 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7658 | os_wait3_impl(PyObject *module, int options) |
| 7659 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7660 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7661 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7662 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7663 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7664 | WAIT_TYPE status; |
| 7665 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7666 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7667 | do { |
| 7668 | Py_BEGIN_ALLOW_THREADS |
| 7669 | pid = wait3(&status, options, &ru); |
| 7670 | Py_END_ALLOW_THREADS |
| 7671 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7672 | if (pid < 0) |
| 7673 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7674 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7675 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7676 | } |
| 7677 | #endif /* HAVE_WAIT3 */ |
| 7678 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7679 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7680 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7681 | /*[clinic input] |
| 7682 | |
| 7683 | os.wait4 |
| 7684 | |
| 7685 | pid: pid_t |
| 7686 | options: int |
| 7687 | |
| 7688 | Wait for completion of a specific child process. |
| 7689 | |
| 7690 | Returns a tuple of information about the child process: |
| 7691 | (pid, status, rusage) |
| 7692 | [clinic start generated code]*/ |
| 7693 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7694 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7695 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
| 7696 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7697 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7698 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7699 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7700 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7701 | WAIT_TYPE status; |
| 7702 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7703 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7704 | do { |
| 7705 | Py_BEGIN_ALLOW_THREADS |
| 7706 | res = wait4(pid, &status, options, &ru); |
| 7707 | Py_END_ALLOW_THREADS |
| 7708 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7709 | if (res < 0) |
| 7710 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7711 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7712 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7713 | } |
| 7714 | #endif /* HAVE_WAIT4 */ |
| 7715 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7716 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7717 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7718 | /*[clinic input] |
| 7719 | os.waitid |
| 7720 | |
| 7721 | idtype: idtype_t |
| 7722 | Must be one of be P_PID, P_PGID or P_ALL. |
| 7723 | id: id_t |
| 7724 | The id to wait on. |
| 7725 | options: int |
| 7726 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 7727 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 7728 | / |
| 7729 | |
| 7730 | Returns the result of waiting for a process or processes. |
| 7731 | |
| 7732 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 7733 | no children in a waitable state. |
| 7734 | [clinic start generated code]*/ |
| 7735 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7736 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7737 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
| 7738 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7739 | { |
| 7740 | PyObject *result; |
| 7741 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7742 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7743 | siginfo_t si; |
| 7744 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7745 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7746 | do { |
| 7747 | Py_BEGIN_ALLOW_THREADS |
| 7748 | res = waitid(idtype, id, &si, options); |
| 7749 | Py_END_ALLOW_THREADS |
| 7750 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7751 | if (res < 0) |
| 7752 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7753 | |
| 7754 | if (si.si_pid == 0) |
| 7755 | Py_RETURN_NONE; |
| 7756 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 7757 | PyObject *WaitidResultType = _posixstate(module)->WaitidResultType; |
| 7758 | result = PyStructSequence_New((PyTypeObject *)WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7759 | if (!result) |
| 7760 | return NULL; |
| 7761 | |
| 7762 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7763 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7764 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 7765 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 7766 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 7767 | if (PyErr_Occurred()) { |
| 7768 | Py_DECREF(result); |
| 7769 | return NULL; |
| 7770 | } |
| 7771 | |
| 7772 | return result; |
| 7773 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7774 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7775 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7776 | |
| 7777 | #if defined(HAVE_WAITPID) |
| 7778 | /*[clinic input] |
| 7779 | os.waitpid |
| 7780 | pid: pid_t |
| 7781 | options: int |
| 7782 | / |
| 7783 | |
| 7784 | Wait for completion of a given child process. |
| 7785 | |
| 7786 | Returns a tuple of information regarding the child process: |
| 7787 | (pid, status) |
| 7788 | |
| 7789 | The options argument is ignored on Windows. |
| 7790 | [clinic start generated code]*/ |
| 7791 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7792 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7793 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
| 7794 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7795 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7796 | pid_t res; |
| 7797 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7798 | WAIT_TYPE status; |
| 7799 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7800 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7801 | do { |
| 7802 | Py_BEGIN_ALLOW_THREADS |
| 7803 | res = waitpid(pid, &status, options); |
| 7804 | Py_END_ALLOW_THREADS |
| 7805 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7806 | if (res < 0) |
| 7807 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7808 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7809 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7810 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7811 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7812 | /* 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] | 7813 | /*[clinic input] |
| 7814 | os.waitpid |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7815 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7816 | options: int |
| 7817 | / |
| 7818 | |
| 7819 | Wait for completion of a given process. |
| 7820 | |
| 7821 | Returns a tuple of information regarding the process: |
| 7822 | (pid, status << 8) |
| 7823 | |
| 7824 | The options argument is ignored on Windows. |
| 7825 | [clinic start generated code]*/ |
| 7826 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7827 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7828 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7829 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7830 | { |
| 7831 | int status; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7832 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7833 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7834 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7835 | do { |
| 7836 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7837 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7838 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7839 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7840 | Py_END_ALLOW_THREADS |
| 7841 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7842 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7843 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7844 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7845 | /* 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] | 7846 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7847 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7848 | #endif |
| 7849 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7850 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7851 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7852 | /*[clinic input] |
| 7853 | os.wait |
| 7854 | |
| 7855 | Wait for completion of a child process. |
| 7856 | |
| 7857 | Returns a tuple of information about the child process: |
| 7858 | (pid, status) |
| 7859 | [clinic start generated code]*/ |
| 7860 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7861 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7862 | os_wait_impl(PyObject *module) |
| 7863 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7864 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7865 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7866 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7867 | WAIT_TYPE status; |
| 7868 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7869 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7870 | do { |
| 7871 | Py_BEGIN_ALLOW_THREADS |
| 7872 | pid = wait(&status); |
| 7873 | Py_END_ALLOW_THREADS |
| 7874 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 7875 | if (pid < 0) |
| 7876 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7877 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7878 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7879 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7880 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7881 | |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 7882 | #if defined(__linux__) && defined(__NR_pidfd_open) |
| 7883 | /*[clinic input] |
| 7884 | os.pidfd_open |
| 7885 | pid: pid_t |
| 7886 | flags: unsigned_int = 0 |
| 7887 | |
| 7888 | Return a file descriptor referring to the process *pid*. |
| 7889 | |
| 7890 | The descriptor can be used to perform process management without races and |
| 7891 | signals. |
| 7892 | [clinic start generated code]*/ |
| 7893 | |
| 7894 | static PyObject * |
| 7895 | os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags) |
| 7896 | /*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/ |
| 7897 | { |
| 7898 | int fd = syscall(__NR_pidfd_open, pid, flags); |
| 7899 | if (fd < 0) { |
| 7900 | return posix_error(); |
| 7901 | } |
| 7902 | return PyLong_FromLong(fd); |
| 7903 | } |
| 7904 | #endif |
| 7905 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7906 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7907 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7908 | /*[clinic input] |
| 7909 | os.readlink |
| 7910 | |
| 7911 | path: path_t |
| 7912 | * |
| 7913 | dir_fd: dir_fd(requires='readlinkat') = None |
| 7914 | |
| 7915 | Return a string representing the path to which the symbolic link points. |
| 7916 | |
| 7917 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 7918 | and path should be relative; path will then be relative to that directory. |
| 7919 | |
| 7920 | dir_fd may not be implemented on your platform. If it is unavailable, |
| 7921 | using it will raise a NotImplementedError. |
| 7922 | [clinic start generated code]*/ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7923 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7924 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7925 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
| 7926 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7927 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7928 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7929 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7930 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7931 | |
| 7932 | Py_BEGIN_ALLOW_THREADS |
| 7933 | #ifdef HAVE_READLINKAT |
| 7934 | if (dir_fd != DEFAULT_DIR_FD) |
| 7935 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); |
| 7936 | else |
| 7937 | #endif |
| 7938 | length = readlink(path->narrow, buffer, MAXPATHLEN); |
| 7939 | Py_END_ALLOW_THREADS |
| 7940 | |
| 7941 | if (length < 0) { |
| 7942 | return path_error(path); |
| 7943 | } |
| 7944 | buffer[length] = '\0'; |
| 7945 | |
| 7946 | if (PyUnicode_Check(path->object)) |
| 7947 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 7948 | else |
| 7949 | return PyBytes_FromStringAndSize(buffer, length); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7950 | #elif defined(MS_WINDOWS) |
| 7951 | DWORD n_bytes_returned; |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7952 | DWORD io_result = 0; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7953 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7954 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 7955 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 7956 | PyObject *result = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7957 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7958 | /* First get a handle to the reparse point */ |
| 7959 | Py_BEGIN_ALLOW_THREADS |
| 7960 | reparse_point_handle = CreateFileW( |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7961 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7962 | 0, |
| 7963 | 0, |
| 7964 | 0, |
| 7965 | OPEN_EXISTING, |
| 7966 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 7967 | 0); |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7968 | if (reparse_point_handle != INVALID_HANDLE_VALUE) { |
| 7969 | /* New call DeviceIoControl to read the reparse point */ |
| 7970 | io_result = DeviceIoControl( |
| 7971 | reparse_point_handle, |
| 7972 | FSCTL_GET_REPARSE_POINT, |
| 7973 | 0, 0, /* in buffer */ |
| 7974 | target_buffer, sizeof(target_buffer), |
| 7975 | &n_bytes_returned, |
| 7976 | 0 /* we're not using OVERLAPPED_IO */ |
| 7977 | ); |
| 7978 | CloseHandle(reparse_point_handle); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7979 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7980 | Py_END_ALLOW_THREADS |
| 7981 | |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7982 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7983 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7984 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7985 | |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7986 | wchar_t *name = NULL; |
| 7987 | Py_ssize_t nameLen = 0; |
| 7988 | if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7989 | { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7990 | name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 7991 | rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 7992 | nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7993 | } |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 7994 | else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) |
| 7995 | { |
| 7996 | name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer + |
| 7997 | rdb->MountPointReparseBuffer.SubstituteNameOffset); |
| 7998 | nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t); |
| 7999 | } |
| 8000 | else |
| 8001 | { |
| 8002 | PyErr_SetString(PyExc_ValueError, "not a symbolic link"); |
| 8003 | } |
| 8004 | if (name) { |
| 8005 | if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) { |
| 8006 | /* Our buffer is mutable, so this is okay */ |
| 8007 | name[1] = L'\\'; |
| 8008 | } |
| 8009 | result = PyUnicode_FromWideChar(name, nameLen); |
Steve Dower | 993ac92 | 2019-09-03 12:50:51 -0700 | [diff] [blame] | 8010 | if (result && path->narrow) { |
Steve Dower | df2d4a6 | 2019-08-21 15:27:33 -0700 | [diff] [blame] | 8011 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
| 8012 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8013 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8014 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8015 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8016 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8017 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8018 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8019 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8020 | |
| 8021 | #if defined(MS_WINDOWS) |
| 8022 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8023 | /* Remove the last portion of the path - return 0 on success */ |
| 8024 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8025 | _dirnameW(WCHAR *path) |
| 8026 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8027 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8028 | size_t length = wcsnlen_s(path, MAX_PATH); |
| 8029 | if (length == MAX_PATH) { |
| 8030 | return -1; |
| 8031 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8032 | |
| 8033 | /* walk the path from the end until a backslash is encountered */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8034 | for(ptr = path + length; ptr != path; ptr--) { |
| 8035 | if (*ptr == L'\\' || *ptr == L'/') { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8036 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8037 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8038 | } |
| 8039 | *ptr = 0; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8040 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8041 | } |
| 8042 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8043 | /* Is this path absolute? */ |
| 8044 | static int |
| 8045 | _is_absW(const WCHAR *path) |
| 8046 | { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8047 | return path[0] == L'\\' || path[0] == L'/' || |
| 8048 | (path[0] && path[1] == L':'); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8049 | } |
| 8050 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8051 | /* join root and rest with a backslash - return 0 on success */ |
| 8052 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8053 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 8054 | { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8055 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8056 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8057 | } |
| 8058 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8059 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
| 8060 | return -1; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8061 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8062 | |
| 8063 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { |
| 8064 | return -1; |
| 8065 | } |
| 8066 | |
| 8067 | return wcscat_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8068 | } |
| 8069 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8070 | /* Return True if the path at src relative to dest is a directory */ |
| 8071 | static int |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8072 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8073 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8074 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 8075 | WCHAR dest_parent[MAX_PATH]; |
| 8076 | WCHAR src_resolved[MAX_PATH] = L""; |
| 8077 | |
| 8078 | /* dest_parent = os.path.dirname(dest) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8079 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 8080 | _dirnameW(dest_parent)) { |
| 8081 | return 0; |
| 8082 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8083 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8084 | if (_joinW(src_resolved, dest_parent, src)) { |
| 8085 | return 0; |
| 8086 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8087 | return ( |
| 8088 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 8089 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 8090 | ); |
| 8091 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8092 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8093 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8094 | |
| 8095 | /*[clinic input] |
| 8096 | os.symlink |
| 8097 | src: path_t |
| 8098 | dst: path_t |
| 8099 | target_is_directory: bool = False |
| 8100 | * |
| 8101 | dir_fd: dir_fd(requires='symlinkat')=None |
| 8102 | |
| 8103 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 8104 | |
| 8105 | Create a symbolic link pointing to src named dst. |
| 8106 | |
| 8107 | target_is_directory is required on Windows if the target is to be |
| 8108 | interpreted as a directory. (On Windows, symlink requires |
| 8109 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 8110 | target_is_directory is ignored on non-Windows platforms. |
| 8111 | |
| 8112 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8113 | and path should be relative; path will then be relative to that directory. |
| 8114 | dir_fd may not be implemented on your platform. |
| 8115 | If it is unavailable, using it will raise a NotImplementedError. |
| 8116 | |
| 8117 | [clinic start generated code]*/ |
| 8118 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8119 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8120 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8121 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8122 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8123 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8124 | #ifdef MS_WINDOWS |
| 8125 | DWORD result; |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8126 | DWORD flags = 0; |
| 8127 | |
| 8128 | /* Assumed true, set to false if detected to not be available. */ |
| 8129 | static int windows_has_symlink_unprivileged_flag = TRUE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8130 | #else |
| 8131 | int result; |
| 8132 | #endif |
| 8133 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8134 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8135 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8136 | if (windows_has_symlink_unprivileged_flag) { |
| 8137 | /* Allow non-admin symlinks if system allows it. */ |
| 8138 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 8139 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8140 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8141 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8142 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8143 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
| 8144 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { |
| 8145 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 8146 | } |
| 8147 | |
| 8148 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8149 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8150 | Py_END_ALLOW_THREADS |
| 8151 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8152 | if (windows_has_symlink_unprivileged_flag && !result && |
| 8153 | ERROR_INVALID_PARAMETER == GetLastError()) { |
| 8154 | |
| 8155 | Py_BEGIN_ALLOW_THREADS |
| 8156 | _Py_BEGIN_SUPPRESS_IPH |
| 8157 | /* This error might be caused by |
| 8158 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. |
| 8159 | Try again, and update windows_has_symlink_unprivileged_flag if we |
| 8160 | are successful this time. |
| 8161 | |
| 8162 | NOTE: There is a risk of a race condition here if there are other |
| 8163 | conditions than the flag causing ERROR_INVALID_PARAMETER, and |
| 8164 | another process (or thread) changes that condition in between our |
| 8165 | calls to CreateSymbolicLink. |
| 8166 | */ |
| 8167 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); |
| 8168 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); |
| 8169 | _Py_END_SUPPRESS_IPH |
| 8170 | Py_END_ALLOW_THREADS |
| 8171 | |
| 8172 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { |
| 8173 | windows_has_symlink_unprivileged_flag = FALSE; |
| 8174 | } |
| 8175 | } |
| 8176 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8177 | if (!result) |
| 8178 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8179 | |
| 8180 | #else |
| 8181 | |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8182 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
| 8183 | PyErr_SetString(PyExc_ValueError, |
| 8184 | "symlink: src and dst must be the same type"); |
| 8185 | return NULL; |
| 8186 | } |
| 8187 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8188 | Py_BEGIN_ALLOW_THREADS |
| 8189 | #if HAVE_SYMLINKAT |
| 8190 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8191 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8192 | else |
| 8193 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8194 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8195 | Py_END_ALLOW_THREADS |
| 8196 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8197 | if (result) |
| 8198 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8199 | #endif |
| 8200 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8201 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8202 | } |
| 8203 | #endif /* HAVE_SYMLINK */ |
| 8204 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8205 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8206 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8207 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8208 | static PyStructSequence_Field times_result_fields[] = { |
| 8209 | {"user", "user time"}, |
| 8210 | {"system", "system time"}, |
| 8211 | {"children_user", "user time of children"}, |
| 8212 | {"children_system", "system time of children"}, |
| 8213 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 8214 | {NULL} |
| 8215 | }; |
| 8216 | |
| 8217 | PyDoc_STRVAR(times_result__doc__, |
| 8218 | "times_result: Result from os.times().\n\n\ |
| 8219 | This object may be accessed either as a tuple of\n\ |
| 8220 | (user, system, children_user, children_system, elapsed),\n\ |
| 8221 | or via the attributes user, system, children_user, children_system,\n\ |
| 8222 | and elapsed.\n\ |
| 8223 | \n\ |
| 8224 | See os.times for more information."); |
| 8225 | |
| 8226 | static PyStructSequence_Desc times_result_desc = { |
| 8227 | "times_result", /* name */ |
| 8228 | times_result__doc__, /* doc */ |
| 8229 | times_result_fields, |
| 8230 | 5 |
| 8231 | }; |
| 8232 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8233 | #ifdef MS_WINDOWS |
| 8234 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 8235 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8236 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8237 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8238 | |
| 8239 | static PyObject * |
| 8240 | build_times_result(double user, double system, |
| 8241 | double children_user, double children_system, |
| 8242 | double elapsed) |
| 8243 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 8244 | PyObject *TimesResultType = _posixstate_global->TimesResultType; |
| 8245 | PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8246 | if (value == NULL) |
| 8247 | return NULL; |
| 8248 | |
| 8249 | #define SET(i, field) \ |
| 8250 | { \ |
| 8251 | PyObject *o = PyFloat_FromDouble(field); \ |
| 8252 | if (!o) { \ |
| 8253 | Py_DECREF(value); \ |
| 8254 | return NULL; \ |
| 8255 | } \ |
| 8256 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 8257 | } \ |
| 8258 | |
| 8259 | SET(0, user); |
| 8260 | SET(1, system); |
| 8261 | SET(2, children_user); |
| 8262 | SET(3, children_system); |
| 8263 | SET(4, elapsed); |
| 8264 | |
| 8265 | #undef SET |
| 8266 | |
| 8267 | return value; |
| 8268 | } |
| 8269 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8270 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8271 | #ifndef MS_WINDOWS |
| 8272 | #define NEED_TICKS_PER_SECOND |
| 8273 | static long ticks_per_second = -1; |
| 8274 | #endif /* MS_WINDOWS */ |
| 8275 | |
| 8276 | /*[clinic input] |
| 8277 | os.times |
| 8278 | |
| 8279 | Return a collection containing process timing information. |
| 8280 | |
| 8281 | The object returned behaves like a named tuple with these fields: |
| 8282 | (utime, stime, cutime, cstime, elapsed_time) |
| 8283 | All fields are floating point numbers. |
| 8284 | [clinic start generated code]*/ |
| 8285 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8286 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8287 | os_times_impl(PyObject *module) |
| 8288 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8289 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8290 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8291 | FILETIME create, exit, kernel, user; |
| 8292 | HANDLE hProc; |
| 8293 | hProc = GetCurrentProcess(); |
| 8294 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 8295 | /* The fields of a FILETIME structure are the hi and lo part |
| 8296 | of a 64-bit value expressed in 100 nanosecond units. |
| 8297 | 1e7 is one second in such units; 1e-7 the inverse. |
| 8298 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 8299 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8300 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8301 | (double)(user.dwHighDateTime*429.4967296 + |
| 8302 | user.dwLowDateTime*1e-7), |
| 8303 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 8304 | kernel.dwLowDateTime*1e-7), |
| 8305 | (double)0, |
| 8306 | (double)0, |
| 8307 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8308 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8309 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8310 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8311 | |
| 8312 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8313 | struct tms t; |
| 8314 | clock_t c; |
| 8315 | errno = 0; |
| 8316 | c = times(&t); |
| 8317 | if (c == (clock_t) -1) |
| 8318 | return posix_error(); |
| 8319 | return build_times_result( |
| 8320 | (double)t.tms_utime / ticks_per_second, |
| 8321 | (double)t.tms_stime / ticks_per_second, |
| 8322 | (double)t.tms_cutime / ticks_per_second, |
| 8323 | (double)t.tms_cstime / ticks_per_second, |
| 8324 | (double)c / ticks_per_second); |
| 8325 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8326 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8327 | #endif /* HAVE_TIMES */ |
| 8328 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8329 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8330 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8331 | /*[clinic input] |
| 8332 | os.getsid |
| 8333 | |
| 8334 | pid: pid_t |
| 8335 | / |
| 8336 | |
| 8337 | Call the system call getsid(pid) and return the result. |
| 8338 | [clinic start generated code]*/ |
| 8339 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8340 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8341 | os_getsid_impl(PyObject *module, pid_t pid) |
| 8342 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8343 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8344 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8345 | sid = getsid(pid); |
| 8346 | if (sid < 0) |
| 8347 | return posix_error(); |
| 8348 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8349 | } |
| 8350 | #endif /* HAVE_GETSID */ |
| 8351 | |
| 8352 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8353 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8354 | /*[clinic input] |
| 8355 | os.setsid |
| 8356 | |
| 8357 | Call the system call setsid(). |
| 8358 | [clinic start generated code]*/ |
| 8359 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8360 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8361 | os_setsid_impl(PyObject *module) |
| 8362 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8363 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8364 | if (setsid() < 0) |
| 8365 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8366 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8367 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8368 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8370 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8371 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8372 | /*[clinic input] |
| 8373 | os.setpgid |
| 8374 | |
| 8375 | pid: pid_t |
| 8376 | pgrp: pid_t |
| 8377 | / |
| 8378 | |
| 8379 | Call the system call setpgid(pid, pgrp). |
| 8380 | [clinic start generated code]*/ |
| 8381 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8382 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8383 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
| 8384 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8385 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8386 | if (setpgid(pid, pgrp) < 0) |
| 8387 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8388 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8389 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8390 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8391 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8392 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8393 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8394 | /*[clinic input] |
| 8395 | os.tcgetpgrp |
| 8396 | |
| 8397 | fd: int |
| 8398 | / |
| 8399 | |
| 8400 | Return the process group associated with the terminal specified by fd. |
| 8401 | [clinic start generated code]*/ |
| 8402 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8403 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8404 | os_tcgetpgrp_impl(PyObject *module, int fd) |
| 8405 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8406 | { |
| 8407 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8408 | if (pgid < 0) |
| 8409 | return posix_error(); |
| 8410 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8411 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8412 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8413 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8414 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8415 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8416 | /*[clinic input] |
| 8417 | os.tcsetpgrp |
| 8418 | |
| 8419 | fd: int |
| 8420 | pgid: pid_t |
| 8421 | / |
| 8422 | |
| 8423 | Set the process group associated with the terminal specified by fd. |
| 8424 | [clinic start generated code]*/ |
| 8425 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8426 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8427 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
| 8428 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8429 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8430 | if (tcsetpgrp(fd, pgid) < 0) |
| 8431 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8432 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8433 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8434 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8435 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8436 | /* Functions acting on file descriptors */ |
| 8437 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8438 | #ifdef O_CLOEXEC |
| 8439 | extern int _Py_open_cloexec_works; |
| 8440 | #endif |
| 8441 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8442 | |
| 8443 | /*[clinic input] |
| 8444 | os.open -> int |
| 8445 | path: path_t |
| 8446 | flags: int |
| 8447 | mode: int = 0o777 |
| 8448 | * |
| 8449 | dir_fd: dir_fd(requires='openat') = None |
| 8450 | |
| 8451 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 8452 | |
| 8453 | Open a file for low level IO. Returns a file descriptor (integer). |
| 8454 | |
| 8455 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 8456 | and path should be relative; path will then be relative to that directory. |
| 8457 | dir_fd may not be implemented on your platform. |
| 8458 | If it is unavailable, using it will raise a NotImplementedError. |
| 8459 | [clinic start generated code]*/ |
| 8460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8461 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8462 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
| 8463 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8464 | { |
| 8465 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8466 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8467 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8468 | #ifdef O_CLOEXEC |
| 8469 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 8470 | #elif !defined(MS_WINDOWS) |
| 8471 | int *atomic_flag_works = NULL; |
| 8472 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8473 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8474 | #ifdef MS_WINDOWS |
| 8475 | flags |= O_NOINHERIT; |
| 8476 | #elif defined(O_CLOEXEC) |
| 8477 | flags |= O_CLOEXEC; |
| 8478 | #endif |
| 8479 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 8480 | if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) { |
| 8481 | return -1; |
| 8482 | } |
| 8483 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8484 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8485 | do { |
| 8486 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8487 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8488 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8489 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8490 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8491 | if (dir_fd != DEFAULT_DIR_FD) |
| 8492 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 8493 | else |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8494 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8495 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8496 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8497 | Py_END_ALLOW_THREADS |
| 8498 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8499 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8500 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8501 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8502 | if (!async_err) |
| 8503 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8504 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8505 | } |
| 8506 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8507 | #ifndef MS_WINDOWS |
| 8508 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 8509 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8510 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8511 | } |
| 8512 | #endif |
| 8513 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8514 | return fd; |
| 8515 | } |
| 8516 | |
| 8517 | |
| 8518 | /*[clinic input] |
| 8519 | os.close |
| 8520 | |
| 8521 | fd: int |
| 8522 | |
| 8523 | Close a file descriptor. |
| 8524 | [clinic start generated code]*/ |
| 8525 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8526 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8527 | os_close_impl(PyObject *module, int fd) |
| 8528 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8529 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8530 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8531 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 8532 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 8533 | * for more details. |
| 8534 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8535 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8536 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8537 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8538 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8539 | Py_END_ALLOW_THREADS |
| 8540 | if (res < 0) |
| 8541 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8542 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8543 | } |
| 8544 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8545 | |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8546 | #ifdef HAVE_FDWALK |
| 8547 | static int |
| 8548 | _fdwalk_close_func(void *lohi, int fd) |
| 8549 | { |
| 8550 | int lo = ((int *)lohi)[0]; |
| 8551 | int hi = ((int *)lohi)[1]; |
| 8552 | |
| 8553 | if (fd >= hi) |
| 8554 | return 1; |
| 8555 | else if (fd >= lo) |
| 8556 | close(fd); |
| 8557 | return 0; |
| 8558 | } |
| 8559 | #endif /* HAVE_FDWALK */ |
| 8560 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8561 | /*[clinic input] |
| 8562 | os.closerange |
| 8563 | |
| 8564 | fd_low: int |
| 8565 | fd_high: int |
| 8566 | / |
| 8567 | |
| 8568 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 8569 | [clinic start generated code]*/ |
| 8570 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8571 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8572 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
| 8573 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8574 | { |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8575 | #ifdef HAVE_FDWALK |
| 8576 | int lohi[2]; |
| 8577 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8578 | int i; |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8579 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8580 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8581 | _Py_BEGIN_SUPPRESS_IPH |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8582 | #ifdef HAVE_FDWALK |
| 8583 | lohi[0] = Py_MAX(fd_low, 0); |
| 8584 | lohi[1] = fd_high; |
| 8585 | fdwalk(_fdwalk_close_func, lohi); |
| 8586 | #else |
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 8587 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 8588 | close(i); |
Jakub Kulík | e20134f | 2019-09-11 17:11:57 +0200 | [diff] [blame] | 8589 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8590 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8591 | Py_END_ALLOW_THREADS |
| 8592 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8593 | } |
| 8594 | |
| 8595 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8596 | /*[clinic input] |
| 8597 | os.dup -> int |
| 8598 | |
| 8599 | fd: int |
| 8600 | / |
| 8601 | |
| 8602 | Return a duplicate of a file descriptor. |
| 8603 | [clinic start generated code]*/ |
| 8604 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8605 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8606 | os_dup_impl(PyObject *module, int fd) |
| 8607 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8608 | { |
| 8609 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8610 | } |
| 8611 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8612 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8613 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8614 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8615 | fd: int |
| 8616 | fd2: int |
| 8617 | inheritable: bool=True |
| 8618 | |
| 8619 | Duplicate file descriptor. |
| 8620 | [clinic start generated code]*/ |
| 8621 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8622 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8623 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8624 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8625 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8626 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8627 | #if defined(HAVE_DUP3) && \ |
| 8628 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 8629 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8630 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8631 | #endif |
| 8632 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8633 | if (fd < 0 || fd2 < 0) { |
| 8634 | posix_error(); |
| 8635 | return -1; |
| 8636 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8637 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8638 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 8639 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 8640 | * upon close(), and therefore below. |
| 8641 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8642 | #ifdef MS_WINDOWS |
| 8643 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8644 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8645 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8646 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8647 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8648 | if (res < 0) { |
| 8649 | posix_error(); |
| 8650 | return -1; |
| 8651 | } |
| 8652 | res = fd2; // msvcrt dup2 returns 0 on success. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8653 | |
| 8654 | /* Character files like console cannot be make non-inheritable */ |
| 8655 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8656 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8657 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8658 | } |
| 8659 | |
| 8660 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 8661 | Py_BEGIN_ALLOW_THREADS |
| 8662 | if (!inheritable) |
| 8663 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 8664 | else |
| 8665 | res = dup2(fd, fd2); |
| 8666 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8667 | if (res < 0) { |
| 8668 | posix_error(); |
| 8669 | return -1; |
| 8670 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8671 | |
| 8672 | #else |
| 8673 | |
| 8674 | #ifdef HAVE_DUP3 |
| 8675 | if (!inheritable && dup3_works != 0) { |
| 8676 | Py_BEGIN_ALLOW_THREADS |
| 8677 | res = dup3(fd, fd2, O_CLOEXEC); |
| 8678 | Py_END_ALLOW_THREADS |
| 8679 | if (res < 0) { |
| 8680 | if (dup3_works == -1) |
| 8681 | dup3_works = (errno != ENOSYS); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8682 | if (dup3_works) { |
| 8683 | posix_error(); |
| 8684 | return -1; |
| 8685 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8686 | } |
| 8687 | } |
| 8688 | |
| 8689 | if (inheritable || dup3_works == 0) |
| 8690 | { |
| 8691 | #endif |
| 8692 | Py_BEGIN_ALLOW_THREADS |
| 8693 | res = dup2(fd, fd2); |
| 8694 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8695 | if (res < 0) { |
| 8696 | posix_error(); |
| 8697 | return -1; |
| 8698 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8699 | |
| 8700 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 8701 | close(fd2); |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8702 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8703 | } |
| 8704 | #ifdef HAVE_DUP3 |
| 8705 | } |
| 8706 | #endif |
| 8707 | |
| 8708 | #endif |
| 8709 | |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8710 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8711 | } |
| 8712 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8713 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8714 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8715 | /*[clinic input] |
| 8716 | os.lockf |
| 8717 | |
| 8718 | fd: int |
| 8719 | An open file descriptor. |
| 8720 | command: int |
| 8721 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 8722 | length: Py_off_t |
| 8723 | The number of bytes to lock, starting at the current position. |
| 8724 | / |
| 8725 | |
| 8726 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 8727 | |
| 8728 | [clinic start generated code]*/ |
| 8729 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8730 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8731 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
| 8732 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8733 | { |
| 8734 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8735 | |
| 8736 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8737 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8738 | Py_END_ALLOW_THREADS |
| 8739 | |
| 8740 | if (res < 0) |
| 8741 | return posix_error(); |
| 8742 | |
| 8743 | Py_RETURN_NONE; |
| 8744 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8745 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8746 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8747 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8748 | /*[clinic input] |
| 8749 | os.lseek -> Py_off_t |
| 8750 | |
| 8751 | fd: int |
| 8752 | position: Py_off_t |
| 8753 | how: int |
| 8754 | / |
| 8755 | |
| 8756 | Set the position of a file descriptor. Return the new position. |
| 8757 | |
| 8758 | Return the new cursor position in number of bytes |
| 8759 | relative to the beginning of the file. |
| 8760 | [clinic start generated code]*/ |
| 8761 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8762 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8763 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
| 8764 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8765 | { |
| 8766 | Py_off_t result; |
| 8767 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8768 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8769 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 8770 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8771 | case 0: how = SEEK_SET; break; |
| 8772 | case 1: how = SEEK_CUR; break; |
| 8773 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8774 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8775 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8776 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8777 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8778 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8779 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8780 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8781 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8782 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8783 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8784 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8785 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8786 | if (result < 0) |
| 8787 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8788 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8789 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8790 | } |
| 8791 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8792 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8793 | /*[clinic input] |
| 8794 | os.read |
| 8795 | fd: int |
| 8796 | length: Py_ssize_t |
| 8797 | / |
| 8798 | |
| 8799 | Read from a file descriptor. Returns a bytes object. |
| 8800 | [clinic start generated code]*/ |
| 8801 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8802 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8803 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
| 8804 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8805 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8806 | Py_ssize_t n; |
| 8807 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8808 | |
| 8809 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8810 | errno = EINVAL; |
| 8811 | return posix_error(); |
| 8812 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8813 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 8814 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8815 | |
| 8816 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8817 | if (buffer == NULL) |
| 8818 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8819 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8820 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 8821 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8822 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8823 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8824 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8825 | |
| 8826 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8827 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8828 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8829 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8830 | } |
| 8831 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8832 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8833 | || defined(__APPLE__))) \ |
| 8834 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ |
| 8835 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 8836 | static int |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8837 | 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] | 8838 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8839 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8840 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8841 | *iov = PyMem_New(struct iovec, cnt); |
| 8842 | if (*iov == NULL) { |
| 8843 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8844 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8845 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8846 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8847 | *buf = PyMem_New(Py_buffer, cnt); |
| 8848 | if (*buf == NULL) { |
| 8849 | PyMem_Del(*iov); |
| 8850 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8851 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8852 | } |
| 8853 | |
| 8854 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8855 | PyObject *item = PySequence_GetItem(seq, i); |
| 8856 | if (item == NULL) |
| 8857 | goto fail; |
| 8858 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 8859 | Py_DECREF(item); |
| 8860 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8861 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8862 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8863 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8864 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8865 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8866 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8867 | |
| 8868 | fail: |
| 8869 | PyMem_Del(*iov); |
| 8870 | for (j = 0; j < i; j++) { |
| 8871 | PyBuffer_Release(&(*buf)[j]); |
| 8872 | } |
| 8873 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8874 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8875 | } |
| 8876 | |
| 8877 | static void |
| 8878 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 8879 | { |
| 8880 | int i; |
| 8881 | PyMem_Del(iov); |
| 8882 | for (i = 0; i < cnt; i++) { |
| 8883 | PyBuffer_Release(&buf[i]); |
| 8884 | } |
| 8885 | PyMem_Del(buf); |
| 8886 | } |
| 8887 | #endif |
| 8888 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8889 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8890 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8891 | /*[clinic input] |
| 8892 | os.readv -> Py_ssize_t |
| 8893 | |
| 8894 | fd: int |
| 8895 | buffers: object |
| 8896 | / |
| 8897 | |
| 8898 | Read from a file descriptor fd into an iterable of buffers. |
| 8899 | |
| 8900 | The buffers should be mutable buffers accepting bytes. |
| 8901 | readv will transfer data into each buffer until it is full |
| 8902 | and then move on to the next buffer in the sequence to hold |
| 8903 | the rest of the data. |
| 8904 | |
| 8905 | readv returns the total number of bytes read, |
| 8906 | which may be less than the total capacity of all the buffers. |
| 8907 | [clinic start generated code]*/ |
| 8908 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8909 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8910 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
| 8911 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8912 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8913 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8914 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8915 | struct iovec *iov; |
| 8916 | Py_buffer *buf; |
| 8917 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8918 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8919 | PyErr_SetString(PyExc_TypeError, |
| 8920 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8921 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8922 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8923 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8924 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8925 | if (cnt < 0) |
| 8926 | return -1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8927 | |
| 8928 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 8929 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8930 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8931 | do { |
| 8932 | Py_BEGIN_ALLOW_THREADS |
| 8933 | n = readv(fd, iov, cnt); |
| 8934 | Py_END_ALLOW_THREADS |
| 8935 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8936 | |
| 8937 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8938 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8939 | if (!async_err) |
| 8940 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8941 | return -1; |
| 8942 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8943 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8944 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8945 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8946 | #endif /* HAVE_READV */ |
| 8947 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8948 | |
| 8949 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8950 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8951 | os.pread |
| 8952 | |
| 8953 | fd: int |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 8954 | length: Py_ssize_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8955 | offset: Py_off_t |
| 8956 | / |
| 8957 | |
| 8958 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 8959 | |
| 8960 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 8961 | the beginning of the file. The file offset remains unchanged. |
| 8962 | [clinic start generated code]*/ |
| 8963 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8964 | static PyObject * |
Dong-hee Na | ad7736f | 2019-09-25 14:47:04 +0900 | [diff] [blame] | 8965 | os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset) |
| 8966 | /*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8967 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8968 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8969 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8970 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8971 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8972 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8973 | errno = EINVAL; |
| 8974 | return posix_error(); |
| 8975 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8976 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8977 | if (buffer == NULL) |
| 8978 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8979 | |
| 8980 | do { |
| 8981 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8982 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8983 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8984 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8985 | Py_END_ALLOW_THREADS |
| 8986 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 8987 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8988 | if (n < 0) { |
| 8989 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8990 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8991 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8992 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8993 | _PyBytes_Resize(&buffer, n); |
| 8994 | return buffer; |
| 8995 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8996 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8997 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 8998 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
| 8999 | /*[clinic input] |
| 9000 | os.preadv -> Py_ssize_t |
| 9001 | |
| 9002 | fd: int |
| 9003 | buffers: object |
| 9004 | offset: Py_off_t |
| 9005 | flags: int = 0 |
| 9006 | / |
| 9007 | |
| 9008 | Reads from a file descriptor into a number of mutable bytes-like objects. |
| 9009 | |
| 9010 | Combines the functionality of readv() and pread(). As readv(), it will |
| 9011 | transfer data into each buffer until it is full and then move on to the next |
| 9012 | buffer in the sequence to hold the rest of the data. Its fourth argument, |
| 9013 | specifies the file offset at which the input operation is to be performed. It |
| 9014 | will return the total number of bytes read (which can be less than the total |
| 9015 | capacity of all the objects). |
| 9016 | |
| 9017 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9018 | |
| 9019 | - RWF_HIPRI |
| 9020 | - RWF_NOWAIT |
| 9021 | |
| 9022 | Using non-zero flags requires Linux 4.6 or newer. |
| 9023 | [clinic start generated code]*/ |
| 9024 | |
| 9025 | static Py_ssize_t |
| 9026 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9027 | int flags) |
| 9028 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ |
| 9029 | { |
| 9030 | Py_ssize_t cnt, n; |
| 9031 | int async_err = 0; |
| 9032 | struct iovec *iov; |
| 9033 | Py_buffer *buf; |
| 9034 | |
| 9035 | if (!PySequence_Check(buffers)) { |
| 9036 | PyErr_SetString(PyExc_TypeError, |
| 9037 | "preadv2() arg 2 must be a sequence"); |
| 9038 | return -1; |
| 9039 | } |
| 9040 | |
| 9041 | cnt = PySequence_Size(buffers); |
| 9042 | if (cnt < 0) { |
| 9043 | return -1; |
| 9044 | } |
| 9045 | |
| 9046 | #ifndef HAVE_PREADV2 |
| 9047 | if(flags != 0) { |
| 9048 | argument_unavailable_error("preadv2", "flags"); |
| 9049 | return -1; |
| 9050 | } |
| 9051 | #endif |
| 9052 | |
| 9053 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { |
| 9054 | return -1; |
| 9055 | } |
| 9056 | #ifdef HAVE_PREADV2 |
| 9057 | do { |
| 9058 | Py_BEGIN_ALLOW_THREADS |
| 9059 | _Py_BEGIN_SUPPRESS_IPH |
| 9060 | n = preadv2(fd, iov, cnt, offset, flags); |
| 9061 | _Py_END_SUPPRESS_IPH |
| 9062 | Py_END_ALLOW_THREADS |
| 9063 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9064 | #else |
| 9065 | do { |
| 9066 | Py_BEGIN_ALLOW_THREADS |
| 9067 | _Py_BEGIN_SUPPRESS_IPH |
| 9068 | n = preadv(fd, iov, cnt, offset); |
| 9069 | _Py_END_SUPPRESS_IPH |
| 9070 | Py_END_ALLOW_THREADS |
| 9071 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9072 | #endif |
| 9073 | |
| 9074 | iov_cleanup(iov, buf, cnt); |
| 9075 | if (n < 0) { |
| 9076 | if (!async_err) { |
| 9077 | posix_error(); |
| 9078 | } |
| 9079 | return -1; |
| 9080 | } |
| 9081 | |
| 9082 | return n; |
| 9083 | } |
| 9084 | #endif /* HAVE_PREADV */ |
| 9085 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9086 | |
| 9087 | /*[clinic input] |
| 9088 | os.write -> Py_ssize_t |
| 9089 | |
| 9090 | fd: int |
| 9091 | data: Py_buffer |
| 9092 | / |
| 9093 | |
| 9094 | Write a bytes object to a file descriptor. |
| 9095 | [clinic start generated code]*/ |
| 9096 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9097 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9098 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
| 9099 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9100 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9101 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9102 | } |
| 9103 | |
| 9104 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9105 | PyDoc_STRVAR(posix_sendfile__doc__, |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9106 | "sendfile(out_fd, in_fd, offset, count) -> byteswritten\n\ |
| 9107 | sendfile(out_fd, in_fd, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9108 | -> byteswritten\n\ |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9109 | 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] | 9110 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9111 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9112 | static PyObject * |
| 9113 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 9114 | { |
| 9115 | int in, out; |
| 9116 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9117 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9118 | off_t offset; |
| 9119 | |
| 9120 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 9121 | #ifndef __APPLE__ |
| 9122 | Py_ssize_t len; |
| 9123 | #endif |
| 9124 | PyObject *headers = NULL, *trailers = NULL; |
| 9125 | Py_buffer *hbuf, *tbuf; |
| 9126 | off_t sbytes; |
| 9127 | struct sf_hdtr sf; |
| 9128 | int flags = 0; |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9129 | static char *keywords[] = {"out_fd", "in_fd", |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 9130 | "offset", "count", |
| 9131 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9132 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 9133 | sf.headers = NULL; |
| 9134 | sf.trailers = NULL; |
| 9135 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9136 | #ifdef __APPLE__ |
| 9137 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9138 | 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] | 9139 | #else |
| 9140 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9141 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9142 | #endif |
| 9143 | &headers, &trailers, &flags)) |
| 9144 | return NULL; |
| 9145 | if (headers != NULL) { |
| 9146 | if (!PySequence_Check(headers)) { |
| 9147 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9148 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9149 | return NULL; |
| 9150 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9151 | Py_ssize_t i = PySequence_Size(headers); |
| 9152 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9153 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9154 | if (i > INT_MAX) { |
| 9155 | PyErr_SetString(PyExc_OverflowError, |
| 9156 | "sendfile() header is too large"); |
| 9157 | return NULL; |
| 9158 | } |
| 9159 | if (i > 0) { |
| 9160 | sf.hdr_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9161 | if (iov_setup(&(sf.headers), &hbuf, |
| 9162 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9163 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9164 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9165 | for (i = 0; i < sf.hdr_cnt; i++) { |
| 9166 | Py_ssize_t blen = sf.headers[i].iov_len; |
| 9167 | # define OFF_T_MAX 0x7fffffffffffffff |
| 9168 | if (sbytes >= OFF_T_MAX - blen) { |
| 9169 | PyErr_SetString(PyExc_OverflowError, |
| 9170 | "sendfile() header is too large"); |
| 9171 | return NULL; |
| 9172 | } |
| 9173 | sbytes += blen; |
| 9174 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9175 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9176 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9177 | } |
| 9178 | } |
| 9179 | if (trailers != NULL) { |
| 9180 | if (!PySequence_Check(trailers)) { |
| 9181 | PyErr_SetString(PyExc_TypeError, |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9182 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9183 | return NULL; |
| 9184 | } else { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9185 | Py_ssize_t i = PySequence_Size(trailers); |
| 9186 | if (i < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9187 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9188 | if (i > INT_MAX) { |
| 9189 | PyErr_SetString(PyExc_OverflowError, |
| 9190 | "sendfile() trailer is too large"); |
| 9191 | return NULL; |
| 9192 | } |
| 9193 | if (i > 0) { |
| 9194 | sf.trl_cnt = (int)i; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9195 | if (iov_setup(&(sf.trailers), &tbuf, |
| 9196 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9197 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9198 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9199 | } |
| 9200 | } |
| 9201 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9202 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9203 | do { |
| 9204 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9205 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9206 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9207 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9208 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9209 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9210 | Py_END_ALLOW_THREADS |
| 9211 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9212 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9213 | |
| 9214 | if (sf.headers != NULL) |
| 9215 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 9216 | if (sf.trailers != NULL) |
| 9217 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 9218 | |
| 9219 | if (ret < 0) { |
| 9220 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 9221 | if (sbytes != 0) { |
| 9222 | // some data has been sent |
| 9223 | goto done; |
| 9224 | } |
| 9225 | else { |
| 9226 | // no data has been sent; upper application is supposed |
| 9227 | // to retry on EAGAIN or EBUSY |
| 9228 | return posix_error(); |
| 9229 | } |
| 9230 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9231 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9232 | } |
| 9233 | goto done; |
| 9234 | |
| 9235 | done: |
| 9236 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 9237 | return Py_BuildValue("l", sbytes); |
| 9238 | #else |
| 9239 | return Py_BuildValue("L", sbytes); |
| 9240 | #endif |
| 9241 | |
| 9242 | #else |
| 9243 | Py_ssize_t count; |
| 9244 | PyObject *offobj; |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9245 | static char *keywords[] = {"out_fd", "in_fd", |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9246 | "offset", "count", NULL}; |
| 9247 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 9248 | keywords, &out, &in, &offobj, &count)) |
| 9249 | return NULL; |
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9250 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9251 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9252 | do { |
| 9253 | Py_BEGIN_ALLOW_THREADS |
| 9254 | ret = sendfile(out, in, NULL, count); |
| 9255 | Py_END_ALLOW_THREADS |
| 9256 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9257 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9258 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9259 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9260 | } |
| 9261 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9262 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9263 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9264 | |
| 9265 | do { |
| 9266 | Py_BEGIN_ALLOW_THREADS |
| 9267 | ret = sendfile(out, in, &offset, count); |
| 9268 | Py_END_ALLOW_THREADS |
| 9269 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9270 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9271 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9272 | return Py_BuildValue("n", ret); |
| 9273 | #endif |
| 9274 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9275 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9276 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9277 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9278 | #if defined(__APPLE__) |
| 9279 | /*[clinic input] |
| 9280 | os._fcopyfile |
| 9281 | |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9282 | in_fd: int |
| 9283 | out_fd: int |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9284 | flags: int |
| 9285 | / |
| 9286 | |
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9287 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9288 | [clinic start generated code]*/ |
| 9289 | |
| 9290 | static PyObject * |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9291 | os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags) |
| 9292 | /*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9293 | { |
| 9294 | int ret; |
| 9295 | |
| 9296 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | 140a7d1 | 2019-10-13 11:59:31 +0300 | [diff] [blame] | 9297 | ret = fcopyfile(in_fd, out_fd, NULL, flags); |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9298 | Py_END_ALLOW_THREADS |
| 9299 | if (ret < 0) |
| 9300 | return posix_error(); |
| 9301 | Py_RETURN_NONE; |
| 9302 | } |
| 9303 | #endif |
| 9304 | |
| 9305 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9306 | /*[clinic input] |
| 9307 | os.fstat |
| 9308 | |
| 9309 | fd : int |
| 9310 | |
| 9311 | Perform a stat system call on the given file descriptor. |
| 9312 | |
| 9313 | Like stat(), but for an open file descriptor. |
| 9314 | Equivalent to os.stat(fd). |
| 9315 | [clinic start generated code]*/ |
| 9316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9317 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9318 | os_fstat_impl(PyObject *module, int fd) |
| 9319 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9320 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9321 | STRUCT_STAT st; |
| 9322 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9323 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9324 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9325 | do { |
| 9326 | Py_BEGIN_ALLOW_THREADS |
| 9327 | res = FSTAT(fd, &st); |
| 9328 | Py_END_ALLOW_THREADS |
| 9329 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9330 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9331 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9332 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9333 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9334 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9335 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9336 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9337 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9338 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9339 | } |
| 9340 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9341 | |
| 9342 | /*[clinic input] |
| 9343 | os.isatty -> bool |
| 9344 | fd: int |
| 9345 | / |
| 9346 | |
| 9347 | Return True if the fd is connected to a terminal. |
| 9348 | |
| 9349 | Return True if the file descriptor is an open file descriptor |
| 9350 | connected to the slave end of a terminal. |
| 9351 | [clinic start generated code]*/ |
| 9352 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9353 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9354 | os_isatty_impl(PyObject *module, int fd) |
| 9355 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9356 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9357 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9358 | _Py_BEGIN_SUPPRESS_IPH |
| 9359 | return_value = isatty(fd); |
| 9360 | _Py_END_SUPPRESS_IPH |
| 9361 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9362 | } |
| 9363 | |
| 9364 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9365 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9366 | /*[clinic input] |
| 9367 | os.pipe |
| 9368 | |
| 9369 | Create a pipe. |
| 9370 | |
| 9371 | Returns a tuple of two file descriptors: |
| 9372 | (read_fd, write_fd) |
| 9373 | [clinic start generated code]*/ |
| 9374 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9375 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9376 | os_pipe_impl(PyObject *module) |
| 9377 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9378 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9379 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9380 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9381 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9382 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9383 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9384 | #else |
| 9385 | int res; |
| 9386 | #endif |
| 9387 | |
| 9388 | #ifdef MS_WINDOWS |
| 9389 | attr.nLength = sizeof(attr); |
| 9390 | attr.lpSecurityDescriptor = NULL; |
| 9391 | attr.bInheritHandle = FALSE; |
| 9392 | |
| 9393 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9394 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9395 | ok = CreatePipe(&read, &write, &attr, 0); |
| 9396 | if (ok) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9397 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
| 9398 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9399 | if (fds[0] == -1 || fds[1] == -1) { |
| 9400 | CloseHandle(read); |
| 9401 | CloseHandle(write); |
| 9402 | ok = 0; |
| 9403 | } |
| 9404 | } |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9405 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9406 | Py_END_ALLOW_THREADS |
| 9407 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9408 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9409 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9410 | #else |
| 9411 | |
| 9412 | #ifdef HAVE_PIPE2 |
| 9413 | Py_BEGIN_ALLOW_THREADS |
| 9414 | res = pipe2(fds, O_CLOEXEC); |
| 9415 | Py_END_ALLOW_THREADS |
| 9416 | |
| 9417 | if (res != 0 && errno == ENOSYS) |
| 9418 | { |
| 9419 | #endif |
| 9420 | Py_BEGIN_ALLOW_THREADS |
| 9421 | res = pipe(fds); |
| 9422 | Py_END_ALLOW_THREADS |
| 9423 | |
| 9424 | if (res == 0) { |
| 9425 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 9426 | close(fds[0]); |
| 9427 | close(fds[1]); |
| 9428 | return NULL; |
| 9429 | } |
| 9430 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 9431 | close(fds[0]); |
| 9432 | close(fds[1]); |
| 9433 | return NULL; |
| 9434 | } |
| 9435 | } |
| 9436 | #ifdef HAVE_PIPE2 |
| 9437 | } |
| 9438 | #endif |
| 9439 | |
| 9440 | if (res != 0) |
| 9441 | return PyErr_SetFromErrno(PyExc_OSError); |
| 9442 | #endif /* !MS_WINDOWS */ |
| 9443 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9444 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9445 | #endif /* HAVE_PIPE */ |
| 9446 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9447 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9448 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9449 | /*[clinic input] |
| 9450 | os.pipe2 |
| 9451 | |
| 9452 | flags: int |
| 9453 | / |
| 9454 | |
| 9455 | Create a pipe with flags set atomically. |
| 9456 | |
| 9457 | Returns a tuple of two file descriptors: |
| 9458 | (read_fd, write_fd) |
| 9459 | |
| 9460 | flags can be constructed by ORing together one or more of these values: |
| 9461 | O_NONBLOCK, O_CLOEXEC. |
| 9462 | [clinic start generated code]*/ |
| 9463 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9464 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9465 | os_pipe2_impl(PyObject *module, int flags) |
| 9466 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9467 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9468 | int fds[2]; |
| 9469 | int res; |
| 9470 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9471 | res = pipe2(fds, flags); |
| 9472 | if (res != 0) |
| 9473 | return posix_error(); |
| 9474 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 9475 | } |
| 9476 | #endif /* HAVE_PIPE2 */ |
| 9477 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9478 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9479 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9480 | /*[clinic input] |
| 9481 | os.writev -> Py_ssize_t |
| 9482 | fd: int |
| 9483 | buffers: object |
| 9484 | / |
| 9485 | |
| 9486 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 9487 | |
| 9488 | Returns the total number of bytes written. |
| 9489 | buffers must be a sequence of bytes-like objects. |
| 9490 | [clinic start generated code]*/ |
| 9491 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9492 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9493 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
| 9494 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9495 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9496 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9497 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9498 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9499 | struct iovec *iov; |
| 9500 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9501 | |
| 9502 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9503 | PyErr_SetString(PyExc_TypeError, |
| 9504 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9505 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9506 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9507 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9508 | if (cnt < 0) |
| 9509 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9510 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9511 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9512 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9513 | } |
| 9514 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9515 | do { |
| 9516 | Py_BEGIN_ALLOW_THREADS |
| 9517 | result = writev(fd, iov, cnt); |
| 9518 | Py_END_ALLOW_THREADS |
| 9519 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9520 | |
| 9521 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9522 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9523 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9524 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9525 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9526 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9527 | #endif /* HAVE_WRITEV */ |
| 9528 | |
| 9529 | |
| 9530 | #ifdef HAVE_PWRITE |
| 9531 | /*[clinic input] |
| 9532 | os.pwrite -> Py_ssize_t |
| 9533 | |
| 9534 | fd: int |
| 9535 | buffer: Py_buffer |
| 9536 | offset: Py_off_t |
| 9537 | / |
| 9538 | |
| 9539 | Write bytes to a file descriptor starting at a particular offset. |
| 9540 | |
| 9541 | Write buffer to fd, starting at offset bytes from the beginning of |
| 9542 | the file. Returns the number of bytes writte. Does not change the |
| 9543 | current file offset. |
| 9544 | [clinic start generated code]*/ |
| 9545 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9546 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9547 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 9548 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9549 | { |
| 9550 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9551 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9552 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9553 | do { |
| 9554 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9555 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9556 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9557 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9558 | Py_END_ALLOW_THREADS |
| 9559 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9560 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9561 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9562 | posix_error(); |
| 9563 | return size; |
| 9564 | } |
| 9565 | #endif /* HAVE_PWRITE */ |
| 9566 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9567 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
| 9568 | /*[clinic input] |
| 9569 | os.pwritev -> Py_ssize_t |
| 9570 | |
| 9571 | fd: int |
| 9572 | buffers: object |
| 9573 | offset: Py_off_t |
| 9574 | flags: int = 0 |
| 9575 | / |
| 9576 | |
| 9577 | Writes the contents of bytes-like objects to a file descriptor at a given offset. |
| 9578 | |
| 9579 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence |
| 9580 | of bytes-like objects. Buffers are processed in array order. Entire contents of first |
| 9581 | buffer is written before proceeding to second, and so on. The operating system may |
| 9582 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. |
| 9583 | This function writes the contents of each object to the file descriptor and returns |
| 9584 | the total number of bytes written. |
| 9585 | |
| 9586 | The flags argument contains a bitwise OR of zero or more of the following flags: |
| 9587 | |
| 9588 | - RWF_DSYNC |
| 9589 | - RWF_SYNC |
| 9590 | |
| 9591 | Using non-zero flags requires Linux 4.7 or newer. |
| 9592 | [clinic start generated code]*/ |
| 9593 | |
| 9594 | static Py_ssize_t |
| 9595 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, |
| 9596 | int flags) |
| 9597 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/ |
| 9598 | { |
| 9599 | Py_ssize_t cnt; |
| 9600 | Py_ssize_t result; |
| 9601 | int async_err = 0; |
| 9602 | struct iovec *iov; |
| 9603 | Py_buffer *buf; |
| 9604 | |
| 9605 | if (!PySequence_Check(buffers)) { |
| 9606 | PyErr_SetString(PyExc_TypeError, |
| 9607 | "pwritev() arg 2 must be a sequence"); |
| 9608 | return -1; |
| 9609 | } |
| 9610 | |
| 9611 | cnt = PySequence_Size(buffers); |
| 9612 | if (cnt < 0) { |
| 9613 | return -1; |
| 9614 | } |
| 9615 | |
| 9616 | #ifndef HAVE_PWRITEV2 |
| 9617 | if(flags != 0) { |
| 9618 | argument_unavailable_error("pwritev2", "flags"); |
| 9619 | return -1; |
| 9620 | } |
| 9621 | #endif |
| 9622 | |
| 9623 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 9624 | return -1; |
| 9625 | } |
| 9626 | #ifdef HAVE_PWRITEV2 |
| 9627 | do { |
| 9628 | Py_BEGIN_ALLOW_THREADS |
| 9629 | _Py_BEGIN_SUPPRESS_IPH |
| 9630 | result = pwritev2(fd, iov, cnt, offset, flags); |
| 9631 | _Py_END_SUPPRESS_IPH |
| 9632 | Py_END_ALLOW_THREADS |
| 9633 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9634 | #else |
| 9635 | do { |
| 9636 | Py_BEGIN_ALLOW_THREADS |
| 9637 | _Py_BEGIN_SUPPRESS_IPH |
| 9638 | result = pwritev(fd, iov, cnt, offset); |
| 9639 | _Py_END_SUPPRESS_IPH |
| 9640 | Py_END_ALLOW_THREADS |
| 9641 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9642 | #endif |
| 9643 | |
| 9644 | iov_cleanup(iov, buf, cnt); |
| 9645 | if (result < 0) { |
| 9646 | if (!async_err) { |
| 9647 | posix_error(); |
| 9648 | } |
| 9649 | return -1; |
| 9650 | } |
| 9651 | |
| 9652 | return result; |
| 9653 | } |
| 9654 | #endif /* HAVE_PWRITEV */ |
| 9655 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9656 | #ifdef HAVE_COPY_FILE_RANGE |
| 9657 | /*[clinic input] |
| 9658 | |
| 9659 | os.copy_file_range |
| 9660 | src: int |
| 9661 | Source file descriptor. |
| 9662 | dst: int |
| 9663 | Destination file descriptor. |
| 9664 | count: Py_ssize_t |
| 9665 | Number of bytes to copy. |
| 9666 | offset_src: object = None |
| 9667 | Starting offset in src. |
| 9668 | offset_dst: object = None |
| 9669 | Starting offset in dst. |
| 9670 | |
| 9671 | Copy count bytes from one file descriptor to another. |
| 9672 | |
| 9673 | If offset_src is None, then src is read from the current position; |
| 9674 | respectively for offset_dst. |
| 9675 | [clinic start generated code]*/ |
| 9676 | |
| 9677 | static PyObject * |
| 9678 | os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, |
| 9679 | PyObject *offset_src, PyObject *offset_dst) |
| 9680 | /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ |
| 9681 | { |
| 9682 | off_t offset_src_val, offset_dst_val; |
| 9683 | off_t *p_offset_src = NULL; |
| 9684 | off_t *p_offset_dst = NULL; |
| 9685 | Py_ssize_t ret; |
| 9686 | int async_err = 0; |
| 9687 | /* The flags argument is provided to allow |
| 9688 | * for future extensions and currently must be to 0. */ |
| 9689 | int flags = 0; |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9690 | |
| 9691 | |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9692 | if (count < 0) { |
| 9693 | PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); |
| 9694 | return NULL; |
| 9695 | } |
| 9696 | |
| 9697 | if (offset_src != Py_None) { |
| 9698 | if (!Py_off_t_converter(offset_src, &offset_src_val)) { |
| 9699 | return NULL; |
| 9700 | } |
| 9701 | p_offset_src = &offset_src_val; |
| 9702 | } |
| 9703 | |
| 9704 | if (offset_dst != Py_None) { |
| 9705 | if (!Py_off_t_converter(offset_dst, &offset_dst_val)) { |
| 9706 | return NULL; |
| 9707 | } |
| 9708 | p_offset_dst = &offset_dst_val; |
| 9709 | } |
| 9710 | |
| 9711 | do { |
| 9712 | Py_BEGIN_ALLOW_THREADS |
| 9713 | ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags); |
| 9714 | Py_END_ALLOW_THREADS |
| 9715 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9716 | |
| 9717 | if (ret < 0) { |
| 9718 | return (!async_err) ? posix_error() : NULL; |
| 9719 | } |
| 9720 | |
| 9721 | return PyLong_FromSsize_t(ret); |
| 9722 | } |
| 9723 | #endif /* HAVE_COPY_FILE_RANGE*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9724 | |
| 9725 | #ifdef HAVE_MKFIFO |
| 9726 | /*[clinic input] |
| 9727 | os.mkfifo |
| 9728 | |
| 9729 | path: path_t |
| 9730 | mode: int=0o666 |
| 9731 | * |
| 9732 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 9733 | |
| 9734 | Create a "fifo" (a POSIX named pipe). |
| 9735 | |
| 9736 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9737 | and path should be relative; path will then be relative to that directory. |
| 9738 | dir_fd may not be implemented on your platform. |
| 9739 | If it is unavailable, using it will raise a NotImplementedError. |
| 9740 | [clinic start generated code]*/ |
| 9741 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9742 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9743 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
| 9744 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9745 | { |
| 9746 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9747 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9748 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9749 | do { |
| 9750 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9751 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9752 | if (dir_fd != DEFAULT_DIR_FD) |
| 9753 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 9754 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9755 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9756 | result = mkfifo(path->narrow, mode); |
| 9757 | Py_END_ALLOW_THREADS |
| 9758 | } while (result != 0 && errno == EINTR && |
| 9759 | !(async_err = PyErr_CheckSignals())); |
| 9760 | if (result != 0) |
| 9761 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9762 | |
| 9763 | Py_RETURN_NONE; |
| 9764 | } |
| 9765 | #endif /* HAVE_MKFIFO */ |
| 9766 | |
| 9767 | |
| 9768 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 9769 | /*[clinic input] |
| 9770 | os.mknod |
| 9771 | |
| 9772 | path: path_t |
| 9773 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9774 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9775 | * |
| 9776 | dir_fd: dir_fd(requires='mknodat')=None |
| 9777 | |
| 9778 | Create a node in the file system. |
| 9779 | |
| 9780 | Create a node in the file system (file, device special file or named pipe) |
| 9781 | at path. mode specifies both the permissions to use and the |
| 9782 | type of node to be created, being combined (bitwise OR) with one of |
| 9783 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 9784 | device defines the newly created device special file (probably using |
| 9785 | os.makedev()). Otherwise device is ignored. |
| 9786 | |
| 9787 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 9788 | and path should be relative; path will then be relative to that directory. |
| 9789 | dir_fd may not be implemented on your platform. |
| 9790 | If it is unavailable, using it will raise a NotImplementedError. |
| 9791 | [clinic start generated code]*/ |
| 9792 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9793 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9794 | 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] | 9795 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9796 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9797 | { |
| 9798 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9799 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9800 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9801 | do { |
| 9802 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9803 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9804 | if (dir_fd != DEFAULT_DIR_FD) |
| 9805 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 9806 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9807 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9808 | result = mknod(path->narrow, mode, device); |
| 9809 | Py_END_ALLOW_THREADS |
| 9810 | } while (result != 0 && errno == EINTR && |
| 9811 | !(async_err = PyErr_CheckSignals())); |
| 9812 | if (result != 0) |
| 9813 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9814 | |
| 9815 | Py_RETURN_NONE; |
| 9816 | } |
| 9817 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 9818 | |
| 9819 | |
| 9820 | #ifdef HAVE_DEVICE_MACROS |
| 9821 | /*[clinic input] |
| 9822 | os.major -> unsigned_int |
| 9823 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9824 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9825 | / |
| 9826 | |
| 9827 | Extracts a device major number from a raw device number. |
| 9828 | [clinic start generated code]*/ |
| 9829 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9830 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9831 | os_major_impl(PyObject *module, dev_t device) |
| 9832 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9833 | { |
| 9834 | return major(device); |
| 9835 | } |
| 9836 | |
| 9837 | |
| 9838 | /*[clinic input] |
| 9839 | os.minor -> unsigned_int |
| 9840 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9841 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9842 | / |
| 9843 | |
| 9844 | Extracts a device minor number from a raw device number. |
| 9845 | [clinic start generated code]*/ |
| 9846 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9847 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9848 | os_minor_impl(PyObject *module, dev_t device) |
| 9849 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9850 | { |
| 9851 | return minor(device); |
| 9852 | } |
| 9853 | |
| 9854 | |
| 9855 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9856 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9857 | |
| 9858 | major: int |
| 9859 | minor: int |
| 9860 | / |
| 9861 | |
| 9862 | Composes a raw device number from the major and minor device numbers. |
| 9863 | [clinic start generated code]*/ |
| 9864 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9865 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9866 | os_makedev_impl(PyObject *module, int major, int minor) |
| 9867 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9868 | { |
| 9869 | return makedev(major, minor); |
| 9870 | } |
| 9871 | #endif /* HAVE_DEVICE_MACROS */ |
| 9872 | |
| 9873 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9874 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9875 | /*[clinic input] |
| 9876 | os.ftruncate |
| 9877 | |
| 9878 | fd: int |
| 9879 | length: Py_off_t |
| 9880 | / |
| 9881 | |
| 9882 | Truncate a file, specified by file descriptor, to a specific length. |
| 9883 | [clinic start generated code]*/ |
| 9884 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9885 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9886 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
| 9887 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9888 | { |
| 9889 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9890 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9891 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 9892 | if (PySys_Audit("os.truncate", "in", fd, length) < 0) { |
| 9893 | return NULL; |
| 9894 | } |
| 9895 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9896 | do { |
| 9897 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9898 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9899 | #ifdef MS_WINDOWS |
| 9900 | result = _chsize_s(fd, length); |
| 9901 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9902 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9903 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9904 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9905 | Py_END_ALLOW_THREADS |
| 9906 | } while (result != 0 && errno == EINTR && |
| 9907 | !(async_err = PyErr_CheckSignals())); |
| 9908 | if (result != 0) |
| 9909 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9910 | Py_RETURN_NONE; |
| 9911 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9912 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9913 | |
| 9914 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9915 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9916 | /*[clinic input] |
| 9917 | os.truncate |
| 9918 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 9919 | length: Py_off_t |
| 9920 | |
| 9921 | Truncate a file, specified by path, to a specific length. |
| 9922 | |
| 9923 | On some platforms, path may also be specified as an open file descriptor. |
| 9924 | If this functionality is unavailable, using it raises an exception. |
| 9925 | [clinic start generated code]*/ |
| 9926 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9927 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9928 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
| 9929 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9930 | { |
| 9931 | int result; |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9932 | #ifdef MS_WINDOWS |
| 9933 | int fd; |
| 9934 | #endif |
| 9935 | |
| 9936 | if (path->fd != -1) |
| 9937 | return os_ftruncate_impl(module, path->fd, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9938 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 9939 | if (PySys_Audit("os.truncate", "On", path->object, length) < 0) { |
| 9940 | return NULL; |
| 9941 | } |
| 9942 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9943 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9944 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9945 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 9946 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 9947 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9948 | result = -1; |
| 9949 | else { |
| 9950 | result = _chsize_s(fd, length); |
| 9951 | close(fd); |
| 9952 | if (result < 0) |
| 9953 | errno = result; |
| 9954 | } |
| 9955 | #else |
| 9956 | result = truncate(path->narrow, length); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9957 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9958 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9959 | Py_END_ALLOW_THREADS |
| 9960 | if (result < 0) |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 9961 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9962 | |
| 9963 | Py_RETURN_NONE; |
| 9964 | } |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9965 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9966 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9967 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9968 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 9969 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 9970 | defined, which is the case in Python on AIX. AIX bug report: |
| 9971 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 9972 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 9973 | # define POSIX_FADVISE_AIX_BUG |
| 9974 | #endif |
| 9975 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9976 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9977 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9978 | /*[clinic input] |
| 9979 | os.posix_fallocate |
| 9980 | |
| 9981 | fd: int |
| 9982 | offset: Py_off_t |
| 9983 | length: Py_off_t |
| 9984 | / |
| 9985 | |
| 9986 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 9987 | |
| 9988 | Ensure that the file specified by fd encompasses a range of bytes |
| 9989 | starting at offset bytes from the beginning and continuing for length bytes. |
| 9990 | [clinic start generated code]*/ |
| 9991 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9992 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9993 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9994 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9995 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9996 | { |
| 9997 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9998 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9999 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10000 | do { |
| 10001 | Py_BEGIN_ALLOW_THREADS |
| 10002 | result = posix_fallocate(fd, offset, length); |
| 10003 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10004 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10005 | |
| 10006 | if (result == 0) |
| 10007 | Py_RETURN_NONE; |
| 10008 | |
| 10009 | if (async_err) |
| 10010 | return NULL; |
| 10011 | |
| 10012 | errno = result; |
| 10013 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10014 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10015 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10016 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10017 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10018 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10019 | /*[clinic input] |
| 10020 | os.posix_fadvise |
| 10021 | |
| 10022 | fd: int |
| 10023 | offset: Py_off_t |
| 10024 | length: Py_off_t |
| 10025 | advice: int |
| 10026 | / |
| 10027 | |
| 10028 | Announce an intention to access data in a specific pattern. |
| 10029 | |
| 10030 | Announce an intention to access data in a specific pattern, thus allowing |
| 10031 | the kernel to make optimizations. |
| 10032 | The advice applies to the region of the file specified by fd starting at |
| 10033 | offset and continuing for length bytes. |
| 10034 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 10035 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 10036 | POSIX_FADV_DONTNEED. |
| 10037 | [clinic start generated code]*/ |
| 10038 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10039 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10040 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10041 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10042 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10043 | { |
| 10044 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10045 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10046 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10047 | do { |
| 10048 | Py_BEGIN_ALLOW_THREADS |
| 10049 | result = posix_fadvise(fd, offset, length, advice); |
| 10050 | Py_END_ALLOW_THREADS |
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10051 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
| 10052 | |
| 10053 | if (result == 0) |
| 10054 | Py_RETURN_NONE; |
| 10055 | |
| 10056 | if (async_err) |
| 10057 | return NULL; |
| 10058 | |
| 10059 | errno = result; |
| 10060 | return posix_error(); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10061 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10062 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10063 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10064 | |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10065 | #ifdef PY_PUTENV_DICT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10066 | static void |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10067 | posix_putenv_dict_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10068 | { |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10069 | /* Install the first arg and newstr in putenv_dict; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10070 | * this will cause previous value to be collected. This has to |
| 10071 | * happen after the real putenv() call because the old value |
| 10072 | * was still accessible until then. */ |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10073 | if (PyDict_SetItem(_posixstate_global->putenv_dict, name, value)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10074 | /* really not much we can do; just leak */ |
| 10075 | PyErr_Clear(); |
| 10076 | else |
| 10077 | Py_DECREF(value); |
| 10078 | } |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10079 | #endif /* PY_PUTENV_DICT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10080 | |
| 10081 | |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10082 | #ifdef HAVE_PUTENV |
| 10083 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 10084 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10085 | /*[clinic input] |
| 10086 | os.putenv |
| 10087 | |
| 10088 | name: unicode |
| 10089 | value: unicode |
| 10090 | / |
| 10091 | |
| 10092 | Change or add an environment variable. |
| 10093 | [clinic start generated code]*/ |
| 10094 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10095 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10096 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10097 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10098 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 10099 | const wchar_t *env; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10100 | Py_ssize_t size; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10101 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10102 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 10103 | defining hidden environment variables. */ |
| 10104 | if (PyUnicode_GET_LENGTH(name) == 0 || |
| 10105 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) |
| 10106 | { |
| 10107 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10108 | return NULL; |
| 10109 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10110 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 10111 | if (unicode == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10112 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10113 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10114 | |
| 10115 | env = PyUnicode_AsUnicodeAndSize(unicode, &size); |
| 10116 | if (env == NULL) |
| 10117 | goto error; |
| 10118 | if (size > _MAX_ENV) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10119 | PyErr_Format(PyExc_ValueError, |
| 10120 | "the environment variable is longer than %u characters", |
| 10121 | _MAX_ENV); |
| 10122 | goto error; |
| 10123 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10124 | if (wcslen(env) != (size_t)size) { |
| 10125 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10126 | goto error; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10127 | } |
| 10128 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10129 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10130 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10131 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10132 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10133 | |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10134 | posix_putenv_dict_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10135 | Py_RETURN_NONE; |
| 10136 | |
| 10137 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10138 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10139 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10140 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10141 | #else /* MS_WINDOWS */ |
| 10142 | /*[clinic input] |
| 10143 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10144 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10145 | name: FSConverter |
| 10146 | value: FSConverter |
| 10147 | / |
| 10148 | |
| 10149 | Change or add an environment variable. |
| 10150 | [clinic start generated code]*/ |
| 10151 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10152 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10153 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
| 10154 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10155 | { |
| 10156 | PyObject *bytes = NULL; |
| 10157 | char *env; |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10158 | const char *name_string = PyBytes_AS_STRING(name); |
| 10159 | const char *value_string = PyBytes_AS_STRING(value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10160 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10161 | if (strchr(name_string, '=') != NULL) { |
| 10162 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 10163 | return NULL; |
| 10164 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10165 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 10166 | if (bytes == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10167 | return NULL; |
| 10168 | } |
| 10169 | |
| 10170 | env = PyBytes_AS_STRING(bytes); |
| 10171 | if (putenv(env)) { |
| 10172 | Py_DECREF(bytes); |
| 10173 | return posix_error(); |
| 10174 | } |
| 10175 | |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10176 | posix_putenv_dict_setitem(name, bytes); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10177 | Py_RETURN_NONE; |
| 10178 | } |
| 10179 | #endif /* MS_WINDOWS */ |
| 10180 | #endif /* HAVE_PUTENV */ |
| 10181 | |
| 10182 | |
Victor Stinner | 56cd371 | 2020-01-21 16:13:09 +0100 | [diff] [blame] | 10183 | #ifdef MS_WINDOWS |
| 10184 | /*[clinic input] |
| 10185 | os.unsetenv |
| 10186 | name: unicode |
| 10187 | / |
| 10188 | |
| 10189 | Delete an environment variable. |
| 10190 | [clinic start generated code]*/ |
| 10191 | |
| 10192 | static PyObject * |
| 10193 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10194 | /*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/ |
| 10195 | { |
| 10196 | /* PyUnicode_AsWideCharString() rejects embedded null characters */ |
| 10197 | wchar_t *name_str = PyUnicode_AsWideCharString(name, NULL); |
| 10198 | if (name_str == NULL) { |
| 10199 | return NULL; |
| 10200 | } |
| 10201 | |
| 10202 | BOOL ok = SetEnvironmentVariableW(name_str, NULL); |
| 10203 | PyMem_Free(name_str); |
| 10204 | |
| 10205 | if (!ok) { |
| 10206 | return PyErr_SetFromWindowsErr(0); |
| 10207 | } |
| 10208 | |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10209 | #ifdef PY_PUTENV_DICT |
| 10210 | /* Remove the key from putenv_dict; |
Victor Stinner | 56cd371 | 2020-01-21 16:13:09 +0100 | [diff] [blame] | 10211 | * this will cause it to be collected. This has to |
| 10212 | * happen after the real unsetenv() call because the |
| 10213 | * old value was still accessible until then. |
| 10214 | */ |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10215 | if (PyDict_DelItem(_posixstate(module)->putenv_dict, name)) { |
Victor Stinner | 56cd371 | 2020-01-21 16:13:09 +0100 | [diff] [blame] | 10216 | /* really not much we can do; just leak */ |
| 10217 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 10218 | return NULL; |
| 10219 | } |
| 10220 | PyErr_Clear(); |
| 10221 | } |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10222 | #endif |
Victor Stinner | 56cd371 | 2020-01-21 16:13:09 +0100 | [diff] [blame] | 10223 | |
| 10224 | Py_RETURN_NONE; |
| 10225 | } |
| 10226 | /* repeat !defined(MS_WINDOWS) to workaround an Argument Clinic issue */ |
| 10227 | #elif defined(HAVE_UNSETENV) && !defined(MS_WINDOWS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10228 | /*[clinic input] |
| 10229 | os.unsetenv |
| 10230 | name: FSConverter |
| 10231 | / |
| 10232 | |
| 10233 | Delete an environment variable. |
| 10234 | [clinic start generated code]*/ |
| 10235 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10236 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10237 | os_unsetenv_impl(PyObject *module, PyObject *name) |
| 10238 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10239 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10240 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10241 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10242 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10243 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10244 | #ifdef HAVE_BROKEN_UNSETENV |
| 10245 | unsetenv(PyBytes_AS_STRING(name)); |
| 10246 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10247 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10248 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10249 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10250 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10251 | |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10252 | #ifdef PY_PUTENV_DICT |
| 10253 | /* Remove the key from putenv_dict; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10254 | * this will cause it to be collected. This has to |
| 10255 | * happen after the real unsetenv() call because the |
| 10256 | * old value was still accessible until then. |
| 10257 | */ |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10258 | if (PyDict_DelItem(_posixstate(module)->putenv_dict, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10259 | /* really not much we can do; just leak */ |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 10260 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 10261 | return NULL; |
| 10262 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10263 | PyErr_Clear(); |
| 10264 | } |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 10265 | #endif |
| 10266 | |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10267 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10268 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10269 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10270 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10271 | |
| 10272 | /*[clinic input] |
| 10273 | os.strerror |
| 10274 | |
| 10275 | code: int |
| 10276 | / |
| 10277 | |
| 10278 | Translate an error code to a message string. |
| 10279 | [clinic start generated code]*/ |
| 10280 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10281 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10282 | os_strerror_impl(PyObject *module, int code) |
| 10283 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10284 | { |
| 10285 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10286 | if (message == NULL) { |
| 10287 | PyErr_SetString(PyExc_ValueError, |
| 10288 | "strerror() argument out of range"); |
| 10289 | return NULL; |
| 10290 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 10291 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10292 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10293 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10294 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10295 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10296 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10297 | /*[clinic input] |
| 10298 | os.WCOREDUMP -> bool |
| 10299 | |
| 10300 | status: int |
| 10301 | / |
| 10302 | |
| 10303 | Return True if the process returning status was dumped to a core file. |
| 10304 | [clinic start generated code]*/ |
| 10305 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10306 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10307 | os_WCOREDUMP_impl(PyObject *module, int status) |
| 10308 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10309 | { |
| 10310 | WAIT_TYPE wait_status; |
| 10311 | WAIT_STATUS_INT(wait_status) = status; |
| 10312 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10313 | } |
| 10314 | #endif /* WCOREDUMP */ |
| 10315 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10316 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10317 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10318 | /*[clinic input] |
| 10319 | os.WIFCONTINUED -> bool |
| 10320 | |
| 10321 | status: int |
| 10322 | |
| 10323 | Return True if a particular process was continued from a job control stop. |
| 10324 | |
| 10325 | Return True if the process returning status was continued from a |
| 10326 | job control stop. |
| 10327 | [clinic start generated code]*/ |
| 10328 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10329 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10330 | os_WIFCONTINUED_impl(PyObject *module, int status) |
| 10331 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10332 | { |
| 10333 | WAIT_TYPE wait_status; |
| 10334 | WAIT_STATUS_INT(wait_status) = status; |
| 10335 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10336 | } |
| 10337 | #endif /* WIFCONTINUED */ |
| 10338 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10339 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10340 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10341 | /*[clinic input] |
| 10342 | os.WIFSTOPPED -> bool |
| 10343 | |
| 10344 | status: int |
| 10345 | |
| 10346 | Return True if the process returning status was stopped. |
| 10347 | [clinic start generated code]*/ |
| 10348 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10349 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10350 | os_WIFSTOPPED_impl(PyObject *module, int status) |
| 10351 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10352 | { |
| 10353 | WAIT_TYPE wait_status; |
| 10354 | WAIT_STATUS_INT(wait_status) = status; |
| 10355 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10356 | } |
| 10357 | #endif /* WIFSTOPPED */ |
| 10358 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10359 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10360 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10361 | /*[clinic input] |
| 10362 | os.WIFSIGNALED -> bool |
| 10363 | |
| 10364 | status: int |
| 10365 | |
| 10366 | Return True if the process returning status was terminated by a signal. |
| 10367 | [clinic start generated code]*/ |
| 10368 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10369 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10370 | os_WIFSIGNALED_impl(PyObject *module, int status) |
| 10371 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10372 | { |
| 10373 | WAIT_TYPE wait_status; |
| 10374 | WAIT_STATUS_INT(wait_status) = status; |
| 10375 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10376 | } |
| 10377 | #endif /* WIFSIGNALED */ |
| 10378 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10379 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10380 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10381 | /*[clinic input] |
| 10382 | os.WIFEXITED -> bool |
| 10383 | |
| 10384 | status: int |
| 10385 | |
| 10386 | Return True if the process returning status exited via the exit() system call. |
| 10387 | [clinic start generated code]*/ |
| 10388 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10389 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10390 | os_WIFEXITED_impl(PyObject *module, int status) |
| 10391 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10392 | { |
| 10393 | WAIT_TYPE wait_status; |
| 10394 | WAIT_STATUS_INT(wait_status) = status; |
| 10395 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10396 | } |
| 10397 | #endif /* WIFEXITED */ |
| 10398 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10399 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10400 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10401 | /*[clinic input] |
| 10402 | os.WEXITSTATUS -> int |
| 10403 | |
| 10404 | status: int |
| 10405 | |
| 10406 | Return the process return code from status. |
| 10407 | [clinic start generated code]*/ |
| 10408 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10409 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10410 | os_WEXITSTATUS_impl(PyObject *module, int status) |
| 10411 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10412 | { |
| 10413 | WAIT_TYPE wait_status; |
| 10414 | WAIT_STATUS_INT(wait_status) = status; |
| 10415 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10416 | } |
| 10417 | #endif /* WEXITSTATUS */ |
| 10418 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10419 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10420 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10421 | /*[clinic input] |
| 10422 | os.WTERMSIG -> int |
| 10423 | |
| 10424 | status: int |
| 10425 | |
| 10426 | Return the signal that terminated the process that provided the status value. |
| 10427 | [clinic start generated code]*/ |
| 10428 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10429 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10430 | os_WTERMSIG_impl(PyObject *module, int status) |
| 10431 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10432 | { |
| 10433 | WAIT_TYPE wait_status; |
| 10434 | WAIT_STATUS_INT(wait_status) = status; |
| 10435 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10436 | } |
| 10437 | #endif /* WTERMSIG */ |
| 10438 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10439 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10440 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10441 | /*[clinic input] |
| 10442 | os.WSTOPSIG -> int |
| 10443 | |
| 10444 | status: int |
| 10445 | |
| 10446 | Return the signal that stopped the process that provided the status value. |
| 10447 | [clinic start generated code]*/ |
| 10448 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10449 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10450 | os_WSTOPSIG_impl(PyObject *module, int status) |
| 10451 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10452 | { |
| 10453 | WAIT_TYPE wait_status; |
| 10454 | WAIT_STATUS_INT(wait_status) = status; |
| 10455 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10456 | } |
| 10457 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10458 | #endif /* HAVE_SYS_WAIT_H */ |
| 10459 | |
| 10460 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10461 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10462 | #ifdef _SCO_DS |
| 10463 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 10464 | needed definitions in sys/statvfs.h */ |
| 10465 | #define _SVID3 |
| 10466 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10467 | #include <sys/statvfs.h> |
| 10468 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10469 | static PyObject* |
| 10470 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 10471 | PyObject *StatVFSResultType = _posixstate_global->StatVFSResultType; |
| 10472 | PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10473 | if (v == NULL) |
| 10474 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10475 | |
| 10476 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10477 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10478 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10479 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 10480 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 10481 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 10482 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 10483 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 10484 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 10485 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10486 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10487 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10488 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 10489 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 10490 | PyStructSequence_SET_ITEM(v, 2, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10491 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10492 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10493 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10494 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10495 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10496 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10497 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10498 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10499 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10500 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10501 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10502 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 10503 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10504 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10505 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
| 10506 | * (issue #32390). */ |
| 10507 | #if defined(_AIX) && defined(_ALL_SOURCE) |
| 10508 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); |
| 10509 | #else |
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10510 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10511 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10512 | if (PyErr_Occurred()) { |
| 10513 | Py_DECREF(v); |
| 10514 | return NULL; |
| 10515 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10516 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10517 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10518 | } |
| 10519 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10520 | |
| 10521 | /*[clinic input] |
| 10522 | os.fstatvfs |
| 10523 | fd: int |
| 10524 | / |
| 10525 | |
| 10526 | Perform an fstatvfs system call on the given fd. |
| 10527 | |
| 10528 | Equivalent to statvfs(fd). |
| 10529 | [clinic start generated code]*/ |
| 10530 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10531 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10532 | os_fstatvfs_impl(PyObject *module, int fd) |
| 10533 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10534 | { |
| 10535 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10536 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10537 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10538 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10539 | do { |
| 10540 | Py_BEGIN_ALLOW_THREADS |
| 10541 | result = fstatvfs(fd, &st); |
| 10542 | Py_END_ALLOW_THREADS |
| 10543 | } while (result != 0 && errno == EINTR && |
| 10544 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10545 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10546 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10547 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10548 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10549 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10550 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10551 | |
| 10552 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10553 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10554 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10555 | /*[clinic input] |
| 10556 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10557 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10558 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 10559 | |
| 10560 | Perform a statvfs system call on the given path. |
| 10561 | |
| 10562 | path may always be specified as a string. |
| 10563 | On some platforms, path may also be specified as an open file descriptor. |
| 10564 | If this functionality is unavailable, using it raises an exception. |
| 10565 | [clinic start generated code]*/ |
| 10566 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10567 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10568 | os_statvfs_impl(PyObject *module, path_t *path) |
| 10569 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10570 | { |
| 10571 | int result; |
| 10572 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10573 | |
| 10574 | Py_BEGIN_ALLOW_THREADS |
| 10575 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10576 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10577 | #ifdef __APPLE__ |
| 10578 | /* handle weak-linking on Mac OS X 10.3 */ |
| 10579 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10580 | fd_specified("statvfs", path->fd); |
| 10581 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10582 | } |
| 10583 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10584 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10585 | } |
| 10586 | else |
| 10587 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10588 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10589 | Py_END_ALLOW_THREADS |
| 10590 | |
| 10591 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10592 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10593 | } |
| 10594 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10595 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10596 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10597 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 10598 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10599 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10600 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10601 | /*[clinic input] |
| 10602 | os._getdiskusage |
| 10603 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10604 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10605 | |
| 10606 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 10607 | [clinic start generated code]*/ |
| 10608 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10609 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10610 | os__getdiskusage_impl(PyObject *module, path_t *path) |
| 10611 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10612 | { |
| 10613 | BOOL retval; |
| 10614 | ULARGE_INTEGER _, total, free; |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10615 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10616 | |
| 10617 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10618 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10619 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10620 | if (retval == 0) { |
| 10621 | if (GetLastError() == ERROR_DIRECTORY) { |
| 10622 | wchar_t *dir_path = NULL; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10623 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10624 | dir_path = PyMem_New(wchar_t, path->length + 1); |
| 10625 | if (dir_path == NULL) { |
| 10626 | return PyErr_NoMemory(); |
| 10627 | } |
| 10628 | |
| 10629 | wcscpy_s(dir_path, path->length + 1, path->wide); |
| 10630 | |
| 10631 | if (_dirnameW(dir_path) != -1) { |
| 10632 | Py_BEGIN_ALLOW_THREADS |
| 10633 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); |
| 10634 | Py_END_ALLOW_THREADS |
| 10635 | } |
| 10636 | /* Record the last error in case it's modified by PyMem_Free. */ |
| 10637 | err = GetLastError(); |
| 10638 | PyMem_Free(dir_path); |
| 10639 | if (retval) { |
| 10640 | goto success; |
| 10641 | } |
| 10642 | } |
| 10643 | return PyErr_SetFromWindowsErr(err); |
| 10644 | } |
| 10645 | |
| 10646 | success: |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10647 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 10648 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10649 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10650 | |
| 10651 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10652 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 10653 | * It maps strings representing configuration variable names to |
| 10654 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10655 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10656 | * rarely-used constants. There are three separate tables that use |
| 10657 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10658 | * |
| 10659 | * This code is always included, even if none of the interfaces that |
| 10660 | * need it are included. The #if hackery needed to avoid it would be |
| 10661 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10662 | */ |
| 10663 | struct constdef { |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10664 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10665 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10666 | }; |
| 10667 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10668 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10669 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10670 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10671 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10672 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10673 | int value = _PyLong_AsInt(arg); |
| 10674 | if (value == -1 && PyErr_Occurred()) |
| 10675 | return 0; |
| 10676 | *valuep = value; |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10677 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10678 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10679 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10680 | /* look up the value in the table using a binary search */ |
| 10681 | size_t lo = 0; |
| 10682 | size_t mid; |
| 10683 | size_t hi = tablesize; |
| 10684 | int cmp; |
| 10685 | const char *confname; |
| 10686 | if (!PyUnicode_Check(arg)) { |
| 10687 | PyErr_SetString(PyExc_TypeError, |
| 10688 | "configuration names must be strings or integers"); |
| 10689 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10690 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10691 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10692 | if (confname == NULL) |
| 10693 | return 0; |
| 10694 | while (lo < hi) { |
| 10695 | mid = (lo + hi) / 2; |
| 10696 | cmp = strcmp(confname, table[mid].name); |
| 10697 | if (cmp < 0) |
| 10698 | hi = mid; |
| 10699 | else if (cmp > 0) |
| 10700 | lo = mid + 1; |
| 10701 | else { |
| 10702 | *valuep = table[mid].value; |
| 10703 | return 1; |
| 10704 | } |
| 10705 | } |
| 10706 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 10707 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10708 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10709 | } |
| 10710 | |
| 10711 | |
| 10712 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 10713 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10714 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10715 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10716 | #endif |
| 10717 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10718 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10719 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10720 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10721 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10722 | #endif |
| 10723 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10724 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10725 | #endif |
| 10726 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10727 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10728 | #endif |
| 10729 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10730 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10731 | #endif |
| 10732 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10733 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10734 | #endif |
| 10735 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10736 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10737 | #endif |
| 10738 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10739 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10740 | #endif |
| 10741 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10742 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10743 | #endif |
| 10744 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10745 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10746 | #endif |
| 10747 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10748 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10749 | #endif |
| 10750 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10751 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10752 | #endif |
| 10753 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10754 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10755 | #endif |
| 10756 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10757 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10758 | #endif |
| 10759 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10760 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10761 | #endif |
| 10762 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10763 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10764 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10765 | #ifdef _PC_ACL_ENABLED |
| 10766 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 10767 | #endif |
| 10768 | #ifdef _PC_MIN_HOLE_SIZE |
| 10769 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 10770 | #endif |
| 10771 | #ifdef _PC_ALLOC_SIZE_MIN |
| 10772 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 10773 | #endif |
| 10774 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 10775 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 10776 | #endif |
| 10777 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 10778 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 10779 | #endif |
| 10780 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 10781 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 10782 | #endif |
| 10783 | #ifdef _PC_REC_XFER_ALIGN |
| 10784 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 10785 | #endif |
| 10786 | #ifdef _PC_SYMLINK_MAX |
| 10787 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 10788 | #endif |
| 10789 | #ifdef _PC_XATTR_ENABLED |
| 10790 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 10791 | #endif |
| 10792 | #ifdef _PC_XATTR_EXISTS |
| 10793 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 10794 | #endif |
| 10795 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 10796 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 10797 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10798 | }; |
| 10799 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10800 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10801 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10802 | { |
| 10803 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 10804 | sizeof(posix_constants_pathconf) |
| 10805 | / sizeof(struct constdef)); |
| 10806 | } |
| 10807 | #endif |
| 10808 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10809 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10810 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10811 | /*[clinic input] |
| 10812 | os.fpathconf -> long |
| 10813 | |
| 10814 | fd: int |
| 10815 | name: path_confname |
| 10816 | / |
| 10817 | |
| 10818 | Return the configuration limit name for the file descriptor fd. |
| 10819 | |
| 10820 | If there is no limit, return -1. |
| 10821 | [clinic start generated code]*/ |
| 10822 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10823 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10824 | os_fpathconf_impl(PyObject *module, int fd, int name) |
| 10825 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10826 | { |
| 10827 | long limit; |
| 10828 | |
| 10829 | errno = 0; |
| 10830 | limit = fpathconf(fd, name); |
| 10831 | if (limit == -1 && errno != 0) |
| 10832 | posix_error(); |
| 10833 | |
| 10834 | return limit; |
| 10835 | } |
| 10836 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10837 | |
| 10838 | |
| 10839 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10840 | /*[clinic input] |
| 10841 | os.pathconf -> long |
| 10842 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 10843 | name: path_confname |
| 10844 | |
| 10845 | Return the configuration limit name for the file or directory path. |
| 10846 | |
| 10847 | If there is no limit, return -1. |
| 10848 | On some platforms, path may also be specified as an open file descriptor. |
| 10849 | If this functionality is unavailable, using it raises an exception. |
| 10850 | [clinic start generated code]*/ |
| 10851 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10852 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10853 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
| 10854 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10855 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10856 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10857 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10858 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10859 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10860 | if (path->fd != -1) |
| 10861 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10862 | else |
| 10863 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10864 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10865 | if (limit == -1 && errno != 0) { |
| 10866 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 10867 | /* could be a path or name problem */ |
| 10868 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10869 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10870 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10871 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10872 | |
| 10873 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10874 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10875 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10876 | |
| 10877 | #ifdef HAVE_CONFSTR |
| 10878 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10879 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10880 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10881 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10882 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10883 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10884 | #endif |
| 10885 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10886 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10887 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10888 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10889 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10890 | #endif |
| 10891 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10892 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10893 | #endif |
| 10894 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10895 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10896 | #endif |
| 10897 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10898 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10899 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10900 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10901 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10902 | #endif |
| 10903 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10904 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10905 | #endif |
| 10906 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10907 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10908 | #endif |
| 10909 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10910 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10911 | #endif |
| 10912 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10913 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10914 | #endif |
| 10915 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10916 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10917 | #endif |
| 10918 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10919 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10920 | #endif |
| 10921 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10922 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10923 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10924 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10925 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10926 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10927 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10928 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10929 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10930 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10931 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10932 | #endif |
| 10933 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10934 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10935 | #endif |
| 10936 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10937 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10938 | #endif |
| 10939 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10940 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10941 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10942 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10943 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10944 | #endif |
| 10945 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10946 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10947 | #endif |
| 10948 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10949 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10950 | #endif |
| 10951 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10952 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10953 | #endif |
| 10954 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10955 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10956 | #endif |
| 10957 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10958 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10959 | #endif |
| 10960 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10961 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10962 | #endif |
| 10963 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10964 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10965 | #endif |
| 10966 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10967 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10968 | #endif |
| 10969 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10970 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10971 | #endif |
| 10972 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10973 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10974 | #endif |
| 10975 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10976 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10977 | #endif |
| 10978 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10979 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10980 | #endif |
| 10981 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10982 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10983 | #endif |
| 10984 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10985 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10986 | #endif |
| 10987 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10988 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10989 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10990 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10991 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10992 | #endif |
| 10993 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10994 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10995 | #endif |
| 10996 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10997 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10998 | #endif |
| 10999 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11000 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11001 | #endif |
| 11002 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11003 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11004 | #endif |
| 11005 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11006 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11007 | #endif |
| 11008 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11009 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11010 | #endif |
| 11011 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11012 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11013 | #endif |
| 11014 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11015 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11016 | #endif |
| 11017 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11018 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11019 | #endif |
| 11020 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11021 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11022 | #endif |
| 11023 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11024 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11025 | #endif |
| 11026 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11027 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11028 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11029 | }; |
| 11030 | |
| 11031 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11032 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11033 | { |
| 11034 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 11035 | sizeof(posix_constants_confstr) |
| 11036 | / sizeof(struct constdef)); |
| 11037 | } |
| 11038 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11039 | |
| 11040 | /*[clinic input] |
| 11041 | os.confstr |
| 11042 | |
| 11043 | name: confstr_confname |
| 11044 | / |
| 11045 | |
| 11046 | Return a string-valued system configuration variable. |
| 11047 | [clinic start generated code]*/ |
| 11048 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11049 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11050 | os_confstr_impl(PyObject *module, int name) |
| 11051 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11052 | { |
| 11053 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11054 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11055 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11056 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11057 | errno = 0; |
| 11058 | len = confstr(name, buffer, sizeof(buffer)); |
| 11059 | if (len == 0) { |
| 11060 | if (errno) { |
| 11061 | posix_error(); |
| 11062 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11063 | } |
| 11064 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11065 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11066 | } |
| 11067 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11068 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11069 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11070 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11071 | char *buf = PyMem_Malloc(len); |
| 11072 | if (buf == NULL) |
| 11073 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11074 | len2 = confstr(name, buf, len); |
| 11075 | assert(len == len2); |
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 11076 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11077 | PyMem_Free(buf); |
| 11078 | } |
| 11079 | else |
| 11080 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11081 | return result; |
| 11082 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11083 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11084 | |
| 11085 | |
| 11086 | #ifdef HAVE_SYSCONF |
| 11087 | static struct constdef posix_constants_sysconf[] = { |
| 11088 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11089 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11090 | #endif |
| 11091 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11092 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11093 | #endif |
| 11094 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11095 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11096 | #endif |
| 11097 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11098 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11099 | #endif |
| 11100 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11101 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11102 | #endif |
| 11103 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11104 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11105 | #endif |
| 11106 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11107 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11108 | #endif |
| 11109 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11110 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11111 | #endif |
| 11112 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11113 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11114 | #endif |
| 11115 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11116 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11117 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11118 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11119 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11120 | #endif |
| 11121 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11122 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11123 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11124 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11125 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11126 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11127 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11128 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11129 | #endif |
| 11130 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11131 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11132 | #endif |
| 11133 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11134 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11135 | #endif |
| 11136 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11137 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11138 | #endif |
| 11139 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11140 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11141 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11142 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11143 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11144 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11145 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11146 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11147 | #endif |
| 11148 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11149 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11150 | #endif |
| 11151 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11152 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11153 | #endif |
| 11154 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11155 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11156 | #endif |
| 11157 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11158 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11159 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11160 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11161 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11162 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11163 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11164 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11165 | #endif |
| 11166 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11167 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11168 | #endif |
| 11169 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11170 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11171 | #endif |
| 11172 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11173 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11174 | #endif |
| 11175 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11176 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11177 | #endif |
| 11178 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11179 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11180 | #endif |
| 11181 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11182 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11183 | #endif |
| 11184 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11185 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11186 | #endif |
| 11187 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11188 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11189 | #endif |
| 11190 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11191 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11192 | #endif |
| 11193 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11194 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11195 | #endif |
| 11196 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11197 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11198 | #endif |
| 11199 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11200 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11201 | #endif |
| 11202 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11203 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11204 | #endif |
| 11205 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11206 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11207 | #endif |
| 11208 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11209 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11210 | #endif |
| 11211 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11212 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11213 | #endif |
| 11214 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11215 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11216 | #endif |
| 11217 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11218 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11219 | #endif |
| 11220 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11221 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11222 | #endif |
| 11223 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11224 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11225 | #endif |
| 11226 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11227 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11228 | #endif |
| 11229 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11230 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11231 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11232 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11233 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11234 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11235 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11236 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11237 | #endif |
| 11238 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11239 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11240 | #endif |
| 11241 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11242 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11243 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11244 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11245 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11246 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11247 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11248 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11249 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11250 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11251 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11252 | #endif |
| 11253 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11254 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11255 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11256 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11257 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11258 | #endif |
| 11259 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11260 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11261 | #endif |
| 11262 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11263 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11264 | #endif |
| 11265 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11266 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11267 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11268 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11269 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11270 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11271 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11272 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11273 | #endif |
| 11274 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11275 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11276 | #endif |
| 11277 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11278 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11279 | #endif |
| 11280 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11281 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11282 | #endif |
| 11283 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11284 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11285 | #endif |
| 11286 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11287 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11288 | #endif |
| 11289 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11290 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11291 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11292 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11293 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11294 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11295 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11296 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11297 | #endif |
| 11298 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11299 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11300 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11301 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11302 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11303 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11304 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11305 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11306 | #endif |
| 11307 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11308 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11309 | #endif |
| 11310 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11311 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11312 | #endif |
| 11313 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11314 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11315 | #endif |
| 11316 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11317 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11318 | #endif |
| 11319 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11320 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11321 | #endif |
| 11322 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11323 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11324 | #endif |
| 11325 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11326 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11327 | #endif |
| 11328 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11329 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11330 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11331 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11332 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11333 | #endif |
| 11334 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11335 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11336 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11337 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11338 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11339 | #endif |
| 11340 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11341 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11342 | #endif |
| 11343 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11344 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11345 | #endif |
| 11346 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11347 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11348 | #endif |
| 11349 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11350 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11351 | #endif |
| 11352 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11353 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11354 | #endif |
| 11355 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11356 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11357 | #endif |
| 11358 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11359 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11360 | #endif |
| 11361 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11362 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11363 | #endif |
| 11364 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11365 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11366 | #endif |
| 11367 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11368 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11369 | #endif |
| 11370 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11371 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11372 | #endif |
| 11373 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11374 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11375 | #endif |
| 11376 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11377 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11378 | #endif |
| 11379 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11380 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11381 | #endif |
| 11382 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11383 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11384 | #endif |
| 11385 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11386 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11387 | #endif |
| 11388 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11389 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11390 | #endif |
| 11391 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11392 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11393 | #endif |
| 11394 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11395 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11396 | #endif |
| 11397 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11398 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11399 | #endif |
| 11400 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11401 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11402 | #endif |
| 11403 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11404 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11405 | #endif |
| 11406 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11407 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11408 | #endif |
| 11409 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11410 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11411 | #endif |
| 11412 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11413 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11414 | #endif |
| 11415 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11416 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11417 | #endif |
| 11418 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11419 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11420 | #endif |
| 11421 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11422 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11423 | #endif |
| 11424 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11425 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11426 | #endif |
| 11427 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11428 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11429 | #endif |
| 11430 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11431 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11432 | #endif |
| 11433 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11434 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11435 | #endif |
| 11436 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11437 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11438 | #endif |
| 11439 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11440 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11441 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11442 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11443 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11444 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11445 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11446 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11447 | #endif |
| 11448 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11449 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11450 | #endif |
| 11451 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11452 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11453 | #endif |
| 11454 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11455 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11456 | #endif |
| 11457 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11458 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11459 | #endif |
| 11460 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11461 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11462 | #endif |
| 11463 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11464 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11465 | #endif |
| 11466 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11467 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11468 | #endif |
| 11469 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11470 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11471 | #endif |
| 11472 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11473 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11474 | #endif |
| 11475 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11476 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11477 | #endif |
| 11478 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11479 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11480 | #endif |
| 11481 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11482 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11483 | #endif |
| 11484 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11485 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11486 | #endif |
| 11487 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11488 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11489 | #endif |
| 11490 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11491 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11492 | #endif |
| 11493 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11494 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11495 | #endif |
| 11496 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11497 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11498 | #endif |
| 11499 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11500 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11501 | #endif |
| 11502 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11503 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11504 | #endif |
| 11505 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11506 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11507 | #endif |
| 11508 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11509 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11510 | #endif |
| 11511 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11512 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11513 | #endif |
| 11514 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11515 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11516 | #endif |
| 11517 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11518 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11519 | #endif |
| 11520 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11521 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11522 | #endif |
| 11523 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11524 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11525 | #endif |
| 11526 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11527 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11528 | #endif |
| 11529 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11530 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11531 | #endif |
| 11532 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11533 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11534 | #endif |
| 11535 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11536 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11537 | #endif |
| 11538 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11539 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11540 | #endif |
| 11541 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11542 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11543 | #endif |
| 11544 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11545 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11546 | #endif |
| 11547 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11548 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11549 | #endif |
| 11550 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11551 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11552 | #endif |
| 11553 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11554 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11555 | #endif |
| 11556 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11557 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11558 | #endif |
| 11559 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11560 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11561 | #endif |
| 11562 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11563 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11564 | #endif |
| 11565 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11566 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11567 | #endif |
| 11568 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11569 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11570 | #endif |
| 11571 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11572 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11573 | #endif |
| 11574 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11575 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11576 | #endif |
| 11577 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11578 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11579 | #endif |
| 11580 | }; |
| 11581 | |
| 11582 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11583 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11584 | { |
| 11585 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 11586 | sizeof(posix_constants_sysconf) |
| 11587 | / sizeof(struct constdef)); |
| 11588 | } |
| 11589 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11590 | |
| 11591 | /*[clinic input] |
| 11592 | os.sysconf -> long |
| 11593 | name: sysconf_confname |
| 11594 | / |
| 11595 | |
| 11596 | Return an integer-valued system configuration variable. |
| 11597 | [clinic start generated code]*/ |
| 11598 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11599 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11600 | os_sysconf_impl(PyObject *module, int name) |
| 11601 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11602 | { |
| 11603 | long value; |
| 11604 | |
| 11605 | errno = 0; |
| 11606 | value = sysconf(name); |
| 11607 | if (value == -1 && errno != 0) |
| 11608 | posix_error(); |
| 11609 | return value; |
| 11610 | } |
| 11611 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11612 | |
| 11613 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11614 | /* 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] | 11615 | * 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] | 11616 | * the exported dictionaries that are used to publish information about the |
| 11617 | * names available on the host platform. |
| 11618 | * |
| 11619 | * Sorting the table at runtime ensures that the table is properly ordered |
| 11620 | * when used, even for platforms we're not able to test on. It also makes |
| 11621 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11622 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11623 | |
| 11624 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11625 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11626 | { |
| 11627 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11628 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11629 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11630 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11631 | |
| 11632 | return strcmp(c1->name, c2->name); |
| 11633 | } |
| 11634 | |
| 11635 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11636 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11637 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11638 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11639 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11640 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11641 | |
| 11642 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 11643 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11644 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11645 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11646 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11647 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11648 | PyObject *o = PyLong_FromLong(table[i].value); |
| 11649 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 11650 | Py_XDECREF(o); |
| 11651 | Py_DECREF(d); |
| 11652 | return -1; |
| 11653 | } |
| 11654 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11655 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11656 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11657 | } |
| 11658 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11659 | /* Return -1 on failure, 0 on success. */ |
| 11660 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11661 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11662 | { |
| 11663 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11664 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11665 | sizeof(posix_constants_pathconf) |
| 11666 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11667 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11668 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11669 | #endif |
| 11670 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11671 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11672 | sizeof(posix_constants_confstr) |
| 11673 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11674 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11675 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11676 | #endif |
| 11677 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11678 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11679 | sizeof(posix_constants_sysconf) |
| 11680 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11681 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11682 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11683 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11684 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11685 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11686 | |
| 11687 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11688 | /*[clinic input] |
| 11689 | os.abort |
| 11690 | |
| 11691 | Abort the interpreter immediately. |
| 11692 | |
| 11693 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 11694 | on the hosting operating system. This function never returns. |
| 11695 | [clinic start generated code]*/ |
| 11696 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11697 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11698 | os_abort_impl(PyObject *module) |
| 11699 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11700 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11701 | abort(); |
| 11702 | /*NOTREACHED*/ |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11703 | #ifndef __clang__ |
| 11704 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). |
| 11705 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang |
| 11706 | is smarter and emits a warning on the return. */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11707 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 11708 | return NULL; |
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11709 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11710 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11711 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11712 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11713 | /* Grab ShellExecute dynamically from shell32 */ |
| 11714 | static int has_ShellExecute = -1; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11715 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 11716 | LPCWSTR, INT); |
| 11717 | static int |
| 11718 | check_ShellExecute() |
| 11719 | { |
| 11720 | HINSTANCE hShell32; |
| 11721 | |
| 11722 | /* only recheck */ |
| 11723 | if (-1 == has_ShellExecute) { |
| 11724 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11725 | /* Security note: this call is not vulnerable to "DLL hijacking". |
| 11726 | SHELL32 is part of "KnownDLLs" and so Windows always load |
| 11727 | the system SHELL32.DLL, even if there is another SHELL32.DLL |
| 11728 | in the DLL search path. */ |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11729 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11730 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11731 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 11732 | "ShellExecuteW"); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11733 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11734 | } else { |
| 11735 | has_ShellExecute = 0; |
| 11736 | } |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 11737 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11738 | } |
| 11739 | return has_ShellExecute; |
| 11740 | } |
| 11741 | |
| 11742 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11743 | /*[clinic input] |
| 11744 | os.startfile |
| 11745 | filepath: path_t |
| 11746 | operation: Py_UNICODE = NULL |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11747 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11748 | Start a file with its associated application. |
| 11749 | |
| 11750 | When "operation" is not specified or "open", this acts like |
| 11751 | double-clicking the file in Explorer, or giving the file name as an |
| 11752 | argument to the DOS "start" command: the file is opened with whatever |
| 11753 | application (if any) its extension is associated. |
| 11754 | When another "operation" is given, it specifies what should be done with |
| 11755 | the file. A typical operation is "print". |
| 11756 | |
| 11757 | startfile returns as soon as the associated application is launched. |
| 11758 | There is no option to wait for the application to close, and no way |
| 11759 | to retrieve the application's exit status. |
| 11760 | |
| 11761 | The filepath is relative to the current directory. If you want to use |
| 11762 | an absolute path, make sure the first character is not a slash ("/"); |
| 11763 | the underlying Win32 ShellExecute function doesn't work if it is. |
| 11764 | [clinic start generated code]*/ |
| 11765 | |
| 11766 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11767 | os_startfile_impl(PyObject *module, path_t *filepath, |
| 11768 | const Py_UNICODE *operation) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 11769 | /*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11770 | { |
| 11771 | HINSTANCE rc; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11772 | |
| 11773 | if(!check_ShellExecute()) { |
| 11774 | /* If the OS doesn't have ShellExecute, return a |
| 11775 | NotImplementedError. */ |
| 11776 | return PyErr_Format(PyExc_NotImplementedError, |
| 11777 | "startfile not available on this platform"); |
| 11778 | } |
| 11779 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11780 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11781 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11782 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11783 | Py_END_ALLOW_THREADS |
| 11784 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11785 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11786 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11787 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11788 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11789 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11790 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11791 | #endif /* MS_WINDOWS */ |
| 11792 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11793 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11794 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11795 | /*[clinic input] |
| 11796 | os.getloadavg |
| 11797 | |
| 11798 | Return average recent system load information. |
| 11799 | |
| 11800 | Return the number of processes in the system run queue averaged over |
| 11801 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 11802 | Raises OSError if the load average was unobtainable. |
| 11803 | [clinic start generated code]*/ |
| 11804 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11805 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11806 | os_getloadavg_impl(PyObject *module) |
| 11807 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11808 | { |
| 11809 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11810 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11811 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 11812 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11813 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11814 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11815 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11816 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11817 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11818 | |
| 11819 | /*[clinic input] |
| 11820 | os.device_encoding |
| 11821 | fd: int |
| 11822 | |
| 11823 | Return a string describing the encoding of a terminal's file descriptor. |
| 11824 | |
| 11825 | The file descriptor must be attached to a terminal. |
| 11826 | If the device is not a terminal, return None. |
| 11827 | [clinic start generated code]*/ |
| 11828 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11829 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11830 | os_device_encoding_impl(PyObject *module, int fd) |
| 11831 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11832 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11833 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 11834 | } |
| 11835 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11836 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11837 | #ifdef HAVE_SETRESUID |
| 11838 | /*[clinic input] |
| 11839 | os.setresuid |
| 11840 | |
| 11841 | ruid: uid_t |
| 11842 | euid: uid_t |
| 11843 | suid: uid_t |
| 11844 | / |
| 11845 | |
| 11846 | Set the current process's real, effective, and saved user ids. |
| 11847 | [clinic start generated code]*/ |
| 11848 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11849 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11850 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
| 11851 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11852 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11853 | if (setresuid(ruid, euid, suid) < 0) |
| 11854 | return posix_error(); |
| 11855 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11856 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11857 | #endif /* HAVE_SETRESUID */ |
| 11858 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11859 | |
| 11860 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11861 | /*[clinic input] |
| 11862 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11863 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11864 | rgid: gid_t |
| 11865 | egid: gid_t |
| 11866 | sgid: gid_t |
| 11867 | / |
| 11868 | |
| 11869 | Set the current process's real, effective, and saved group ids. |
| 11870 | [clinic start generated code]*/ |
| 11871 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11872 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11873 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 11874 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11875 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11876 | if (setresgid(rgid, egid, sgid) < 0) |
| 11877 | return posix_error(); |
| 11878 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11879 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11880 | #endif /* HAVE_SETRESGID */ |
| 11881 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11882 | |
| 11883 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11884 | /*[clinic input] |
| 11885 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11886 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11887 | Return a tuple of the current process's real, effective, and saved user ids. |
| 11888 | [clinic start generated code]*/ |
| 11889 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11890 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11891 | os_getresuid_impl(PyObject *module) |
| 11892 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11893 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11894 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11895 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 11896 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11897 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 11898 | _PyLong_FromUid(euid), |
| 11899 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11900 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11901 | #endif /* HAVE_GETRESUID */ |
| 11902 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11903 | |
| 11904 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11905 | /*[clinic input] |
| 11906 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11907 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11908 | Return a tuple of the current process's real, effective, and saved group ids. |
| 11909 | [clinic start generated code]*/ |
| 11910 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11911 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11912 | os_getresgid_impl(PyObject *module) |
| 11913 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11914 | { |
| 11915 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11916 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 11917 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11918 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 11919 | _PyLong_FromGid(egid), |
| 11920 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11921 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11922 | #endif /* HAVE_GETRESGID */ |
| 11923 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11924 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11925 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11926 | /*[clinic input] |
| 11927 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11928 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11929 | path: path_t(allow_fd=True) |
| 11930 | attribute: path_t |
| 11931 | * |
| 11932 | follow_symlinks: bool = True |
| 11933 | |
| 11934 | Return the value of extended attribute attribute on path. |
| 11935 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11936 | 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] | 11937 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 11938 | link, getxattr will examine the symbolic link itself instead of the file |
| 11939 | the link points to. |
| 11940 | |
| 11941 | [clinic start generated code]*/ |
| 11942 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11943 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11944 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11945 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11946 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11947 | { |
| 11948 | Py_ssize_t i; |
| 11949 | PyObject *buffer = NULL; |
| 11950 | |
| 11951 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 11952 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11953 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11954 | for (i = 0; ; i++) { |
| 11955 | void *ptr; |
| 11956 | ssize_t result; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11957 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11958 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 11959 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11960 | path_error(path); |
| 11961 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11962 | } |
| 11963 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 11964 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11965 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11966 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11967 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11968 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11969 | if (path->fd >= 0) |
| 11970 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11971 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11972 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11973 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11974 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11975 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11976 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11977 | if (result < 0) { |
| 11978 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11979 | if (errno == ERANGE) |
| 11980 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11981 | path_error(path); |
| 11982 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11983 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11984 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11985 | if (result != buffer_size) { |
| 11986 | /* Can only shrink. */ |
| 11987 | _PyBytes_Resize(&buffer, result); |
| 11988 | } |
| 11989 | break; |
| 11990 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11991 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11992 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11993 | } |
| 11994 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11995 | |
| 11996 | /*[clinic input] |
| 11997 | os.setxattr |
| 11998 | |
| 11999 | path: path_t(allow_fd=True) |
| 12000 | attribute: path_t |
| 12001 | value: Py_buffer |
| 12002 | flags: int = 0 |
| 12003 | * |
| 12004 | follow_symlinks: bool = True |
| 12005 | |
| 12006 | Set extended attribute attribute on path to value. |
| 12007 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12008 | 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] | 12009 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12010 | link, setxattr will modify the symbolic link itself instead of the file |
| 12011 | the link points to. |
| 12012 | |
| 12013 | [clinic start generated code]*/ |
| 12014 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12015 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12016 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12017 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12018 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12019 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12020 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12021 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12022 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12023 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12024 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12025 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12026 | if (path->fd > -1) |
| 12027 | result = fsetxattr(path->fd, attribute->narrow, |
| 12028 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12029 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12030 | result = setxattr(path->narrow, attribute->narrow, |
| 12031 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12032 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12033 | result = lsetxattr(path->narrow, attribute->narrow, |
| 12034 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12035 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12036 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12037 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12038 | path_error(path); |
| 12039 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12040 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12041 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12042 | Py_RETURN_NONE; |
| 12043 | } |
| 12044 | |
| 12045 | |
| 12046 | /*[clinic input] |
| 12047 | os.removexattr |
| 12048 | |
| 12049 | path: path_t(allow_fd=True) |
| 12050 | attribute: path_t |
| 12051 | * |
| 12052 | follow_symlinks: bool = True |
| 12053 | |
| 12054 | Remove extended attribute attribute on path. |
| 12055 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12056 | 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] | 12057 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12058 | link, removexattr will modify the symbolic link itself instead of the file |
| 12059 | the link points to. |
| 12060 | |
| 12061 | [clinic start generated code]*/ |
| 12062 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12063 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12064 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12065 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12066 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12067 | { |
| 12068 | ssize_t result; |
| 12069 | |
| 12070 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 12071 | return NULL; |
| 12072 | |
| 12073 | Py_BEGIN_ALLOW_THREADS; |
| 12074 | if (path->fd > -1) |
| 12075 | result = fremovexattr(path->fd, attribute->narrow); |
| 12076 | else if (follow_symlinks) |
| 12077 | result = removexattr(path->narrow, attribute->narrow); |
| 12078 | else |
| 12079 | result = lremovexattr(path->narrow, attribute->narrow); |
| 12080 | Py_END_ALLOW_THREADS; |
| 12081 | |
| 12082 | if (result) { |
| 12083 | return path_error(path); |
| 12084 | } |
| 12085 | |
| 12086 | Py_RETURN_NONE; |
| 12087 | } |
| 12088 | |
| 12089 | |
| 12090 | /*[clinic input] |
| 12091 | os.listxattr |
| 12092 | |
| 12093 | path: path_t(allow_fd=True, nullable=True) = None |
| 12094 | * |
| 12095 | follow_symlinks: bool = True |
| 12096 | |
| 12097 | Return a list of extended attributes on path. |
| 12098 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12099 | 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] | 12100 | if path is None, listxattr will examine the current directory. |
| 12101 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 12102 | link, listxattr will examine the symbolic link itself instead of the file |
| 12103 | the link points to. |
| 12104 | [clinic start generated code]*/ |
| 12105 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12106 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12107 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12108 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12109 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12110 | Py_ssize_t i; |
| 12111 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12112 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12113 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12114 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12115 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12116 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12117 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12118 | name = path->narrow ? path->narrow : "."; |
| 12119 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12120 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12121 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12122 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12123 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12124 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 12125 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 12126 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12127 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12128 | break; |
| 12129 | } |
| 12130 | buffer = PyMem_MALLOC(buffer_size); |
| 12131 | if (!buffer) { |
| 12132 | PyErr_NoMemory(); |
| 12133 | break; |
| 12134 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12135 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12136 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12137 | if (path->fd > -1) |
| 12138 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12139 | else if (follow_symlinks) |
| 12140 | length = listxattr(name, buffer, buffer_size); |
| 12141 | else |
| 12142 | length = llistxattr(name, buffer, buffer_size); |
| 12143 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12144 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12145 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12146 | if (errno == ERANGE) { |
| 12147 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 12148 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12149 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12150 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12151 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12152 | break; |
| 12153 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12154 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12155 | result = PyList_New(0); |
| 12156 | if (!result) { |
| 12157 | goto exit; |
| 12158 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12159 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12160 | end = buffer + length; |
| 12161 | for (trace = start = buffer; trace != end; trace++) { |
| 12162 | if (!*trace) { |
| 12163 | int error; |
| 12164 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 12165 | trace - start); |
| 12166 | if (!attribute) { |
| 12167 | Py_DECREF(result); |
| 12168 | result = NULL; |
| 12169 | goto exit; |
| 12170 | } |
| 12171 | error = PyList_Append(result, attribute); |
| 12172 | Py_DECREF(attribute); |
| 12173 | if (error) { |
| 12174 | Py_DECREF(result); |
| 12175 | result = NULL; |
| 12176 | goto exit; |
| 12177 | } |
| 12178 | start = trace + 1; |
| 12179 | } |
| 12180 | } |
| 12181 | break; |
| 12182 | } |
| 12183 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12184 | if (buffer) |
| 12185 | PyMem_FREE(buffer); |
| 12186 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12187 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12188 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12189 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12190 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12191 | /*[clinic input] |
| 12192 | os.urandom |
| 12193 | |
| 12194 | size: Py_ssize_t |
| 12195 | / |
| 12196 | |
| 12197 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 12198 | [clinic start generated code]*/ |
| 12199 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12200 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12201 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
| 12202 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12203 | { |
| 12204 | PyObject *bytes; |
| 12205 | int result; |
| 12206 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12207 | if (size < 0) |
| 12208 | return PyErr_Format(PyExc_ValueError, |
| 12209 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12210 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 12211 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12212 | return NULL; |
| 12213 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 12214 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12215 | if (result == -1) { |
| 12216 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12217 | return NULL; |
| 12218 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12219 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12220 | } |
| 12221 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 12222 | #ifdef HAVE_MEMFD_CREATE |
| 12223 | /*[clinic input] |
| 12224 | os.memfd_create |
| 12225 | |
| 12226 | name: FSConverter |
| 12227 | flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC |
| 12228 | |
| 12229 | [clinic start generated code]*/ |
| 12230 | |
| 12231 | static PyObject * |
| 12232 | os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags) |
| 12233 | /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/ |
| 12234 | { |
| 12235 | int fd; |
| 12236 | const char *bytes = PyBytes_AS_STRING(name); |
| 12237 | Py_BEGIN_ALLOW_THREADS |
| 12238 | fd = memfd_create(bytes, flags); |
| 12239 | Py_END_ALLOW_THREADS |
| 12240 | if (fd == -1) { |
| 12241 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12242 | } |
| 12243 | return PyLong_FromLong(fd); |
| 12244 | } |
| 12245 | #endif |
| 12246 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12247 | /* Terminal size querying */ |
| 12248 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12249 | PyDoc_STRVAR(TerminalSize_docstring, |
| 12250 | "A tuple of (columns, lines) for holding terminal window size"); |
| 12251 | |
| 12252 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 12253 | {"columns", "width of the terminal window in characters"}, |
| 12254 | {"lines", "height of the terminal window in characters"}, |
| 12255 | {NULL, NULL} |
| 12256 | }; |
| 12257 | |
| 12258 | static PyStructSequence_Desc TerminalSize_desc = { |
| 12259 | "os.terminal_size", |
| 12260 | TerminalSize_docstring, |
| 12261 | TerminalSize_fields, |
| 12262 | 2, |
| 12263 | }; |
| 12264 | |
| 12265 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12266 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12267 | PyDoc_STRVAR(termsize__doc__, |
| 12268 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 12269 | "\n" \ |
| 12270 | "The optional argument fd (default standard output) specifies\n" \ |
| 12271 | "which file descriptor should be queried.\n" \ |
| 12272 | "\n" \ |
| 12273 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 12274 | "is thrown.\n" \ |
| 12275 | "\n" \ |
| 12276 | "This function will only be defined if an implementation is\n" \ |
| 12277 | "available for this system.\n" \ |
| 12278 | "\n" \ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 12279 | "shutil.get_terminal_size is the high-level function which should\n" \ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12280 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 12281 | |
| 12282 | static PyObject* |
| 12283 | get_terminal_size(PyObject *self, PyObject *args) |
| 12284 | { |
| 12285 | int columns, lines; |
| 12286 | PyObject *termsize; |
| 12287 | |
| 12288 | int fd = fileno(stdout); |
| 12289 | /* Under some conditions stdout may not be connected and |
| 12290 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 12291 | * GUI apps don't have valid standard streams by default. |
| 12292 | * |
| 12293 | * If this happens, and the optional fd argument is not present, |
| 12294 | * the ioctl below will fail returning EBADF. This is what we want. |
| 12295 | */ |
| 12296 | |
| 12297 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 12298 | return NULL; |
| 12299 | |
| 12300 | #ifdef TERMSIZE_USE_IOCTL |
| 12301 | { |
| 12302 | struct winsize w; |
| 12303 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 12304 | return PyErr_SetFromErrno(PyExc_OSError); |
| 12305 | columns = w.ws_col; |
| 12306 | lines = w.ws_row; |
| 12307 | } |
| 12308 | #endif /* TERMSIZE_USE_IOCTL */ |
| 12309 | |
| 12310 | #ifdef TERMSIZE_USE_CONIO |
| 12311 | { |
| 12312 | DWORD nhandle; |
| 12313 | HANDLE handle; |
| 12314 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 12315 | switch (fd) { |
| 12316 | case 0: nhandle = STD_INPUT_HANDLE; |
| 12317 | break; |
| 12318 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 12319 | break; |
| 12320 | case 2: nhandle = STD_ERROR_HANDLE; |
| 12321 | break; |
| 12322 | default: |
| 12323 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 12324 | } |
| 12325 | handle = GetStdHandle(nhandle); |
| 12326 | if (handle == NULL) |
| 12327 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 12328 | if (handle == INVALID_HANDLE_VALUE) |
| 12329 | return PyErr_SetFromWindowsErr(0); |
| 12330 | |
| 12331 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 12332 | return PyErr_SetFromWindowsErr(0); |
| 12333 | |
| 12334 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 12335 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 12336 | } |
| 12337 | #endif /* TERMSIZE_USE_CONIO */ |
| 12338 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12339 | PyObject *TerminalSizeType = _posixstate(self)->TerminalSizeType; |
| 12340 | termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12341 | if (termsize == NULL) |
| 12342 | return NULL; |
| 12343 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 12344 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 12345 | if (PyErr_Occurred()) { |
| 12346 | Py_DECREF(termsize); |
| 12347 | return NULL; |
| 12348 | } |
| 12349 | return termsize; |
| 12350 | } |
| 12351 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 12352 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12353 | |
| 12354 | /*[clinic input] |
| 12355 | os.cpu_count |
| 12356 | |
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 12357 | Return the number of CPUs in the system; return None if indeterminable. |
| 12358 | |
| 12359 | This number is not equivalent to the number of CPUs the current process can |
| 12360 | use. The number of usable CPUs can be obtained with |
| 12361 | ``len(os.sched_getaffinity(0))`` |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12362 | [clinic start generated code]*/ |
| 12363 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12364 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12365 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12366 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12367 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12368 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12369 | #ifdef MS_WINDOWS |
Steve Dower | aa92927 | 2019-09-11 16:15:39 +0100 | [diff] [blame] | 12370 | ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12371 | #elif defined(__hpux) |
| 12372 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 12373 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 12374 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12375 | #elif defined(__DragonFly__) || \ |
| 12376 | defined(__OpenBSD__) || \ |
| 12377 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12378 | defined(__NetBSD__) || \ |
| 12379 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12380 | int mib[2]; |
| 12381 | size_t len = sizeof(ncpu); |
| 12382 | mib[0] = CTL_HW; |
| 12383 | mib[1] = HW_NCPU; |
| 12384 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 12385 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12386 | #endif |
| 12387 | if (ncpu >= 1) |
| 12388 | return PyLong_FromLong(ncpu); |
| 12389 | else |
| 12390 | Py_RETURN_NONE; |
| 12391 | } |
| 12392 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12393 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12394 | /*[clinic input] |
| 12395 | os.get_inheritable -> bool |
| 12396 | |
| 12397 | fd: int |
| 12398 | / |
| 12399 | |
| 12400 | Get the close-on-exe flag of the specified file descriptor. |
| 12401 | [clinic start generated code]*/ |
| 12402 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12403 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12404 | os_get_inheritable_impl(PyObject *module, int fd) |
| 12405 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12406 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12407 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12408 | _Py_BEGIN_SUPPRESS_IPH |
| 12409 | return_value = _Py_get_inheritable(fd); |
| 12410 | _Py_END_SUPPRESS_IPH |
| 12411 | return return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12412 | } |
| 12413 | |
| 12414 | |
| 12415 | /*[clinic input] |
| 12416 | os.set_inheritable |
| 12417 | fd: int |
| 12418 | inheritable: int |
| 12419 | / |
| 12420 | |
| 12421 | Set the inheritable flag of the specified file descriptor. |
| 12422 | [clinic start generated code]*/ |
| 12423 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12424 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12425 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
| 12426 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12427 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12428 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12429 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12430 | _Py_BEGIN_SUPPRESS_IPH |
| 12431 | result = _Py_set_inheritable(fd, inheritable, NULL); |
| 12432 | _Py_END_SUPPRESS_IPH |
| 12433 | if (result < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12434 | return NULL; |
| 12435 | Py_RETURN_NONE; |
| 12436 | } |
| 12437 | |
| 12438 | |
| 12439 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12440 | /*[clinic input] |
| 12441 | os.get_handle_inheritable -> bool |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12442 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12443 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12444 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12445 | Get the close-on-exe flag of the specified file descriptor. |
| 12446 | [clinic start generated code]*/ |
| 12447 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12448 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12449 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12450 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12451 | { |
| 12452 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12453 | |
| 12454 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 12455 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12456 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12457 | } |
| 12458 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12459 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12460 | } |
| 12461 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12462 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12463 | /*[clinic input] |
| 12464 | os.set_handle_inheritable |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12465 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12466 | inheritable: bool |
| 12467 | / |
| 12468 | |
| 12469 | Set the inheritable flag of the specified handle. |
| 12470 | [clinic start generated code]*/ |
| 12471 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12472 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12473 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12474 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12475 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12476 | { |
| 12477 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12478 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 12479 | PyErr_SetFromWindowsErr(0); |
| 12480 | return NULL; |
| 12481 | } |
| 12482 | Py_RETURN_NONE; |
| 12483 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12484 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12485 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12486 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12487 | /*[clinic input] |
| 12488 | os.get_blocking -> bool |
| 12489 | fd: int |
| 12490 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12491 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12492 | Get the blocking mode of the file descriptor. |
| 12493 | |
| 12494 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. |
| 12495 | [clinic start generated code]*/ |
| 12496 | |
| 12497 | static int |
| 12498 | os_get_blocking_impl(PyObject *module, int fd) |
| 12499 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12500 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12501 | int blocking; |
| 12502 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12503 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12504 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12505 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12506 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12507 | } |
| 12508 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12509 | /*[clinic input] |
| 12510 | os.set_blocking |
| 12511 | fd: int |
| 12512 | blocking: bool(accept={int}) |
| 12513 | / |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12514 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12515 | Set the blocking mode of the specified file descriptor. |
| 12516 | |
| 12517 | Set the O_NONBLOCK flag if blocking is False, |
| 12518 | clear the O_NONBLOCK flag otherwise. |
| 12519 | [clinic start generated code]*/ |
| 12520 | |
| 12521 | static PyObject * |
| 12522 | os_set_blocking_impl(PyObject *module, int fd, int blocking) |
| 12523 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12524 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12525 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12526 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12527 | _Py_BEGIN_SUPPRESS_IPH |
| 12528 | result = _Py_set_blocking(fd, blocking); |
| 12529 | _Py_END_SUPPRESS_IPH |
| 12530 | if (result < 0) |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12531 | return NULL; |
| 12532 | Py_RETURN_NONE; |
| 12533 | } |
| 12534 | #endif /* !MS_WINDOWS */ |
| 12535 | |
| 12536 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12537 | /*[clinic input] |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12538 | class os.DirEntry "DirEntry *" "DirEntryType" |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12539 | [clinic start generated code]*/ |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12540 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12541 | |
| 12542 | typedef struct { |
| 12543 | PyObject_HEAD |
| 12544 | PyObject *name; |
| 12545 | PyObject *path; |
| 12546 | PyObject *stat; |
| 12547 | PyObject *lstat; |
| 12548 | #ifdef MS_WINDOWS |
| 12549 | struct _Py_stat_struct win32_lstat; |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12550 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12551 | int got_file_index; |
| 12552 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12553 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12554 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12555 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12556 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12557 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12558 | #endif |
| 12559 | } DirEntry; |
| 12560 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12561 | static PyObject * |
| 12562 | _disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 12563 | { |
| 12564 | PyErr_Format(PyExc_TypeError, |
| 12565 | "cannot create '%.100s' instances", _PyType_Name(type)); |
| 12566 | return NULL; |
| 12567 | } |
| 12568 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12569 | static void |
| 12570 | DirEntry_dealloc(DirEntry *entry) |
| 12571 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12572 | PyTypeObject *tp = Py_TYPE(entry); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12573 | Py_XDECREF(entry->name); |
| 12574 | Py_XDECREF(entry->path); |
| 12575 | Py_XDECREF(entry->stat); |
| 12576 | Py_XDECREF(entry->lstat); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12577 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 12578 | free_func(entry); |
| 12579 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12580 | } |
| 12581 | |
| 12582 | /* Forward reference */ |
| 12583 | static int |
| 12584 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 12585 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12586 | /*[clinic input] |
| 12587 | os.DirEntry.is_symlink -> bool |
| 12588 | |
| 12589 | Return True if the entry is a symbolic link; cached per entry. |
| 12590 | [clinic start generated code]*/ |
| 12591 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12592 | static int |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12593 | os_DirEntry_is_symlink_impl(DirEntry *self) |
| 12594 | /*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12595 | { |
| 12596 | #ifdef MS_WINDOWS |
| 12597 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12598 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 12599 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12600 | if (self->d_type != DT_UNKNOWN) |
| 12601 | return self->d_type == DT_LNK; |
| 12602 | else |
| 12603 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12604 | #else |
| 12605 | /* POSIX without d_type */ |
| 12606 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12607 | #endif |
| 12608 | } |
| 12609 | |
| 12610 | static PyObject * |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12611 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 12612 | { |
| 12613 | int result; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12614 | STRUCT_STAT st; |
| 12615 | PyObject *ub; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12616 | |
| 12617 | #ifdef MS_WINDOWS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12618 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
| 12619 | return NULL; |
| 12620 | const wchar_t *path = PyUnicode_AsUnicode(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12621 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12622 | if (!PyUnicode_FSConverter(self->path, &ub)) |
| 12623 | return NULL; |
| 12624 | const char *path = PyBytes_AS_STRING(ub); |
| 12625 | if (self->dir_fd != DEFAULT_DIR_FD) { |
| 12626 | #ifdef HAVE_FSTATAT |
| 12627 | result = fstatat(self->dir_fd, path, &st, |
| 12628 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 12629 | #else |
| 12630 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); |
| 12631 | return NULL; |
| 12632 | #endif /* HAVE_FSTATAT */ |
| 12633 | } |
| 12634 | else |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12635 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12636 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12637 | if (follow_symlinks) |
| 12638 | result = STAT(path, &st); |
| 12639 | else |
| 12640 | result = LSTAT(path, &st); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12641 | } |
| 12642 | Py_DECREF(ub); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12643 | |
| 12644 | if (result != 0) |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12645 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12646 | |
| 12647 | return _pystat_fromstructstat(&st); |
| 12648 | } |
| 12649 | |
| 12650 | static PyObject * |
| 12651 | DirEntry_get_lstat(DirEntry *self) |
| 12652 | { |
| 12653 | if (!self->lstat) { |
| 12654 | #ifdef MS_WINDOWS |
| 12655 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 12656 | #else /* POSIX */ |
| 12657 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 12658 | #endif |
| 12659 | } |
| 12660 | Py_XINCREF(self->lstat); |
| 12661 | return self->lstat; |
| 12662 | } |
| 12663 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12664 | /*[clinic input] |
| 12665 | os.DirEntry.stat |
| 12666 | * |
| 12667 | follow_symlinks: bool = True |
| 12668 | |
| 12669 | Return stat_result object for the entry; cached per entry. |
| 12670 | [clinic start generated code]*/ |
| 12671 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12672 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12673 | os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks) |
| 12674 | /*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12675 | { |
| 12676 | if (!follow_symlinks) |
| 12677 | return DirEntry_get_lstat(self); |
| 12678 | |
| 12679 | if (!self->stat) { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12680 | int result = os_DirEntry_is_symlink_impl(self); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12681 | if (result == -1) |
| 12682 | return NULL; |
| 12683 | else if (result) |
| 12684 | self->stat = DirEntry_fetch_stat(self, 1); |
| 12685 | else |
| 12686 | self->stat = DirEntry_get_lstat(self); |
| 12687 | } |
| 12688 | |
| 12689 | Py_XINCREF(self->stat); |
| 12690 | return self->stat; |
| 12691 | } |
| 12692 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12693 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 12694 | static int |
| 12695 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 12696 | { |
| 12697 | PyObject *stat = NULL; |
| 12698 | PyObject *st_mode = NULL; |
| 12699 | long mode; |
| 12700 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12701 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12702 | int is_symlink; |
| 12703 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12704 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12705 | #ifdef MS_WINDOWS |
| 12706 | unsigned long dir_bits; |
| 12707 | #endif |
| 12708 | |
| 12709 | #ifdef MS_WINDOWS |
| 12710 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 12711 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12712 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12713 | is_symlink = self->d_type == DT_LNK; |
| 12714 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 12715 | #endif |
| 12716 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12717 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12718 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12719 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12720 | stat = os_DirEntry_stat_impl(self, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12721 | if (!stat) { |
| 12722 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 12723 | /* If file doesn't exist (anymore), then return False |
| 12724 | (i.e., say it's not a file/directory) */ |
| 12725 | PyErr_Clear(); |
| 12726 | return 0; |
| 12727 | } |
| 12728 | goto error; |
| 12729 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12730 | st_mode = PyObject_GetAttr(stat, _posixstate_global->st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12731 | if (!st_mode) |
| 12732 | goto error; |
| 12733 | |
| 12734 | mode = PyLong_AsLong(st_mode); |
| 12735 | if (mode == -1 && PyErr_Occurred()) |
| 12736 | goto error; |
| 12737 | Py_CLEAR(st_mode); |
| 12738 | Py_CLEAR(stat); |
| 12739 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12740 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12741 | } |
| 12742 | else if (is_symlink) { |
| 12743 | assert(mode_bits != S_IFLNK); |
| 12744 | result = 0; |
| 12745 | } |
| 12746 | else { |
| 12747 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 12748 | #ifdef MS_WINDOWS |
| 12749 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 12750 | if (mode_bits == S_IFDIR) |
| 12751 | result = dir_bits != 0; |
| 12752 | else |
| 12753 | result = dir_bits == 0; |
| 12754 | #else /* POSIX */ |
| 12755 | if (mode_bits == S_IFDIR) |
| 12756 | result = self->d_type == DT_DIR; |
| 12757 | else |
| 12758 | result = self->d_type == DT_REG; |
| 12759 | #endif |
| 12760 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12761 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12762 | |
| 12763 | return result; |
| 12764 | |
| 12765 | error: |
| 12766 | Py_XDECREF(st_mode); |
| 12767 | Py_XDECREF(stat); |
| 12768 | return -1; |
| 12769 | } |
| 12770 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12771 | /*[clinic input] |
| 12772 | os.DirEntry.is_dir -> bool |
| 12773 | * |
| 12774 | follow_symlinks: bool = True |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12775 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12776 | Return True if the entry is a directory; cached per entry. |
| 12777 | [clinic start generated code]*/ |
| 12778 | |
| 12779 | static int |
| 12780 | os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks) |
| 12781 | /*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/ |
| 12782 | { |
| 12783 | return DirEntry_test_mode(self, follow_symlinks, S_IFDIR); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12784 | } |
| 12785 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12786 | /*[clinic input] |
| 12787 | os.DirEntry.is_file -> bool |
| 12788 | * |
| 12789 | follow_symlinks: bool = True |
| 12790 | |
| 12791 | Return True if the entry is a file; cached per entry. |
| 12792 | [clinic start generated code]*/ |
| 12793 | |
| 12794 | static int |
| 12795 | os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks) |
| 12796 | /*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12797 | { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12798 | return DirEntry_test_mode(self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12799 | } |
| 12800 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12801 | /*[clinic input] |
| 12802 | os.DirEntry.inode |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12803 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12804 | Return inode of the entry; cached per entry. |
| 12805 | [clinic start generated code]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12806 | |
| 12807 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12808 | os_DirEntry_inode_impl(DirEntry *self) |
| 12809 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12810 | { |
| 12811 | #ifdef MS_WINDOWS |
| 12812 | if (!self->got_file_index) { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12813 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12814 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12815 | STRUCT_STAT stat; |
| 12816 | int result; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12817 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12818 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12819 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12820 | path = PyUnicode_AsUnicode(unicode); |
| 12821 | result = LSTAT(path, &stat); |
| 12822 | Py_DECREF(unicode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12823 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12824 | if (result != 0) |
| 12825 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12826 | |
| 12827 | self->win32_file_index = stat.st_ino; |
| 12828 | self->got_file_index = 1; |
| 12829 | } |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12830 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
| 12831 | return PyLong_FromUnsignedLongLong(self->win32_file_index); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12832 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 12833 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
| 12834 | return PyLong_FromUnsignedLongLong(self->d_ino); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12835 | #endif |
| 12836 | } |
| 12837 | |
| 12838 | static PyObject * |
| 12839 | DirEntry_repr(DirEntry *self) |
| 12840 | { |
| 12841 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 12842 | } |
| 12843 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12844 | /*[clinic input] |
| 12845 | os.DirEntry.__fspath__ |
| 12846 | |
| 12847 | Returns the path for the entry. |
| 12848 | [clinic start generated code]*/ |
| 12849 | |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12850 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12851 | os_DirEntry___fspath___impl(DirEntry *self) |
| 12852 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ |
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12853 | { |
| 12854 | Py_INCREF(self->path); |
| 12855 | return self->path; |
| 12856 | } |
| 12857 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12858 | static PyMemberDef DirEntry_members[] = { |
| 12859 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 12860 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 12861 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 12862 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 12863 | {NULL} |
| 12864 | }; |
| 12865 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12866 | #include "clinic/posixmodule.c.h" |
| 12867 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12868 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12869 | OS_DIRENTRY_IS_DIR_METHODDEF |
| 12870 | OS_DIRENTRY_IS_FILE_METHODDEF |
| 12871 | OS_DIRENTRY_IS_SYMLINK_METHODDEF |
| 12872 | OS_DIRENTRY_STAT_METHODDEF |
| 12873 | OS_DIRENTRY_INODE_METHODDEF |
| 12874 | OS_DIRENTRY___FSPATH___METHODDEF |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12875 | {NULL} |
| 12876 | }; |
| 12877 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12878 | static PyType_Slot DirEntryType_slots[] = { |
| 12879 | {Py_tp_new, _disabled_new}, |
| 12880 | {Py_tp_dealloc, DirEntry_dealloc}, |
| 12881 | {Py_tp_repr, DirEntry_repr}, |
| 12882 | {Py_tp_methods, DirEntry_methods}, |
| 12883 | {Py_tp_members, DirEntry_members}, |
| 12884 | {0, 0}, |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12885 | }; |
| 12886 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12887 | static PyType_Spec DirEntryType_spec = { |
| 12888 | MODNAME ".DirEntry", |
| 12889 | sizeof(DirEntry), |
| 12890 | 0, |
| 12891 | Py_TPFLAGS_DEFAULT, |
| 12892 | DirEntryType_slots |
| 12893 | }; |
| 12894 | |
| 12895 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12896 | #ifdef MS_WINDOWS |
| 12897 | |
| 12898 | static wchar_t * |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12899 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12900 | { |
| 12901 | Py_ssize_t path_len; |
| 12902 | Py_ssize_t size; |
| 12903 | wchar_t *result; |
| 12904 | wchar_t ch; |
| 12905 | |
| 12906 | if (!path_wide) { /* Default arg: "." */ |
| 12907 | path_wide = L"."; |
| 12908 | path_len = 1; |
| 12909 | } |
| 12910 | else { |
| 12911 | path_len = wcslen(path_wide); |
| 12912 | } |
| 12913 | |
| 12914 | /* The +1's are for the path separator and the NUL */ |
| 12915 | size = path_len + 1 + wcslen(filename) + 1; |
| 12916 | result = PyMem_New(wchar_t, size); |
| 12917 | if (!result) { |
| 12918 | PyErr_NoMemory(); |
| 12919 | return NULL; |
| 12920 | } |
| 12921 | wcscpy(result, path_wide); |
| 12922 | if (path_len > 0) { |
| 12923 | ch = result[path_len - 1]; |
| 12924 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 12925 | result[path_len++] = SEP; |
| 12926 | wcscpy(result + path_len, filename); |
| 12927 | } |
| 12928 | return result; |
| 12929 | } |
| 12930 | |
| 12931 | static PyObject * |
| 12932 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 12933 | { |
| 12934 | DirEntry *entry; |
| 12935 | BY_HANDLE_FILE_INFORMATION file_info; |
| 12936 | ULONG reparse_tag; |
| 12937 | wchar_t *joined_path; |
| 12938 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 12939 | PyObject *DirEntryType = _posixstate_global->DirEntryType; |
| 12940 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12941 | if (!entry) |
| 12942 | return NULL; |
| 12943 | entry->name = NULL; |
| 12944 | entry->path = NULL; |
| 12945 | entry->stat = NULL; |
| 12946 | entry->lstat = NULL; |
| 12947 | entry->got_file_index = 0; |
| 12948 | |
| 12949 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 12950 | if (!entry->name) |
| 12951 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12952 | if (path->narrow) { |
| 12953 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); |
| 12954 | if (!entry->name) |
| 12955 | goto error; |
| 12956 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12957 | |
| 12958 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 12959 | if (!joined_path) |
| 12960 | goto error; |
| 12961 | |
| 12962 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 12963 | PyMem_Free(joined_path); |
| 12964 | if (!entry->path) |
| 12965 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12966 | if (path->narrow) { |
| 12967 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); |
| 12968 | if (!entry->path) |
| 12969 | goto error; |
| 12970 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12971 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12972 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12973 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 12974 | |
| 12975 | return (PyObject *)entry; |
| 12976 | |
| 12977 | error: |
| 12978 | Py_DECREF(entry); |
| 12979 | return NULL; |
| 12980 | } |
| 12981 | |
| 12982 | #else /* POSIX */ |
| 12983 | |
| 12984 | static char * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12985 | 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] | 12986 | { |
| 12987 | Py_ssize_t path_len; |
| 12988 | Py_ssize_t size; |
| 12989 | char *result; |
| 12990 | |
| 12991 | if (!path_narrow) { /* Default arg: "." */ |
| 12992 | path_narrow = "."; |
| 12993 | path_len = 1; |
| 12994 | } |
| 12995 | else { |
| 12996 | path_len = strlen(path_narrow); |
| 12997 | } |
| 12998 | |
| 12999 | if (filename_len == -1) |
| 13000 | filename_len = strlen(filename); |
| 13001 | |
| 13002 | /* The +1's are for the path separator and the NUL */ |
| 13003 | size = path_len + 1 + filename_len + 1; |
| 13004 | result = PyMem_New(char, size); |
| 13005 | if (!result) { |
| 13006 | PyErr_NoMemory(); |
| 13007 | return NULL; |
| 13008 | } |
| 13009 | strcpy(result, path_narrow); |
| 13010 | if (path_len > 0 && result[path_len - 1] != '/') |
| 13011 | result[path_len++] = '/'; |
| 13012 | strcpy(result + path_len, filename); |
| 13013 | return result; |
| 13014 | } |
| 13015 | |
| 13016 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 13017 | 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] | 13018 | ino_t d_ino |
| 13019 | #ifdef HAVE_DIRENT_D_TYPE |
| 13020 | , unsigned char d_type |
| 13021 | #endif |
| 13022 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13023 | { |
| 13024 | DirEntry *entry; |
| 13025 | char *joined_path; |
| 13026 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13027 | PyObject *DirEntryType = _posixstate_global->DirEntryType; |
| 13028 | entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13029 | if (!entry) |
| 13030 | return NULL; |
| 13031 | entry->name = NULL; |
| 13032 | entry->path = NULL; |
| 13033 | entry->stat = NULL; |
| 13034 | entry->lstat = NULL; |
| 13035 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13036 | if (path->fd != -1) { |
| 13037 | entry->dir_fd = path->fd; |
| 13038 | joined_path = NULL; |
| 13039 | } |
| 13040 | else { |
| 13041 | entry->dir_fd = DEFAULT_DIR_FD; |
| 13042 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 13043 | if (!joined_path) |
| 13044 | goto error; |
| 13045 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13046 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 13047 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13048 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13049 | if (joined_path) |
| 13050 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13051 | } |
| 13052 | else { |
| 13053 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13054 | if (joined_path) |
| 13055 | entry->path = PyBytes_FromString(joined_path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13056 | } |
| 13057 | PyMem_Free(joined_path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13058 | if (!entry->name) |
| 13059 | goto error; |
| 13060 | |
| 13061 | if (path->fd != -1) { |
| 13062 | entry->path = entry->name; |
| 13063 | Py_INCREF(entry->path); |
| 13064 | } |
| 13065 | else if (!entry->path) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13066 | goto error; |
| 13067 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13068 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13069 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13070 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13071 | entry->d_ino = d_ino; |
| 13072 | |
| 13073 | return (PyObject *)entry; |
| 13074 | |
| 13075 | error: |
| 13076 | Py_XDECREF(entry); |
| 13077 | return NULL; |
| 13078 | } |
| 13079 | |
| 13080 | #endif |
| 13081 | |
| 13082 | |
| 13083 | typedef struct { |
| 13084 | PyObject_HEAD |
| 13085 | path_t path; |
| 13086 | #ifdef MS_WINDOWS |
| 13087 | HANDLE handle; |
| 13088 | WIN32_FIND_DATAW file_data; |
| 13089 | int first_time; |
| 13090 | #else /* POSIX */ |
| 13091 | DIR *dirp; |
| 13092 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13093 | #ifdef HAVE_FDOPENDIR |
| 13094 | int fd; |
| 13095 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13096 | } ScandirIterator; |
| 13097 | |
| 13098 | #ifdef MS_WINDOWS |
| 13099 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13100 | static int |
| 13101 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13102 | { |
| 13103 | return iterator->handle == INVALID_HANDLE_VALUE; |
| 13104 | } |
| 13105 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13106 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13107 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13108 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13109 | HANDLE handle = iterator->handle; |
| 13110 | |
| 13111 | if (handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13112 | return; |
| 13113 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13114 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13115 | Py_BEGIN_ALLOW_THREADS |
| 13116 | FindClose(handle); |
| 13117 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13118 | } |
| 13119 | |
| 13120 | static PyObject * |
| 13121 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13122 | { |
| 13123 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 13124 | BOOL success; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13125 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13126 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13127 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13128 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13129 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13130 | |
| 13131 | while (1) { |
| 13132 | if (!iterator->first_time) { |
| 13133 | Py_BEGIN_ALLOW_THREADS |
| 13134 | success = FindNextFileW(iterator->handle, file_data); |
| 13135 | Py_END_ALLOW_THREADS |
| 13136 | if (!success) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13137 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13138 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13139 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13140 | break; |
| 13141 | } |
| 13142 | } |
| 13143 | iterator->first_time = 0; |
| 13144 | |
| 13145 | /* Skip over . and .. */ |
| 13146 | if (wcscmp(file_data->cFileName, L".") != 0 && |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13147 | wcscmp(file_data->cFileName, L"..") != 0) { |
| 13148 | entry = DirEntry_from_find_data(&iterator->path, file_data); |
| 13149 | if (!entry) |
| 13150 | break; |
| 13151 | return entry; |
| 13152 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13153 | |
| 13154 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13155 | } |
| 13156 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13157 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13158 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13159 | return NULL; |
| 13160 | } |
| 13161 | |
| 13162 | #else /* POSIX */ |
| 13163 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13164 | static int |
| 13165 | ScandirIterator_is_closed(ScandirIterator *iterator) |
| 13166 | { |
| 13167 | return !iterator->dirp; |
| 13168 | } |
| 13169 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13170 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13171 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13172 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13173 | DIR *dirp = iterator->dirp; |
| 13174 | |
| 13175 | if (!dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13176 | return; |
| 13177 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13178 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13179 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13180 | #ifdef HAVE_FDOPENDIR |
| 13181 | if (iterator->path.fd != -1) |
| 13182 | rewinddir(dirp); |
| 13183 | #endif |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13184 | closedir(dirp); |
| 13185 | Py_END_ALLOW_THREADS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13186 | return; |
| 13187 | } |
| 13188 | |
| 13189 | static PyObject * |
| 13190 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 13191 | { |
| 13192 | struct dirent *direntp; |
| 13193 | Py_ssize_t name_len; |
| 13194 | int is_dot; |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13195 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13196 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13197 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13198 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13199 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13200 | |
| 13201 | while (1) { |
| 13202 | errno = 0; |
| 13203 | Py_BEGIN_ALLOW_THREADS |
| 13204 | direntp = readdir(iterator->dirp); |
| 13205 | Py_END_ALLOW_THREADS |
| 13206 | |
| 13207 | if (!direntp) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13208 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13209 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13210 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13211 | break; |
| 13212 | } |
| 13213 | |
| 13214 | /* Skip over . and .. */ |
| 13215 | name_len = NAMLEN(direntp); |
| 13216 | is_dot = direntp->d_name[0] == '.' && |
| 13217 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 13218 | if (!is_dot) { |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13219 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13220 | name_len, direntp->d_ino |
| 13221 | #ifdef HAVE_DIRENT_D_TYPE |
| 13222 | , direntp->d_type |
| 13223 | #endif |
| 13224 | ); |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13225 | if (!entry) |
| 13226 | break; |
| 13227 | return entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13228 | } |
| 13229 | |
| 13230 | /* Loop till we get a non-dot directory or finish iterating */ |
| 13231 | } |
| 13232 | |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13233 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13234 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13235 | return NULL; |
| 13236 | } |
| 13237 | |
| 13238 | #endif |
| 13239 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13240 | static PyObject * |
| 13241 | ScandirIterator_close(ScandirIterator *self, PyObject *args) |
| 13242 | { |
| 13243 | ScandirIterator_closedir(self); |
| 13244 | Py_RETURN_NONE; |
| 13245 | } |
| 13246 | |
| 13247 | static PyObject * |
| 13248 | ScandirIterator_enter(PyObject *self, PyObject *args) |
| 13249 | { |
| 13250 | Py_INCREF(self); |
| 13251 | return self; |
| 13252 | } |
| 13253 | |
| 13254 | static PyObject * |
| 13255 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) |
| 13256 | { |
| 13257 | ScandirIterator_closedir(self); |
| 13258 | Py_RETURN_NONE; |
| 13259 | } |
| 13260 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13261 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13262 | ScandirIterator_finalize(ScandirIterator *iterator) |
| 13263 | { |
| 13264 | PyObject *error_type, *error_value, *error_traceback; |
| 13265 | |
| 13266 | /* Save the current exception, if any. */ |
| 13267 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 13268 | |
| 13269 | if (!ScandirIterator_is_closed(iterator)) { |
| 13270 | ScandirIterator_closedir(iterator); |
| 13271 | |
| 13272 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, |
| 13273 | "unclosed scandir iterator %R", iterator)) { |
| 13274 | /* Spurious errors can appear at shutdown */ |
| 13275 | if (PyErr_ExceptionMatches(PyExc_Warning)) { |
| 13276 | PyErr_WriteUnraisable((PyObject *) iterator); |
| 13277 | } |
| 13278 | } |
| 13279 | } |
| 13280 | |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13281 | path_cleanup(&iterator->path); |
| 13282 | |
| 13283 | /* Restore the saved exception. */ |
| 13284 | PyErr_Restore(error_type, error_value, error_traceback); |
| 13285 | } |
| 13286 | |
| 13287 | static void |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13288 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 13289 | { |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13290 | PyTypeObject *tp = Py_TYPE(iterator); |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13291 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
| 13292 | return; |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13293 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13294 | freefunc free_func = PyType_GetSlot(tp, Py_tp_free); |
| 13295 | free_func(iterator); |
| 13296 | Py_DECREF(tp); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13297 | } |
| 13298 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13299 | static PyMethodDef ScandirIterator_methods[] = { |
| 13300 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, |
| 13301 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, |
| 13302 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, |
| 13303 | {NULL} |
| 13304 | }; |
| 13305 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13306 | static PyType_Slot ScandirIteratorType_slots[] = { |
| 13307 | {Py_tp_new, _disabled_new}, |
| 13308 | {Py_tp_dealloc, ScandirIterator_dealloc}, |
| 13309 | {Py_tp_finalize, ScandirIterator_finalize}, |
| 13310 | {Py_tp_iter, PyObject_SelfIter}, |
| 13311 | {Py_tp_iternext, ScandirIterator_iternext}, |
| 13312 | {Py_tp_methods, ScandirIterator_methods}, |
| 13313 | {0, 0}, |
| 13314 | }; |
| 13315 | |
| 13316 | static PyType_Spec ScandirIteratorType_spec = { |
| 13317 | MODNAME ".ScandirIterator", |
| 13318 | sizeof(ScandirIterator), |
| 13319 | 0, |
| 13320 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 13321 | ScandirIteratorType_slots |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13322 | }; |
| 13323 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13324 | /*[clinic input] |
| 13325 | os.scandir |
| 13326 | |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13327 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13328 | |
| 13329 | Return an iterator of DirEntry objects for given path. |
| 13330 | |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13331 | 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] | 13332 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
| 13333 | all other circumstances they will be str. |
| 13334 | |
| 13335 | If path is None, uses the path='.'. |
| 13336 | [clinic start generated code]*/ |
| 13337 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13338 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13339 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13340 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13341 | { |
| 13342 | ScandirIterator *iterator; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13343 | #ifdef MS_WINDOWS |
| 13344 | wchar_t *path_strW; |
| 13345 | #else |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13346 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13347 | #ifdef HAVE_FDOPENDIR |
| 13348 | int fd = -1; |
| 13349 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13350 | #endif |
| 13351 | |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 13352 | if (PySys_Audit("os.scandir", "O", |
| 13353 | path->object ? path->object : Py_None) < 0) { |
| 13354 | return NULL; |
| 13355 | } |
| 13356 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13357 | PyObject *ScandirIteratorType = _posixstate(module)->ScandirIteratorType; |
| 13358 | iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13359 | if (!iterator) |
| 13360 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13361 | |
| 13362 | #ifdef MS_WINDOWS |
| 13363 | iterator->handle = INVALID_HANDLE_VALUE; |
| 13364 | #else |
| 13365 | iterator->dirp = NULL; |
| 13366 | #endif |
| 13367 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13368 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 13369 | /* Move the ownership to iterator->path */ |
| 13370 | path->object = NULL; |
| 13371 | path->cleanup = NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13372 | |
| 13373 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13374 | iterator->first_time = 1; |
| 13375 | |
| 13376 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 13377 | if (!path_strW) |
| 13378 | goto error; |
| 13379 | |
| 13380 | Py_BEGIN_ALLOW_THREADS |
| 13381 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 13382 | Py_END_ALLOW_THREADS |
| 13383 | |
| 13384 | PyMem_Free(path_strW); |
| 13385 | |
| 13386 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 13387 | path_error(&iterator->path); |
| 13388 | goto error; |
| 13389 | } |
| 13390 | #else /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13391 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13392 | #ifdef HAVE_FDOPENDIR |
| 13393 | if (path->fd != -1) { |
| 13394 | /* closedir() closes the FD, so we duplicate it */ |
| 13395 | fd = _Py_dup(path->fd); |
| 13396 | if (fd == -1) |
| 13397 | goto error; |
| 13398 | |
| 13399 | Py_BEGIN_ALLOW_THREADS |
| 13400 | iterator->dirp = fdopendir(fd); |
| 13401 | Py_END_ALLOW_THREADS |
| 13402 | } |
| 13403 | else |
| 13404 | #endif |
| 13405 | { |
| 13406 | if (iterator->path.narrow) |
| 13407 | path_str = iterator->path.narrow; |
| 13408 | else |
| 13409 | path_str = "."; |
| 13410 | |
| 13411 | Py_BEGIN_ALLOW_THREADS |
| 13412 | iterator->dirp = opendir(path_str); |
| 13413 | Py_END_ALLOW_THREADS |
| 13414 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13415 | |
| 13416 | if (!iterator->dirp) { |
| 13417 | path_error(&iterator->path); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13418 | #ifdef HAVE_FDOPENDIR |
| 13419 | if (fd != -1) { |
| 13420 | Py_BEGIN_ALLOW_THREADS |
| 13421 | close(fd); |
| 13422 | Py_END_ALLOW_THREADS |
| 13423 | } |
| 13424 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13425 | goto error; |
| 13426 | } |
| 13427 | #endif |
| 13428 | |
| 13429 | return (PyObject *)iterator; |
| 13430 | |
| 13431 | error: |
| 13432 | Py_DECREF(iterator); |
| 13433 | return NULL; |
| 13434 | } |
| 13435 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13436 | /* |
| 13437 | Return the file system path representation of the object. |
| 13438 | |
| 13439 | If the object is str or bytes, then allow it to pass through with |
| 13440 | an incremented refcount. If the object defines __fspath__(), then |
| 13441 | return the result of that method. All other types raise a TypeError. |
| 13442 | */ |
| 13443 | PyObject * |
| 13444 | PyOS_FSPath(PyObject *path) |
| 13445 | { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13446 | /* For error message reasons, this function is manually inlined in |
| 13447 | path_converter(). */ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13448 | PyObject *func = NULL; |
| 13449 | PyObject *path_repr = NULL; |
| 13450 | |
| 13451 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { |
| 13452 | Py_INCREF(path); |
| 13453 | return path; |
| 13454 | } |
| 13455 | |
| 13456 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); |
| 13457 | if (NULL == func) { |
| 13458 | return PyErr_Format(PyExc_TypeError, |
| 13459 | "expected str, bytes or os.PathLike object, " |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13460 | "not %.200s", |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13461 | _PyType_Name(Py_TYPE(path))); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13462 | } |
| 13463 | |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13464 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13465 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13466 | if (NULL == path_repr) { |
| 13467 | return NULL; |
| 13468 | } |
| 13469 | |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13470 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
| 13471 | PyErr_Format(PyExc_TypeError, |
| 13472 | "expected %.200s.__fspath__() to return str or bytes, " |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 13473 | "not %.200s", _PyType_Name(Py_TYPE(path)), |
| 13474 | _PyType_Name(Py_TYPE(path_repr))); |
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13475 | Py_DECREF(path_repr); |
| 13476 | return NULL; |
| 13477 | } |
| 13478 | |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13479 | return path_repr; |
| 13480 | } |
| 13481 | |
| 13482 | /*[clinic input] |
| 13483 | os.fspath |
| 13484 | |
| 13485 | path: object |
| 13486 | |
| 13487 | Return the file system path representation of the object. |
| 13488 | |
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13489 | If the object is str or bytes, then allow it to pass through as-is. If the |
| 13490 | object defines __fspath__(), then return the result of that method. All other |
| 13491 | types raise a TypeError. |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13492 | [clinic start generated code]*/ |
| 13493 | |
| 13494 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13495 | os_fspath_impl(PyObject *module, PyObject *path) |
| 13496 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13497 | { |
| 13498 | return PyOS_FSPath(path); |
| 13499 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13500 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13501 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 13502 | /*[clinic input] |
| 13503 | os.getrandom |
| 13504 | |
| 13505 | size: Py_ssize_t |
| 13506 | flags: int=0 |
| 13507 | |
| 13508 | Obtain a series of random bytes. |
| 13509 | [clinic start generated code]*/ |
| 13510 | |
| 13511 | static PyObject * |
| 13512 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) |
| 13513 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ |
| 13514 | { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13515 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13516 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13517 | |
| 13518 | if (size < 0) { |
| 13519 | errno = EINVAL; |
| 13520 | return posix_error(); |
| 13521 | } |
| 13522 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13523 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 13524 | if (bytes == NULL) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13525 | PyErr_NoMemory(); |
| 13526 | return NULL; |
| 13527 | } |
| 13528 | |
| 13529 | while (1) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13530 | n = syscall(SYS_getrandom, |
| 13531 | PyBytes_AS_STRING(bytes), |
| 13532 | PyBytes_GET_SIZE(bytes), |
| 13533 | flags); |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13534 | if (n < 0 && errno == EINTR) { |
| 13535 | if (PyErr_CheckSignals() < 0) { |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13536 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13537 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13538 | |
| 13539 | /* getrandom() was interrupted by a signal: retry */ |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13540 | continue; |
| 13541 | } |
| 13542 | break; |
| 13543 | } |
| 13544 | |
| 13545 | if (n < 0) { |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13546 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13547 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13548 | } |
| 13549 | |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13550 | if (n != size) { |
| 13551 | _PyBytes_Resize(&bytes, n); |
| 13552 | } |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13553 | |
| 13554 | return bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13555 | |
| 13556 | error: |
| 13557 | Py_DECREF(bytes); |
| 13558 | return NULL; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13559 | } |
| 13560 | #endif /* HAVE_GETRANDOM_SYSCALL */ |
| 13561 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13562 | #ifdef MS_WINDOWS |
| 13563 | /* bpo-36085: Helper functions for managing DLL search directories |
| 13564 | * on win32 |
| 13565 | */ |
| 13566 | |
| 13567 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); |
| 13568 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); |
| 13569 | |
| 13570 | /*[clinic input] |
| 13571 | os._add_dll_directory |
| 13572 | |
| 13573 | path: path_t |
| 13574 | |
| 13575 | Add a path to the DLL search path. |
| 13576 | |
| 13577 | This search path is used when resolving dependencies for imported |
| 13578 | extension modules (the module itself is resolved through sys.path), |
| 13579 | and also by ctypes. |
| 13580 | |
| 13581 | Returns an opaque value that may be passed to os.remove_dll_directory |
| 13582 | to remove this directory from the search path. |
| 13583 | [clinic start generated code]*/ |
| 13584 | |
| 13585 | static PyObject * |
| 13586 | os__add_dll_directory_impl(PyObject *module, path_t *path) |
| 13587 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ |
| 13588 | { |
| 13589 | HMODULE hKernel32; |
| 13590 | PAddDllDirectory AddDllDirectory; |
| 13591 | DLL_DIRECTORY_COOKIE cookie = 0; |
| 13592 | DWORD err = 0; |
| 13593 | |
| 13594 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13595 | infrequent operation, just do it each time. Kernel32 is always |
| 13596 | loaded. */ |
| 13597 | Py_BEGIN_ALLOW_THREADS |
| 13598 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13599 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( |
| 13600 | hKernel32, "AddDllDirectory")) || |
| 13601 | !(cookie = (*AddDllDirectory)(path->wide))) { |
| 13602 | err = GetLastError(); |
| 13603 | } |
| 13604 | Py_END_ALLOW_THREADS |
| 13605 | |
| 13606 | if (err) { |
| 13607 | return win32_error_object_err("add_dll_directory", |
| 13608 | path->object, err); |
| 13609 | } |
| 13610 | |
| 13611 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); |
| 13612 | } |
| 13613 | |
| 13614 | /*[clinic input] |
| 13615 | os._remove_dll_directory |
| 13616 | |
| 13617 | cookie: object |
| 13618 | |
| 13619 | Removes a path from the DLL search path. |
| 13620 | |
| 13621 | The parameter is an opaque value that was returned from |
| 13622 | os.add_dll_directory. You can only remove directories that you added |
| 13623 | yourself. |
| 13624 | [clinic start generated code]*/ |
| 13625 | |
| 13626 | static PyObject * |
| 13627 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) |
| 13628 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ |
| 13629 | { |
| 13630 | HMODULE hKernel32; |
| 13631 | PRemoveDllDirectory RemoveDllDirectory; |
| 13632 | DLL_DIRECTORY_COOKIE cookieValue; |
| 13633 | DWORD err = 0; |
| 13634 | |
| 13635 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { |
| 13636 | PyErr_SetString(PyExc_TypeError, |
| 13637 | "Provided cookie was not returned from os.add_dll_directory"); |
| 13638 | return NULL; |
| 13639 | } |
| 13640 | |
| 13641 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( |
| 13642 | cookie, "DLL directory cookie"); |
| 13643 | |
| 13644 | /* For Windows 7, we have to load this. As this will be a fairly |
| 13645 | infrequent operation, just do it each time. Kernel32 is always |
| 13646 | loaded. */ |
| 13647 | Py_BEGIN_ALLOW_THREADS |
| 13648 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || |
| 13649 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( |
| 13650 | hKernel32, "RemoveDllDirectory")) || |
| 13651 | !(*RemoveDllDirectory)(cookieValue)) { |
| 13652 | err = GetLastError(); |
| 13653 | } |
| 13654 | Py_END_ALLOW_THREADS |
| 13655 | |
| 13656 | if (err) { |
| 13657 | return win32_error_object_err("remove_dll_directory", |
| 13658 | NULL, err); |
| 13659 | } |
| 13660 | |
| 13661 | if (PyCapsule_SetName(cookie, NULL)) { |
| 13662 | return NULL; |
| 13663 | } |
| 13664 | |
| 13665 | Py_RETURN_NONE; |
| 13666 | } |
| 13667 | |
| 13668 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13669 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13670 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13671 | |
| 13672 | OS_STAT_METHODDEF |
| 13673 | OS_ACCESS_METHODDEF |
| 13674 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13675 | OS_CHDIR_METHODDEF |
| 13676 | OS_CHFLAGS_METHODDEF |
| 13677 | OS_CHMOD_METHODDEF |
| 13678 | OS_FCHMOD_METHODDEF |
| 13679 | OS_LCHMOD_METHODDEF |
| 13680 | OS_CHOWN_METHODDEF |
| 13681 | OS_FCHOWN_METHODDEF |
| 13682 | OS_LCHOWN_METHODDEF |
| 13683 | OS_LCHFLAGS_METHODDEF |
| 13684 | OS_CHROOT_METHODDEF |
| 13685 | OS_CTERMID_METHODDEF |
| 13686 | OS_GETCWD_METHODDEF |
| 13687 | OS_GETCWDB_METHODDEF |
| 13688 | OS_LINK_METHODDEF |
| 13689 | OS_LISTDIR_METHODDEF |
| 13690 | OS_LSTAT_METHODDEF |
| 13691 | OS_MKDIR_METHODDEF |
| 13692 | OS_NICE_METHODDEF |
| 13693 | OS_GETPRIORITY_METHODDEF |
| 13694 | OS_SETPRIORITY_METHODDEF |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13695 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 13696 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13697 | OS_READLINK_METHODDEF |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 13698 | OS_COPY_FILE_RANGE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13699 | OS_RENAME_METHODDEF |
| 13700 | OS_REPLACE_METHODDEF |
| 13701 | OS_RMDIR_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13702 | OS_SYMLINK_METHODDEF |
| 13703 | OS_SYSTEM_METHODDEF |
| 13704 | OS_UMASK_METHODDEF |
| 13705 | OS_UNAME_METHODDEF |
| 13706 | OS_UNLINK_METHODDEF |
| 13707 | OS_REMOVE_METHODDEF |
| 13708 | OS_UTIME_METHODDEF |
| 13709 | OS_TIMES_METHODDEF |
| 13710 | OS__EXIT_METHODDEF |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13711 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13712 | OS_EXECV_METHODDEF |
| 13713 | OS_EXECVE_METHODDEF |
| 13714 | OS_SPAWNV_METHODDEF |
| 13715 | OS_SPAWNVE_METHODDEF |
| 13716 | OS_FORK1_METHODDEF |
| 13717 | OS_FORK_METHODDEF |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 13718 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13719 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 13720 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 13721 | OS_SCHED_GETPARAM_METHODDEF |
| 13722 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 13723 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 13724 | OS_SCHED_SETPARAM_METHODDEF |
| 13725 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 13726 | OS_SCHED_YIELD_METHODDEF |
| 13727 | OS_SCHED_SETAFFINITY_METHODDEF |
| 13728 | OS_SCHED_GETAFFINITY_METHODDEF |
| 13729 | OS_OPENPTY_METHODDEF |
| 13730 | OS_FORKPTY_METHODDEF |
| 13731 | OS_GETEGID_METHODDEF |
| 13732 | OS_GETEUID_METHODDEF |
| 13733 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 13734 | #ifdef HAVE_GETGROUPLIST |
| 13735 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 13736 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13737 | OS_GETGROUPS_METHODDEF |
| 13738 | OS_GETPID_METHODDEF |
| 13739 | OS_GETPGRP_METHODDEF |
| 13740 | OS_GETPPID_METHODDEF |
| 13741 | OS_GETUID_METHODDEF |
| 13742 | OS_GETLOGIN_METHODDEF |
| 13743 | OS_KILL_METHODDEF |
| 13744 | OS_KILLPG_METHODDEF |
| 13745 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13746 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13747 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13748 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13749 | OS_SETUID_METHODDEF |
| 13750 | OS_SETEUID_METHODDEF |
| 13751 | OS_SETREUID_METHODDEF |
| 13752 | OS_SETGID_METHODDEF |
| 13753 | OS_SETEGID_METHODDEF |
| 13754 | OS_SETREGID_METHODDEF |
| 13755 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13756 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13757 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13758 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13759 | OS_GETPGID_METHODDEF |
| 13760 | OS_SETPGRP_METHODDEF |
| 13761 | OS_WAIT_METHODDEF |
| 13762 | OS_WAIT3_METHODDEF |
| 13763 | OS_WAIT4_METHODDEF |
| 13764 | OS_WAITID_METHODDEF |
| 13765 | OS_WAITPID_METHODDEF |
Benjamin Peterson | 6c4c45e | 2019-11-05 19:21:29 -0800 | [diff] [blame] | 13766 | OS_PIDFD_OPEN_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13767 | OS_GETSID_METHODDEF |
| 13768 | OS_SETSID_METHODDEF |
| 13769 | OS_SETPGID_METHODDEF |
| 13770 | OS_TCGETPGRP_METHODDEF |
| 13771 | OS_TCSETPGRP_METHODDEF |
| 13772 | OS_OPEN_METHODDEF |
| 13773 | OS_CLOSE_METHODDEF |
| 13774 | OS_CLOSERANGE_METHODDEF |
| 13775 | OS_DEVICE_ENCODING_METHODDEF |
| 13776 | OS_DUP_METHODDEF |
| 13777 | OS_DUP2_METHODDEF |
| 13778 | OS_LOCKF_METHODDEF |
| 13779 | OS_LSEEK_METHODDEF |
| 13780 | OS_READ_METHODDEF |
| 13781 | OS_READV_METHODDEF |
| 13782 | OS_PREAD_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13783 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13784 | OS_WRITE_METHODDEF |
| 13785 | OS_WRITEV_METHODDEF |
| 13786 | OS_PWRITE_METHODDEF |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13787 | OS_PWRITEV_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13788 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 13789 | {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13790 | posix_sendfile__doc__}, |
| 13791 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13792 | OS_FSTAT_METHODDEF |
| 13793 | OS_ISATTY_METHODDEF |
| 13794 | OS_PIPE_METHODDEF |
| 13795 | OS_PIPE2_METHODDEF |
| 13796 | OS_MKFIFO_METHODDEF |
| 13797 | OS_MKNOD_METHODDEF |
| 13798 | OS_MAJOR_METHODDEF |
| 13799 | OS_MINOR_METHODDEF |
| 13800 | OS_MAKEDEV_METHODDEF |
| 13801 | OS_FTRUNCATE_METHODDEF |
| 13802 | OS_TRUNCATE_METHODDEF |
| 13803 | OS_POSIX_FALLOCATE_METHODDEF |
| 13804 | OS_POSIX_FADVISE_METHODDEF |
| 13805 | OS_PUTENV_METHODDEF |
| 13806 | OS_UNSETENV_METHODDEF |
| 13807 | OS_STRERROR_METHODDEF |
| 13808 | OS_FCHDIR_METHODDEF |
| 13809 | OS_FSYNC_METHODDEF |
| 13810 | OS_SYNC_METHODDEF |
| 13811 | OS_FDATASYNC_METHODDEF |
| 13812 | OS_WCOREDUMP_METHODDEF |
| 13813 | OS_WIFCONTINUED_METHODDEF |
| 13814 | OS_WIFSTOPPED_METHODDEF |
| 13815 | OS_WIFSIGNALED_METHODDEF |
| 13816 | OS_WIFEXITED_METHODDEF |
| 13817 | OS_WEXITSTATUS_METHODDEF |
| 13818 | OS_WTERMSIG_METHODDEF |
| 13819 | OS_WSTOPSIG_METHODDEF |
| 13820 | OS_FSTATVFS_METHODDEF |
| 13821 | OS_STATVFS_METHODDEF |
| 13822 | OS_CONFSTR_METHODDEF |
| 13823 | OS_SYSCONF_METHODDEF |
| 13824 | OS_FPATHCONF_METHODDEF |
| 13825 | OS_PATHCONF_METHODDEF |
| 13826 | OS_ABORT_METHODDEF |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 13827 | OS__GETFULLPATHNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13828 | OS__GETDISKUSAGE_METHODDEF |
| 13829 | OS__GETFINALPATHNAME_METHODDEF |
| 13830 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 13831 | OS_GETLOADAVG_METHODDEF |
| 13832 | OS_URANDOM_METHODDEF |
| 13833 | OS_SETRESUID_METHODDEF |
| 13834 | OS_SETRESGID_METHODDEF |
| 13835 | OS_GETRESUID_METHODDEF |
| 13836 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 13837 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13838 | OS_GETXATTR_METHODDEF |
| 13839 | OS_SETXATTR_METHODDEF |
| 13840 | OS_REMOVEXATTR_METHODDEF |
| 13841 | OS_LISTXATTR_METHODDEF |
| 13842 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13843 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 13844 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 13845 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13846 | OS_CPU_COUNT_METHODDEF |
| 13847 | OS_GET_INHERITABLE_METHODDEF |
| 13848 | OS_SET_INHERITABLE_METHODDEF |
| 13849 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 13850 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13851 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13852 | OS_GET_BLOCKING_METHODDEF |
| 13853 | OS_SET_BLOCKING_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13854 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13855 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13856 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13857 | OS_GETRANDOM_METHODDEF |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 13858 | OS_MEMFD_CREATE_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13859 | #ifdef MS_WINDOWS |
| 13860 | OS__ADD_DLL_DIRECTORY_METHODDEF |
| 13861 | OS__REMOVE_DLL_DIRECTORY_METHODDEF |
| 13862 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13863 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13864 | }; |
| 13865 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13866 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13867 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13868 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13869 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13870 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13871 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13872 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13873 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13874 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13875 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13876 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13877 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13878 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13879 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13880 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13881 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13882 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13883 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13884 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13885 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13886 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13887 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13888 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13889 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13890 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13891 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13892 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13893 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13894 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13895 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13896 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13897 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13898 | #endif |
| 13899 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13900 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13901 | #endif |
| 13902 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13903 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13904 | #endif |
| 13905 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13906 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13907 | #endif |
| 13908 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13909 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13910 | #endif |
| 13911 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13912 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13913 | #endif |
| 13914 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13915 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13916 | #endif |
| 13917 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13918 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13919 | #endif |
| 13920 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13921 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13922 | #endif |
| 13923 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13924 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13925 | #endif |
| 13926 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13927 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13928 | #endif |
| 13929 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13930 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13931 | #endif |
| 13932 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13933 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13934 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13935 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13936 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13937 | #endif |
| 13938 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13939 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13940 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13941 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13942 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13943 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13944 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13945 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13946 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13947 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13948 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13949 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13950 | #endif |
| 13951 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13952 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13953 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13954 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13955 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13956 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13957 | #endif |
| 13958 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13959 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13960 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13961 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13962 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13963 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13964 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13965 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13966 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 13967 | #ifdef O_TMPFILE |
| 13968 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 13969 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13970 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13971 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13972 | #endif |
| 13973 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13974 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13975 | #endif |
| 13976 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13977 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13978 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13979 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13980 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 13981 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13982 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13983 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13984 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 13985 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13986 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13987 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13988 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13989 | #endif |
| 13990 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13991 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 13992 | #endif |
| 13993 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13994 | /* MS Windows */ |
| 13995 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13996 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13997 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13998 | #endif |
| 13999 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14000 | /* Optimize for short life (keep in memory). */ |
| 14001 | /* 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] | 14002 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14003 | #endif |
| 14004 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14005 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14006 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14007 | #endif |
| 14008 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14009 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14010 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14011 | #endif |
| 14012 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14013 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14014 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14015 | #endif |
| 14016 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14017 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14018 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14019 | /* Send a SIGIO signal whenever input or output |
| 14020 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14021 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14022 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14023 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14024 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14025 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14026 | #endif |
| 14027 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14028 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14029 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14030 | #endif |
| 14031 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14032 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14033 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14034 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14035 | #ifdef O_NOLINKS |
| 14036 | /* 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] | 14037 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14038 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14039 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14040 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14041 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14042 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 14043 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14044 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14045 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14046 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14047 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14048 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14049 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14050 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14051 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14052 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14053 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14054 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14055 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14056 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14057 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14058 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14059 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14060 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14061 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14062 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14063 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14064 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14065 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14066 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14067 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14068 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14069 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14070 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14071 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14072 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14073 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14074 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14075 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14076 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14077 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14078 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14079 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14080 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14081 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14082 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14083 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14084 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14085 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14086 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14087 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14088 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14089 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14090 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14091 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14092 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14093 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14094 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14095 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14096 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 14097 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14098 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14099 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14100 | #endif /* ST_RDONLY */ |
| 14101 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14102 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14103 | #endif /* ST_NOSUID */ |
| 14104 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 14105 | /* GNU extensions */ |
| 14106 | #ifdef ST_NODEV |
| 14107 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 14108 | #endif /* ST_NODEV */ |
| 14109 | #ifdef ST_NOEXEC |
| 14110 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 14111 | #endif /* ST_NOEXEC */ |
| 14112 | #ifdef ST_SYNCHRONOUS |
| 14113 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 14114 | #endif /* ST_SYNCHRONOUS */ |
| 14115 | #ifdef ST_MANDLOCK |
| 14116 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 14117 | #endif /* ST_MANDLOCK */ |
| 14118 | #ifdef ST_WRITE |
| 14119 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 14120 | #endif /* ST_WRITE */ |
| 14121 | #ifdef ST_APPEND |
| 14122 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 14123 | #endif /* ST_APPEND */ |
| 14124 | #ifdef ST_NOATIME |
| 14125 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 14126 | #endif /* ST_NOATIME */ |
| 14127 | #ifdef ST_NODIRATIME |
| 14128 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 14129 | #endif /* ST_NODIRATIME */ |
| 14130 | #ifdef ST_RELATIME |
| 14131 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 14132 | #endif /* ST_RELATIME */ |
| 14133 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14134 | /* FreeBSD sendfile() constants */ |
| 14135 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14136 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14137 | #endif |
| 14138 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14139 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14140 | #endif |
| 14141 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14142 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14143 | #endif |
| 14144 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14145 | /* constants for posix_fadvise */ |
| 14146 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14147 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14148 | #endif |
| 14149 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14150 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14151 | #endif |
| 14152 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14153 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14154 | #endif |
| 14155 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14156 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14157 | #endif |
| 14158 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14159 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14160 | #endif |
| 14161 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14162 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14163 | #endif |
| 14164 | |
| 14165 | /* constants for waitid */ |
| 14166 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14167 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 14168 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 14169 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Benjamin Peterson | 5c0c325 | 2019-11-05 21:58:31 -0800 | [diff] [blame] | 14170 | #ifdef P_PIDFD |
| 14171 | if (PyModule_AddIntMacro(m, P_PIDFD)) return -1; |
| 14172 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14173 | #endif |
| 14174 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14175 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14176 | #endif |
| 14177 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14178 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14179 | #endif |
| 14180 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14181 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14182 | #endif |
| 14183 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14184 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14185 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14186 | #ifdef CLD_KILLED |
| 14187 | if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1; |
| 14188 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14189 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14190 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14191 | #endif |
| 14192 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14193 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14194 | #endif |
Dong-hee Na | 2eba6ad | 2019-10-21 16:01:05 +0900 | [diff] [blame] | 14195 | #ifdef CLD_STOPPED |
| 14196 | if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1; |
| 14197 | #endif |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14198 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14199 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14200 | #endif |
| 14201 | |
| 14202 | /* constants for lockf */ |
| 14203 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14204 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14205 | #endif |
| 14206 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14207 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14208 | #endif |
| 14209 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14210 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14211 | #endif |
| 14212 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14213 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14214 | #endif |
| 14215 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14216 | #ifdef RWF_DSYNC |
| 14217 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; |
| 14218 | #endif |
| 14219 | #ifdef RWF_HIPRI |
| 14220 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; |
| 14221 | #endif |
| 14222 | #ifdef RWF_SYNC |
| 14223 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; |
| 14224 | #endif |
| 14225 | #ifdef RWF_NOWAIT |
| 14226 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; |
| 14227 | #endif |
| 14228 | |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14229 | /* constants for posix_spawn */ |
| 14230 | #ifdef HAVE_POSIX_SPAWN |
| 14231 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; |
| 14232 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; |
| 14233 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; |
| 14234 | #endif |
| 14235 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14236 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14237 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 14238 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14239 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14240 | #endif |
| 14241 | #ifdef HAVE_SPAWNV |
| 14242 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14243 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 14244 | #endif |
| 14245 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14246 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14247 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14248 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14249 | #endif |
| 14250 | #ifdef SCHED_FIFO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14251 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14252 | #endif |
| 14253 | #ifdef SCHED_RR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14254 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14255 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14256 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 14257 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14258 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14259 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14260 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14261 | #endif |
| 14262 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14263 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14264 | #endif |
| 14265 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14266 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14267 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14268 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14269 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14270 | #endif |
| 14271 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14272 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14273 | #endif |
| 14274 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14275 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14276 | #endif |
| 14277 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14278 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14279 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14280 | #endif |
| 14281 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 14282 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14283 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 14284 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 14285 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 14286 | #endif |
| 14287 | |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14288 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14289 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14290 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14291 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14292 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14293 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14294 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14295 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14296 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14297 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14298 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14299 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14300 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14301 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14302 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14303 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14304 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14305 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14306 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14307 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14308 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 14309 | #if HAVE_DECL_RTLD_MEMBER |
| 14310 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; |
| 14311 | #endif |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14312 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14313 | #ifdef HAVE_GETRANDOM_SYSCALL |
| 14314 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; |
| 14315 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; |
| 14316 | #endif |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14317 | #ifdef HAVE_MEMFD_CREATE |
| 14318 | if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1; |
| 14319 | if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1; |
| 14320 | #ifdef MFD_HUGETLB |
| 14321 | if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14322 | #endif |
| 14323 | #ifdef MFD_HUGE_SHIFT |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14324 | if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14325 | #endif |
| 14326 | #ifdef MFD_HUGE_MASK |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14327 | if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14328 | #endif |
| 14329 | #ifdef MFD_HUGE_64KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14330 | if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14331 | #endif |
| 14332 | #ifdef MFD_HUGE_512KB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14333 | if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14334 | #endif |
| 14335 | #ifdef MFD_HUGE_1MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14336 | if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14337 | #endif |
| 14338 | #ifdef MFD_HUGE_2MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14339 | if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14340 | #endif |
| 14341 | #ifdef MFD_HUGE_8MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14342 | if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14343 | #endif |
| 14344 | #ifdef MFD_HUGE_16MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14345 | if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14346 | #endif |
| 14347 | #ifdef MFD_HUGE_32MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14348 | if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14349 | #endif |
| 14350 | #ifdef MFD_HUGE_256MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14351 | if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14352 | #endif |
| 14353 | #ifdef MFD_HUGE_512MB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14354 | if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14355 | #endif |
| 14356 | #ifdef MFD_HUGE_1GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14357 | if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14358 | #endif |
| 14359 | #ifdef MFD_HUGE_2GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14360 | if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14361 | #endif |
| 14362 | #ifdef MFD_HUGE_16GB |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14363 | if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1; |
| 14364 | #endif |
| 14365 | #endif |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14366 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14367 | #if defined(__APPLE__) |
| 14368 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; |
| 14369 | #endif |
| 14370 | |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14371 | #ifdef MS_WINDOWS |
| 14372 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; |
| 14373 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; |
| 14374 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; |
| 14375 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; |
| 14376 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; |
| 14377 | #endif |
| 14378 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14379 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14380 | } |
| 14381 | |
| 14382 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14383 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14384 | PyModuleDef_HEAD_INIT, |
| 14385 | MODNAME, |
| 14386 | posix__doc__, |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14387 | sizeof(_posixstate), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14388 | posix_methods, |
| 14389 | NULL, |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14390 | _posix_traverse, |
| 14391 | _posix_clear, |
| 14392 | _posix_free, |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14393 | }; |
| 14394 | |
| 14395 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14396 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14397 | |
| 14398 | #ifdef HAVE_FACCESSAT |
| 14399 | "HAVE_FACCESSAT", |
| 14400 | #endif |
| 14401 | |
| 14402 | #ifdef HAVE_FCHDIR |
| 14403 | "HAVE_FCHDIR", |
| 14404 | #endif |
| 14405 | |
| 14406 | #ifdef HAVE_FCHMOD |
| 14407 | "HAVE_FCHMOD", |
| 14408 | #endif |
| 14409 | |
| 14410 | #ifdef HAVE_FCHMODAT |
| 14411 | "HAVE_FCHMODAT", |
| 14412 | #endif |
| 14413 | |
| 14414 | #ifdef HAVE_FCHOWN |
| 14415 | "HAVE_FCHOWN", |
| 14416 | #endif |
| 14417 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 14418 | #ifdef HAVE_FCHOWNAT |
| 14419 | "HAVE_FCHOWNAT", |
| 14420 | #endif |
| 14421 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14422 | #ifdef HAVE_FEXECVE |
| 14423 | "HAVE_FEXECVE", |
| 14424 | #endif |
| 14425 | |
| 14426 | #ifdef HAVE_FDOPENDIR |
| 14427 | "HAVE_FDOPENDIR", |
| 14428 | #endif |
| 14429 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14430 | #ifdef HAVE_FPATHCONF |
| 14431 | "HAVE_FPATHCONF", |
| 14432 | #endif |
| 14433 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14434 | #ifdef HAVE_FSTATAT |
| 14435 | "HAVE_FSTATAT", |
| 14436 | #endif |
| 14437 | |
| 14438 | #ifdef HAVE_FSTATVFS |
| 14439 | "HAVE_FSTATVFS", |
| 14440 | #endif |
| 14441 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 14442 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14443 | "HAVE_FTRUNCATE", |
| 14444 | #endif |
| 14445 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14446 | #ifdef HAVE_FUTIMENS |
| 14447 | "HAVE_FUTIMENS", |
| 14448 | #endif |
| 14449 | |
| 14450 | #ifdef HAVE_FUTIMES |
| 14451 | "HAVE_FUTIMES", |
| 14452 | #endif |
| 14453 | |
| 14454 | #ifdef HAVE_FUTIMESAT |
| 14455 | "HAVE_FUTIMESAT", |
| 14456 | #endif |
| 14457 | |
| 14458 | #ifdef HAVE_LINKAT |
| 14459 | "HAVE_LINKAT", |
| 14460 | #endif |
| 14461 | |
| 14462 | #ifdef HAVE_LCHFLAGS |
| 14463 | "HAVE_LCHFLAGS", |
| 14464 | #endif |
| 14465 | |
| 14466 | #ifdef HAVE_LCHMOD |
| 14467 | "HAVE_LCHMOD", |
| 14468 | #endif |
| 14469 | |
| 14470 | #ifdef HAVE_LCHOWN |
| 14471 | "HAVE_LCHOWN", |
| 14472 | #endif |
| 14473 | |
| 14474 | #ifdef HAVE_LSTAT |
| 14475 | "HAVE_LSTAT", |
| 14476 | #endif |
| 14477 | |
| 14478 | #ifdef HAVE_LUTIMES |
| 14479 | "HAVE_LUTIMES", |
| 14480 | #endif |
| 14481 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14482 | #ifdef HAVE_MEMFD_CREATE |
| 14483 | "HAVE_MEMFD_CREATE", |
| 14484 | #endif |
| 14485 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14486 | #ifdef HAVE_MKDIRAT |
| 14487 | "HAVE_MKDIRAT", |
| 14488 | #endif |
| 14489 | |
| 14490 | #ifdef HAVE_MKFIFOAT |
| 14491 | "HAVE_MKFIFOAT", |
| 14492 | #endif |
| 14493 | |
| 14494 | #ifdef HAVE_MKNODAT |
| 14495 | "HAVE_MKNODAT", |
| 14496 | #endif |
| 14497 | |
| 14498 | #ifdef HAVE_OPENAT |
| 14499 | "HAVE_OPENAT", |
| 14500 | #endif |
| 14501 | |
| 14502 | #ifdef HAVE_READLINKAT |
| 14503 | "HAVE_READLINKAT", |
| 14504 | #endif |
| 14505 | |
| 14506 | #ifdef HAVE_RENAMEAT |
| 14507 | "HAVE_RENAMEAT", |
| 14508 | #endif |
| 14509 | |
| 14510 | #ifdef HAVE_SYMLINKAT |
| 14511 | "HAVE_SYMLINKAT", |
| 14512 | #endif |
| 14513 | |
| 14514 | #ifdef HAVE_UNLINKAT |
| 14515 | "HAVE_UNLINKAT", |
| 14516 | #endif |
| 14517 | |
| 14518 | #ifdef HAVE_UTIMENSAT |
| 14519 | "HAVE_UTIMENSAT", |
| 14520 | #endif |
| 14521 | |
| 14522 | #ifdef MS_WINDOWS |
| 14523 | "MS_WINDOWS", |
| 14524 | #endif |
| 14525 | |
| 14526 | NULL |
| 14527 | }; |
| 14528 | |
| 14529 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 14530 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 14531 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14532 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14533 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14534 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14535 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14536 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14537 | m = PyState_FindModule(&posixmodule); |
| 14538 | if (m != NULL) { |
| 14539 | Py_INCREF(m); |
| 14540 | return m; |
| 14541 | } |
| 14542 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14543 | m = PyModule_Create(&posixmodule); |
| 14544 | if (m == NULL) |
| 14545 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14546 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14547 | /* Initialize environ dictionary */ |
| 14548 | v = convertenviron(); |
| 14549 | Py_XINCREF(v); |
| 14550 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 14551 | return NULL; |
| 14552 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14553 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14554 | if (all_ins(m)) |
| 14555 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14556 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14557 | if (setup_confname_tables(m)) |
| 14558 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14559 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14560 | Py_INCREF(PyExc_OSError); |
| 14561 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 14562 | |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 14563 | #ifdef PY_PUTENV_DICT |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14564 | /* Save putenv() parameters as values here, so we can collect them when they |
| 14565 | * get re-set with another call for the same key. */ |
Victor Stinner | 623ed61 | 2020-01-21 19:25:32 +0100 | [diff] [blame^] | 14566 | _posixstate(m)->putenv_dict = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 14567 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 14568 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14569 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14570 | waitid_result_desc.name = MODNAME ".waitid_result"; |
| 14571 | PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc); |
| 14572 | if (WaitidResultType == NULL) { |
| 14573 | return NULL; |
| 14574 | } |
| 14575 | Py_INCREF(WaitidResultType); |
| 14576 | PyModule_AddObject(m, "waitid_result", WaitidResultType); |
| 14577 | _posixstate(m)->WaitidResultType = WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14578 | #endif |
| 14579 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14580 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
| 14581 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 14582 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 14583 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
| 14584 | PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc); |
| 14585 | if (StatResultType == NULL) { |
| 14586 | return NULL; |
| 14587 | } |
| 14588 | Py_INCREF(StatResultType); |
| 14589 | PyModule_AddObject(m, "stat_result", StatResultType); |
| 14590 | _posixstate(m)->StatResultType = StatResultType; |
| 14591 | structseq_new = ((PyTypeObject *)StatResultType)->tp_new; |
| 14592 | ((PyTypeObject *)StatResultType)->tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14593 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14594 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
| 14595 | PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc); |
| 14596 | if (StatVFSResultType == NULL) { |
| 14597 | return NULL; |
| 14598 | } |
| 14599 | Py_INCREF(StatVFSResultType); |
| 14600 | PyModule_AddObject(m, "statvfs_result", StatVFSResultType); |
| 14601 | _posixstate(m)->StatVFSResultType = StatVFSResultType; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14602 | #ifdef NEED_TICKS_PER_SECOND |
| 14603 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14604 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14605 | # elif defined(HZ) |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14606 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14607 | # else |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14608 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14609 | # endif |
| 14610 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14611 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14612 | #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] | 14613 | sched_param_desc.name = MODNAME ".sched_param"; |
| 14614 | PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc); |
| 14615 | if (SchedParamType == NULL) { |
| 14616 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14617 | } |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14618 | Py_INCREF(SchedParamType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14619 | PyModule_AddObject(m, "sched_param", SchedParamType); |
| 14620 | _posixstate(m)->SchedParamType = SchedParamType; |
| 14621 | ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param; |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14622 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14623 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14624 | /* initialize TerminalSize_info */ |
| 14625 | PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc); |
| 14626 | if (TerminalSizeType == NULL) { |
| 14627 | return NULL; |
| 14628 | } |
| 14629 | Py_INCREF(TerminalSizeType); |
| 14630 | PyModule_AddObject(m, "terminal_size", TerminalSizeType); |
| 14631 | _posixstate(m)->TerminalSizeType = TerminalSizeType; |
| 14632 | |
| 14633 | /* initialize scandir types */ |
| 14634 | PyObject *ScandirIteratorType = PyType_FromSpec(&ScandirIteratorType_spec); |
| 14635 | if (ScandirIteratorType == NULL) { |
| 14636 | return NULL; |
| 14637 | } |
| 14638 | _posixstate(m)->ScandirIteratorType = ScandirIteratorType; |
| 14639 | |
| 14640 | PyObject *DirEntryType = PyType_FromSpec(&DirEntryType_spec); |
| 14641 | if (DirEntryType == NULL) { |
| 14642 | return NULL; |
| 14643 | } |
| 14644 | Py_INCREF(DirEntryType); |
| 14645 | PyModule_AddObject(m, "DirEntry", DirEntryType); |
| 14646 | _posixstate(m)->DirEntryType = DirEntryType; |
| 14647 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14648 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14649 | PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(×_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14650 | if (TimesResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14651 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14652 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14653 | Py_INCREF(TimesResultType); |
| 14654 | PyModule_AddObject(m, "times_result", TimesResultType); |
| 14655 | _posixstate(m)->TimesResultType = TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14656 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14657 | PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14658 | if (UnameResultType == NULL) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14659 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14660 | } |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14661 | Py_INCREF(UnameResultType); |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14662 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14663 | _posixstate(m)->UnameResultType = (PyObject *)UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14664 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14665 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14666 | /* |
| 14667 | * Step 2 of weak-linking support on Mac OS X. |
| 14668 | * |
| 14669 | * The code below removes functions that are not available on the |
| 14670 | * currently active platform. |
| 14671 | * |
| 14672 | * 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] | 14673 | * 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] | 14674 | * OSX 10.4. |
| 14675 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14676 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14677 | if (fstatvfs == NULL) { |
| 14678 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 14679 | return NULL; |
| 14680 | } |
| 14681 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14682 | #endif /* HAVE_FSTATVFS */ |
| 14683 | |
| 14684 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14685 | if (statvfs == NULL) { |
| 14686 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 14687 | return NULL; |
| 14688 | } |
| 14689 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14690 | #endif /* HAVE_STATVFS */ |
| 14691 | |
| 14692 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14693 | if (lchown == NULL) { |
| 14694 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 14695 | return NULL; |
| 14696 | } |
| 14697 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14698 | #endif /* HAVE_LCHOWN */ |
| 14699 | |
| 14700 | |
| 14701 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14702 | |
Eddie Elizondo | b396663 | 2019-11-05 07:16:14 -0800 | [diff] [blame] | 14703 | if ((_posixstate(m)->billion = PyLong_FromLong(1000000000)) == NULL) |
| 14704 | return NULL; |
| 14705 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 14706 | _posixstate(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage"); |
| 14707 | if (_posixstate(m)->struct_rusage == NULL) |
| 14708 | return NULL; |
| 14709 | #endif |
| 14710 | _posixstate(m)->st_mode = PyUnicode_InternFromString("st_mode"); |
| 14711 | if (_posixstate(m)->st_mode == NULL) |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 14712 | return NULL; |
| 14713 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14714 | /* suppress "function not used" warnings */ |
| 14715 | { |
| 14716 | int ignored; |
| 14717 | fd_specified("", -1); |
| 14718 | follow_symlinks_specified("", 1); |
| 14719 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 14720 | dir_fd_converter(Py_None, &ignored); |
| 14721 | dir_fd_unavailable(Py_None, &ignored); |
| 14722 | } |
| 14723 | |
| 14724 | /* |
| 14725 | * provide list of locally available functions |
| 14726 | * so os.py can populate support_* lists |
| 14727 | */ |
| 14728 | list = PyList_New(0); |
| 14729 | if (!list) |
| 14730 | return NULL; |
| 14731 | for (trace = have_functions; *trace; trace++) { |
| 14732 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 14733 | if (!unicode) |
| 14734 | return NULL; |
| 14735 | if (PyList_Append(list, unicode)) |
| 14736 | return NULL; |
| 14737 | Py_DECREF(unicode); |
| 14738 | } |
| 14739 | PyModule_AddObject(m, "_have_functions", list); |
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 14740 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14741 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14742 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14743 | |
| 14744 | #ifdef __cplusplus |
| 14745 | } |
| 14746 | #endif |